1. application.yml修改为application-dev.yml和application-prod.yml
2. 添加更多Exception拦截器 3. 编写状态模式处理task状态的更改 4. 添加StateChangeService,用以处理所有任务状态转换相关的内容 5. 添加StateEnum, ProtocolEnum,TaskTypeEnum用以处理任务和协议相关的所有状态和类型
This commit is contained in:
@@ -2,6 +2,7 @@ package com.realtime.protection.server.defense.template;
|
||||
|
||||
import com.realtime.protection.configuration.entity.defense.template.ProtectLevel;
|
||||
import com.realtime.protection.configuration.entity.defense.template.Template;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -16,6 +17,8 @@ class TemplateServiceTest {
|
||||
|
||||
private final TemplateService templateService;
|
||||
private Template template;
|
||||
private Long startTime;
|
||||
|
||||
|
||||
@Autowired
|
||||
TemplateServiceTest(TemplateService templateService) {
|
||||
@@ -42,6 +45,14 @@ class TemplateServiceTest {
|
||||
template.setProtectLevelLow(protectLevelLow);
|
||||
template.setProtectLevelMedium(protectLevelMedium);
|
||||
template.setProtectLevelHigh(protectLevelHigh);
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void summary() {
|
||||
Long endTime = System.currentTimeMillis();
|
||||
System.out.printf("total time: %d ms", endTime - startTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,6 +67,8 @@ class TemplateServiceTest {
|
||||
assertEquals(5, templates.size());
|
||||
for (Template template : templates) {
|
||||
assertTrue(template.getTemplateId() > 0);
|
||||
assertNotNull(template.getTemplateRunningTasks());
|
||||
assertNotNull(template.getTemplateUsedTimes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,16 +81,4 @@ class TemplateServiceTest {
|
||||
assertTrue(templateService.updateTemplate(testTemplate.getTemplateId(), testTemplate));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddTemplateUsedTimes() {
|
||||
template.setTemplateName("add test");
|
||||
templateService.newTemplate(template);
|
||||
|
||||
List<Template> templates = templateService.queryTemplates("add", 1, 1);
|
||||
Template originalTemplate = templates.get(0);
|
||||
Boolean success = templateService.addTemplateUsedTimes(originalTemplate.getTemplateId(), 4);
|
||||
templates = templateService.queryTemplates("add", 1, 1);
|
||||
assertEquals(originalTemplate.getTemplateUsedTimes() + 4, templates.get(0).getTemplateUsedTimes());
|
||||
assertTrue(success);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.realtime.protection.server.task;
|
||||
|
||||
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
||||
import com.realtime.protection.configuration.entity.task.Task;
|
||||
import org.apache.ibatis.exceptions.PersistenceException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -27,31 +28,37 @@ class TaskServiceTest {
|
||||
this.task = new Task();
|
||||
task.setTaskName("静态测试");
|
||||
|
||||
LocalDateTime taskStartTime = LocalDateTime.parse("2023-12-24T11:45:14");
|
||||
LocalDateTime taskEndTime = LocalDateTime.parse("2023-12-29T11:45:12");
|
||||
LocalDateTime taskStartTime = LocalDateTime.now().plusDays(1);
|
||||
LocalDateTime taskEndTime = LocalDateTime.now().plusDays(5);
|
||||
|
||||
task.setTaskStartTime(taskStartTime);
|
||||
task.setTaskEndTime(taskEndTime);
|
||||
task.setTaskAct("阻断");
|
||||
task.setTaskType("静态任务");
|
||||
task.setStaticRuleIds(List.of(1, 2));
|
||||
task.setTaskType(1);
|
||||
task.setStaticRuleIds(List.of(1L, 2L));
|
||||
task.setDynamicRuleIds(List.of());
|
||||
task.setTaskCreateUserId(1);
|
||||
task.setTaskCreateUserId(1L);
|
||||
task.setTaskCreateUsername("xxx");
|
||||
task.setTaskCreateDepart("xxx");;
|
||||
task.setTaskCreateDepart("xxx");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNewTaskSuccess() {
|
||||
assertDoesNotThrow(() -> {Integer taskId = taskService.newTask(task); assertTrue(taskId > 0);});
|
||||
assertTrue(task.getTaskId() > 0);
|
||||
for (int i = 0; i < 100; i++){
|
||||
LocalDateTime taskStartTime = LocalDateTime.now().plusDays(i);
|
||||
LocalDateTime taskEndTime = LocalDateTime.now().plusDays(i+10);
|
||||
task.setTaskStartTime(taskStartTime);
|
||||
task.setTaskEndTime(taskEndTime);
|
||||
assertDoesNotThrow(() -> {Long taskId = taskService.newTask(task); assertTrue(taskId > 0);});
|
||||
assertTrue(task.getTaskId() > 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNewTaskLostData() {
|
||||
this.task.setTaskStartTime(null);
|
||||
assertThrows(PersistenceException.class, () -> {
|
||||
Integer taskId = taskService.newTask(task);
|
||||
Long taskId = taskService.newTask(task);
|
||||
assertTrue(taskId > 0);
|
||||
});
|
||||
}
|
||||
@@ -64,30 +71,37 @@ class TaskServiceTest {
|
||||
|
||||
@Test
|
||||
void testUpdateTasks() {
|
||||
task.setStaticRuleIds(List.of(6, 7, 8));
|
||||
task.setTaskId(26);
|
||||
task.setTaskName("修改测试");
|
||||
Task originalTask = taskService.queryTask(38L);
|
||||
|
||||
assertTrue(taskService.updateTask(task));
|
||||
assertEquals("修改测试", taskService.queryTask(26).getTaskName());
|
||||
originalTask.setStaticRuleIds(List.of(16L, 17L, 18L, 19L));
|
||||
originalTask.setTaskName("修改测试");
|
||||
|
||||
assertTrue(taskService.updateTask(originalTask));
|
||||
assertEquals("修改测试", taskService.queryTask(38L).getTaskName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteTask() {
|
||||
int testNum = taskService.queryTasks(null, null, null, null, 1, 10)
|
||||
long testNum = taskService.queryTasks(null, null, null, null, 1, 10)
|
||||
.get(0).getTaskId();
|
||||
|
||||
assertTrue(taskService.deleteTask(testNum));
|
||||
assertFalse(taskService.deleteTask(235235));
|
||||
assertFalse(taskService.deleteTask(235235L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testChangeAuditStatus() {
|
||||
int testNum = taskService.queryTasks(null, null, null, null, 1, 1)
|
||||
long testNum = taskService.queryTasks(null, null, null, null, 1, 1)
|
||||
.get(0).getTaskId();
|
||||
|
||||
assertTrue(taskService.changeTaskAuditStatus(testNum, 2));
|
||||
assertFalse(taskService.changeTaskAuditStatus(testNum, 0));
|
||||
assertFalse(taskService.changeTaskAuditStatus(testNum, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetStaticCommands() {
|
||||
List<TaskCommandInfo> taskCommandInfos = taskService.getStaticCommandInfos(26L);
|
||||
assertEquals(3, taskCommandInfos.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.realtime.protection.server.task.status;
|
||||
|
||||
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
||||
import com.realtime.protection.configuration.entity.task.Command;
|
||||
import com.realtime.protection.configuration.entity.task.FiveTupleWithMask;
|
||||
import com.realtime.protection.server.command.CommandService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
class CommandServiceTest {
|
||||
|
||||
private final CommandService commandService;
|
||||
private TaskCommandInfo taskCommandInfo;
|
||||
private Command command;
|
||||
|
||||
private Long startTime;
|
||||
|
||||
@Autowired
|
||||
CommandServiceTest(CommandService commandService) {
|
||||
this.commandService = commandService;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void mockCommand() {
|
||||
FiveTupleWithMask fiveTupleWithMask = new FiveTupleWithMask();
|
||||
fiveTupleWithMask.setMaskSourceIP("192.168.155.24");
|
||||
|
||||
taskCommandInfo = new TaskCommandInfo();
|
||||
taskCommandInfo.setFrequency(30);
|
||||
taskCommandInfo.setTaskId(30L);
|
||||
taskCommandInfo.setFiveTupleWithMask(fiveTupleWithMask);
|
||||
taskCommandInfo.setOperation("阻断");
|
||||
taskCommandInfo.setEndTime(LocalDateTime.now().plusDays(1));
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void summary() {
|
||||
Long endTime = System.currentTimeMillis();
|
||||
System.out.printf("total time: %d ms", endTime - startTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createCommand() {
|
||||
assertDoesNotThrow(() -> commandService.createCommand(taskCommandInfo));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createCommands() {
|
||||
List<TaskCommandInfo> taskCommandInfos = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int port = i + 1000;
|
||||
taskCommandInfo = new TaskCommandInfo();
|
||||
taskCommandInfo.setFiveTupleWithMask(new FiveTupleWithMask());
|
||||
taskCommandInfo.setTaskId(24L);
|
||||
taskCommandInfo.getFiveTupleWithMask().setSourcePort(Integer.toString(port));
|
||||
taskCommandInfo.setStartTime(LocalDateTime.now().plusDays(5));
|
||||
taskCommandInfo.setEndTime(LocalDateTime.now().plusDays(10));
|
||||
taskCommandInfo.setFrequency(20);
|
||||
|
||||
taskCommandInfos.add(taskCommandInfo);
|
||||
}
|
||||
|
||||
assertDoesNotThrow(() -> commandService.createCommands(taskCommandInfos));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user