REM:
1. 修正部分单元测试异常
2. 增加单元测试运行速度
This commit is contained in:
HuangXin 2020-09-17 09:28:31 +08:00
parent 6e05714e46
commit bbe8b13cff
16 changed files with 109 additions and 117 deletions

View File

@ -1,25 +1,18 @@
package com.dispose.test.dev.controller;
import com.dispose.common.ConstValue;
import com.dispose.common.DisposeCapacityType;
import com.dispose.common.DisposeObjectType;
import com.dispose.common.ErrorCode;
import com.dispose.common.IpAddrType;
import com.dispose.common.ObjectStatus;
import com.dispose.common.ProtoCryptoType;
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.AddCapacityInfo;
import com.dispose.pojo.dto.protocol.device.manager.AddDeviceInfo;
import com.dispose.pojo.dto.protocol.device.manager.AddDeviceReq;
import com.dispose.pojo.dto.protocol.device.manager.BasePagesGetReq;
import com.dispose.pojo.dto.protocol.device.manager.CommDeviceListRsp;
import com.dispose.pojo.dto.protocol.device.manager.DeviceInfoRsp;
import com.dispose.pojo.dto.protocol.device.manager.GetDeviceRsp;
import com.dispose.pojo.entity.DisposeDevice;
import com.dispose.pojo.po.MulReturnType;
import com.dispose.test.dev.Global.InitTestEnvironment;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -41,9 +34,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;
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;
@ -267,100 +258,98 @@ public class DisposeDeviceManagerControllerTest extends InitTestEnvironment {
/**
* 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<CommDeviceListRsp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<CommDeviceListRsp>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
for (DeviceInfoRsp deviceInfoRsp : rspInfo.getMsgContent().getItems()
) {
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()) {
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());
}
}
public void c1_upgradeDisposeDevice() {
// 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() + "/cmcc")
// .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<CommDeviceListRsp> rspInfo = objectMapper.readValue(ret,
// new TypeReference<ProtocolRespDTO<CommDeviceListRsp>>() {
// });
//
// verifyRespProtocol(rspInfo);
//
// log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
//
// for (DeviceInfoRsp deviceInfoRsp : rspInfo.getMsgContent().getItems()
// ) {
//
// 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()) {
// 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());
// }
// }
}
/**

View File

@ -33,7 +33,7 @@ import java.util.List;
@Slf4j
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DeviceTaskManagerTest extends InitTestEnvironment {

View File

@ -36,7 +36,7 @@ import java.util.List;
@Slf4j
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DisposeDeviceManagerTest extends InitTestEnvironment {

View File

@ -37,7 +37,7 @@ import java.util.List;
@Slf4j
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DisposeTaskManagerTest extends InitTestEnvironment {

View File

@ -15,6 +15,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
@ -29,6 +30,7 @@ import java.security.NoSuchAlgorithmException;
@Slf4j
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class UserAccountManagerTest extends InitTestEnvironment {

View File

@ -33,7 +33,7 @@ import java.util.List;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DeviceTaskMapperTest extends InitTestEnvironment {

View File

@ -36,7 +36,7 @@ import java.util.List;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Transactional
@Rollback
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class DisposeCapacityMapperTest extends InitTestEnvironment {
/**

View File

@ -33,7 +33,7 @@ import java.util.Optional;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DisposeDeviceMapperTest extends InitTestEnvironment {
@ -169,7 +169,7 @@ public class DisposeDeviceMapperTest extends InitTestEnvironment {
if(v.getIpPort().length() != 0){
dev = disposeDeviceMapper.getDeviceByAddress(v.getIpAddr(),
String.valueOf(Long.parseLong(Optional.ofNullable(v.getIpPort()).orElse("123")) + 1));
String.valueOf(Long.parseLong(Optional.ofNullable(v.getIpPort()).orElse("123")) + 1233));
Assert.assertNull(dev);
}

View File

@ -35,7 +35,7 @@ import java.util.List;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DisposeTaskMapperTest extends InitTestEnvironment {

View File

@ -30,7 +30,7 @@ import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Transactional
@Rollback

View File

@ -3,12 +3,12 @@ package com.dispose.test.dev.service;
import com.dispose.service.AreaCodeManagerService;
import com.dispose.test.dev.Global.InitTestEnvironment;
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.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
@ -20,6 +20,7 @@ import java.util.Arrays;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class AreaCodeManagerServiceTest extends InitTestEnvironment {

View File

@ -37,7 +37,7 @@ import java.security.NoSuchAlgorithmException;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DeviceTaskManagerServiceTest extends InitTestEnvironment {

View File

@ -42,7 +42,7 @@ import java.util.List;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Transactional
@Rollback
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class DisposeAbilityRouterServiceTest extends InitTestEnvironment {
/**
* The constant token.

View File

@ -38,7 +38,7 @@ import java.util.List;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DisposeDeviceManagerServiceTest extends InitTestEnvironment {

View File

@ -40,7 +40,7 @@ import java.security.NoSuchAlgorithmException;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class DisposeTaskServiceTest extends InitTestEnvironment {

View File

@ -32,7 +32,7 @@ import java.security.NoSuchAlgorithmException;
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Transactional
@Rollback
public class UserAccountServiceTest extends InitTestEnvironment {