zabbixApi4j-Template
2018-01-11 本文已影响30人
差不多先生_tl
Template
template.create: 创建新模板
template.delete: 删除模板
template.exists: 检查模板是否存在
template.get: 检索模板
template.isreadable: 检查模板是否是可读的
template.iswritable: 检查模板是否是可写的
template.massadd: 将相关对象添加到模板
template.massremove: 将相关对象从模板移除
template.massupdate: 更新模板相关对象
template.update: 模板更新
DummyTemplate
package cn.com.yeexun.testzabbix.zabbix4j.example.template;
import java.util.Date;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestDummyMethodBase;
import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.template.TemplateCreateRequest;
import com.zabbix4j.template.TemplateCreateResponse;
import com.zabbix4j.template.TemplateDeleteRequest;
import com.zabbix4j.template.TemplateDeleteResponse;
/**
* @author Suguru Yajima
*/
public class DummyTemplate extends ZabbixApiTestDummyMethodBase {
public DummyTemplate(ZabbixApi zabbixApi) {
super(zabbixApi);
}
public Integer create() throws ZabbixApiException {
final Integer groupId = 25;
final Integer hostId = 10084;
TemplateCreateRequest request = new TemplateCreateRequest();
TemplateCreateRequest.Params params = request.getParams();
params.setHost("test.template.create." + new Date().getTime());
params.addGroupId(groupId);
params.addHostId(hostId);
TemplateCreateResponse response = zabbixApi.template().create(request);
Integer acutalId = response.getResult().getTemplateids().get(0);
return acutalId;
}
public void delete(Integer targetId) throws ZabbixApiException {
TemplateDeleteRequest request = new TemplateDeleteRequest();
request.addTemplateId(targetId);
TemplateDeleteResponse response = zabbixApi.template().delete(request);
}
}
TemplateCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.template;
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.template.TemplateCreateRequest;
import com.zabbix4j.template.TemplateCreateResponse;
/**
* @author Suguru Yajima
*/
public class TemplateCreateTest extends ZabbixApiTestBase {
public TemplateCreateTest() {
super();
}
@Test
public void testCreate() throws Exception {
final Integer groupId = 25;
final Integer hostId = 10084;
TemplateCreateRequest request = new TemplateCreateRequest();
TemplateCreateRequest.Params params = request.getParams();
params.setHost("test.template.create." + new Date().getTime());
params.addGroupId(groupId);
params.addHostId(hostId);
TemplateCreateResponse response = zabbixApi.template().create(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
Integer acutalId = response.getResult().getTemplateids().get(0);
assertNotNull(acutalId);
}
}
TemplateDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.template;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.hamcrest.core.Is;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.template.TemplateDeleteRequest;
import com.zabbix4j.template.TemplateDeleteResponse;
/**
* @author Suguru Yajima
*/
public class TemplateDeleteTest extends ZabbixApiTestBase {
public TemplateDeleteTest() {
super();
}
@Test
public void testDelete() throws Exception {
DummyTemplate dummyTemplate = new DummyTemplate(zabbixApi);
Integer targetId = dummyTemplate.create();
TemplateDeleteRequest request = new TemplateDeleteRequest();
request.addTemplateId(targetId);
TemplateDeleteResponse response = zabbixApi.template().delete(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
Integer actualId = response.getResult().getTemplateids().get(0);
assertThat(actualId, Is.is(targetId));
}
}
TemplateGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.template;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.hamcrest.core.Is;
import org.junit.Test;
import cn.com.yeexun.testzabbix.zabbix4j.common.ZabbixApiTestBase;
import com.zabbix4j.template.TemplateGetRequest;
import com.zabbix4j.template.TemplateGetResponse;
/**
* @author Suguru Yajima
*/
public class TemplateGetTest extends ZabbixApiTestBase {
public TemplateGetTest() {
super();
}
@Test
public void testGet() throws Exception {
DummyTemplate dummyTemplate = new DummyTemplate(zabbixApi);
Integer targetId = dummyTemplate.create();
try {
TemplateGetRequest request = new TemplateGetRequest();
TemplateGetRequest.Params params = request.getParams();
params.addTemplateId(targetId);
TemplateGetResponse response = zabbixApi.template().get(request);
assertNotNull(response);
logger.debug(getGson().toJson(response));
TemplateGetResponse.Result result = response.getResult().get(0);
assertThat(result.getTemplateid(), Is.is(targetId));
} finally {
dummyTemplate.delete(targetId);
}
}
}