zabbixApi4j-Image

Image

image.create: 创建新图像
image.delete: 删除图像
image.exists: 检查图像是否存在
image.get: 检索图像
image.getobjects: 通过过滤器检索图像
image.update: 更新图像

zabbixApi4j-Image_第1张图片
image.png


DummyImage
package cn.com.yeexun.testzabbix.zabbix4j.example.image;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;

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

import com.zabbix4j.ZabbixApi;
import com.zabbix4j.ZabbixApiException;
import com.zabbix4j.image.ImageCreateRequest;
import com.zabbix4j.image.ImageCreateResponse;
import com.zabbix4j.image.ImageDeleteRequest;
import com.zabbix4j.image.ImageDeleteResponse;
import com.zabbix4j.image.ImageObject;

/**
 * @author Suguru Yajima
 */
public class DummyImage extends ZabbixApiTestDummyMethodBase {

    public DummyImage(ZabbixApi zabbixApi) {
        super(zabbixApi);
    }

    public String encodeBase64(String imageFilePath) {
        File file = new File(imageFilePath);
        BufferedImage image = null;

        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        image.flush();
        try {
            ImageIO.write(image, "jpg", bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
        }

        byte[] bImage = baos.toByteArray();

        //return Base64.encodeBase64(bImage).toString();
        return Base64.encodeBase64String(bImage);
    }

    public Integer create() throws ZabbixApiException {

        String filepath = getClass().getClassLoader().getResource("cat.jpeg").getPath();
        DummyImage dummyImage = new DummyImage(zabbixApi);
        String base64Image = dummyImage.encodeBase64(filepath);

        ImageCreateRequest request = new ImageCreateRequest();
        ImageCreateRequest.Params params = request.getParams();
        params.setName("test image.create." + new Date().getTime());
        params.setImagetype(ImageObject.IMAGE_TYPE.ICON.value);
        params.setImage(base64Image);

        ImageCreateResponse response = zabbixApi.image().create(request);


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

        return actualId;
    }

    public void delete(Integer id) throws ZabbixApiException {
        ImageDeleteRequest request = new ImageDeleteRequest();
        request.addImageId(id);

        ImageDeleteResponse response = zabbixApi.image().delete(request);
    }
}


ImageCreateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.image;

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.image.ImageCreateRequest;
import com.zabbix4j.image.ImageCreateResponse;
import com.zabbix4j.image.ImageObject;

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

    public ImageCreateTest() {
        super();
    }

    @Test
    public void testCreate1() throws Exception {

        String filepath = getClass().getClassLoader().getResource("cat.jpeg").getPath();
        DummyImage dummyImage = new DummyImage(zabbixApi);
        String base64Image = dummyImage.encodeBase64(filepath);

        ImageCreateRequest request = new ImageCreateRequest();
        ImageCreateRequest.Params params = request.getParams();
        params.setName("test image.create." + new Date().getTime());
        params.setImagetype(ImageObject.IMAGE_TYPE.ICON.value);
        params.setImage(base64Image);

        ImageCreateResponse response = zabbixApi.image().create(request);
        assertNotNull(response);

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

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


ImageDeleteTest
package cn.com.yeexun.testzabbix.zabbix4j.example.image;

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

import org.junit.Test;

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

import com.zabbix4j.image.ImageDeleteRequest;
import com.zabbix4j.image.ImageDeleteResponse;


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

    public ImageDeleteTest() {
        super();
    }

    @Test
    public void testDelete() throws Exception {

        DummyImage dummyImage = new DummyImage(zabbixApi);
        Integer targetId = dummyImage.create();

        ImageDeleteRequest request = new ImageDeleteRequest();
        request.addImageId(targetId);

        ImageDeleteResponse response = zabbixApi.image().delete(request);
        assertNotNull(response);

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

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

    }
}


ImageGetTest
package cn.com.yeexun.testzabbix.zabbix4j.example.image;

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.image.ImageGetRequest;
import com.zabbix4j.image.ImageGetResponse;

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

    public ImageGetTest() {
        super();
    }

    @Test
    public void testGet1() throws Exception {

        Integer targetId = null;
        DummyImage dummyImage = new DummyImage(zabbixApi);
        try {
            targetId = dummyImage.create();

            ImageGetRequest request = new ImageGetRequest();
            ImageGetRequest.Params params = request.getParams();
            params.addImageId(targetId);
            params.setSelect_image(true);

            ImageGetResponse response = zabbixApi.image().get(request);
            assertNotNull(response);

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

            assertThat(targetId, is(actualId));

            logger.debug(getGson().toJson(response));
        } finally {
            dummyImage.delete(targetId);
        }
    }
}


ImageUpdateTest
package cn.com.yeexun.testzabbix.zabbix4j.example.image;

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.image.ImageUpdateRequest;
import com.zabbix4j.image.ImageUpdateResponse;


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

    @Test
    public void testUpdate1() throws Exception {

        DummyImage dummyImage = new DummyImage(zabbixApi);

        Integer targetId = dummyImage.create();

        ImageUpdateRequest request = new ImageUpdateRequest();
        ImageUpdateRequest.Params params = request.getParams();
        params.setImageid(targetId);

        String filepath = getClass().getClassLoader().getResource("cat.jpeg").getPath();
        String updateImage = dummyImage.encodeBase64(filepath);
        params.setImage(updateImage);

        params.setName("test.image.update");

        ImageUpdateResponse response = zabbixApi.image().update(request);
        assertNotNull(response);

        dummyImage.delete(targetId);

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

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

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