2024-01-03 09:13:22 +08:00
|
|
|
package com.realtime.protection.server.task;
|
|
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
import com.realtime.protection.ProtectionApplicationTests;
|
2024-01-19 15:09:23 +08:00
|
|
|
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
|
|
|
|
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
2024-01-03 09:13:22 +08:00
|
|
|
import com.realtime.protection.configuration.entity.task.Task;
|
2024-01-12 19:25:14 +08:00
|
|
|
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
|
2024-01-19 15:09:23 +08:00
|
|
|
import com.realtime.protection.server.rule.dynamicrule.DynamicRuleService;
|
|
|
|
|
import com.realtime.protection.server.rule.staticrule.StaticRuleService;
|
|
|
|
|
import com.realtime.protection.server.task.status.StateChangeService;
|
2024-01-03 09:13:22 +08:00
|
|
|
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;
|
2024-01-12 19:24:19 +08:00
|
|
|
import org.springframework.dao.DataIntegrityViolationException;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
2024-01-19 15:09:23 +08:00
|
|
|
import java.util.ArrayList;
|
2024-01-03 22:53:02 +08:00
|
|
|
import java.util.List;
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
|
|
@SpringBootTest
|
2024-01-15 20:40:55 +08:00
|
|
|
class TaskServiceTest extends ProtectionApplicationTests {
|
2024-01-03 09:13:22 +08:00
|
|
|
private final TaskService taskService;
|
2024-01-19 15:09:23 +08:00
|
|
|
private final StaticRuleService staticRuleService;
|
|
|
|
|
private final DynamicRuleService dynamicRuleService;
|
2024-01-03 09:13:22 +08:00
|
|
|
private Task task;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
2024-02-01 09:08:45 +08:00
|
|
|
TaskServiceTest(TaskService taskService, StaticRuleService staticRuleService, DynamicRuleService dynamicRuleService) {
|
2024-01-03 09:13:22 +08:00
|
|
|
this.taskService = taskService;
|
2024-01-19 15:09:23 +08:00
|
|
|
this.staticRuleService = staticRuleService;
|
|
|
|
|
this.dynamicRuleService = dynamicRuleService;
|
2024-01-03 09:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
public void taskInit() {
|
|
|
|
|
this.task = new Task();
|
|
|
|
|
task.setTaskName("静态测试");
|
|
|
|
|
|
2024-01-19 15:09:23 +08:00
|
|
|
LocalDateTime taskStartTime = LocalDateTime.now().plusMinutes(1);
|
|
|
|
|
LocalDateTime taskEndTime = LocalDateTime.now().plusYears(5);
|
2024-01-03 09:13:22 +08:00
|
|
|
|
|
|
|
|
task.setTaskStartTime(taskStartTime);
|
|
|
|
|
task.setTaskEndTime(taskEndTime);
|
|
|
|
|
task.setTaskAct("阻断");
|
2024-01-11 19:49:07 +08:00
|
|
|
task.setTaskType(1);
|
2024-01-19 15:09:23 +08:00
|
|
|
|
2024-01-15 20:40:55 +08:00
|
|
|
task.setTaskCreateUserId(1);
|
2024-01-03 09:13:22 +08:00
|
|
|
task.setTaskCreateUsername("xxx");
|
2024-01-11 19:49:07 +08:00
|
|
|
task.setTaskCreateDepart("xxx");
|
2024-01-03 09:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testNewTaskSuccess() {
|
2024-01-19 15:09:23 +08:00
|
|
|
for (int i = 1; i < 1000; i++) {
|
|
|
|
|
List<StaticRuleObject> staticRuleObjects = staticRuleService.queryStaticRule(
|
|
|
|
|
null, null, null, null, i, 2);
|
|
|
|
|
List<Integer> staticRuleIds = new ArrayList<>();
|
|
|
|
|
staticRuleObjects.forEach(staticRuleObject ->
|
|
|
|
|
staticRuleIds.add(staticRuleObject.getStaticRuleId()));
|
|
|
|
|
task.setStaticRuleIds(staticRuleIds);
|
|
|
|
|
|
|
|
|
|
List<DynamicRuleObject> dynamicRuleObjects = dynamicRuleService.queryDynamicRuleObject(
|
|
|
|
|
null, null, null, null, i, 2
|
|
|
|
|
);
|
|
|
|
|
List<Integer> dynamicRuleIds = new ArrayList<>();
|
|
|
|
|
dynamicRuleObjects.forEach(dynamicRuleObject ->
|
|
|
|
|
dynamicRuleIds.add(dynamicRuleObject.getDynamicRuleId()));
|
|
|
|
|
task.setDynamicRuleIds(dynamicRuleIds);
|
|
|
|
|
|
2024-01-13 10:23:48 +08:00
|
|
|
assertDoesNotThrow(() -> {
|
|
|
|
|
Long taskId = taskService.newTask(task);
|
|
|
|
|
assertTrue(taskId > 0);
|
|
|
|
|
});
|
2024-01-11 19:49:07 +08:00
|
|
|
assertTrue(task.getTaskId() > 0);
|
|
|
|
|
}
|
2024-01-03 09:13:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testNewTaskLostData() {
|
|
|
|
|
this.task.setTaskStartTime(null);
|
2024-01-12 19:24:19 +08:00
|
|
|
assertThrows(DataIntegrityViolationException.class, () -> {
|
2024-01-11 19:49:07 +08:00
|
|
|
Long taskId = taskService.newTask(task);
|
2024-01-03 09:13:22 +08:00
|
|
|
assertTrue(taskId > 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-01-03 22:53:02 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testQueryTasks() {
|
2024-01-24 16:36:03 +08:00
|
|
|
String testName = "test query";
|
|
|
|
|
String testCreateName = "xxx query";
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
task.setTaskName(testName);
|
|
|
|
|
task.setTaskCreateUsername(testCreateName);
|
|
|
|
|
List<StaticRuleObject> staticRuleObjects = staticRuleService.queryStaticRule(
|
|
|
|
|
null, null, null, null, 1, 5);
|
|
|
|
|
List<Integer> staticRuleIds = new ArrayList<>();
|
|
|
|
|
staticRuleObjects.forEach(staticRuleObject ->
|
|
|
|
|
staticRuleIds.add(staticRuleObject.getStaticRuleId()));
|
|
|
|
|
task.setStaticRuleIds(staticRuleIds);
|
|
|
|
|
|
|
|
|
|
List<DynamicRuleObject> dynamicRuleObjects = dynamicRuleService.queryDynamicRuleObject(
|
|
|
|
|
null, null, null, null, 1, 5
|
|
|
|
|
);
|
|
|
|
|
List<Integer> dynamicRuleIds = new ArrayList<>();
|
|
|
|
|
dynamicRuleObjects.forEach(dynamicRuleObject ->
|
|
|
|
|
dynamicRuleIds.add(dynamicRuleObject.getDynamicRuleId()));
|
|
|
|
|
task.setDynamicRuleIds(dynamicRuleIds);
|
|
|
|
|
|
|
|
|
|
assertDoesNotThrow(() -> {
|
|
|
|
|
Long taskId = taskService.newTask(task);
|
|
|
|
|
assertTrue(taskId > 0);
|
|
|
|
|
});
|
|
|
|
|
assertTrue(task.getTaskId() > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-08 20:01:20 +08:00
|
|
|
List<Task> tasks = taskService.queryTasks(null, null, null, null, 1, 10);
|
2024-01-24 16:36:03 +08:00
|
|
|
assertEquals(10, tasks.size());
|
|
|
|
|
|
|
|
|
|
tasks = taskService.queryTasks(0, null, null, null, 1, 10);
|
|
|
|
|
assertEquals(10, tasks.size());
|
|
|
|
|
tasks.forEach(task -> assertEquals(0, task.getTaskStatus()));
|
|
|
|
|
|
|
|
|
|
tasks = taskService.queryTasks(null, 0, null, null, 1, 10);
|
|
|
|
|
assertEquals(0, tasks.size());
|
|
|
|
|
|
|
|
|
|
tasks = taskService.queryTasks(null, null, testName, null, 1, 10);
|
|
|
|
|
assertEquals(10, tasks.size());
|
|
|
|
|
tasks.forEach(task -> assertEquals(testName, task.getTaskName()));
|
2024-01-03 22:53:02 +08:00
|
|
|
}
|
2024-01-08 20:01:20 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testUpdateTasks() {
|
2024-01-23 15:44:18 +08:00
|
|
|
Task originalTask = taskService.queryTasks(
|
|
|
|
|
null, null, null, null, 1, 1)
|
|
|
|
|
.get(0);
|
|
|
|
|
|
|
|
|
|
List<StaticRuleObject> staticRuleObjects = staticRuleService.queryStaticRule(
|
|
|
|
|
null, null, null, null, 1, 4
|
|
|
|
|
);
|
|
|
|
|
List<Integer> staticRuleIds = new ArrayList<>();
|
|
|
|
|
staticRuleObjects.forEach(staticRuleObject -> staticRuleIds.add(staticRuleObject.getStaticRuleId()));
|
|
|
|
|
originalTask.setStaticRuleIds(staticRuleIds);
|
2024-01-11 19:49:07 +08:00
|
|
|
originalTask.setTaskName("修改测试");
|
|
|
|
|
|
|
|
|
|
assertTrue(taskService.updateTask(originalTask));
|
2024-01-23 15:44:18 +08:00
|
|
|
assertEquals("修改测试", taskService.queryTask(originalTask.getTaskId()).getTaskName());
|
2024-01-08 20:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testDeleteTask() {
|
2024-01-13 10:23:48 +08:00
|
|
|
long testNum = taskService.queryTasks(null, null, null, null, 1, 10)
|
2024-01-08 20:01:20 +08:00
|
|
|
.get(0).getTaskId();
|
|
|
|
|
|
|
|
|
|
assertTrue(taskService.deleteTask(testNum));
|
2024-01-24 16:36:03 +08:00
|
|
|
assertFalse(taskService.deleteTask(235156235L)); // 尝试一个不可能达到的数字
|
2024-01-08 20:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testChangeAuditStatus() {
|
2024-01-11 19:49:07 +08:00
|
|
|
long testNum = taskService.queryTasks(null, null, null, null, 1, 1)
|
2024-01-08 20:01:20 +08:00
|
|
|
.get(0).getTaskId();
|
|
|
|
|
|
|
|
|
|
assertTrue(taskService.changeTaskAuditStatus(testNum, 2));
|
|
|
|
|
assertFalse(taskService.changeTaskAuditStatus(testNum, 0));
|
|
|
|
|
assertFalse(taskService.changeTaskAuditStatus(testNum, 1));
|
|
|
|
|
}
|
2024-01-11 19:49:07 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testGetStaticCommands() {
|
2024-01-12 19:24:19 +08:00
|
|
|
List<TaskCommandInfo> taskCommandInfos = taskService.getStaticCommandInfos(38L);
|
|
|
|
|
assertNotNull(taskCommandInfos);
|
2024-01-11 19:49:07 +08:00
|
|
|
}
|
2024-01-03 09:13:22 +08:00
|
|
|
}
|