diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetAreaInfoRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetAreaInfoRsp.java index f0130b63..e00a0c3e 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetAreaInfoRsp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetAreaInfoRsp.java @@ -4,10 +4,7 @@ package com.dispose.pojo.dto.protocol.device.info; import com.dispose.pojo.dto.protocol.base.BaseRespStatus; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.EqualsAndHashCode; +import lombok.*; import java.util.List; @@ -19,6 +16,7 @@ import java.util.List; @EqualsAndHashCode(callSuper = true) @Data @Builder +@NoArgsConstructor @AllArgsConstructor @JsonPropertyOrder({"items", "status", "message"}) @JsonInclude(JsonInclude.Include.NON_NULL) diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetDeviceInfoRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetDeviceInfoRsp.java index 4c78e00c..decf4031 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetDeviceInfoRsp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetDeviceInfoRsp.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.List; @@ -14,6 +15,7 @@ import java.util.List; */ @Data @Builder +@NoArgsConstructor @AllArgsConstructor @JsonPropertyOrder({"items", "status", "message"}) public class GetDeviceInfoRsp { diff --git a/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetPlatformVerInfoRsp.java b/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetPlatformVerInfoRsp.java index bd840bf5..f8b7880b 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetPlatformVerInfoRsp.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/device/info/GetPlatformVerInfoRsp.java @@ -3,10 +3,7 @@ package com.dispose.pojo.dto.protocol.device.info; import com.dispose.common.ProjectGitVersionInfo; import com.dispose.pojo.dto.protocol.base.BaseRespStatus; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.EqualsAndHashCode; +import lombok.*; /** * The type Get version info rsp. @@ -16,6 +13,7 @@ import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) @Data @Builder +@NoArgsConstructor @AllArgsConstructor @JsonPropertyOrder({"platVer", "status", "message"}) public class GetPlatformVerInfoRsp extends BaseRespStatus { diff --git a/src/test/java/com/dispose/test/controller/DisposeInfoControllerTest.java b/src/test/java/com/dispose/test/controller/DisposeInfoControllerTest.java new file mode 100644 index 00000000..0bb14ab1 --- /dev/null +++ b/src/test/java/com/dispose/test/controller/DisposeInfoControllerTest.java @@ -0,0 +1,341 @@ +package com.dispose.test.controller; + +import com.dispose.common.ConstValue; +import com.dispose.common.ErrorCode; +import com.dispose.common.ProtoCryptoType; +import com.dispose.pojo.dto.protocol.base.IdArraysReq; +import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO; +import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; +import com.dispose.pojo.dto.protocol.device.info.GetAreaInfoRsp; +import com.dispose.pojo.dto.protocol.device.info.GetDeviceInfoRsp; +import com.dispose.pojo.dto.protocol.device.info.GetPlatformVerInfoRsp; +import com.dispose.service.DisposeDeviceManagerService; +import com.dispose.test.Global.InitTestEnvironment; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import jodd.net.HttpStatus; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +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 javax.annotation.Resource; +import java.util.ArrayList; +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; + +/** + * The type Auth controller test. + * + * @author + */ +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Slf4j +public class DisposeInfoControllerTest extends InitTestEnvironment { + /** + * The Mock mvc. + */ + @Resource + private MockMvc mockMvc; + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + /** + * The dispose device manager service. + */ + @Resource + private DisposeDeviceManagerService disposeDeviceManagerService; + + /** + * A 1 Gets all dispose device area info. + * + * @throws Exception the exception + */ + @Test + public void a1_getAllDisposeDeviceAreaInfo() throws Exception { + String ret = mockMvc.perform(MockMvcRequestBuilders + .get("/info/areaInfo") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken()) + .content(objectMapper.writeValueAsString(null))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(HttpStatus.ok().status())) + .andReturn() + .getResponse() + .getContentAsString(); + + ProtocolRespDTO rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + verifyRespProtocol(rspInfo); + + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo)); + + Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_OK.getCode())); + Assert.assertEquals(rspInfo.getMsgContent().getMessage()[0], ErrorCode.ERR_OK.getMsg()); + + rspInfo.getMsgContent().getItems().forEach(v -> { + Assert.assertNotNull(v.getNodeId()); + Assert.assertNotNull(v.getTotalNetflow()); + Assert.assertNotNull(v.getOnlineDevices()); + Assert.assertNotNull(v.getCapacityType()); + }); + } + + /** + * A 2 Gets platform version info. + * + * @throws Exception the exception + */ + @Test + public void a2_getPlatformVersionInfo() throws Exception { + String ret = mockMvc.perform(MockMvcRequestBuilders + .get("/info/platformVersion") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken()) + .content(objectMapper.writeValueAsString(null))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(HttpStatus.ok().status())) + .andReturn() + .getResponse() + .getContentAsString(); + + ProtocolRespDTO rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + verifyRespProtocol(rspInfo); + + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo)); + + Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_OK.getCode())); + Assert.assertEquals(rspInfo.getMsgContent().getMessage()[0], ErrorCode.ERR_OK.getMsg()); + + Assert.assertNotNull(rspInfo.getMsgContent().getPlatVer()); + } + + /** + * A 3 Gets device version info. + * + * @throws Exception the exception + */ + @Test + public void a3_getDeviceVersionInfo() throws Exception { + List devIdList = new ArrayList<>(); + + disposeDeviceManagerService.getAllDisposeDevice().forEach(v -> devIdList.add(String.valueOf(v.getId()))); + + String[] devIds = new String[devIdList.size()]; + IdArraysReq req = IdArraysReq.builder() + .id(devIdList.toArray(devIds)) + .build(); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(req); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/info/deviceVersion") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(HttpStatus.ok().status())) + .andReturn() + .getResponse() + .getContentAsString(); + + ProtocolRespDTO rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + verifyRespProtocol(rspInfo); + + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo)); + + rspInfo.getMsgContent().getItems().forEach(v -> { + if (v.getStatus() == ErrorCode.ERR_OK.getCode()) { + Assert.assertEquals(v.getMessage()[0], ErrorCode.ERR_OK.getMsg()); + Assert.assertNotEquals(rspInfo.getMsgContent().getItems().size(), 0); + + Assert.assertNotNull(v.getId()); + Assert.assertNotNull(v.getIpAddr()); + Assert.assertNotNull(v.getIpPort()); + Assert.assertNotNull(v.getVersion()); + } else { + Assert.assertEquals(v.getMessage()[0], ErrorCode.ERR_NOSUCHDEVICE.getMsg()); + Assert.assertEquals(rspInfo.getMsgContent().getItems().size(), 0); + Assert.assertNotNull(v.getId()); + } + }); + } + + /** + * A 4 Gets not existed device version info. + * + * @throws Exception the exception + */ + @Test + public void a4_getDeviceNotExistedVersionInfo() throws Exception { + IdArraysReq req = IdArraysReq.builder() + .id(new String[]{"999", "1000"}) + .build(); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(req); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/info/deviceVersion") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(HttpStatus.ok().status())) + .andReturn() + .getResponse() + .getContentAsString(); + + ProtocolRespDTO rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + verifyRespProtocol(rspInfo); + + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo)); + + rspInfo.getMsgContent().getItems().forEach(v -> { + Assert.assertEquals(v.getMessage()[0], ErrorCode.ERR_NOSUCHDEVICE.getMsg()); + Assert.assertNotEquals(rspInfo.getMsgContent().getItems().size(), 0); + Assert.assertNotNull(v.getId()); + }); + } + + /** + * A 5 Gets device link status info. + * + * @throws Exception the exception + */ + @Test + public void a5_getDeviceLinkStatusInfo() throws Exception { + List devIdList = new ArrayList<>(); + + disposeDeviceManagerService.getAllDisposeDevice().forEach(v -> devIdList.add(String.valueOf(v.getId()))); + + String[] devIds = new String[devIdList.size()]; + IdArraysReq req = IdArraysReq.builder() + .id(devIdList.toArray(devIds)) + .build(); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(req); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/info/deviceLinkStatus") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(HttpStatus.ok().status())) + .andReturn() + .getResponse() + .getContentAsString(); + + ProtocolRespDTO rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + verifyRespProtocol(rspInfo); + + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo)); + + rspInfo.getMsgContent().getItems().forEach(v -> { + if (v.getStatus() == ErrorCode.ERR_OK.getCode()) { + Assert.assertEquals(v.getMessage()[0], ErrorCode.ERR_OK.getMsg()); + Assert.assertNotEquals(rspInfo.getMsgContent().getItems().size(), 0); + + Assert.assertNotNull(v.getId()); + Assert.assertNotNull(v.getIpAddr()); + Assert.assertNotNull(v.getIpPort()); + Assert.assertNotNull(v.getOnline()); + } else { + Assert.assertEquals(v.getMessage()[0], ErrorCode.ERR_NOSUCHDEVICE.getMsg()); + Assert.assertEquals(rspInfo.getMsgContent().getItems().size(), 0); + Assert.assertNotNull(v.getId()); + } + }); + } + + /** + * A 6 Gets not existed device link status info. + * + * @throws Exception the exception + */ + @Test + public void a6_getDeviceLinkStatusInfo() throws Exception { + IdArraysReq req = IdArraysReq.builder() + .id(new String[]{"999", "1000"}) + .build(); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(req); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/info/deviceLinkStatus") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken()) + .content(objectMapper.writeValueAsString(reqInfo))) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(HttpStatus.ok().status())) + .andReturn() + .getResponse() + .getContentAsString(); + + ProtocolRespDTO rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + verifyRespProtocol(rspInfo); + + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo)); + + rspInfo.getMsgContent().getItems().forEach(v -> { + Assert.assertEquals(v.getMessage()[0], ErrorCode.ERR_NOSUCHDEVICE.getMsg()); + Assert.assertNotEquals(rspInfo.getMsgContent().getItems().size(), 0); + Assert.assertNotNull(v.getId()); + }); + } +}