REM:
1. 添加单元测试用例
2. 移除实体类部分字段的无用注解
This commit is contained in:
HuangXin 2020-05-20 19:40:32 +08:00
parent 2f634fc540
commit d03c41a70f
5 changed files with 555 additions and 76 deletions

View File

@ -5,7 +5,6 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
/**
* The type New node info.
@ -35,26 +34,21 @@ public class NewNodeInfo {
/**
* The Name.
*/
@Nullable
private String name;
/**
* The Manufacturer.
*/
@Nullable
private String manufacturer;
/**
* The Model.
*/
@Nullable
private String model;
/**
* The Version.
*/
@Nullable
private String version;
/**
* The Readme.
*/
@Nullable
private String readme;
}

View File

@ -5,7 +5,6 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
/**
* The type Start task req.
@ -35,16 +34,13 @@ public class StartTaskReq {
/**
* The Flow direction.
*/
@Nullable
private Integer flowDirection;
/**
* The Attack type.
*/
@Nullable
private Integer[] attackType;
/**
* The Flow band width.
*/
@Nullable
private Integer flowBandwidth;
}

View File

@ -1,12 +1,12 @@
package com.dispose.test.controller;
import com.dispose.common.ErrorCode;
import com.dispose.test.Global.InitTestEnvironment;
import com.dispose.common.ConstValue;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.dto.ProtocolReqDTO;
import com.dispose.pojo.dto.ProtocolRespDTO;
import com.dispose.pojo.vo.auth.LoginReq;
import com.dispose.pojo.vo.auth.LoginRsp;
import com.dispose.test.Global.InitTestEnvironment;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
@ -57,9 +57,9 @@ public class AuthControllerTest extends InitTestEnvironment {
@Test
public void a1_login() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName(getUSER_NAME())
.password(getPASSWORD())
.build();
.userName(getUSER_NAME())
.password(getPASSWORD())
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION);
@ -68,14 +68,14 @@ public class AuthControllerTest extends InitTestEnvironment {
reqInfo.setMsgContent(objectMapper.writeValueAsString(logReq));
String ret = mockMvc.perform(MockMvcRequestBuilders
.post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andReturn()
.getResponse()
.getContentAsString();
.post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andReturn()
.getResponse()
.getContentAsString();
ProtocolRespDTO rspInfo = objectMapper.readValue(ret, ProtocolRespDTO.class);
LoginRsp logRsp = objectMapper.readValue(rspInfo.getMsgContent(), LoginRsp.class);
@ -83,16 +83,36 @@ public class AuthControllerTest extends InitTestEnvironment {
Assert.assertEquals(getLogToken(), logRsp.getToken());
}
/**
* Logout 2.
*
* @throws Exception the exception
*/
@Test
public void a2_logout() throws Exception {
public void a1_loginBadHead() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName("admin")
.build();
.userName(getUSER_NAME())
.password(getPASSWORD())
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(1);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(objectMapper.writeValueAsString(logReq));
mockMvc.perform(MockMvcRequestBuilders
.post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_VERSION.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
}
@Test
public void a1_loginBadUsername() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName(getUSER_NAME() + "123")
.password(getPASSWORD())
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION);
@ -101,15 +121,14 @@ public class AuthControllerTest extends InitTestEnvironment {
reqInfo.setMsgContent(objectMapper.writeValueAsString(logReq));
mockMvc.perform(MockMvcRequestBuilders
.post("/auth/logout")
.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();
.post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_USERNOTFOUND.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
}
/**
@ -118,7 +137,36 @@ public class AuthControllerTest extends InitTestEnvironment {
* @throws Exception the exception
*/
@Test
public void a3_logout() throws Exception {
public void b1_logout() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName("admin")
.build();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(objectMapper.writeValueAsString(logReq));
mockMvc.perform(MockMvcRequestBuilders
.post("/auth/logout")
.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();
}
/**
* Logout 2.
*
* @throws Exception the exception
*/
@Test
public void b2_logout() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName("admin2")
.build();
@ -142,7 +190,7 @@ public class AuthControllerTest extends InitTestEnvironment {
}
@Test
public void a4_logoutBadVersionTest() throws Exception {
public void b3_logoutBadVersionTest() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName("admin2")
.build();
@ -166,7 +214,7 @@ public class AuthControllerTest extends InitTestEnvironment {
}
@Test
public void a5_logoutBadAuthHeadTest() throws Exception {
public void b4_logoutBadAuthHeadTest() throws Exception {
LoginReq logReq = LoginReq.builder()
.userName("admin2")
.build();

View File

@ -255,7 +255,6 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
}
}
/**
* T 3 del device err 1.
*
@ -288,4 +287,134 @@ public class DeviceNodeManagerControllerTest extends InitTestEnvironment {
System.out.println(delAll);
}
@Test
public void t4_addDeviceBadHead() throws Exception {
AddNodeReq addReq = AddNodeReq.builder()
.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());
Long reqTimeStamp = System.currentTimeMillis();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(1);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(reqTimeStamp);
reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq));
List<DisposeDevice> decsBef = disposeNodeManager.getAllDisposeDevice();
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(ErrorCode.ERR_VERSION.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
}
@Test
public void t5_addDeviceBadAuth() throws Exception {
AddNodeReq addReq = AddNodeReq.builder()
.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());
Long reqTimeStamp = System.currentTimeMillis();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(1);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
reqInfo.setTimeStamp(reqTimeStamp);
reqInfo.setMsgContent(objectMapper.writeValueAsString(addReq));
List<DisposeDevice> decsBef = disposeNodeManager.getAllDisposeDevice();
mockMvc.perform(MockMvcRequestBuilders
.put("/manager/device")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken() + "dsasdfa")
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_LOGOUT.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
}
@Test
public void t6_delDeviceBadAuth() throws Exception {
IDArrayReq reqData = IDArrayReq.builder()
.id(new String[]{"1"})
.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));
mockMvc.perform(MockMvcRequestBuilders
.delete("/manager/device")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken() + "1234")
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_LOGOUT.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
}
@Test
public void t6_delDeviceBadHead() throws Exception {
IDArrayReq reqData = IDArrayReq.builder()
.id(new String[]{"1"})
.build();
Long reqTimeStamp = System.currentTimeMillis();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(1);
reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE);
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(ErrorCode.ERR_VERSION.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
}
}

View File

@ -1,5 +1,8 @@
package com.dispose.test.controller;
import com.dispose.common.DisposeTaskStatus;
import com.dispose.common.FlowDirection;
import com.dispose.service.TaskService;
import com.dispose.test.Global.InitTestEnvironment;
import com.dispose.common.ConstValue;
import com.dispose.common.DeviceCapacity;
@ -21,6 +24,7 @@ import com.dispose.pojo.vo.task.StopTaskData;
import com.dispose.pojo.vo.task.StopTaskReq;
import com.dispose.pojo.vo.task.TaskInfoRsp;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
@ -54,36 +58,23 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Transactional
@Rollback
@Slf4j
public class TaskControllerTest extends InitTestEnvironment {
/**
* The Mock mvc.
*/
@Resource
private MockMvc mockMvc;
/**
* The Object mapper.
*/
@Resource
private ObjectMapper objectMapper;
/**
* The Task cache manager.
*/
@Resource
private TaskCacheManager taskCacheManager;
/**
* The Dispose device mapper.
*/
@Resource
private DisposeDeviceMapper disposeDeviceMapper;
/**
* Gets exists device id.
*
* @return the exists device id
*/
@Resource
private TaskService taskService;
private Long getExistsDeviceId() {
List<DisposeDevice> lt = disposeDeviceMapper.selectAll();
@ -94,11 +85,6 @@ public class TaskControllerTest extends InitTestEnvironment {
return -1L;
}
/**
* Gets exists task id.
*
* @return the exists task id
*/
private Long getExistsTaskId() {
List<TaskInfoDetail> lt = taskCacheManager.getAllTask();
@ -109,6 +95,25 @@ public class TaskControllerTest extends InitTestEnvironment {
return -1L;
}
private Long createRunTaskStart() {
TaskInfoDetail taskData = TaskInfoDetail.builder()
.id(-1L)
.deviceId(1L)
.accountId(1L)
.type(DeviceCapacity.CLEANUP.getCode())
.disposeIp("192.168.5.3")
.attackType("0")
.flowDirection(FlowDirection.DIRECTION_TWOWAY.getCode())
.currentStatus(DisposeTaskStatus.TASK_NEW.getCode())
.planEndTime("60")
.build();
taskService.createTask(taskData);
taskCacheManager.upgradeTaskStatus(taskData.getId(), DisposeTaskStatus.TASK_RUNNING.getCode());
return taskData.getId();
}
/**
* T 1 start task.
@ -164,12 +169,65 @@ public class TaskControllerTest extends InitTestEnvironment {
}
/**
* T 2 stop task.
* T 2 a stop task.
*
* @throws Exception the exception
*/
@Test
public void t2_stopTask() throws Exception {
public void t2a_stopTask() throws Exception {
Long taskId = createRunTaskStart();
IDArrayReq reqData = IDArrayReq.builder()
.taskId(new String[]{String.valueOf(taskId), "2"})
.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));
String taskStop = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
String msgContent = verifyRep(taskStop, reqTimeStamp);
System.out.print("msgContent=" + msgContent);
//将json字符串转为TaskInfoRsp类
TaskInfoRsp taskStopRsp = objectMapper.readValue(msgContent, TaskInfoRsp.class);
List<TaskInfoData> taskInfoList = taskStopRsp.getItems();
for (TaskInfoData task : taskInfoList
) {
Assert.assertNotNull(task.getStatus());
Assert.assertNotNull(task.getMessage());
if (task.getStatus() == 0) {
Assert.assertNotNull(task.getId());
Assert.assertNotNull(task.getTaskId());
Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode()));
} else {
Assert.assertNotEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode()));
}
}
}
/**
* T 2 b stop task.
*
* @throws Exception the exception
*/
@Test
public void t2b_stopTask() throws Exception {
IDArrayReq reqData = IDArrayReq.builder()
.taskId(new String[]{String.valueOf(getExistsTaskId()), "2"})
.build();
@ -221,6 +279,68 @@ public class TaskControllerTest extends InitTestEnvironment {
*/
@Test
public void t3_stopNodeIpTask() throws Exception {
Long taskId = createRunTaskStart();
String disposeIp = taskCacheManager.getTaskById(taskId).getDisposeIp();
Long devId = taskCacheManager.getTaskById(taskId).getDeviceId();
StopTaskData itemData = StopTaskData.builder()
.disposeIp(disposeIp)
.type(DeviceCapacity.CLEANUP.getCode())
.id(String.valueOf(devId))
.build();
StopTaskReq reqData = new StopTaskReq();
reqData.setItems(new ArrayList<>());
reqData.getItems().add(itemData);
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));
String taskStopByIp = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop_ip")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
String msgContent = verifyRep(taskStopByIp, reqTimeStamp);
System.out.print("msgContent=" + msgContent);
//将json字符串转为TaskInfoRsp类
TaskInfoRsp taskStopByIpRsp = objectMapper.readValue(msgContent, TaskInfoRsp.class);
List<TaskInfoData> taskInfoList = taskStopByIpRsp.getItems();
for (TaskInfoData task : taskInfoList
) {
Assert.assertNotNull(task.getStatus());
Assert.assertNotNull(task.getMessage());
if (task.getStatus() == 0) {
Assert.assertNotNull(task.getId());
Assert.assertNotNull(task.getTaskId());
Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode()));
} else {
Assert.assertNotEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode()));
}
}
}
/**
* T 3 b stop node ip task.
*
* @throws Exception the exception
*/
@Test
public void t3b_stopNodeIpTask() throws Exception {
StopTaskData itemData = StopTaskData.builder()
.disposeIp("192.168.1.1")
.type(DeviceCapacity.CLEANUP.getCode())
@ -272,11 +392,18 @@ public class TaskControllerTest extends InitTestEnvironment {
}
}
/**
* T 3 a stop all node ip task.
*
* @throws Exception the exception
*/
@Test
public void t3_stopAllNodeIpTask() throws Exception {
public void t3a_stopAllNodeIpTask() throws Exception {
Long taskId = createRunTaskStart();
String disposeIp = taskCacheManager.getTaskById(taskId).getDisposeIp();
StopTaskData itemData = StopTaskData.builder()
.disposeIp("192.168.5.2")
.disposeIp(disposeIp)
.type(DeviceCapacity.CLEANUP.getCode())
.id("-1")
.build();
@ -327,14 +454,143 @@ public class TaskControllerTest extends InitTestEnvironment {
}
}
/**
* T 4 stop node task.
* T 3 b stop all node ip task.
*
* @throws Exception the exception
*/
@Test
public void t4_stopNodeTask() throws Exception {
public void t3b_stopAllNodeIpTask() throws Exception {
String devId;
List<DisposeDevice> devList = disposeDeviceMapper.selectAll();
if (devList != null && devList.size() > 0) {
devId = devList.get(0).getId().toString();
} else {
return;
}
StopTaskData itemData = StopTaskData.builder()
.disposeIp("192.168.5.2")
.type(DeviceCapacity.CLEANUP.getCode())
.id(devId)
.build();
StopTaskReq reqData = new StopTaskReq();
reqData.setItems(new ArrayList<>());
reqData.getItems().add(itemData);
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));
String taskStopByIp = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop_ip")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
String msgContent = verifyRep(taskStopByIp, reqTimeStamp);
System.out.print("msgContent=" + msgContent);
//将json字符串转为TaskInfoRsp类
TaskInfoRsp taskStopByIpRsp = objectMapper.readValue(msgContent, TaskInfoRsp.class);
List<TaskInfoData> taskInfoList = taskStopByIpRsp.getItems();
for (TaskInfoData task : taskInfoList
) {
Assert.assertNotNull(task.getStatus());
Assert.assertNotNull(task.getMessage());
if (task.getStatus() == ErrorCode.ERR_OK.getCode()) {
Assert.assertEquals((int) task.getStatus(), ErrorCode.ERR_OK.getCode());
Assert.assertEquals(task.getMessage(), ErrorCode.ERR_OK.getMsg());
Assert.assertNotNull(task.getId());
Assert.assertNotNull(task.getTaskId());
} else {
Assert.assertNotEquals((int) task.getStatus(), ErrorCode.ERR_OK.getCode());
}
}
}
/**
* T 4 a stop node task.
*
* @throws Exception the exception
*/
@Test
public void t4a_stopNodeTask() throws Exception {
Long taskId = createRunTaskStart();
Long devId = taskCacheManager.getTaskById(taskId).getDeviceId();
StopTaskData itemData = StopTaskData.builder()
.type(DeviceCapacity.CLEANUP.getCode())
.id(String.valueOf(devId))
.build();
StopTaskReq reqData = new StopTaskReq();
reqData.setItems(new ArrayList<>());
reqData.getItems().add(itemData);
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));
String taskStopByNode = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop_node")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
String msgContent = verifyRep(taskStopByNode, reqTimeStamp);
System.out.print("msgContent=" + msgContent);
//将json字符串转为TaskInfoRsp类
TaskInfoRsp taskStopByIpRsp = objectMapper.readValue(msgContent, TaskInfoRsp.class);
List<TaskInfoData> taskInfoList = taskStopByIpRsp.getItems();
for (TaskInfoData task : taskInfoList
) {
Assert.assertNotNull(task.getStatus());
Assert.assertNotNull(task.getMessage());
if (task.getStatus() == ErrorCode.ERR_OK.getCode()) {
Assert.assertEquals((int) task.getStatus(), ErrorCode.ERR_OK.getCode());
Assert.assertEquals(task.getMessage(), ErrorCode.ERR_OK.getMsg());
Assert.assertNotNull(task.getId());
Assert.assertNotNull(task.getTaskId());
} else {
Assert.assertNotEquals((int) task.getStatus(), ErrorCode.ERR_OK.getCode());
}
}
}
/**
* T 4 b stop node task.
*
* @throws Exception the exception
*/
@Test
public void t4b_stopNodeTask() throws Exception {
StopTaskData itemData = StopTaskData.builder()
.type(DeviceCapacity.CLEANUP.getCode())
.id("418")
@ -387,12 +643,66 @@ public class TaskControllerTest extends InitTestEnvironment {
}
/**
* T 5 stop all task.
* T 5 a stop all task.
*
* @throws Exception the exception
*/
@Test
public void t5_stopAllTask() throws Exception {
public void t5a_stopAllTask() throws Exception {
createRunTaskStart();
StopTaskData reqData = StopTaskData.builder()
.type(DeviceCapacity.CLEANUP.getCode())
.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));
String taskStopAll = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop_all")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + getLogToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
String msgContent = verifyRep(taskStopAll, reqTimeStamp);
System.out.print("msgContent=" + msgContent);
//将json字符串转为TaskInfoRsp类
TaskInfoRsp taskStopByIpRsp = objectMapper.readValue(msgContent, TaskInfoRsp.class);
List<TaskInfoData> taskInfoList = taskStopByIpRsp.getItems();
for (TaskInfoData task : taskInfoList
) {
Assert.assertNotNull(task.getStatus());
Assert.assertNotNull(task.getMessage());
if (task.getStatus() == ErrorCode.ERR_OK.getCode()) {
Assert.assertEquals((int) task.getStatus(), ErrorCode.ERR_OK.getCode());
Assert.assertEquals(task.getMessage(), ErrorCode.ERR_OK.getMsg());
Assert.assertNotNull(task.getId());
Assert.assertNotNull(task.getTaskId());
} else {
Assert.assertNotEquals((int) task.getStatus(), ErrorCode.ERR_OK.getCode());
}
}
}
/**
* T 5 b stop all task.
*
* @throws Exception the exception
*/
@Test
public void t5b_stopAllTask() throws Exception {
StopTaskData reqData = StopTaskData.builder()
.type(DeviceCapacity.CLEANUP.getCode())
.build();
@ -457,6 +767,8 @@ public class TaskControllerTest extends InitTestEnvironment {
reqInfo.setTimeStamp(reqTimeStamp);
reqInfo.setMsgContent(objectMapper.writeValueAsString(reqData));
createRunTaskStart();
String getTask = mockMvc.perform(MockMvcRequestBuilders
.post("/task/get_node")
.contentType(MediaType.APPLICATION_JSON)