diff --git a/src/test/java/com/dispose/test/manager/DeviceTaskManagerTest.java b/src/test/java/com/dispose/test/manager/DeviceTaskManagerTest.java new file mode 100644 index 00000000..6a93461a --- /dev/null +++ b/src/test/java/com/dispose/test/manager/DeviceTaskManagerTest.java @@ -0,0 +1,262 @@ +package com.dispose.test.manager; + +import com.dispose.common.DisposeTaskStatus; +import com.dispose.common.NetflowDirection; +import com.dispose.manager.DeviceTaskManager; +import com.dispose.mapper.DeviceTaskMapper; +import com.dispose.mapper.DisposeTaskMapper; +import com.dispose.pojo.entity.DeviceTask; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +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.Rollback; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.List; + +/** + * The type Dispose device manager test. + * + * @author + */ +@RunWith(SpringRunner.class) +@Slf4j +@SpringBootTest +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Transactional +@Rollback +public class DeviceTaskManagerTest { + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + + /** + * The device task mapper. + */ + @Resource + private DeviceTaskMapper deviceTaskMapper; + /** + * The device task manager. + */ + @Resource + private DeviceTaskManager deviceTaskManager; + /** + * The Dispose task mapper. + */ + @Resource + private DisposeTaskMapper disposeTaskMapper; + + /** + * A 1 add dispose device task info boolean. + */ + @Test + public void a1_addDisposeDeviceTaskInfo() { + Long deviceId = disposeTaskMapper.selectAll().get(0).getDeviceId(); + Long taskId = disposeTaskMapper.selectAll().get(0).getId(); + + // 如果已经存在相同任务,不再添加重复任务 + deviceTaskMapper.selectAll().forEach(v -> { + boolean result = deviceTaskManager.addDisposeDeviceTaskInfo(v.getTaskId(), v.getDeviceId(), v.getTaskAttackType()); + log.info("result-->{}", result); + Assert.assertTrue(result); + }); + + + DeviceTask deviceTask = DeviceTask.builder() + .taskId(taskId) + .deviceId(deviceId) + .taskAttackType(10737508815L) + .build(); + + boolean addResult = deviceTaskManager.addDisposeDeviceTaskInfo(deviceTask.getTaskId(), deviceTask.getDeviceId(), + deviceTask.getTaskAttackType()); + log.info("addResult-->{}", addResult); + Assert.assertTrue(addResult); + } + + /** + * A 2 gets new dispose device task info. + */ + @Test + public void a2_getNewDisposeDeviceTaskInfo() throws JsonProcessingException { + List deviceTaskList = deviceTaskMapper.selectAll(); + + long newNumber = deviceTaskList.stream().filter(v -> (v.getStatus() == (long) DisposeTaskStatus.TASK_NEW.getValue()) + || (v.getStatus() == (long) DisposeTaskStatus.TASK_STARTING.getValue())).count(); + log.info("newNumber-->{}", newNumber); + + if (newNumber > 0) { + List newTask = deviceTaskManager.getNewDisposeDeviceTaskInfo(); + log.info("newTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(newTask)); + + Assert.assertNotNull(newTask); + Assert.assertNotEquals(newTask.size(), 0); + } + } + + /** + * A 3 gets started dispose device task info. + */ + @Test + public void a3_getStartedDisposeDeviceTaskInfo() throws JsonProcessingException { + List deviceTaskList = deviceTaskMapper.selectAll(); + + long startedNumber = deviceTaskList.stream().filter(v -> (v.getStatus() == (long) DisposeTaskStatus.TASK_NEW.getValue()) + || (v.getStatus() == (long) DisposeTaskStatus.TASK_STARTING.getValue() + || (v.getStatus() == (long) DisposeTaskStatus.TASK_STARTED.getValue()))).count(); + log.info("startedNumber-->{}", startedNumber); + + if (startedNumber > 0) { + List startedTask = deviceTaskManager.getStartedDisposeDeviceTaskInfo(); + log.info("startedTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(startedTask)); + + Assert.assertNotNull(startedTask); + Assert.assertNotEquals(startedTask.size(), 0); + } + } + + /** + * A 4 change dispose device task info status. + */ + @Test + public void a4_changeDisposeDeviceTaskInfoStatus() { + DeviceTask deviceTask = deviceTaskMapper.selectAll().get(0); + + for (DisposeTaskStatus status : DisposeTaskStatus.values() + ) { + boolean result = deviceTaskManager.changeDisposeDeviceTaskInfoStatus(deviceTask.getId(), status); + Assert.assertTrue(result); + Assert.assertEquals(Long.valueOf(status.getValue()), deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getStatus()); + } + } + + /** + * A 5 exec attack type. + */ + @Test + public void a5_execAttackType() { + DeviceTask deviceTask = deviceTaskMapper.selectAll().get(0); + + //sets exec attack type + for (NetflowDirection nf : NetflowDirection.values() + ) { + for (long i = 10737508814L; i <= 10737508824L; i++) { + boolean result = deviceTaskManager.setExecAttackType(deviceTask.getId(), nf, i); + Assert.assertTrue(result); + + if (nf == NetflowDirection.DIRECTION_IN) { + Assert.assertEquals(String.valueOf(i), String.valueOf(deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getExecAttackTypeIn())); + } else { + Assert.assertEquals(String.valueOf(i), String.valueOf(deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getExecAttackTypeOut())); + } + } + } + + //exec attack type clean bits + for (NetflowDirection nf : NetflowDirection.values() + ) { + deviceTaskManager.execAttackTypeCleanBit(deviceTask.getId(), nf, 2); + deviceTaskManager.execAttackTypeCleanBit(deviceTask.getId(), nf, 4); + } + + //exec attack type set bits + for (NetflowDirection nf : NetflowDirection.values() + ) { + deviceTaskManager.execAttackTypeSetBit(deviceTask.getId(), nf, 2); + deviceTaskManager.execAttackTypeSetBit(deviceTask.getId(), nf, 4); + } + } + + /** + * A 6 exec attack type status. + */ + @Test + public void a6_attackTypeStatus() { + DeviceTask deviceTask = deviceTaskMapper.selectAll().get(0); + + //sets exec attack type + for (NetflowDirection nf : NetflowDirection.values() + ) { + for (long i = 10737508814L; i <= 10737508824L; i++) { + boolean result = deviceTaskManager.setAttackTypeStatus(deviceTask.getId(), nf, i); + Assert.assertTrue(result); + + if (nf == NetflowDirection.DIRECTION_IN) { + Assert.assertEquals(String.valueOf(i), String.valueOf(deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getAttackTypeStatusIn())); + } else { + Assert.assertEquals(String.valueOf(i), String.valueOf(deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getAttackTypeStatusOut())); + } + } + } + + //attack type status clean bits + for (NetflowDirection nf : NetflowDirection.values() + ) { + deviceTaskManager.attackTypeStatusCleanBit(deviceTask.getId(), nf, 2); + deviceTaskManager.attackTypeStatusCleanBit(deviceTask.getId(), nf, 4); + } + + //attack type status set bits + for (NetflowDirection nf : NetflowDirection.values() + ) { + deviceTaskManager.attackTypeStatusSetBit(deviceTask.getId(), nf, 2); + deviceTaskManager.attackTypeStatusSetBit(deviceTask.getId(), nf, 4); + } + } + + /** + * A 7 sets task error retry times. + */ + @Test + public void a7_setTaskErrRetryTimes() { + DeviceTask deviceTask = deviceTaskMapper.selectAll().get(0); + + for (int i = 0; i <= 5; i++) { + boolean result = deviceTaskManager.setTaskErrRetryTimes(deviceTask.getId(), i); + Assert.assertTrue(result); + Assert.assertEquals(String.valueOf(i), String.valueOf(deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getErrRetry())); + } + } + + /** + * A 8 sets task extern id. + */ + @Test + public void a8_setTaskExternId() { + DeviceTask deviceTask = deviceTaskMapper.selectAll().get(0); + + for (long i = 223L; i <= 230L; i++) { + boolean result = deviceTaskManager.setTaskExternId(deviceTask.getId(), i); + Assert.assertTrue(result); + Assert.assertEquals(String.valueOf(i), String.valueOf(deviceTaskMapper.getTaskInfoById(deviceTask.getId()).getExternId())); + } + } + + /** + * A 9 gets task by id. + */ + @Test + public void a9_getTaskById() { + deviceTaskMapper.selectAll().forEach(v -> { + DeviceTask deviceTask = deviceTaskManager.getTaskById(v.getId()); + + try { + log.info("deviceTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(deviceTask)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + Assert.assertNotNull(deviceTask); + }); + } +}