From d60bf3da8ecfaf7773ef2cfd5f0f52f4ac293065 Mon Sep 17 00:00:00 2001 From: chenlinghy Date: Wed, 29 Apr 2020 18:21:45 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E4=BF=AE=E6=94=B9=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E4=B8=AD=E7=9A=84?= =?UTF-8?q?json=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=A7=A3=E6=9E=90=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=202.=20=E4=BF=AE=E6=94=B9=E5=90=84=E4=B8=AA=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=AD=E5=93=8D=E5=BA=94=E6=8A=A5=E6=96=87=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=88=A4=E7=A9=BA=203.=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=97=A0=E7=94=A8=E7=9A=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DeviceNodeInfoControllerTest.java | 1323 ++++++++--------- .../DeviceNodeManagerControllerTest.java | 190 +-- 2 files changed, 658 insertions(+), 855 deletions(-) diff --git a/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java b/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java index 8b92699d..9172867d 100644 --- a/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java +++ b/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java @@ -2,25 +2,22 @@ package com.dispose.controller; import com.dispose.Global.InitTestEnvironment; import com.dispose.common.ConstValue; -import com.dispose.common.ErrorCode; -import com.dispose.dispose.po.DeviceInfo; + import com.dispose.mapper.DisposeDeviceMapper; import com.dispose.pojo.dto.ProtocolReqDTO; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.vo.common.DisposeCapacity; import com.dispose.pojo.vo.common.IDArrayReq; import com.dispose.pojo.vo.common.TaskInfoData; -import com.dispose.pojo.vo.information.DisposeNodeData; +import com.dispose.pojo.vo.information.*; import com.dispose.service.DisposeNodeManager; import com.dispose.service.UserAccountService; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.EnumMap; -import java.util.Iterator; + import java.util.List; import javax.annotation.Resource; -import net.sf.json.JSONArray; -import net.sf.json.JSONObject; -import net.sf.json.JsonConfig; + import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; @@ -33,6 +30,7 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -92,6 +90,29 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { return -1L; } + /** + * test Determine whether the Id exists.. + * + * @return boolean true-exists + */ + public boolean verifyDeviceIdExists(String deviceId) { + boolean exists = false; + List decs = disposeNodeManager.getAllDisposeDevice(); + + if (decs != null && decs.size() > 0) { + for (DisposeDevice d : decs + ) { + if (String.valueOf(d.getId()).equals(deviceId)) { + exists = true; + break; + } + } + } + + return exists; + } + + /** * T 1 get version. * @@ -100,8 +121,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t1_getVersion() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -111,43 +132,35 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String ver = mockMvc.perform(MockMvcRequestBuilders - .post("/information/version") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/version") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(ver, reqTimeStamp); System.out.print("msgContent=" + msgContent); - JSONArray json = JSONArray.fromObject(msgContent); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String id = Content.getString("id"); - //判断是否存在该能力唯一标识符 - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs - ) { - String status = Content.getString("status"); - String message = Content.getString("message"); - //Mysql存在能力节点标识符,status"0" message"成功" 有对应的设备型号 - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - Assert.assertNotNull(Content.getString("version")); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } - } + //将json字符串转为VersionRsp类 + List verList = objectMapper.readValue(msgContent, new TypeReference>() { + }); + + for (VersionRsp versionRsp : verList + ) { + if (verifyDeviceIdExists(versionRsp.getId())) { + Assert.assertEquals(String.valueOf(versionRsp.getStatus()), "0"); + Assert.assertEquals(versionRsp.getMessage(), "成功"); + Assert.assertNotNull(versionRsp.getId()); + Assert.assertNotNull(versionRsp.getVersion()); + } else { + Assert.assertNotEquals(String.valueOf(versionRsp.getStatus()), "0"); + Assert.assertNotEquals(versionRsp.getMessage(), "成功"); } } - } @@ -159,8 +172,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t1_getVersionAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -170,29 +183,34 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String verAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/version") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/version") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(verAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - JSONArray json = JSONArray.fromObject(msgContent); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); + List verList = objectMapper.readValue(msgContent, new TypeReference>() { + }); - String status = Content.getString("status"); - String message = Content.getString("message"); - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - Assert.assertNotNull(Content.getString("version")); + for (VersionRsp versionRsp : verList + ) { + if (verifyDeviceIdExists(versionRsp.getId())) { + Assert.assertEquals(String.valueOf(versionRsp.getStatus()), "0"); + Assert.assertEquals(versionRsp.getMessage(), "成功"); + Assert.assertNotNull(versionRsp.getId()); + Assert.assertNotNull(versionRsp.getVersion()); + } else { + Assert.assertNotEquals(String.valueOf(versionRsp.getStatus()), "0"); + Assert.assertNotEquals(versionRsp.getMessage(), "成功"); + } } } @@ -205,8 +223,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t2_getDeviceInfo() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -216,71 +234,38 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String deviceInfo = mockMvc.perform(MockMvcRequestBuilders - .post("/information/deviceinfo") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/deviceinfo") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(deviceInfo, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - Iterator it = jsonObject.keys(); - System.out.println(); - while (it.hasNext()) { - String key = (String) it.next(); - String value = jsonObject.getString(key); - System.out.println("key: " + key + ",value:" + value); - } + //将json字符串转为DeviceInfoRsp类 + DeviceInfoRsp deviceInfoRsp = objectMapper.readValue(msgContent, DeviceInfoRsp.class); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); - - JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String id = Content.getString("id"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs - ) { - String status = Content.getString("status"); - String message = Content.getString("message"); - String memory = Content.getString("memory"); - String freeMemory = Content.getString("freeMemory"); - String cpuUsed = Content.getString("cpuUsed"); - - //Mysql存在能力节点标识符,status"0" message"成功" 有对应的设备型号 - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - Assert.assertNotEquals(memory, "0"); - Assert.assertNotEquals(freeMemory, "0"); - Assert.assertNotEquals(cpuUsed, "0"); - Assert.assertNotEquals(Content.getString("vendor"), null); - Assert.assertNotEquals(Content.getString("model"), null); - Assert.assertNotEquals(Content.getString("firmware"), null); - Assert.assertNotEquals(Content.getString("os"), null); - Assert.assertNotEquals(Content.getString("kernel"), null); - Assert.assertNotEquals(Content.getString("arch"), null); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - Assert.assertEquals(memory, "0"); - Assert.assertEquals(freeMemory, "0"); - Assert.assertEquals(cpuUsed, "0"); - } - } + List deviceInfoList = deviceInfoRsp.getItems(); + for (DeviceInfoData d : deviceInfoList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(d.getStatus(), 0); + Assert.assertEquals(d.getMessage(), "成功"); + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(d.getStatus(), 0); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } + /** * T 2 get device info all. * @@ -289,8 +274,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t2_getDeviceInfoAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -300,56 +285,33 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String deviceInfoAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/deviceinfo") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/deviceinfo") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(deviceInfoAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DeviceInfoRsp deviceInfoRsp = objectMapper.readValue(msgContent, DeviceInfoRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - //json字符串转成对象 解析设备信息 - DeviceInfo dis = (DeviceInfo) JSONObject.toBean(Content, DeviceInfo.class); - System.out.println(dis); - Assert.assertNotEquals(String.valueOf(dis.getVendor()), null); - Assert.assertNotEquals(String.valueOf(dis.getModel()), null); - Assert.assertNotEquals(String.valueOf(dis.getFirmware()), null); - Assert.assertNotEquals(String.valueOf(dis.getOs()), null); - Assert.assertNotEquals(String.valueOf(dis.getKernel()), null); - Assert.assertNotEquals(String.valueOf(dis.getArch()), null); - Assert.assertNotEquals(String.valueOf(dis.getMemory()), null); - Assert.assertNotEquals(String.valueOf(dis.getFreeMemory()), null); - Assert.assertNotEquals(String.valueOf(dis.getCpuUsed()), null); - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - Assert.assertEquals(Content.getString("memory"), "0"); - Assert.assertEquals(Content.getString("freeMemory"), "0"); - Assert.assertEquals(Content.getString("cpuUsed"), "0"); - } - } + List deviceInfoList = deviceInfoRsp.getItems(); + for (DeviceInfoData d : deviceInfoList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(d.getStatus(), 0); + Assert.assertEquals(d.getMessage(), "成功"); + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(d.getStatus(), 0); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -363,8 +325,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t3_getDeviceCapacity() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -374,57 +336,44 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String capacity = mockMvc.perform(MockMvcRequestBuilders - .post("/information/capacity") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/capacity") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(capacity, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.println("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DeviceCapacityRsp deviceCapacityRsp = objectMapper.readValue(msgContent, DeviceCapacityRsp.class); - JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String id = Content.getString("id"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs - ) { - String status = Content.getString("status"); - String message = Content.getString("message"); - - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - String capacity1 = Content.getString("capacity"); - //解析capacityArray - System.out.print("capacity=" + capacity1); - JSONArray json1 = JSONArray.fromObject(capacity1); - for (int j = 0; j < json1.size(); j++) { - JSONObject capContent = json1.getJSONObject(j); - Assert.assertNotEquals(capContent.getString("type"), "-1"); - Assert.assertNotEquals(capContent.getString("tolCapacity"), "-1"); - Assert.assertNotEquals(capContent.getString("usedCapacity"), "-1"); - } - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); + List deviceCapacityList = deviceCapacityRsp.getItems(); + for (DeviceCapacityData d : deviceCapacityList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + if (d.getCapacity().size() != 0) { + for (DisposeCapacity cap : d.getCapacity() + ) { + Assert.assertNotNull(cap.getType()); } } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } + /** * T 3 get device capacity all. * @@ -433,8 +382,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t3_getDeviceCapacityAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -444,52 +393,39 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String capacityAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/capacity") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/capacity") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(capacityAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.println("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DeviceCapacityRsp deviceCapacityRsp = objectMapper.readValue(msgContent, DeviceCapacityRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String id = Content.getString("id"); - String status = Content.getString("status"); - String message = Content.getString("message"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs - ) { - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - String cap = Content.getString("capacity"); - - net.sf.json.JSONArray capArray = JSONArray.fromObject(cap); - List list = JSONArray.toList(capArray, new DisposeCapacity(), new JsonConfig()); - for (Object o : list) { - DisposeCapacity dis = (DisposeCapacity) o; - Assert.assertNotEquals(String.valueOf(dis.getType()), "-1"); - Assert.assertNotEquals(String.valueOf(dis.getTolCapacity()), "-1"); - Assert.assertNotEquals(String.valueOf(dis.getUsedCapacity()), "-1"); - } - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); + List deviceCapacityList = deviceCapacityRsp.getItems(); + for (DeviceCapacityData d : deviceCapacityList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + if (d.getCapacity().size() != 0) { + for (DisposeCapacity cap : d.getCapacity() + ) { + Assert.assertNotNull(cap.getType()); } } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -502,8 +438,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t4_getDeviceProtectedIp() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -513,57 +449,45 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String protectedIp = mockMvc.perform(MockMvcRequestBuilders - .post("/information/protected_ip") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/protected_ip") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); - String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; + //String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String msgContent = verifyRep(protectedIp, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.println("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DeviceCapacityRsp deviceCapacityRsp = objectMapper.readValue(msgContent, DeviceCapacityRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String id = Content.getString("id"); - String status = Content.getString("status"); - String message = Content.getString("message"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs - ) { - //Mysql存在能力节点标识符,status"0" message"成功" 有对应的设备型号 - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - String cap = Content.getString("capacity"); - - net.sf.json.JSONArray capArray = JSONArray.fromObject(cap); - List list = JSONArray.toList(capArray, new DisposeCapacity(), new JsonConfig()); - for (Object o : list) { - DisposeCapacity dis = (DisposeCapacity) o; - Assert.assertNotEquals(String.valueOf(dis.getType()), "-1"); - Assert.assertNotEquals(String.valueOf(dis.getDisposeIp()), null); - //String[] disposeIps = dis.getDisposeIp().split(","); - //for (String disposeIp : disposeIps) { - //Assert.assertTrue(disposeIp.matches(regex)); - //} - } - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); + List deviceCapacityList = deviceCapacityRsp.getItems(); + for (DeviceCapacityData d : deviceCapacityList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + if (d.getCapacity().size() != 0) { + for (DisposeCapacity cap : d.getCapacity() + ) { + Assert.assertNotNull(cap.getType()); + Assert.assertNotNull(cap.getDisposeIp()); + //String[] disposeIps = cap.getDisposeIp().split(","); + //for (String disposeIp : disposeIps) { + //Assert.assertTrue(disposeIp.matches(regex)); + //} } } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -576,8 +500,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t4_getDeviceProtectedIpAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -587,61 +511,50 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String protectedIpAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/protected_ip") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/protected_ip") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); - String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; + //String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String msgContent = verifyRep(protectedIpAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.println("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DeviceCapacityRsp deviceCapacityRsp = objectMapper.readValue(msgContent, DeviceCapacityRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String id = Content.getString("id"); - String status = Content.getString("status"); - String message = Content.getString("message"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs - ) { - //Mysql存在能力节点标识符,status"0" message"成功" 有对应的设备型号 - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - String cap = Content.getString("capacity"); - - net.sf.json.JSONArray capArray = JSONArray.fromObject(cap); - List list = JSONArray.toList(capArray, new DisposeCapacity(), new JsonConfig()); - for (Object o : list) { - DisposeCapacity dis = (DisposeCapacity) o; - Assert.assertNotEquals(String.valueOf(dis.getType()), "-1"); - Assert.assertNotEquals(String.valueOf(dis.getDisposeIp()), null); - //String[] disposeIps = dis.getDisposeIp().split(","); - //for (String disposeIp : disposeIps) { - //Assert.assertTrue(disposeIp.matches(regex)); - //} - } - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); + List deviceCapacityList = deviceCapacityRsp.getItems(); + for (DeviceCapacityData d : deviceCapacityList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + if (d.getCapacity().size() != 0) { + for (DisposeCapacity cap : d.getCapacity() + ) { + Assert.assertNotNull(cap.getType()); + Assert.assertNotNull(cap.getDisposeIp()); + //String[] disposeIps = cap.getDisposeIp().split(","); + //for (String disposeIp : disposeIps) { + //Assert.assertTrue(disposeIp.matches(regex)); + //} } } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } + /** * T 3 get dispose node list. * @@ -650,8 +563,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t5_getDisposeNodeList() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -661,49 +574,38 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String nodeList = mockMvc.perform(MockMvcRequestBuilders - .post("/information/node_list") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/node_list") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String msgContent = verifyRep(nodeList, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DisposeNodeListRsp disposeNodeListRsp = objectMapper.readValue(msgContent, DisposeNodeListRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - //json字符串转成对象 解析设备信息 - DisposeNodeData dis = (DisposeNodeData) JSONObject.toBean(Content, DisposeNodeData.class); - System.out.println(dis); - Assert.assertNotEquals(dis.getName(), null); - Assert.assertNotEquals(dis.getType(), -1); - Assert.assertNotEquals(dis.getIp(), null); - Assert.assertTrue(dis.getIp().matches(regex)); - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } - } + List disposeNodeDataList = disposeNodeListRsp.getItems(); + for (DisposeNodeData d : disposeNodeDataList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + Assert.assertNotNull(String.valueOf(d.getType())); + Assert.assertNotNull(d.getIp()); + Assert.assertTrue(d.getIp().matches(regex)); + Assert.assertNotNull(d.getName()); + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -716,8 +618,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t5_getDisposeNodeListAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -727,49 +629,38 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String nodeListAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/node_list") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/node_list") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String msgContent = verifyRep(nodeListAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DisposeNodeListRsp disposeNodeListRsp = objectMapper.readValue(msgContent, DisposeNodeListRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - //json字符串转成对象 解析设备信息 - DisposeNodeData dis = (DisposeNodeData) JSONObject.toBean(Content, DisposeNodeData.class); - System.out.println(dis); - Assert.assertNotEquals(dis.getName(), null); - Assert.assertNotEquals(dis.getType(), -1); - Assert.assertNotEquals(dis.getIp(), null); - Assert.assertTrue(dis.getIp().matches(regex)); - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } - } + List disposeNodeDataList = disposeNodeListRsp.getItems(); + for (DisposeNodeData d : disposeNodeDataList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + Assert.assertNotNull(String.valueOf(d.getType())); + Assert.assertNotNull(d.getIp()); + Assert.assertTrue(d.getIp().matches(regex)); + Assert.assertNotNull(d.getName()); + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -782,8 +673,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t6_getDisposeNodeDetails() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -793,74 +684,43 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String disposeNodeDetails = mockMvc.perform(MockMvcRequestBuilders - .post("/information/node_details") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/node_details") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); - String cap = null; String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String msgContent = verifyRep(disposeNodeDetails, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DisposeNodeListRsp nodeDetailsRsp = objectMapper.readValue(msgContent, DisposeNodeListRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - String type = Content.getString("type"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertNotEquals(Content.getString("name"), null); - Assert.assertNotEquals(Content.getString("ip"), null); - Assert.assertTrue(Content.getString("ip").matches(regex)); - Assert.assertNotNull(Content.getString("areaCode")); -// Assert.assertNotNull(Content.getString("manufacturer")); -// Assert.assertNotNull(Content.getString("model")); -// Assert.assertNotNull(Content.getString("version")); -// Assert.assertNotNull(Content.getString("readme")); - - //解析capacityArray - if (Content.getString("capacity") != null) { - cap = Content.getString("capacity"); - } - - net.sf.json.JSONArray capArray = JSONArray.fromObject(cap); - List list = JSONArray.toList(capArray, new DisposeCapacity(), new JsonConfig()); - for (Object o : list) { - DisposeCapacity d = (DisposeCapacity) o; - Assert.assertNotEquals(String.valueOf(d.getType()), "-1"); - Assert.assertNotEquals(String.valueOf(d.getDisposeIp()), null); - //String[] disposeIps = dis.getDisposeIp().split(","); - //for (String disposeIp : disposeIps) { - //Assert.assertTrue(disposeIp.matches(regex)); - //} - - Assert.assertNotNull(String.valueOf(d.getTolCapacity())); - Assert.assertNotNull(String.valueOf(d.getUsedCapacity())); - } - - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } - Assert.assertNotEquals(type, "-1"); + List disposeNodeDataList = nodeDetailsRsp.getItems(); + for (DisposeNodeData d : disposeNodeDataList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + Assert.assertNotNull(String.valueOf(d.getType())); + Assert.assertNotNull(d.getIp()); + Assert.assertTrue(d.getIp().matches(regex)); + Assert.assertNotNull(d.getName()); + for (DisposeCapacity cap : d.getCapacity() + ) { + Assert.assertNotNull(cap.getType()); + Assert.assertNotNull(cap.getDisposeIp()); } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -873,8 +733,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t6_getDisposeNodeDetailsAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -884,74 +744,43 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String disposeNodeDetailsAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/node_details") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/node_details") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); - String cap = null; String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; String msgContent = verifyRep(disposeNodeDetailsAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + //将json字符串转为DeviceInfoRsp类 + DisposeNodeListRsp nodeDetailsRsp = objectMapper.readValue(msgContent, DisposeNodeListRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - String type = Content.getString("type"); - - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertNotEquals(Content.getString("name"), null); - Assert.assertNotEquals(Content.getString("ip"), null); - Assert.assertTrue(Content.getString("ip").matches(regex)); - Assert.assertNotNull(Content.getString("areaCode")); -// Assert.assertNotNull(Content.getString("manufacturer")); -// Assert.assertNotNull(Content.getString("model")); -// Assert.assertNotNull(Content.getString("version")); -// Assert.assertNotNull(Content.getString("readme")); - - //解析capacityArray - if (Content.getString("capacity") != null) { - cap = Content.getString("capacity"); - } - - net.sf.json.JSONArray capArray = JSONArray.fromObject(cap); - List list = JSONArray.toList(capArray, new DisposeCapacity(), new JsonConfig()); - for (Object o : list) { - DisposeCapacity d = (DisposeCapacity) o; - Assert.assertNotEquals(String.valueOf(d.getType()), "-1"); - Assert.assertNotEquals(String.valueOf(d.getDisposeIp()), null); - //String[] disposeIps = dis.getDisposeIp().split(","); - //for (String disposeIp : disposeIps) { - //Assert.assertTrue(disposeIp.matches(regex)); - //} - - Assert.assertNotNull(String.valueOf(d.getTolCapacity())); - Assert.assertNotNull(String.valueOf(d.getUsedCapacity())); - } - - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } - Assert.assertNotEquals(type, "-1"); + List disposeNodeDataList = nodeDetailsRsp.getItems(); + for (DisposeNodeData d : disposeNodeDataList + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + Assert.assertNotNull(String.valueOf(d.getType())); + Assert.assertNotNull(d.getIp()); + Assert.assertTrue(d.getIp().matches(regex)); + Assert.assertNotNull(d.getName()); + for (DisposeCapacity cap : d.getCapacity() + ) { + Assert.assertNotNull(cap.getType()); + Assert.assertNotNull(cap.getDisposeIp()); } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -959,8 +788,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t7_getLinkStatus() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -970,40 +799,34 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String linkstatus = mockMvc.perform(MockMvcRequestBuilders - .post("/information/linkstatus") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/linkstatus") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(linkstatus, reqTimeStamp); System.out.print("msgContent=" + msgContent); - net.sf.json.JSONArray json = JSONArray.fromObject(msgContent); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - String online = Content.getString("online"); + List linkStatusList = objectMapper.readValue(msgContent, new TypeReference>() { + }); - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - Assert.assertEquals(online, "1"); - } - } + for (LinkStatusRsp linkStatusRsp : linkStatusList + ) { + if (verifyDeviceIdExists(linkStatusRsp.getId())) { + Assert.assertEquals(String.valueOf(linkStatusRsp.getStatus()), "0"); + Assert.assertEquals(linkStatusRsp.getMessage(), "成功"); + Assert.assertNotNull(linkStatusRsp.getId()); + Assert.assertNotNull(String.valueOf(linkStatusRsp.getOnline())); + Assert.assertEquals(String.valueOf(linkStatusRsp.getOnline()), "1"); } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - Assert.assertEquals(online, "0"); + Assert.assertNotEquals(String.valueOf(linkStatusRsp.getStatus()), "0"); + Assert.assertNotEquals(linkStatusRsp.getMessage(), "成功"); + Assert.assertEquals(String.valueOf(linkStatusRsp.getOnline()), "0"); } } } @@ -1012,8 +835,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t7_getLinkStatusAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -1023,40 +846,34 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String linkStatusAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/linkstatus") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/linkstatus") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); String msgContent = verifyRep(linkStatusAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - net.sf.json.JSONArray json = JSONArray.fromObject(msgContent); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - String online = Content.getString("online"); + List linkStatusList = objectMapper.readValue(msgContent, new TypeReference>() { + }); - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - Assert.assertEquals(online, "1"); - } - } + for (LinkStatusRsp linkStatusRsp : linkStatusList + ) { + if (verifyDeviceIdExists(linkStatusRsp.getId())) { + Assert.assertEquals(String.valueOf(linkStatusRsp.getStatus()), "0"); + Assert.assertEquals(linkStatusRsp.getMessage(), "成功"); + Assert.assertNotNull(linkStatusRsp.getId()); + Assert.assertNotNull(String.valueOf(linkStatusRsp.getOnline())); + Assert.assertEquals(String.valueOf(linkStatusRsp.getOnline()), "1"); } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - Assert.assertEquals(online, "0"); + Assert.assertNotEquals(String.valueOf(linkStatusRsp.getStatus()), "0"); + Assert.assertNotEquals(linkStatusRsp.getMessage(), "成功"); + Assert.assertEquals(String.valueOf(linkStatusRsp.getOnline()), "0"); } } } @@ -1064,8 +881,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t8_getNodeAllRunTask() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -1075,59 +892,49 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String runTask = mockMvc.perform(MockMvcRequestBuilders - .post("/information/run_task") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/run_task") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); - String taskArray = null; String msgContent = verifyRep(runTask, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + NodeTaskRsp runTaskRsp = objectMapper.readValue(msgContent, NodeTaskRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); + List nodeTaskData = runTaskRsp.getItems(); + for (NodeTaskData d : nodeTaskData + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - //解析taskArray - if (Content.getString("taskArray") != null) { - taskArray = Content.getString("taskArray"); - } - - net.sf.json.JSONArray taskInfos = JSONArray.fromObject(taskArray); - List list = JSONArray.toList(taskInfos, new TaskInfoData(), new JsonConfig()); - for (Object o : list) { - TaskInfoData taskInfo = (TaskInfoData) o; - Assert.assertNotEquals(taskInfo.getTaskId(), -1); - Assert.assertNotEquals(String.valueOf(taskInfo.getType()), -1); - Assert.assertNotEquals(String.valueOf(taskInfo.getStartTime()), -1); - Assert.assertNotEquals(String.valueOf(taskInfo.getDisposeTime()), -1); - Assert.assertNotEquals(taskInfo.getDisposeIp().length(), 0); - Assert.assertNotNull(taskInfo.getFlowAttack()); - Assert.assertNotNull(taskInfo.getFlowClean()); - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } + // no task + if (d.getTaskArray().size() == 0) { + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); + } else { + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); } + + Assert.assertNotNull(d.getTaskArray()); + for (TaskInfoData task : d.getTaskArray() + ) { + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getType()); + Assert.assertNotNull(task.getStartTime()); + Assert.assertNotNull(task.getDisposeTime()); + Assert.assertNotNull(task.getDisposeIp()); + } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -1136,8 +943,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t8_getNodeAllRunTaskAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -1147,59 +954,49 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String runTaskAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/run_task") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/run_task") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); - String taskArray = null; String msgContent = verifyRep(runTaskAll, reqTimeStamp); System.out.print("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String items = jsonObject.getString("items"); - System.out.print("items=" + items); + NodeTaskRsp runTaskRsp = objectMapper.readValue(msgContent, NodeTaskRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(items); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); + List nodeTaskData = runTaskRsp.getItems(); + for (NodeTaskData d : nodeTaskData + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); - List decs = disposeNodeManager.getAllDisposeDevice(); - if (decs != null && decs.size() > 0) { - for (DisposeDevice v : decs) { - if (String.valueOf(v.getId()).equals(id)) { - //解析taskArray - if (Content.getString("taskArray") != null) { - taskArray = Content.getString("taskArray"); - } - - net.sf.json.JSONArray taskInfos = JSONArray.fromObject(taskArray); - List list = JSONArray.toList(taskInfos, new TaskInfoData(), new JsonConfig()); - for (Object o : list) { - TaskInfoData taskInfo = (TaskInfoData) o; - Assert.assertNotEquals(taskInfo.getTaskId(), -1); - Assert.assertNotEquals(String.valueOf(taskInfo.getType()), -1); - Assert.assertNotEquals(String.valueOf(taskInfo.getStartTime()), -1); - Assert.assertNotEquals(String.valueOf(taskInfo.getDisposeTime()), -1); - Assert.assertNotEquals(taskInfo.getDisposeIp().length(), 0); - Assert.assertNotNull(taskInfo.getFlowAttack()); - Assert.assertNotNull(taskInfo.getFlowClean()); - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } - } else { - Assert.assertNotEquals(status, "0"); - Assert.assertNotEquals(message, "成功"); - } + // no task + if (d.getTaskArray().size() == 0) { + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); + } else { + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); } + + Assert.assertNotNull(d.getTaskArray()); + for (TaskInfoData task : d.getTaskArray() + ) { + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getType()); + Assert.assertNotNull(task.getStartTime()); + Assert.assertNotNull(task.getDisposeTime()); + Assert.assertNotNull(task.getDisposeIp()); + } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); } } } @@ -1208,8 +1005,8 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { @Test public void t9_getNodeAllTask() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) - .build(); + .id(new String[]{String.valueOf(getExistsDeviceId()), "123"}) + .build(); Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); @@ -1219,40 +1016,116 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); String taskAll = mockMvc.perform(MockMvcRequestBuilders - .post("/information/all_task") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); + .post("/information/all_task") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + + String msgContent = verifyRep(taskAll, reqTimeStamp); + System.out.print("msgContent=" + msgContent); + + NodeTaskRsp allTaskRsp = objectMapper.readValue(msgContent, NodeTaskRsp.class); + + List allTaskData = allTaskRsp.getItems(); + for (NodeTaskData d : allTaskData + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + + // no task + if (d.getTaskArray().size() == 0) { + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); + } else { + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + } + + Assert.assertNotNull(d.getTaskArray()); + for (TaskInfoData task : d.getTaskArray() + ) { + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getCurrentStatus()); + Assert.assertNotNull(task.getType()); + Assert.assertNotNull(task.getStartTime()); + Assert.assertNotNull(task.getDisposeTime()); + Assert.assertNotNull(task.getDisposeIp()); + } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); + Assert.assertNotNull(d.getTaskArray()); + } + } } @Test public void t9_getNodeAllTaskAll() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{}) - .build(); + .id(new String[]{}) + .build(); + Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); reqInfo.setVer(ConstValue.Protocol.VERSION); reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); - reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setTimeStamp(reqTimeStamp); reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); - mockMvc.perform(MockMvcRequestBuilders - .post("/information/all_task") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); - } + String allTaskAll = mockMvc.perform(MockMvcRequestBuilders + .post("/information/all_task") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + String msgContent = verifyRep(allTaskAll, reqTimeStamp); + System.out.print("msgContent=" + msgContent); + + NodeTaskRsp allTaskRsp = objectMapper.readValue(msgContent, NodeTaskRsp.class); + + List allTaskData = allTaskRsp.getItems(); + for (NodeTaskData d : allTaskData + ) { + if (verifyDeviceIdExists(d.getId())) { + Assert.assertNotNull(d.getId()); + + // no task + if (d.getTaskArray().size() == 0) { + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); + } else { + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + } + + Assert.assertNotNull(d.getTaskArray()); + for (TaskInfoData task : d.getTaskArray() + ) { + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getCurrentStatus()); + Assert.assertNotNull(task.getType()); + Assert.assertNotNull(task.getStartTime()); + Assert.assertNotNull(task.getDisposeTime()); + Assert.assertNotNull(task.getDisposeIp()); + } + } else { + Assert.assertNotNull(d.getId()); + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getMessage(), "成功"); + Assert.assertNotNull(d.getTaskArray()); + } + } + } } diff --git a/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java b/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java index 2e998bfc..d2ca385f 100644 --- a/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java +++ b/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java @@ -3,15 +3,17 @@ package com.dispose.controller; import com.dispose.Global.InitTestEnvironment; import com.dispose.common.ConstValue; import com.dispose.common.DisposeDeviceType; -import com.dispose.common.ErrorCode; -import com.dispose.dispose.po.DeviceInfo; import com.dispose.pojo.dto.ProtocolReqDTO; import com.dispose.pojo.entity.DisposeDevice; import com.dispose.pojo.po.MReturnType; import com.dispose.pojo.po.NewNodeInfo; import com.dispose.pojo.vo.common.IDArrayReq; +import com.dispose.pojo.vo.common.IDReturnStatus; import com.dispose.pojo.vo.device.AddNodeReq; +import com.dispose.pojo.vo.device.AddNodeRetData; +import com.dispose.pojo.vo.device.AddNodeRsp; import com.dispose.service.DisposeNodeManager; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.ArrayList; @@ -19,8 +21,6 @@ import java.util.List; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; -import net.sf.json.JSONArray; -import net.sf.json.JSONObject; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,7 +32,6 @@ import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import static java.lang.Long.valueOf; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -118,26 +117,12 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { Long reqTimeStamp = System.currentTimeMillis(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); - reqInfo.setVer(ConstValue.Protocol.VERSION); reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); reqInfo.setTimeStamp(reqTimeStamp); reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq)); - List> res = new ArrayList<>(); - List nesNodes = addReq.getItems(); - - for (NewNodeInfo node : nesNodes - ) { - MReturnType re = verifyIpAddr(node.getIpAddr()); - if (re != null) { - boolean a = re.getFirstParam(); - String b = re.getSecondParam(); - System.out.println(a); - System.out.println(b); - res.add(re); - } - } + List decsBef = disposeNodeManager.getAllDisposeDevice(); String deviceAdd = mockMvc.perform(MockMvcRequestBuilders .put("/manager/device") @@ -150,30 +135,46 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { .getResponse() .getContentAsString(); + List decsAfter = disposeNodeManager.getAllDisposeDevice(); String msgContent = verifyRep(deviceAdd, reqTimeStamp); System.out.println("msgContent=" + msgContent); - org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent); - String result = jsonObject.getString("result"); - System.out.println("result=" + result); + //将json字符串转为AddNodeRsp类 + AddNodeRsp addNodeRsp = objectMapper.readValue(msgContent, AddNodeRsp.class); - net.sf.json.JSONArray json = JSONArray.fromObject(result); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String ipAddr = Content.getString("ipAddr"); - - if (disposeNodeManager.getDisposeDeviceByIp(ipAddr) != null) { - for (MReturnType r : res - ) { - if (r.getSecondParam().equals(ipAddr) && (!r.getFirstParam())) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); + List addNodeList = addNodeRsp.getResult(); + for (AddNodeRetData d : addNodeList + ) { + //before: no device information after: device information exists + boolean before = false; + if (decsBef != null && decsBef.size() > 0) { + for (DisposeDevice v : decsBef) { + if (v.getIpAddr().equals(d.getIpAddr())) { + before = true; break; } } } + + boolean after = false; + if (decsAfter != null && decsAfter.size() > 0) { + for (DisposeDevice v : decsAfter) { + if (v.getIpAddr().equals(d.getIpAddr())) { + after = true; + break; + } + } + } + + Assert.assertNotNull(d.getIpAddr()); + Assert.assertNotNull(d.getDevId()); + if ((!before) && after) { + Assert.assertEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertEquals(d.getMessage(), "成功"); + } else { + Assert.assertNotEquals(String.valueOf(d.getStatus()), "0"); + Assert.assertNotEquals(d.getIpAddr(), "成功"); + } } } @@ -184,75 +185,6 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { */ @Test public void t2_delDevice() throws Exception { - IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{"120"}) - .build(); - - Long reqTimeStamp = System.currentTimeMillis(); - ProtocolReqDTO reqInfo = new ProtocolReqDTO(); - reqInfo.setVer(ConstValue.Protocol.VERSION); - reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); - reqInfo.setTimeStamp(reqTimeStamp); - reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); - - log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo)); - - List> res = new ArrayList<>(); - String[] ids = reqData.getId(); - - for (String s : ids) { - MReturnType re = verifyId(s); - if (re != null) { - System.out.println(re.getFirstParam()); - System.out.println(re.getSecondParam()); - res.add(re); - } - } - - String deviceDel = mockMvc.perform(MockMvcRequestBuilders - .delete("/manager/device") - .contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + getLogToken()) - .content(objectMapper.writeValueAsString(reqInfo))) - .andDo(print()).andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value(200)) - .andReturn() - .getResponse() - .getContentAsString(); - - String msgContent = verifyRep(deviceDel, reqTimeStamp); - System.out.println("msgContent=" + msgContent); - - net.sf.json.JSONArray json = JSONArray.fromObject(msgContent); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); - - //delete success, no dispose device information - if (disposeNodeManager.getDisposeDeviceById(Long.valueOf(id)) == null) { - for (MReturnType r : res - ) { - if (r.getSecondParam().equals(id) && (r.getFirstParam().equals(true))) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertEquals(status, "19"); - Assert.assertEquals(message, "没有这个设备"); - } - } - } - } - } - - /** - * T 2 del device. - * - * @throws Exception the exception - */ - @Test - public void t2_delDevice1() throws Exception { IDArrayReq reqData = IDArrayReq.builder() .id(new String[]{"330"}) .build(); @@ -279,46 +211,44 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { .getContentAsString(); List decsAfter = disposeNodeManager.getAllDisposeDevice(); + String msgContent = verifyRep(deviceDel, reqTimeStamp); System.out.println("msgContent=" + msgContent); - net.sf.json.JSONArray json = JSONArray.fromObject(msgContent); - for (int i = 0; i < json.size(); i++) { - JSONObject Content = json.getJSONObject(i); - String status = Content.getString("status"); - String message = Content.getString("message"); - String id = Content.getString("id"); + List delNodeList = objectMapper.readValue(msgContent, new TypeReference>() { + }); - //before: device information exists - //after: no device information + for (IDReturnStatus iDReturnStatus : delNodeList + ) { + //before: device information exists after: no device information boolean before = false; if (decsBef != null && decsBef.size() > 0) { for (DisposeDevice v : decsBef) { - if (String.valueOf(v.getId()).equals(id)) { + if (String.valueOf(v.getId()).equals(iDReturnStatus.getId())) { before = true; break; } } + } - boolean after = false; - if (decsAfter != null && decsAfter.size() > 0) { - for (DisposeDevice v : decsAfter) { - if (String.valueOf(v.getId()).equals(id)) { - after = true; - break; - } - } - - //delete success, no dispose device information - if (before && !after) { - Assert.assertEquals(status, "0"); - Assert.assertEquals(message, "成功"); - } else { - Assert.assertEquals(status, "19"); - Assert.assertEquals(message, "没有这个设备"); + boolean after = false; + if (decsAfter != null && decsAfter.size() > 0) { + for (DisposeDevice v : decsAfter) { + if (String.valueOf(v.getId()).equals(iDReturnStatus.getId())) { + after = true; + break; } } } + + Assert.assertNotNull(iDReturnStatus.getId()); + if (before && (!after)) { + Assert.assertEquals(String.valueOf(iDReturnStatus.getStatus()), "0"); + Assert.assertEquals(iDReturnStatus.getMessage(), "成功"); + } else { + Assert.assertNotEquals(String.valueOf(iDReturnStatus.getStatus()), "0"); + Assert.assertNotEquals(iDReturnStatus.getMessage(), "成功"); + } } }