diff --git a/src/test/java/com/dispose/test/controller/DisposeDeviceManagerControllerTest.java b/src/test/java/com/dispose/test/controller/DisposeDeviceManagerControllerTest.java new file mode 100644 index 00000000..a71d7ce3 --- /dev/null +++ b/src/test/java/com/dispose/test/controller/DisposeDeviceManagerControllerTest.java @@ -0,0 +1,355 @@ +package com.dispose.test.controller; + +import com.dispose.common.*; +import com.dispose.mapper.DisposeDeviceMapper; +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.manager.*; +import com.dispose.pojo.entity.DisposeDevice; +import com.dispose.pojo.po.MulReturnType; +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 java.util.stream.Collectors; + +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 DisposeDeviceManagerControllerTest extends InitTestEnvironment { + /** + * The Mock mvc. + */ + @Resource + private MockMvc mockMvc; + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + /** + * The dispose device mapper. + */ + @Resource + private DisposeDeviceMapper disposeDeviceMapper; + + /** + * A 1 add dispose device. + * + * @throws Exception the exception + */ + @Test + public void a1_addDisposeDevice() throws Exception { + AddDeviceReq addReq = AddDeviceReq.builder() + .items(new ArrayList<>()) + .build(); + + List newCapList = new ArrayList<>(); + + newCapList.add(AddCapacityInfo.builder() + .capacityType(DisposeCapacityType.CLEANUP.getValue()) + .objectType(DisposeObjectType.IP.getValue()) + .ipType(IpAddrType.IPV4_IPV6.getValue()) + .protectIp("") + .build()); + + newCapList.add(AddCapacityInfo.builder() + .capacityType(DisposeCapacityType.BLOCKING.getValue()) + .objectType(DisposeObjectType.URL.getValue()) + .ipType(IpAddrType.IPV4_IPV6.getValue()) + .protectIp("") + .build()); + + addReq.getItems().add(AddDeviceInfo.builder() + .ipAddr("10.88.77.15") + .ipPort("") + .deviceType(DisposeDeviceType.DPTECH_UMC.getValue()) + .areaCode(0) + .deviceName("中移杭研实验室迪普清洗设备") + .manufacturer("DPTech") + .model("UMC") + .version("5.7.13") + .userName("admin") + .password("UMCAdministrator") + .urlPath("UMC/service/AbnormalFlowCleaningService") + .urlType(HttpType.HTTP.getValue()) + .readme("实验室测试设备") + .capacity(newCapList) + .build()); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(addReq); + + List decsBef = disposeDeviceMapper.selectAll(); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .put("/manager/device") + .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)); + + List decsAfter = disposeDeviceMapper.selectAll(); + + for (AddDeviceRet d : rspInfo.getMsgContent().getItems() + ) { + //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()) && v.getIpPort().equals(d.getIpPort())) { + before = true; + break; + } + } + } + + boolean after = false; + if (decsAfter != null && decsAfter.size() > 0) { + for (DisposeDevice v : decsAfter) { + if (v.getIpAddr().equals(d.getIpAddr()) && v.getIpPort().equals(d.getIpPort())) { + after = true; + break; + } + } + } + + Assert.assertNotNull(d.getIpAddr()); + Assert.assertNotNull(d.getIpPort()); + Assert.assertNotNull(d.getStatus()); + Assert.assertNotNull(d.getMessage()); + + if ((!before) && after) { + Assert.assertEquals(Long.valueOf(d.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + } else { + Assert.assertNotEquals(Long.valueOf(d.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + } + } + } + + /** + * B 1 remove dispose device. + * + * @throws Exception the exception + */ + @Test + public void b1_removeDisposeDevice() throws Exception { + IdArraysReq reqData = IdArraysReq.builder() + .id(new String[]{"1", "2"}) + .build(); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(reqData); + + List decsBef = disposeDeviceMapper.selectAll(); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .delete("/manager/device") + .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)); + + List decsAfter = disposeDeviceMapper.selectAll(); + + for (DeviceInfoRsp d : rspInfo.getMsgContent() + ) { + //before: device information exists after: no device information + boolean before = false; + if (decsBef != null && decsBef.size() > 0) { + for (DisposeDevice v : decsBef) { + if (v.getId().equals(Long.valueOf(d.getId()))) { + before = true; + break; + } + } + } + + boolean after = false; + if (decsAfter != null && decsAfter.size() > 0) { + for (DisposeDevice v : decsAfter) { + if (v.getId().equals(Long.valueOf(d.getId())) && (v.getStatus() == ObjectStatus.DELETED)) { + after = true; + break; + } + } + } + + Assert.assertNotNull(d.getId()); + Assert.assertNotNull(d.getStatus()); + Assert.assertNotNull(d.getMessage()); + + if (before && after) { + Assert.assertEquals(Long.valueOf(d.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + Assert.assertEquals(d.getDevStatus(), ObjectStatus.DELETED); + } else { + Assert.assertNotEquals(Long.valueOf(d.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + Assert.assertNotEquals(d.getDevStatus(), ObjectStatus.DELETED); + } + } + } + + /** + * C 1 upgrade dispose device. + * + * @throws Exception the exception + */ + @Test + public void c1_upgradeDisposeDevice() throws Exception { + AddDeviceReq addReq = AddDeviceReq.builder() + .items(new ArrayList<>()) + .build(); + + List newCapList = new ArrayList<>(); + + List> beforeRed = new ArrayList<>(); + + disposeDeviceMapper.selectAll().forEach(v -> { + if (v.getDevCapacity().size() == 0) { + newCapList.add(AddCapacityInfo.builder() + .capacityType(DisposeCapacityType.CLEANUP.getValue()) + .objectType(DisposeObjectType.IP.getValue()) + .ipType(IpAddrType.IPV4_IPV6.getValue()) + .protectIp("") + .build()); + } + + beforeRed.add(new MulReturnType<>(v.getId(), v.getVersion())); + + if (v.getVersion().equals("B5.2.27.7")) { + v.setVersion("5.7.13"); + } else { + v.setVersion("B5.2.27.7"); + } + + addReq.getItems().add(AddDeviceInfo.builder() + .ipAddr(v.getIpAddr()) + .ipPort(v.getIpPort()) + .deviceType(v.getDeviceType().getValue()) + .areaCode(v.getAreaCode()) + .deviceName(v.getDeviceName()) + .manufacturer(v.getManufacturer()) + .model(v.getModel()) + .version(v.getVersion()) + .userName(v.getUserName()) + .password(v.getPassword()) + .urlPath(v.getUrlPath()) + .urlType(v.getUrlType().getValue()) + .readme(v.getReadme()) + .capacity(newCapList) + .build()); + }); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO<>(); + + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode()); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(addReq); + + String ret = mockMvc.perform(MockMvcRequestBuilders + .post("/manager/upgrade") + .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)); + + for (DeviceInfoRsp deviceInfoRsp : rspInfo.getMsgContent() + ) { + + List> beforeDev = beforeRed.stream() + .filter(k -> k.getFirstParam().equals(Long.valueOf(deviceInfoRsp.getId()))) + .collect(Collectors.toList()); + + DisposeDevice afterDev = disposeDeviceMapper.getDeviceById(Long.valueOf(deviceInfoRsp.getId())); + log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(afterDev)); + + if (deviceInfoRsp.getStatus() == ErrorCode.ERR_OK.getCode()) { + log.info("+++++++++++++++++++++before{}", beforeDev.get(0).getSecondParam()); + log.info("+++++++++++++++++++++after{}", afterDev.getVersion()); + + Assert.assertNotNull(deviceInfoRsp); + Assert.assertNotNull(deviceInfoRsp.getId()); + Assert.assertNotNull(deviceInfoRsp.getMessage()); + Assert.assertNotEquals(afterDev.getVersion(), beforeDev.get(0).getSecondParam()); + } else { + Assert.assertEquals(afterDev.getVersion(), beforeDev.get(0).getSecondParam()); + } + } + } +}