REM:
1. 添加新增能力节点的单元测试(目前单个节点增加,支持批量增加)
2. 添加删除能力节点的单元测试(目前仅支持单个删除,不支持批量删除)
This commit is contained in:
chenlinghy 2020-04-29 11:30:00 +08:00
parent d2dcf85201
commit 6f60271227
2 changed files with 359 additions and 48 deletions

View File

@ -980,7 +980,6 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment {
.getResponse() .getResponse()
.getContentAsString(); .getContentAsString();
EnumMap<ErrorCode, String> ret = new EnumMap<ErrorCode, String>(ErrorCode.class);
String msgContent = verifyRep(linkstatus, reqTimeStamp); String msgContent = verifyRep(linkstatus, reqTimeStamp);
System.out.print("msgContent=" + msgContent); System.out.print("msgContent=" + msgContent);

View File

@ -3,14 +3,25 @@ package com.dispose.controller;
import com.dispose.Global.InitTestEnvironment; import com.dispose.Global.InitTestEnvironment;
import com.dispose.common.ConstValue; import com.dispose.common.ConstValue;
import com.dispose.common.DisposeDeviceType; 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.dto.ProtocolReqDTO;
import com.dispose.pojo.entity.DisposeDevice;
import com.dispose.pojo.po.MReturnType;
import com.dispose.pojo.po.NewNodeInfo; import com.dispose.pojo.po.NewNodeInfo;
import com.dispose.pojo.vo.common.IDArrayReq; import com.dispose.pojo.vo.common.IDArrayReq;
import com.dispose.pojo.vo.device.AddNodeReq; import com.dispose.pojo.vo.device.AddNodeReq;
import com.dispose.service.DisposeNodeManager;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource; import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 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.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -45,6 +58,42 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
@Resource @Resource
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
/**
* The Dispose device manager.
*/
@Resource
private DisposeNodeManager disposeNodeManager;
/**
* test Determine whether the ipAddr exists.
*/
public MReturnType<Boolean, String> verifyIpAddr(String ipAddr) {
MReturnType<Boolean, String> 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<Boolean, String> verifyId(String id) {
MReturnType<Boolean, String> 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. * T 1 add device.
* *
@ -53,38 +102,183 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
@Test @Test
public void t1_addDevice() throws Exception { public void t1_addDevice() throws Exception {
AddNodeReq addReq = AddNodeReq.builder() AddNodeReq addReq = AddNodeReq.builder()
.items(new ArrayList<>()) .items(new ArrayList<>())
.build(); .build();
addReq.getItems().add(NewNodeInfo.builder() addReq.getItems().add(NewNodeInfo.builder()
.ipAddr("10.88.77.15") .ipAddr("10.88.77.15")
.type(DisposeDeviceType.DPTECH_UMC.getCode()) .type(DisposeDeviceType.DPTECH_UMC.getCode())
.areaCode(0) .areaCode(0)
.name("中移杭研实验室清洗设备") .name("中移杭研实验室清洗设备")
.manufacturer("DPTech") .manufacturer("DPTech")
.model("UMC") .model("UMC")
.version("5.7.13") .version("5.7.13")
.readme("实验室测试设备") .readme("实验室测试设备")
.build()); .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<MReturnType<Boolean, String>> res = new ArrayList<MReturnType<Boolean, String>>();
List<NewNodeInfo> nesNodes = addReq.getItems();
for (NewNodeInfo node : nesNodes
) {
MReturnType<Boolean, String> 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<Boolean, String> 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(); ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION); reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(System.currentTimeMillis()); reqInfo.setTimeStamp(reqTimeStamp);
reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq)); reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq));
mockMvc.perform(MockMvcRequestBuilders List<DisposeDevice> decsBef = disposeNodeManager.getAllDisposeDevice();
.put("/manager/device")
.contentType(MediaType.APPLICATION_JSON) String deviceAdd = mockMvc.perform(MockMvcRequestBuilders
.header("Authorization", "Bearer " + getLogToken()) .put("/manager/device")
.content(objectMapper.writeValueAsString(reqInfo))) .contentType(MediaType.APPLICATION_JSON)
.andDo(print()).andExpect(status().isOk()) .header("Authorization", "Bearer " + getLogToken())
.andExpect(jsonPath("$.code").value(200)) .content(objectMapper.writeValueAsString(reqInfo)))
.andReturn() .andDo(print()).andExpect(status().isOk())
.getResponse() .andExpect(jsonPath("$.code").value(200))
.getContentAsString(); .andReturn()
.getResponse()
.getContentAsString();
List<DisposeDevice> 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. * T 2 del device.
* *
@ -93,29 +287,144 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
@Test @Test
public void t2_delDevice() throws Exception { public void t2_delDevice() throws Exception {
IDArrayReq reqData = IDArrayReq.builder() IDArrayReq reqData = IDArrayReq.builder()
.id(new String[]{"0"}) .id(new String[]{"120"})
.build(); .build();
Long reqTimeStamp = System.currentTimeMillis();
ProtocolReqDTO reqInfo = new ProtocolReqDTO(); ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION); reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(System.currentTimeMillis()); reqInfo.setTimeStamp(reqTimeStamp);
reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData)); reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData));
log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo)); log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo));
mockMvc.perform(MockMvcRequestBuilders List<MReturnType<Boolean, String>> res = new ArrayList<MReturnType<Boolean, String>>();
.delete("/manager/device") String[] ids = reqData.getId();
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken()) for (String s : ids) {
.content(objectMapper.writeValueAsString(reqInfo))) MReturnType<Boolean, String> re = verifyId(s);
.andDo(print()).andExpect(status().isOk()) if (re != null) {
.andExpect(jsonPath("$.code").value(200)) System.out.println(re.getFirstParam());
.andReturn() System.out.println(re.getSecondParam());
.getResponse() res.add(re);
.getContentAsString(); }
}
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<Boolean, String> 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<DisposeDevice> 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<DisposeDevice> 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. * T 3 del device err 1.
* *
@ -124,8 +433,8 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
@Test @Test
public void t3_delDeviceErr1() throws Exception { public void t3_delDeviceErr1() throws Exception {
IDArrayReq reqData = IDArrayReq.builder() IDArrayReq reqData = IDArrayReq.builder()
.id(new String[]{"0", "1", "2"}) .id(new String[]{"0", "1", "2"})
.build(); .build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO(); ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION); reqInfo.setVer(ConstValue.Protocol.VERSION);
@ -135,16 +444,19 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo)); log.info("Request Json:" + objectMapper.writeValueAsString(reqInfo));
mockMvc.perform(MockMvcRequestBuilders String delAll = mockMvc.perform(MockMvcRequestBuilders
.delete("/manager/device") .delete("/manager/device")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken()) .header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo))) .content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk()) .andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(525)) .andExpect(jsonPath("$.code").value(525))
.andReturn() .andReturn()
.getResponse() .getResponse()
.getContentAsString(); .getContentAsString();
System.out.println(delAll);
} }
} }