zabbixApi4j-Proxy

Proxy:这个类被设计成与代理一起工作。

proxy.create: 创建新的代理
proxy.delete: 删除代理
proxy.get: 检索代理
proxy.isreadable: 检查代理是否是可读的
proxy.iswritable: 检查代理是否是可写的
proxy.update: 更新代理

zabbixApi4j-Proxy_第1张图片
image.png


DummyProxy
package cn.com.yeexun.testzabbix.zabbix4j.example.proxy;

import java.util.Date;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestDummyMethodBase;

import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.proxy.ProxyCreateRequest;
import com.zabbix4j.proxy.ProxyCreateResponse;
import com.zabbix4j.proxy.ProxyDeleteRequest;
import com.zabbix4j.proxy.ProxyDeleteResponse;
import com.zabbix4j.proxy.ProxyObject;

/**
 * @author Suguru Yajima
 */
public class DummyProxy extends ZabbixApiTestDummyMethodBase {
    public DummyProxy(ZabbixApi zabbixApi) {
        super(zabbixApi);
    }

    public Integer create() throws ZabbixApiException {
        final Integer hostId = 10147;

        ProxyCreateRequest request = new ProxyCreateRequest();
        ProxyCreateRequest.Params params = request.getParams();
        params.setHost("test.proxy.create." + new Date().getTime());
        params.setStatus(ProxyObject.STATUS.ACTIVE_PROXY.value);
        params.addHostId(hostId);

        ProxyCreateResponse response = zabbixApi.proxy().create(request);

        Integer actualId = response.getResult().getProxyids().get(0);

        return actualId;
    }

    public Integer createWithoutHost() throws ZabbixApiException {
        final Integer hostId = 10147;

        ProxyCreateRequest request = new ProxyCreateRequest();
        ProxyCreateRequest.Params params = request.getParams();
        params.setHost("test.proxy.create." + new Date().getTime());
        params.setStatus(ProxyObject.STATUS.ACTIVE_PROXY.value);
        //params.addHostId(hostId);

        ProxyCreateResponse response = zabbixApi.proxy().create(request);

        Integer actualId = response.getResult().getProxyids().get(0);

        return actualId;
    }

    public void delete(Integer targetId) throws ZabbixApiException {

        ProxyDeleteRequest request = new ProxyDeleteRequest();
        request.addProxyId(targetId);

        ProxyDeleteResponse response = zabbixApi.proxy().delete(request);
    }
}


ProxyCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.proxy;

import static org.junit.Assert.assertNotNull;

import java.util.Date;

import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.proxy.ProxyCreateRequest;
import com.zabbix4j.proxy.ProxyCreateResponse;
import com.zabbix4j.proxy.ProxyObject;

/**
 * @author Suguru Yajima
 */
public class ProxyCreateTest extends ZabbixApiTestBase {

    public ProxyCreateTest() {
        super();
    }

    @Test
    public void testCreate() throws Exception {
        final Integer hostId = 10147;

        ProxyCreateRequest request = new ProxyCreateRequest();
        ProxyCreateRequest.Params params = request.getParams();
        params.setHost("test.proxy.create." + new Date().getTime());
        params.setStatus(ProxyObject.STATUS.ACTIVE_PROXY.value);
        params.addHostId(hostId);

        ProxyCreateResponse response = zabbixApi.proxy().create(request);
        assertNotNull(response);

        Integer actualId = response.getResult().getProxyids().get(0);
        assertNotNull(actualId);
    }
}


ProxyDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.proxy;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.proxy.ProxyDeleteRequest;
import com.zabbix4j.proxy.ProxyDeleteResponse;

/**
 * @author Suguru Yajima
 */
public class ProxyDeleteTest extends ZabbixApiTestBase {
    public ProxyDeleteTest() {
        super();
    }

    @Test
    public void testDelete() throws Exception {
        DummyProxy dummyProxy = new DummyProxy(zabbixApi);
        Integer targetId = dummyProxy.createWithoutHost();

        ProxyDeleteRequest request = new ProxyDeleteRequest();
        request.addProxyId(targetId);

        ProxyDeleteResponse response = zabbixApi.proxy().delete(request);
        assertNotNull(response);

        Integer actualId = response.getResult().getProxyids().get(0);
        assertThat(actualId, is(targetId));
    }
}


ProxyGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.proxy;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.ZabbixApiParamter;
import com.zabbix4j.proxy.ProxyGetRequest;
import com.zabbix4j.proxy.ProxyGetResponse;

/**
 * @author Suguru Yajima
 */
public class ProxyGetTest extends ZabbixApiTestBase {
    public ProxyGetTest() {
        super();
    }

    @Test
    public void testGet() throws Exception {

        DummyProxy dummyProxy = new DummyProxy(zabbixApi);
        Integer targetId = dummyProxy.createWithoutHost();

        try {
            ProxyGetRequest request = new ProxyGetRequest();
            ProxyGetRequest.Params params = request.getParams();
            params.addProxyId(targetId);
            params.setSelectHosts(ZabbixApiParamter.QUERY.extend.name());

            ProxyGetResponse response = zabbixApi.proxy().get(request);
            assertNotNull(response);

            logger.debug(getGson().toJson(response));

            ProxyGetResponse.Result result = response.getResult().get(0);
            assertThat(result.getProxyid(), is(targetId));

        } finally {
            dummyProxy.delete(targetId);
        }
    }
}


ProxyUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.proxy;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;

import com.zabbix4j.proxy.ProxyObject;
import com.zabbix4j.proxy.ProxyUpdateRequest;
import com.zabbix4j.proxy.ProxyUpdateResponse;

/**
 * @author Suguru Yajima
 */
public class ProxyUpdateTest extends ZabbixApiTestBase {

    public ProxyUpdateTest() {
        super();
    }

    @Test
    public void testUpdate() throws Exception {
        DummyProxy dummyProxy = new DummyProxy(zabbixApi);
        Integer targetId = dummyProxy.createWithoutHost();

        try {
            ProxyUpdateRequest request = new ProxyUpdateRequest();
            ProxyUpdateRequest.Params params = request.getParams();

            params.setProxyid(targetId);
            params.setHost("Active proxy");
            params.setStatus(ProxyObject.STATUS.ACTIVE_PROXY.value);

            ProxyUpdateResponse response = zabbixApi.proxy().update(request);
            assertNotNull(response);

            logger.debug(getGson().toJson(response));

            Integer actualId = response.getResult().getProxyids().get(0);
            assertThat(actualId, is(targetId));
        } finally {
            dummyProxy.delete(targetId);
        }
    }
}

你可能感兴趣的:(zabbixApi4j-Proxy)