REM:
1. 修改接口单元测试中的json字符串解析方式
2. 修改各个接口中响应报文的字段判空
3. 删除无用的类
This commit is contained in:
chenlinghy 2020-04-29 18:21:45 +08:00
parent 46e0d940ca
commit d60bf3da8e
2 changed files with 658 additions and 855 deletions

View File

@ -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<MReturnType<Boolean, String>> res = new ArrayList<>();
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);
}
}
List<DisposeDevice> decsBef = disposeNodeManager.getAllDisposeDevice();
String deviceAdd = mockMvc.perform(MockMvcRequestBuilders
.put("/manager/device")
@ -150,30 +135,46 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
.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);
//将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<Boolean, String> r : res
) {
if (r.getSecondParam().equals(ipAddr) && (!r.getFirstParam())) {
Assert.assertEquals(status, "0");
Assert.assertEquals(message, "成功");
List<AddNodeRetData> 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<MReturnType<Boolean, String>> res = new ArrayList<>();
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();
@ -279,46 +211,44 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
.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");
List<IDReturnStatus> delNodeList = objectMapper.readValue(msgContent, new TypeReference<List<IDReturnStatus>>() {
});
//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(), "成功");
}
}
}