package test.com.qiniu.qvs;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.qvs.DeviceManager;
import com.qiniu.qvs.model.Device;
import com.qiniu.qvs.model.PatchOperation;
import com.qiniu.util.Auth;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import test.com.qiniu.TestConfig;

public class DeviceManagerTest {
    Auth auth = TestConfig.testAuth;
    private DeviceManager deviceManager;
    private Response res = null;
    private String namespaceId = "2xenzw3lpzpdz";
    private String gbId = "31011500991320000126";
    private String[] channels = {"31011500991320000126"};


    @Before
    public void setUp() throws Exception {
        this.deviceManager = new DeviceManager(auth);
    }

    @Test
    public void testCreateDevice() {
        Device device = new Device();
        device.setUsername("admin");
        device.setPassword("QQQNNN111");
        try {
            res = deviceManager.createDevice(namespaceId, device);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Assert.assertEquals("401", res.statusCode);
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testQueryDevice() {
        try {
            res = deviceManager.queryDevice(namespaceId, gbId);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Assert.assertEquals("401", res.statusCode);
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testUpdateDevice() {
        PatchOperation[] patchOperation = {new PatchOperation("replace", "name", "GBTEST")};
        try {
            res = deviceManager.updateDevice(namespaceId, gbId, patchOperation);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Assert.assertEquals("401", res.statusCode);
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testListDevice() {
        int offset = 0;
        int line = 3;
        int qtype = 0;
        String prefix = "310";
        String state = "notReg";
        try {
            res = deviceManager.listDevice(namespaceId, offset, line, prefix, state, qtype);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Assert.assertEquals("401", res.statusCode);
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testListChannels() {
        String prefix = "310";
        try {
            res = deviceManager.listChannels(namespaceId, gbId, prefix);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testStartDevice() {
        try {
            res = deviceManager.startDevice(namespaceId, gbId, channels);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testStopDevice() {
        try {
            res = deviceManager.stopDevice(namespaceId, gbId, channels);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }

    @Test
    public void testDeleteDevice() {
        try {
            res = deviceManager.deleteDevice(namespaceId, gbId);
            Assert.assertNotNull(res);
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Assert.assertEquals("401", res.statusCode);
            e.printStackTrace();
        } finally {
            if (res != null) {
                res.close();
            }
        }
    }
}

@@ -584,17 +584,17 @@
 32000012
-6
+7
 %22;%0A    p
@@ -645,9 +645,9 @@
 0012
-6
+7
 %22%7D;%0A
@@ -4199,459 +4199,7 @@
    %7D
-%0A%0A    @Test%0A    public void testDeleteDevice() %7B%0A        try %7B%0A            res = deviceManager.deleteDevice(namespaceId, gbId);%0A            Assert.assertNotNull(res);%0A            System.out.println(res.bodyString());%0A        %7D catch (QiniuException e) %7B%0A            Assert.assertEquals(%22401%22, res.statusCode);%0A            e.printStackTrace();%0A        %7D finally %7B%0A            if (res != null) %7B%0A                res.close();%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

update test of DeviceManager