REM:
1. 添加能力节点管理接口(添加节点、删除节点、修改节点)的单元测试
This commit is contained in:
chenlinghy 2020-08-07 10:32:11 +08:00
parent 1b73f87d8e
commit 0667a3d889
1 changed files with 355 additions and 0 deletions

View File

@ -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 <huangxin@cmhi.chinamoblie.com>
*/
@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<AddCapacityInfo> 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<AddDeviceReq> reqInfo = new ProtocolReqDTO<>();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode());
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(addReq);
List<DisposeDevice> 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<AddDeviceRsp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<AddDeviceRsp>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
List<DisposeDevice> 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<IdArraysReq> reqInfo = new ProtocolReqDTO<>();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode());
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(reqData);
List<DisposeDevice> 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<List<DeviceInfoRsp>> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<List<DeviceInfoRsp>>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
List<DisposeDevice> 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<AddCapacityInfo> newCapList = new ArrayList<>();
List<MulReturnType<Long, String>> 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<AddDeviceReq> 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<List<DeviceInfoRsp>> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<List<DeviceInfoRsp>>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
for (DeviceInfoRsp deviceInfoRsp : rspInfo.getMsgContent()
) {
List<MulReturnType<Long, String>> 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());
}
}
}
}