diff --git a/src/main/java/com/dispose/controller/DisposeTaskController.java b/src/main/java/com/dispose/controller/DisposeTaskController.java index 133a191e..ec1faee8 100644 --- a/src/main/java/com/dispose/controller/DisposeTaskController.java +++ b/src/main/java/com/dispose/controller/DisposeTaskController.java @@ -104,19 +104,18 @@ public class DisposeTaskController { StartTaskReq reqInfo = mr.getRequestObject(StartTaskReq.class); - if(reqInfo.getDisposeIp().length() == 0 || reqInfo.getDisposeIp().equals("null")){ + if (reqInfo.getDisposeIp().length() == 0 || reqInfo.getDisposeIp().equals("null")) { return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); } - if(reqInfo.getId() == null || reqInfo.getType() == null || String.valueOf(reqInfo.getType()).length() == 0){ + if (reqInfo.getId() == null || reqInfo.getType() == null || String.valueOf(reqInfo.getType()).length() == 0) { return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); } - if(reqInfo.getDisposeTime() == null || reqInfo.getDisposeTime() < 0 || String.valueOf(reqInfo.getDisposeTime()).length() == 0) { + if (reqInfo.getDisposeTime() == null || reqInfo.getDisposeTime() < 0 || String.valueOf(reqInfo.getDisposeTime()).length() == 0) { return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); } - StartTaskRsp rspInfo = new StartTaskRsp(); TaskInfoDetail task = TaskInfoDetail.builder() @@ -168,25 +167,20 @@ public class DisposeTaskController { } IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class); - TaskInfoRsp rspInfo = new TaskInfoRsp(); - if (reqInfo.getTaskId() == null - || reqInfo.getTaskId().length == 0) { - return ProtocolRespDTO.result(ErrorCode.ERR_INPUTMISS); + if (reqInfo.getTaskId() == null || reqInfo.getTaskId().length == 0) { + return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); } + TaskInfoRsp rspInfo = new TaskInfoRsp(); + Arrays.stream(reqInfo.getTaskId()).forEach(v -> { TaskInfoData taskData = TaskInfoData.builder().type(DeviceCapacity.CLEANUP.getCode()).build(); List> ret = taskService.stopTaskByTaskId(Long.parseLong(v)); if (ret.size() == 0) { - TaskInfoDetail taskDetail = disposeTaskMapper.getTaskInfoById(Long.valueOf(v)); - if (taskDetail != null) { - taskData.setId(String.valueOf(taskDetail.getDeviceId())); - } else { - taskData.setId("-1"); - } + taskData.setId("-1"); taskData.setTaskId(v); taskData.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode()); taskData.setMessage(ErrorCode.ERR_NOSUCHTASK.getMsg()); diff --git a/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java b/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java index 938f9254..8fab99c3 100644 --- a/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java +++ b/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java @@ -2,7 +2,9 @@ package com.dispose.controller; import com.dispose.Global.InitTestEnvironment; import com.dispose.common.ErrorCode; +import com.dispose.pojo.vo.common.TaskInfoData; import com.dispose.pojo.vo.task.StartTaskRsp; +import com.dispose.pojo.vo.task.TaskInfoRsp; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; @@ -19,6 +21,8 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import javax.annotation.Resource; +import java.util.List; + 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; @@ -69,16 +73,14 @@ public class TaskControllerExceptionSmokeTest extends InitTestEnvironment { Assert.assertNotNull(startTaskRsp.getStatus()); Assert.assertNotNull(startTaskRsp.getMessage()); - if(startTaskRsp.getStatus() == 0){ + if (startTaskRsp.getStatus() == 0) { Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); - }else if(startTaskRsp.getStatus() == 26){ + } else if (startTaskRsp.getStatus() == 26) { Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_TASKRUNNING.getCode())); } - } - @Test public void t2_NormalContainsNonEmptyTaskTest() throws Exception { String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + @@ -106,9 +108,9 @@ public class TaskControllerExceptionSmokeTest extends InitTestEnvironment { Assert.assertNotNull(startTaskRsp.getStatus()); Assert.assertNotNull(startTaskRsp.getMessage()); - if(startTaskRsp.getStatus() == 0){ + if (startTaskRsp.getStatus() == 0) { Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); - }else if(startTaskRsp.getStatus() == 26){ + } else if (startTaskRsp.getStatus() == 26) { Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_TASKRUNNING.getCode())); } } @@ -116,7 +118,7 @@ public class TaskControllerExceptionSmokeTest extends InitTestEnvironment { @Test public void t3_startTaskNoDisPoseIpExceptionTest() throws Exception { String reqDataNoIp = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + - "\\\"type\\\":0, \\\"disposeIp\\\":\\\"\\\", \\\"disposeTime\\\":120}\"}"; + "\\\"type\\\":0, \\\"disposeIp\\\":, \\\"disposeTime\\\":120}\"}"; String taskStart = mockMvc.perform(MockMvcRequestBuilders .post("/task/start") @@ -142,7 +144,7 @@ public class TaskControllerExceptionSmokeTest extends InitTestEnvironment { public void t4_startTaskNullDisposeIpExceptionTest() throws Exception { //disposeIp null String reqDataNullIp = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + - "\\\"type\\\":0, \\\"disposeIp\\\":\\\"null\\\", \\\"disposeTime\\\":120}\"}"; + "\\\"type\\\":0, \\\"disposeIp\\\":null, \\\"disposeTime\\\":120}\"}"; String taskStart = mockMvc.perform(MockMvcRequestBuilders .post("/task/start") @@ -343,4 +345,134 @@ public class TaskControllerExceptionSmokeTest extends InitTestEnvironment { Assert.assertNotNull(startTaskRsp.getMessage()); Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); } + + @Test + public void t12_NormalStopTaskTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"taskId\\\":[\\\"236\\\", \\\"235\\\" ]}\"}"; + + String taskStop = mockMvc.perform(MockMvcRequestBuilders + .post("/task/stop") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + + String regex = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z"; + TaskInfoRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStop), TaskInfoRsp.class); + List taskInfoList = startTaskRsp.getItems(); + for (TaskInfoData task : taskInfoList + ) { + Assert.assertNotNull(task.getId()); + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getStatus()); + Assert.assertNotNull(task.getMessage()); + + if (task.getStatus() == 0) { + Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + } else if (task.getStatus() == 30) { + Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_NOSUCHTASK.getCode())); + } + + if (task.getType() != null) { + Assert.assertNotEquals(String.valueOf(task.getType()), -1); + } + + if (task.getDisposeIp() != null) { + Assert.assertTrue(task.getDisposeIp().matches(regex)); + } + + if (task.getDisposeTime() != null) { + Assert.assertNotEquals(String.valueOf(task.getDisposeTime()), -1); + } + } + } + + @Test + public void t13_NormalStopNoTaskIdTaskTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"taskId\\\":}\"}"; + + String taskStop = mockMvc.perform(MockMvcRequestBuilders + .post("/task/stop") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + TaskInfoRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStop), TaskInfoRsp.class); + List taskInfoList = startTaskRsp.getItems(); + for (TaskInfoData task : taskInfoList + ) { + Assert.assertNotNull(task.getId()); + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getStatus()); + Assert.assertNotNull(task.getMessage()); + Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + } + + @Test + public void t14_NormalStopNullTaskIdTaskTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"taskId\\\":null}\"}"; + + String taskStop = mockMvc.perform(MockMvcRequestBuilders + .post("/task/stop") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + TaskInfoRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStop), TaskInfoRsp.class); + List taskInfoList = startTaskRsp.getItems(); + for (TaskInfoData task : taskInfoList + ) { + Assert.assertNotNull(task.getId()); + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getStatus()); + Assert.assertNotNull(task.getMessage()); + Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + } + + @Test + public void t15_NormalIntegerTaskIdTaskTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"taskId\\\":[445, 235]}\"}"; + + String taskStop = mockMvc.perform(MockMvcRequestBuilders + .post("/task/stop") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + + TaskInfoRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStop), TaskInfoRsp.class); + List taskInfoList = startTaskRsp.getItems(); + for (TaskInfoData task : taskInfoList + ) { + Assert.assertNotNull(task.getId()); + Assert.assertNotNull(task.getTaskId()); + Assert.assertNotNull(task.getStatus()); + Assert.assertNotNull(task.getMessage()); + //Assert.assertEquals(Long.valueOf(task.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + } }