From 6f6027122777825ed33ef6edf3773823e8c45062 Mon Sep 17 00:00:00 2001 From: chenlinghy Date: Wed, 29 Apr 2020 11:30:00 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=83=BD=E5=8A=9B=E8=8A=82=E7=82=B9=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=88=E7=9B=AE=E5=89=8D=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=E5=A2=9E=E5=8A=A0=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=89=B9=E9=87=8F=E5=A2=9E=E5=8A=A0=EF=BC=89=202.=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E8=83=BD=E5=8A=9B=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=88?= =?UTF-8?q?=E7=9B=AE=E5=89=8D=E4=BB=85=E6=94=AF=E6=8C=81=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E5=88=A0=E9=99=A4=EF=BC=8C=E4=B8=8D=E6=94=AF=E6=8C=81=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=88=A0=E9=99=A4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DeviceNodeInfoControllerTest.java | 1 - .../DeviceNodeManagerControllerTest.java | 406 ++++++++++++++++-- 2 files changed, 359 insertions(+), 48 deletions(-) diff --git a/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java b/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java index 84139c46..cc60fc74 100644 --- a/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java +++ b/src/test/java/com/dispose/controller/DeviceNodeInfoControllerTest.java @@ -980,7 +980,6 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment { .getResponse() .getContentAsString(); - EnumMap ret = new EnumMap(ErrorCode.class); String msgContent = verifyRep(linkstatus, reqTimeStamp); System.out.print("msgContent=" + msgContent); diff --git a/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java b/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java index 987b8a6d..a27fb53b 100644 --- a/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java +++ b/src/test/java/com/dispose/controller/DeviceNodeManagerControllerTest.java @@ -3,14 +3,25 @@ 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.device.AddNodeReq; +import com.dispose.service.DisposeNodeManager; import com.fasterxml.jackson.databind.ObjectMapper; + import java.util.ArrayList; +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; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -20,6 +31,8 @@ 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 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; @@ -45,6 +58,42 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { @Resource private ObjectMapper objectMapper; + /** + * The Dispose device manager. + */ + @Resource + private DisposeNodeManager disposeNodeManager; + + /** + * test Determine whether the ipAddr exists. + */ + public MReturnType verifyIpAddr(String ipAddr) { + MReturnType ret; + boolean result = false; + DisposeDevice disposeDevice = disposeNodeManager.getDisposeDeviceByIp(ipAddr); + if (disposeDevice != null) { + result = true; + } + + ret = new MReturnType<>(result, ipAddr); + return ret; + } + + /** + * test Determine whether the Id exists. + */ + public MReturnType verifyId(String id) { + MReturnType ret; + boolean result = false; + DisposeDevice disposeDevice = disposeNodeManager.getDisposeDeviceById(Long.valueOf(id)); + if (disposeDevice != null) { + result = true; + } + + ret = new MReturnType<>(result, id); + return ret; + } + /** * T 1 add device. * @@ -53,38 +102,183 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { @Test public void t1_addDevice() throws Exception { AddNodeReq addReq = AddNodeReq.builder() - .items(new ArrayList<>()) - .build(); + .items(new ArrayList<>()) + .build(); addReq.getItems().add(NewNodeInfo.builder() - .ipAddr("10.88.77.15") - .type(DisposeDeviceType.DPTECH_UMC.getCode()) - .areaCode(0) - .name("中移杭研实验室清洗设备") - .manufacturer("DPTech") - .model("UMC") - .version("5.7.13") - .readme("实验室测试设备") - .build()); + .ipAddr("10.88.77.15") + .type(DisposeDeviceType.DPTECH_UMC.getCode()) + .areaCode(0) + .name("中移杭研实验室清洗设备") + .manufacturer("DPTech") + .model("UMC") + .version("5.7.13") + .readme("实验室测试设备") + .build()); + addReq.getItems().add(NewNodeInfo.builder() + .ipAddr("10.88.77.16") + .type(DisposeDeviceType.DPTECH_UMC.getCode()) + .areaCode(0) + .name("清洗设备") + .manufacturer("DPTech") + .model("UMC") + .version("5.7.10") + .readme("测试设备") + .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(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); + } + } + + String deviceAdd = mockMvc.perform(MockMvcRequestBuilders + .put("/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(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); + + 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, "成功"); + break; + } + } + } + } + } + + /** + * T 1 add device. + * + * @throws Exception the exception + */ + @Test + public void t1_addDevice1() throws Exception { + AddNodeReq addReq = AddNodeReq.builder() + .items(new ArrayList<>()) + .build(); + + addReq.getItems().add(NewNodeInfo.builder() + .ipAddr("10.88.77.16") + .type(DisposeDeviceType.DPTECH_UMC.getCode()) + .areaCode(0) + .name("清洗设备") + .manufacturer("DPTech") + .model("UMC") + .version("5.7.10") + .readme("测试设备") + .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(addReq)); - mockMvc.perform(MockMvcRequestBuilders - .put("/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(); + List decsBef = disposeNodeManager.getAllDisposeDevice(); + + String deviceAdd = mockMvc.perform(MockMvcRequestBuilders + .put("/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(); + + 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); + + 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"); + + //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(ipAddr)) { + before = true; + break; + } + } + + boolean after = false; + if (decsAfter != null && decsAfter.size() > 0) { + for (DisposeDevice v : decsAfter) { + if (v.getIpAddr().equals(ipAddr)) { + after = true; + break; + } + } + + if ((!before) && after) { + Assert.assertEquals(status, "0"); + Assert.assertEquals(message, "成功"); + } else { + Assert.assertEquals(status, "20"); + Assert.assertEquals(message, "设备已经存在"); + } + } + } + } } + /** * T 2 del device. * @@ -93,29 +287,144 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { @Test public void t2_delDevice() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{"0"}) - .build(); + .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(System.currentTimeMillis()); + reqInfo.setTimeStamp(reqTimeStamp); reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo)); - 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(); + 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(); + + 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 decsBef = disposeNodeManager.getAllDisposeDevice(); + + 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(); + + 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"); + + //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)) { + 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, "没有这个设备"); + } + } + } + } + } + + /** * T 3 del device err 1. * @@ -124,8 +433,8 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { @Test public void t3_delDeviceErr1() throws Exception { IDArrayReq reqData = IDArrayReq.builder() - .id(new String[]{"0", "1", "2"}) - .build(); + .id(new String[]{"0", "1", "2"}) + .build(); ProtocolReqDTO reqInfo = new ProtocolReqDTO(); reqInfo.setVer(ConstValue.Protocol.VERSION); @@ -135,16 +444,19 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment { log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo)); - 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(525)) - .andReturn() - .getResponse() - .getContentAsString(); + String delAll = 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(525)) + .andReturn() + .getResponse() + .getContentAsString(); + + System.out.println(delAll); + } }