OCT
REM: 1. 添加新增能力节点的单元测试(目前单个节点增加,支持批量增加) 2. 添加删除能力节点的单元测试(目前仅支持单个删除,不支持批量删除)
This commit is contained in:
parent
d2dcf85201
commit
6f60271227
|
@ -980,7 +980,6 @@ public class DeviceNodeInfoControllerTest extends InitTestEnvironment {
|
|||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
EnumMap<ErrorCode, String> ret = new EnumMap<ErrorCode, String>(ErrorCode.class);
|
||||
String msgContent = verifyRep(linkstatus, reqTimeStamp);
|
||||
System.out.print("msgContent=" + msgContent);
|
||||
|
||||
|
|
|
@ -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<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.
|
||||
*
|
||||
|
@ -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<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();
|
||||
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<DisposeDevice> 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<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.
|
||||
*
|
||||
|
@ -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<MReturnType<Boolean, String>> res = new ArrayList<MReturnType<Boolean, String>>();
|
||||
String[] ids = reqData.getId();
|
||||
|
||||
for (String s : ids) {
|
||||
MReturnType<Boolean, String> 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<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.
|
||||
*
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue