2024-01-03 09:13:22 +08:00
package com.realtime.protection.server.task ;
2024-05-29 22:05:17 +08:00
import com.baomidou.dynamic.datasource.annotation.DSTransactional ;
2024-01-15 20:40:55 +08:00
import com.realtime.protection.ProtectionApplicationTests ;
2024-04-29 17:32:13 +08:00
import com.realtime.protection.configuration.entity.defense.object.ProtectObject ;
2024-05-22 10:10:32 +08:00
import com.realtime.protection.configuration.entity.defense.template.TemplateNew ;
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-04-25 01:41:28 +08:00
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo ;
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-04-25 01:41:28 +08:00
import com.realtime.protection.configuration.exception.DorisStartException ;
2024-04-29 17:32:13 +08:00
import com.realtime.protection.server.command.CommandService ;
import com.realtime.protection.server.defense.object.ProtectObjectService ;
2024-05-22 10:10:32 +08:00
import com.realtime.protection.server.defense.templatenew.TemplateService ;
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-05-28 02:21:58 +08:00
import org.springframework.transaction.annotation.Transactional ;
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-04-23 21:05:04 +08:00
import java.util.HashMap ;
2024-01-03 22:53:02 +08:00
import java.util.List ;
2024-04-23 21:05:04 +08:00
import java.util.Map ;
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-04-25 01:41:28 +08:00
private final StateChangeService stateChangeService ;
2024-04-29 17:32:13 +08:00
private final CommandService commandService ;
private final ProtectObjectService protectObjectService ;
private final TemplateService templateService ;
2024-01-03 09:13:22 +08:00
private Task task ;
@Autowired
2024-04-29 17:32:13 +08:00
TaskServiceTest ( TaskService taskService , StaticRuleService staticRuleService , DynamicRuleService dynamicRuleService , StateChangeService stateChangeService , CommandService commandService , ProtectObjectService protectObjectService , TemplateService templateService ) {
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-04-25 01:41:28 +08:00
this . stateChangeService = stateChangeService ;
2024-04-29 17:32:13 +08:00
this . commandService = commandService ;
this . protectObjectService = protectObjectService ;
this . templateService = templateService ;
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-05-13 15:03:48 +08:00
// for (int i = 1; i < 10; i++) {
// List<StaticRuleObject> staticRuleObjects = staticRuleService.queryStaticRule(
// null, null, null, null,null, null, null,2, 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,null, null,null,null,i, 2
// );
// 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-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
2024-04-29 19:49:50 +08:00
@Test
void testPageQueryTask ( ) {
2024-05-28 02:21:58 +08:00
List < Task > tasks = taskService . queryTasks ( null , null , " " , " " , null , " " , null , " " , " " , null , null , null , null , 1 , 10 ) ;
2024-05-09 21:58:05 +08:00
// System.out.println(tasks);
tasks . forEach ( task - > System . out . println ( task ) ) ;
2024-04-29 19:49:50 +08:00
}
2024-01-03 22:53:02 +08:00
@Test
void testQueryTasks ( ) {
2024-05-13 15:03:48 +08:00
// String testName = "test query";
// String testCreateName = "xxx query";
//
// for (int i = 0; i < 10; i++) {
// task.setTaskName(testName);
// task.setTaskCreateUsername(testCreateName);
2024-05-28 02:21:58 +08:00
// List<StaticRuleObject> staticRuleObjects = s taticRuleService.queryStaticRule(
2024-05-13 15:03:48 +08:00
// null, null, null, null, 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, 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-05-28 02:21:58 +08:00
List < Task > tasks = taskService . queryTasks ( null , null , null , null , null , null , null , null , null , null , null , null , null , 1 , 10 ) ;
assertEquals ( 10 , tasks . size ( ) ) ;
2024-05-13 15:03:48 +08:00
//
// tasks = taskService.queryTasks(0, null, null, null, null,null, null, null, null, 1, 10);
// assertEquals(10, tasks.size());
// tasks.forEach(task -> assertEquals(0, task.getTaskStatus()));
//
// tasks = taskService.queryTasks(null, 0, null, null, null, null, null, null, null,1, 10);
// assertEquals(0, tasks.size());
//
// tasks = taskService.queryTasks(null, null, testName, null, null, null, null, null, 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 (
2024-05-28 02:21:58 +08:00
null , null , null , null , null , null , null , null , null , null , null , null , null , 1 , 1 )
2024-01-23 15:44:18 +08:00
. get ( 0 ) ;
List < StaticRuleObject > staticRuleObjects = staticRuleService . queryStaticRule (
2024-05-22 17:36:25 +08:00
null , null , null , null , null , null , null , null , null , null , 1 , 4
2024-01-23 15:44:18 +08:00
) ;
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-05-28 02:21:58 +08:00
long testNum = taskService . queryTasks ( null , null , null , null , null , null , null , null , null , null , null , null , null , 1 , 10 )
2024-01-08 20:01:20 +08:00
. get ( 0 ) . getTaskId ( ) ;
assertTrue ( taskService . deleteTask ( testNum ) ) ;
2024-05-13 15:03:48 +08:00
// assertFalse(taskService.deleteTask(235156235L)); // 尝试一个不可能达到的数字
2024-01-08 20:01:20 +08:00
}
@Test
void testChangeAuditStatus ( ) {
2024-05-13 15:03:48 +08:00
// long testNum = taskService.queryTasks(null, null, null, null, null, null, null, null, null,1, 1)
// .get(0).getTaskId();
//
// assertTrue(taskService.changeTaskAuditStatus(testNum, 2));
// assertFalse(taskService.changeTaskAuditStatus(testNum, 0));
// assertFalse(taskService.changeTaskAuditStatus(testNum, 1));
2024-01-08 20:01:20 +08:00
}
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-04-23 21:05:04 +08:00
@Test
void testUpdateTaskAuditStatusBatch ( ) {
2024-05-13 15:03:48 +08:00
// Map<Integer, Integer> map = new HashMap<>();
// map.put(43830, 1);
// map.put(43831, 1);
// map.put(43832, 1);
2024-04-23 21:05:04 +08:00
2024-05-13 15:03:48 +08:00
// System.out.println(taskService.updateAuditStatusBatch(map));
2024-04-23 21:05:04 +08:00
}
2024-04-25 01:41:28 +08:00
@Test
void testGetDynamicTaskInfos ( ) {
List < DynamicTaskInfo > dynamicTaskInfos = taskService . getDynamicTaskInfos ( 43844L ) ;
System . out . println ( dynamicTaskInfos ) ;
}
@Test
void changeTaskstatus ( ) throws DorisStartException {
2024-05-13 15:03:48 +08:00
// stateChangeService.changeState(2, 43844L, false);
2024-04-25 01:41:28 +08:00
}
2024-04-29 17:32:13 +08:00
2024-05-29 22:05:17 +08:00
@DSTransactional
2024-04-29 17:32:13 +08:00
@Test
void testStartStaticTask ( ) throws DorisStartException {
2024-05-28 02:21:58 +08:00
for ( int n = 10 ; n < 20 ; n + + ) {
List < Integer > staticRuleIds = new ArrayList < > ( ) ;
for ( int i = 10 ; i < 15 ; i + + ) {
StaticRuleObject staticRuleTest = new StaticRuleObject ( ) ;
staticRuleTest . setStaticRuleName ( n + " test_staticrule " + i ) ;
staticRuleTest . setStaticRuleCreateUsername ( " NSADD管理员 " ) ;
staticRuleTest . setStaticRuleCreateDepart ( " 组织树 " ) ;
staticRuleTest . setStaticRuleCreateUserId ( 22222222 ) ;
staticRuleTest . setAuditStatus ( 0 ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
staticRuleTest . setStaticRuleSip ( " 32.2.3. " + i ) ;
staticRuleTest . setStaticRuleDip ( n + " .2.3.2 " ) ;
staticRuleService . newStaticRuleObject ( staticRuleTest ) ;
staticRuleIds . add ( staticRuleTest . getStaticRuleId ( ) ) ;
staticRuleService . updateAuditStatus ( staticRuleTest . getStaticRuleId ( ) , 2 , " xxx管理员 " , 1111111 , " 组织树 " ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
}
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
Task task = new Task ( ) ;
task . setTaskName ( " 静态task测试 " + n ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
LocalDateTime taskStartTime = LocalDateTime . now ( ) . plusMinutes ( 1 ) ;
LocalDateTime taskEndTime = LocalDateTime . now ( ) . plusYears ( 5 ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
task . setTaskStartTime ( taskStartTime ) ;
task . setTaskEndTime ( taskEndTime ) ;
task . setTaskAct ( " 23 " ) ;
task . setTaskType ( 1 ) ;
task . setTaskRange ( " 1007 " ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
task . setTaskCreateUserId ( 111111 ) ;
task . setTaskCreateUsername ( " xxx管理员 " ) ;
task . setTaskCreateDepart ( " xxx " ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
task . setStaticRuleIds ( staticRuleIds ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
List < ProtectObject > protectObject = protectObjectService . queryProtectObjects (
null , null , null , null ,
null , null , null , null ,
null , null , 1 , 1 ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
Long taskId = taskService . newTask ( task ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
//
taskService . changeTaskAuditStatus ( taskId , 2 ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
stateChangeService . changeState ( 2 , taskId , false ) ;
2024-04-29 17:32:13 +08:00
2024-05-28 02:21:58 +08:00
// System.out.println(commandService.queryCommandInfos(taskId, null, null, null, null, 1, 5));
}
2024-04-29 17:32:13 +08:00
}
@Test
void testQueryCommandsByTaskId ( ) {
List < TaskCommandInfo > taskCommandInfos = commandService . queryCommandInfos ( 43912L , null , null , null , null , 1 , 5 ) ;
System . out . println ( taskCommandInfos ) ;
}
@Test
void testStartDynamicTask ( ) throws DorisStartException {
List < ProtectObject > protectObject = protectObjectService . queryProtectObjects (
null , null , null , null ,
null , null , null , null ,
null , null , 1 , 1 ) ;
2024-05-22 10:10:32 +08:00
List < TemplateNew > templates = templateService . queryTemplates (
null , null , null , null , null , null , null , 1 , 1 ) ;
2024-04-29 17:32:13 +08:00
DynamicRuleObject object = new DynamicRuleObject ( ) ;
object . setDynamicRuleName ( " testStartDynamicTask " ) ;
object . setDynamicRuleFrequency ( 1 ) ;
object . setDynamicRulePriority ( 1 ) ;
2024-05-29 22:05:17 +08:00
object . setDynamicRuleSourceSystem ( " bw " ) ;
// object.setDynamicRuleSourceSystem("bw");
object . setBwSql ( " select c_src_ipv4,c_src_port,c_dest_ipv4,c_dest_port from topic_xxxxxx where c_event_id in (11113333311,222222222222) and c_netnum = 111111111 and c_flowid=22222222 " ) ;
2024-04-29 17:32:13 +08:00
// object.setDynamicRuleRange("北京");
2024-05-28 02:21:58 +08:00
// object.setDynamicRuleProtectLevel(1);
// object.setLogRuleId(1L);
// object.setTemplateId(templates.get(0).getTemplateId());
// object.setProtectObjectIds(List.of(new Integer[]{protectObject.get(0).getProtectObjectId()}));
2024-04-29 17:32:13 +08:00
Integer dynamicRuleId = dynamicRuleService . newDynamicRuleObject ( object ) ;
2024-05-21 12:37:03 +08:00
dynamicRuleService . updateAuditStatus ( object . getDynamicRuleId ( ) , 2 , null , null , null ) ;
2024-04-29 17:32:13 +08:00
Task task = new Task ( ) ;
2024-05-29 22:05:17 +08:00
task . setTaskName ( " test动态任务sql " ) ;
2024-04-29 17:32:13 +08:00
LocalDateTime taskStartTime = LocalDateTime . now ( ) . plusMinutes ( 1 ) ;
LocalDateTime taskEndTime = LocalDateTime . now ( ) . plusYears ( 5 ) ;
task . setTaskStartTime ( taskStartTime ) ;
task . setTaskEndTime ( taskEndTime ) ;
2024-05-29 22:05:17 +08:00
task . setTaskAct ( " 23 " ) ;
2024-04-29 17:32:13 +08:00
task . setTaskType ( 2 ) ;
2024-05-29 22:05:17 +08:00
task . setTaskRange ( " 1007 " ) ;
2024-04-29 17:32:13 +08:00
task . setTaskCreateUserId ( 1 ) ;
task . setTaskCreateUsername ( " xxx " ) ;
task . setTaskCreateDepart ( " xxx " ) ;
task . setDynamicRuleIds ( List . of ( new Integer [ ] { dynamicRuleId } ) ) ;
2024-05-29 22:05:17 +08:00
task . setEventType ( templates . get ( 0 ) . getEventType ( ) ) ;
task . setProtectLevel ( Integer . valueOf ( templates . get ( 0 ) . getProtectLevel ( ) ) ) ;
2024-05-28 02:21:58 +08:00
task . setTemplateName ( templates . get ( 0 ) . getTemplateName ( ) ) ;
2024-05-29 22:05:17 +08:00
task . setSourceSystem ( templates . get ( 0 ) . getSourceSystem ( ) ) ;
2024-05-28 02:21:58 +08:00
task . setTemplateId ( templates . get ( 0 ) . getTemplateId ( ) ) ;
2024-05-29 22:05:17 +08:00
2024-05-28 02:21:58 +08:00
task . setProtectObjectIds ( List . of ( new Integer [ ] { protectObject . get ( 0 ) . getProtectObjectId ( ) } ) ) ;
2024-04-29 17:32:13 +08:00
Long taskId = taskService . newTask ( task ) ;
System . out . println ( taskId ) ;
//审核状态
2024-05-29 22:05:17 +08:00
taskService . changeTaskAuditStatus ( taskId , 2 ) ;
2024-04-29 17:32:13 +08:00
//启动任务
2024-05-29 22:05:17 +08:00
stateChangeService . changeState ( 2 , taskId , false ) ;
2024-04-29 17:32:13 +08:00
2024-05-13 15:03:48 +08:00
// System.out.println(commandService.queryCommandInfos(taskId, null, null, null, null, 1, 5));
2024-04-29 17:32:13 +08:00
}
2024-05-07 22:33:59 +08:00
@Test
void testUpdateDynamicRuleAuditStatusBatch ( ) {
2024-05-13 15:03:48 +08:00
// Map<Integer, Integer> map = new HashMap<>();
//// map.put(101, 1);
// map.put(43848, 0);
// map.put(43849,0);
//
//// System.out.println(dynamicRuleMapper.queryAuditStatusByIds(map));
// System.out.println(taskService.updateAuditStatusBatch(map));
2024-05-07 22:33:59 +08:00
}
@Test
void testQueryDynamicRuleAuditStatus ( ) {
System . out . println ( taskService . queryTaskAuditStatus ( 43848L ) ) ;
Map < Integer , Integer > map = new HashMap < > ( ) ;
map . put ( 43848 , 0 ) ;
map . put ( 43849 , 0 ) ;
System . out . println ( taskService . queryAuditStatusBatch ( map ) ) ;
}
2024-05-09 21:58:05 +08:00
@Test
void testQueryTaskTotalNum ( ) {
2024-05-28 02:21:58 +08:00
System . out . println ( taskService . queryTaskTotalNum ( null , null , " " , " " , null , " " , " " , " " , " " , null , null , null , null ) ) ;
2024-05-09 21:58:05 +08:00
}
2024-05-07 22:33:59 +08:00
}