OCT
REM: 1. 修改接口单元测试中的json字符串解析方式 2. 修改各个接口中响应报文的字段判空 3. 删除无用的类
This commit is contained in:
parent
46e0d940ca
commit
d60bf3da8e
File diff suppressed because it is too large
Load Diff
|
@ -3,15 +3,17 @@ 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.entity.DisposeDevice;
|
||||||
import com.dispose.pojo.po.MReturnType;
|
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.common.IDReturnStatus;
|
||||||
import com.dispose.pojo.vo.device.AddNodeReq;
|
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.dispose.service.DisposeNodeManager;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -19,8 +21,6 @@ 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.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.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;
|
||||||
|
@ -118,26 +117,12 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
|
||||||
|
|
||||||
Long reqTimeStamp = System.currentTimeMillis();
|
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(reqTimeStamp);
|
reqInfo.setTimeStamp(reqTimeStamp);
|
||||||
reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq));
|
reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq));
|
||||||
|
|
||||||
List<MReturnType<Boolean, String>> res = new ArrayList<>();
|
List<DisposeDevice> decsBef = disposeNodeManager.getAllDisposeDevice();
|
||||||
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
|
String deviceAdd = mockMvc.perform(MockMvcRequestBuilders
|
||||||
.put("/manager/device")
|
.put("/manager/device")
|
||||||
|
@ -150,30 +135,46 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
|
||||||
.getResponse()
|
.getResponse()
|
||||||
.getContentAsString();
|
.getContentAsString();
|
||||||
|
|
||||||
|
List<DisposeDevice> decsAfter = disposeNodeManager.getAllDisposeDevice();
|
||||||
String msgContent = verifyRep(deviceAdd, reqTimeStamp);
|
String msgContent = verifyRep(deviceAdd, reqTimeStamp);
|
||||||
System.out.println("msgContent=" + msgContent);
|
System.out.println("msgContent=" + msgContent);
|
||||||
|
|
||||||
org.json.JSONObject jsonObject = new org.json.JSONObject(msgContent);
|
//将json字符串转为AddNodeRsp类
|
||||||
String result = jsonObject.getString("result");
|
AddNodeRsp addNodeRsp = objectMapper.readValue(msgContent, AddNodeRsp.class);
|
||||||
System.out.println("result=" + result);
|
|
||||||
|
|
||||||
net.sf.json.JSONArray json = JSONArray.fromObject(result);
|
List<AddNodeRetData> addNodeList = addNodeRsp.getResult();
|
||||||
for (int i = 0; i < json.size(); i++) {
|
for (AddNodeRetData d : addNodeList
|
||||||
JSONObject Content = json.getJSONObject(i);
|
) {
|
||||||
String status = Content.getString("status");
|
//before: no device information after: device information exists
|
||||||
String message = Content.getString("message");
|
boolean before = false;
|
||||||
String ipAddr = Content.getString("ipAddr");
|
if (decsBef != null && decsBef.size() > 0) {
|
||||||
|
for (DisposeDevice v : decsBef) {
|
||||||
if (disposeNodeManager.getDisposeDeviceByIp(ipAddr) != null) {
|
if (v.getIpAddr().equals(d.getIpAddr())) {
|
||||||
for (MReturnType<Boolean, String> r : res
|
before = true;
|
||||||
) {
|
|
||||||
if (r.getSecondParam().equals(ipAddr) && (!r.getFirstParam())) {
|
|
||||||
Assert.assertEquals(status, "0");
|
|
||||||
Assert.assertEquals(message, "成功");
|
|
||||||
break;
|
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
|
@Test
|
||||||
public void t2_delDevice() throws Exception {
|
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()
|
IDArrayReq reqData = IDArrayReq.builder()
|
||||||
.id(new String[]{"330"})
|
.id(new String[]{"330"})
|
||||||
.build();
|
.build();
|
||||||
|
@ -279,46 +211,44 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
|
||||||
.getContentAsString();
|
.getContentAsString();
|
||||||
|
|
||||||
List<DisposeDevice> decsAfter = disposeNodeManager.getAllDisposeDevice();
|
List<DisposeDevice> decsAfter = disposeNodeManager.getAllDisposeDevice();
|
||||||
|
|
||||||
String msgContent = verifyRep(deviceDel, reqTimeStamp);
|
String msgContent = verifyRep(deviceDel, reqTimeStamp);
|
||||||
System.out.println("msgContent=" + msgContent);
|
System.out.println("msgContent=" + msgContent);
|
||||||
|
|
||||||
net.sf.json.JSONArray json = JSONArray.fromObject(msgContent);
|
List<IDReturnStatus> delNodeList = objectMapper.readValue(msgContent, new TypeReference<List<IDReturnStatus>>() {
|
||||||
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
|
for (IDReturnStatus iDReturnStatus : delNodeList
|
||||||
//after: no device information
|
) {
|
||||||
|
//before: device information exists after: no device information
|
||||||
boolean before = false;
|
boolean before = false;
|
||||||
if (decsBef != null && decsBef.size() > 0) {
|
if (decsBef != null && decsBef.size() > 0) {
|
||||||
for (DisposeDevice v : decsBef) {
|
for (DisposeDevice v : decsBef) {
|
||||||
if (String.valueOf(v.getId()).equals(id)) {
|
if (String.valueOf(v.getId()).equals(iDReturnStatus.getId())) {
|
||||||
before = true;
|
before = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
boolean after = false;
|
boolean after = false;
|
||||||
if (decsAfter != null && decsAfter.size() > 0) {
|
if (decsAfter != null && decsAfter.size() > 0) {
|
||||||
for (DisposeDevice v : decsAfter) {
|
for (DisposeDevice v : decsAfter) {
|
||||||
if (String.valueOf(v.getId()).equals(id)) {
|
if (String.valueOf(v.getId()).equals(iDReturnStatus.getId())) {
|
||||||
after = true;
|
after = true;
|
||||||
break;
|
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, "没有这个设备");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(), "成功");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue