From 54f51224cad4d02bc5204431b3b3c53f310984c6 Mon Sep 17 00:00:00 2001 From: chenlinghy Date: Wed, 26 Aug 2020 16:10:51 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E7=BC=96=E5=86=99service?= =?UTF-8?q?=E5=B1=82=E8=AE=BE=E5=A4=87=E4=BB=BB=E5=8A=A1=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=202.=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/DeviceTaskManagerServiceImpl.java | 16 +-- .../service/DeviceTaskManagerServiceTest.java | 114 ++++++++++++++++++ 2 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/dispose/test/service/DeviceTaskManagerServiceTest.java diff --git a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java index b15ca83a..07d06bc6 100644 --- a/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/DeviceTaskManagerServiceImpl.java @@ -201,7 +201,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 遍历设备攻击类型 for (DpTechAttackType t : DpTechAttackType.maskToDdosAttackType(deviceTask.getTaskAttackType())) { - // 入方向 + // 出方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_IN) { // 已经启动过的任务不再重复启动 if ((deviceTask.getExecAttackTypeOut() & t.getAttackTypeMask()) != 0) { @@ -239,7 +239,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } } - // 出方向 + // 入方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_OUT) { // 已经启动过的任务不再重复启动 if ((deviceTask.getExecAttackTypeIn() & t.getAttackTypeMask()) != 0) { @@ -284,7 +284,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 获取处置任务当前在设备上的执行状态 DeviceTask devTask = deviceTaskManager.getTaskById(deviceTask.getId()); - // 入方向 + // 出方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_IN) { if (!devTask.getExecAttackTypeOut().equals(devTask.getTaskAttackType()) || !devTask.getExecAttackTypeOut().equals(devTask.getAttackTypeStatusOut())) { @@ -292,7 +292,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } } - // 出方向 + // 入方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_OUT) { if (!devTask.getExecAttackTypeIn().equals(devTask.getTaskAttackType()) || !devTask.getExecAttackTypeIn().equals(devTask.getAttackTypeStatusIn())) { @@ -322,7 +322,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 遍历处置设备执行成功的攻击类型 for (DpTechAttackType t : DpTechAttackType.maskToDdosAttackType(deviceTask.getAttackTypeStatusIn())) { - // 入方向 + // 出方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_IN) { // 调用迪普设备停止处置任务 ret = ai.getDb().stopDispose(disposeTask.getDisposeIp(), disposeTask.getDisposeCapacity(), @@ -348,7 +348,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } } - // 出方向 + // 入方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_OUT) { // 调用迪普设备启动处置任务 ret = ai.getDb().stopDispose(disposeTask.getDisposeIp(), disposeTask.getDisposeCapacity(), @@ -382,14 +382,14 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { // 获取处置任务当前在设备上的执行状态 DeviceTask devTask = deviceTaskManager.getTaskById(deviceTask.getId()); - // 入方向 + // 出方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_IN) { if (devTask.getAttackTypeStatusOut() != 0) { taskStopSucceed = false; } } - // 出方向 + // 入方向 if (disposeTask.getFlowDirection() != NetflowDirection.DIRECTION_OUT) { if (devTask.getAttackTypeStatusIn() != 0) { taskStopSucceed = false; diff --git a/src/test/java/com/dispose/test/service/DeviceTaskManagerServiceTest.java b/src/test/java/com/dispose/test/service/DeviceTaskManagerServiceTest.java new file mode 100644 index 00000000..466daea3 --- /dev/null +++ b/src/test/java/com/dispose/test/service/DeviceTaskManagerServiceTest.java @@ -0,0 +1,114 @@ +package com.dispose.test.service; + +import com.dispose.common.*; +import com.dispose.mapper.DisposeDeviceMapper; +import com.dispose.mapper.DisposeTaskMapper; +import com.dispose.mapper.UserAccountMapper; +import com.dispose.pojo.entity.DisposeTask; +import com.dispose.pojo.po.MulReturnType; +import com.dispose.service.DeviceTaskManagerService; +import com.dispose.service.UserAccountService; +import com.dispose.test.Global.InitTestEnvironment; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.junit.Before; +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.Rollback; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.security.NoSuchAlgorithmException; + +/** + * The type User account service test. + * + * @author + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@Slf4j +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Transactional +@Rollback +public class DeviceTaskManagerServiceTest extends InitTestEnvironment { + /** + * The constant token. + */ + private static String token = ""; + /** + * The User account service. + */ + @Resource + private UserAccountService userAccountService; + /** + * The device task manager service. + */ + @Resource + private DeviceTaskManagerService deviceTaskManagerService; + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + /** + * The user account mapper. + */ + @Resource + private UserAccountMapper userAccountMapper; + /** + * The Dispose device mapper. + */ + @Resource + private DisposeDeviceMapper disposeDeviceMapper; + /** + * The Dispose task mapper. + */ + @Resource + private DisposeTaskMapper disposeTaskMapper; + + /** + * User login test. + * + * @throws NoSuchAlgorithmException the no such algorithm exception + */ + @Before + public void userLoginTest() throws NoSuchAlgorithmException { + MulReturnType ret = userAccountService.loginService(getUSER_NAME(), + getPASSWORD()); + + if (ret.getFirstParam() == ErrorCode.ERR_OK) { + DeviceTaskManagerServiceTest.token = ret.getSecondParam(); + } + } + + /** + * Schedule runner thread. + */ + @Test + public void a1_scheduleRunnerThread() throws JsonProcessingException { + Long uId = userAccountMapper.selectAll().get(0).getId(); + Long dId = disposeDeviceMapper.selectAll().get(0).getId(); + + DisposeTask task = DisposeTask.builder() + .deviceId(dId) + .accountId(uId) + .disposeCapacity(DisposeCapacityType.CLEANUP) + .disposeIp("192.168.0.1") + .planEndTime("30") + .flowDirection(NetflowDirection.DIRECTION_BI) + .attackType(DDoSAttackType.getTypeMaskFromAttackType(new DDoSAttackType[]{DDoSAttackType.ALL_ATTACKS})) + .flowBandWidth(1024) + .build(); + + disposeTaskMapper.addNewTask(task); + log.info("DisposeTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task)); + + deviceTaskManagerService.scheduleRunnerThread(); + } +} \ No newline at end of file