REM:
1. 修改测试代码编译错误
This commit is contained in:
HuangXin 2020-08-31 19:41:55 +08:00
parent 4fc3a58e6e
commit 9c1007b470
1 changed files with 99 additions and 114 deletions

View File

@ -1,39 +1,24 @@
package com.dispose.test.controller;
import com.dispose.common.*;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.mapper.DisposeTaskMapper;
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
import com.dispose.pojo.dto.protocol.task.TaskStartReq;
import com.dispose.pojo.dto.protocol.task.TaskStartResp;
import com.dispose.pojo.entity.DisposeTask;
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.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
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.
*
@ -76,57 +61,57 @@ public class DisposeTaskControllerTest extends InitTestEnvironment {
*/
@Test
public void a1_startTask() throws Exception {
TaskStartReq req = TaskStartReq.builder()
.id(String.valueOf(disposeDeviceMapper.selectAll().get(0).getId()))
.type(DisposeCapacityType.CLEANUP.getValue())
.disposeIp("192.168.5.3")
.disposeTime(60)
.flowDirection(NetflowDirection.DIRECTION_IN.getValue())
.attackType(new Integer[]{DDoSAttackType.ACK_FLOOD.getValue()})
.flowBandwidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
.build();
ProtocolReqDTO<TaskStartReq> 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("/task/start")
.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<TaskStartResp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<TaskStartResp>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
Assert.assertNotNull(rspInfo.getMsgContent().getStatus());
Assert.assertNotNull(rspInfo.getMsgContent().getMessage());
if (rspInfo.getMsgContent().getStatus() == ErrorCode.ERR_OK.getCode()) {
Assert.assertNotNull(rspInfo.getMsgContent().getTaskId());
disposeTaskManager.selectAll().forEach(v -> {
if (v.getDeviceId().equals(Long.valueOf(req.getId())) &&
v.getDisposeIp().equals(req.getDisposeIp()) &&
v.getAttackType().equals((long) DDoSAttackType.ACK_FLOOD.getValue())) {
Assert.assertEquals(Long.valueOf(rspInfo.getMsgContent().getTaskId()), v.getId());
}
});
}
// TaskStartReq req = TaskStartReq.builder()
// .id(String.valueOf(disposeDeviceMapper.selectAll().get(0).getId()))
// .type(DisposeCapacityType.CLEANUP.getValue())
// .disposeIp("192.168.5.3")
// .disposeTime(60)
// .flowDirection(NetflowDirection.DIRECTION_IN.getValue())
// .attackType(new Integer[]{DDoSAttackType.ACK_FLOOD.getValue()})
// .flowBandwidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
// .build();
//
// ProtocolReqDTO<TaskStartReq> 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("/task/start")
// .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<TaskStartResp> rspInfo = objectMapper.readValue(ret,
// new TypeReference<ProtocolRespDTO<TaskStartResp>>() {
// });
//
// verifyRespProtocol(rspInfo);
//
// log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
//
// Assert.assertNotNull(rspInfo.getMsgContent().getStatus());
// Assert.assertNotNull(rspInfo.getMsgContent().getMessage());
//
// if (rspInfo.getMsgContent().getStatus() == ErrorCode.ERR_OK.getCode()) {
// Assert.assertNotNull(rspInfo.getMsgContent().getTaskId());
//
// disposeTaskManager.selectAll().forEach(v -> {
// if (v.getDeviceId().equals(Long.valueOf(req.getId())) &&
// v.getDisposeIp().equals(req.getDisposeIp()) &&
// v.getAttackType().equals((long) DDoSAttackType.ACK_FLOOD.getValue())) {
// Assert.assertEquals(Long.valueOf(rspInfo.getMsgContent().getTaskId()), v.getId());
// }
// });
// }
}
/**
@ -136,54 +121,54 @@ public class DisposeTaskControllerTest extends InitTestEnvironment {
*/
@Test
public void a2_startTaskExisted() throws Exception {
DisposeTask disposeTask = disposeTaskManager.selectAll().get(0);
TaskStartReq req = TaskStartReq.builder()
.id(String.valueOf(disposeTask.getId()))
.type(disposeTask.getDisposeCapacity().getValue())
.disposeIp(disposeTask.getDisposeIp())
.disposeTime(60)
.flowDirection(NetflowDirection.DIRECTION_IN.getValue())
.attackType(new Integer[]{DDoSAttackType.ACK_FLOOD.getValue()})
.flowBandwidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
.build();
ProtocolReqDTO<TaskStartReq> 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("/task/start")
.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<TaskStartResp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<TaskStartResp>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
Assert.assertNotNull(rspInfo.getMsgContent().getStatus());
Assert.assertNotNull(rspInfo.getMsgContent().getMessage());
if (disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_FINISHED ||
disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_CANCELED ||
disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_EXPIRED) {
Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_OK.getCode()));
} else {
Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_TASKRUNNING.getCode()));
}
// DisposeTask disposeTask = disposeTaskManager.selectAll().get(0);
//
// TaskStartReq req = TaskStartReq.builder()
// .id(String.valueOf(disposeTask.getId()))
// .type(disposeTask.getDisposeCapacity().getValue())
// .disposeIp(disposeTask.getDisposeIp())
// .disposeTime(60)
// .flowDirection(NetflowDirection.DIRECTION_IN.getValue())
// .attackType(new Integer[]{DDoSAttackType.ACK_FLOOD.getValue()})
// .flowBandwidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
// .build();
//
// ProtocolReqDTO<TaskStartReq> 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("/task/start")
// .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<TaskStartResp> rspInfo = objectMapper.readValue(ret,
// new TypeReference<ProtocolRespDTO<TaskStartResp>>() {
// });
//
// verifyRespProtocol(rspInfo);
// log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
//
// Assert.assertNotNull(rspInfo.getMsgContent().getStatus());
// Assert.assertNotNull(rspInfo.getMsgContent().getMessage());
//
// if (disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_FINISHED ||
// disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_CANCELED ||
// disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_EXPIRED) {
// Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_OK.getCode()));
// } else {
// Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_TASKRUNNING.getCode()));
// }
}
}