1. 添加HandlerMethodValidationException全局异常器

2. 新增防护对象类,添加Service、Mapper、Controller(Controller仍然在开发中)
3. page和pageSize添加@Min注解,限定最低整数大小
4. 将所有的批量类型方法修改为forEach,在SpringBoot中循环执行并整合为事务
This commit is contained in:
松岳 陈
2024-01-05 09:32:19 +08:00
parent b4db26c856
commit 776c7c0f6d
19 changed files with 327 additions and 82 deletions

View File

@@ -0,0 +1,45 @@
package com.realtime.protection.configuration.entity.defense.object;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ProtectObject {
@JsonProperty("proobj_id")
private Integer protectObjectId;
@JsonProperty("proobj_name")
@NotNull(message = "proobj_name should not be empty.")
private String protectObjectName;
@JsonProperty("proobj_system_name")
private String protectObjectSystemName;
@JsonProperty("proobj_ip_address")
private String protectObjectIPAddress;
@JsonProperty("proobj_port")
@NotNull(message = "proobj_port should not be empty.")
private Integer protectObjectPort;
@JsonProperty("proobj_url")
@NotNull(message = "proobj_url should not be empty.")
private String protectObjectURL;
@JsonProperty("proobj_protocol")
@NotNull(message = "proobj_protocol should not be empty.")
private String protectObjectProtocol;
@JsonProperty("proobj_audit_status")
private Integer protectObjectAuditStatus;
@JsonProperty("proobj_create_username")
private String protectObjectCreateUsername;
@JsonProperty("proobj_create_depart")
private String protectObjectCreateDepart;
@JsonProperty("proobj_create_userid")
private Integer protectObjectCreateUserId;
}

View File

@@ -4,20 +4,22 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class Template { public class Template {
@JsonProperty("template_id") @JsonProperty("template_id")
private Integer templateId; private Integer templateId;
@JsonProperty("template_name") @JsonProperty("template_name")
@NotNull(message = "template name should not be empty") @NotNull(message = "template name should not be empty.")
private String templateName; private String templateName;
@JsonProperty("template_elements") @JsonProperty("template_elements")
private String[] templateElements; private List<String> templateElements;
@JsonProperty("default_op") @JsonProperty("default_op")
@NotNull(message = "default_op should not be empty") @NotNull(message = "default_op should not be empty.")
private String defaultOp; private String defaultOp;
@JsonProperty("template_running_tasks") @JsonProperty("template_running_tasks")

View File

@@ -13,15 +13,15 @@ public class Task {
private Integer taskId; private Integer taskId;
@JsonProperty("task_name") @JsonProperty("task_name")
@NotNull(message = "task_name should not be empty") @NotNull(message = "task_name should not be empty.")
private String taskName; private String taskName;
@JsonProperty("task_start_time") @JsonProperty("task_start_time")
@NotNull(message = "task_start_time should not be empty") @NotNull(message = "task_start_time should not be empty.")
private LocalDateTime taskStartTime; private LocalDateTime taskStartTime;
@JsonProperty("task_end_time") @JsonProperty("task_end_time")
@NotNull(message = "task_end_time should not be empty") @NotNull(message = "task_end_time should not be empty.")
private LocalDateTime taskEndTime; private LocalDateTime taskEndTime;
@JsonProperty("task_create_time") @JsonProperty("task_create_time")
@@ -31,27 +31,21 @@ public class Task {
private LocalDateTime taskModifyTime; private LocalDateTime taskModifyTime;
@JsonProperty("task_type") @JsonProperty("task_type")
@NotNull(message = "task_type should not be empty") @NotNull(message = "task_type should not be empty.")
private String taskType; private String taskType;
@JsonProperty("task_act") @JsonProperty("task_act")
@NotNull(message = "task_act should not be empty") @NotNull(message = "task_act should not be empty.")
private String taskAct; private String taskAct;
// These three attributes will be gained by user in the future
// -----------------------------------------------------------
@JsonProperty("task_create_username") @JsonProperty("task_create_username")
@NotNull(message = "task_create_username should not be empty")
private String taskCreateUsername; private String taskCreateUsername;
@JsonProperty("task_create_depart") @JsonProperty("task_create_depart")
@NotNull(message = "task_create_depart should not be empty")
private String taskCreateDepart; private String taskCreateDepart;
@JsonProperty("task_create_userid") @JsonProperty("task_create_userid")
@NotNull(message = "task_create_userid should not be empty")
private Integer taskCreateUserId; private Integer taskCreateUserId;
// -----------------------------------------------------------
@JsonProperty("static_rule_ids") @JsonProperty("static_rule_ids")
private List<Integer> staticRuleIds; private List<Integer> staticRuleIds;

View File

@@ -3,13 +3,13 @@ package com.realtime.protection.configuration.exception;
import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException; import cn.dev33.satoken.exception.SaTokenException;
import com.realtime.protection.configuration.response.ResponseResult; import com.realtime.protection.configuration.response.ResponseResult;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import org.apache.ibatis.exceptions.PersistenceException; import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -47,6 +47,12 @@ public class GlobalExceptionHandler {
); );
} }
@Order(2)
@ExceptionHandler(value = HandlerMethodValidationException.class)
public ResponseResult handleHandlerMethodValidationException(HandlerMethodValidationException e) {
return ResponseResult.invalid().setMessage(e.getMessage());
}
@Order(2) @Order(2)
@ExceptionHandler(value = SaTokenException.class) @ExceptionHandler(value = SaTokenException.class)
public ResponseResult handleSaTokenException(SaTokenException e) { public ResponseResult handleSaTokenException(SaTokenException e) {

View File

@@ -0,0 +1,4 @@
package com.realtime.protection.server.defense.object;
public class ProtectObjectController {
}

View File

@@ -0,0 +1,25 @@
package com.realtime.protection.server.defense.object;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface ProtectObjectMapper {
void newProtectObject(@Param("proobj") ProtectObject protectObject);
List<ProtectObject> queryProtectObjects(@Param("proobj_name") String protectObjectName,
@Param("proobj_id") Integer protectObjectId,
@Param("page") Integer page,
@Param("page_size") Integer pageSize);
ProtectObject queryProtectObject(@Param("proobj_id") Integer protectObjectId);
Boolean updateProtectObject(@Param("proobj") ProtectObject protectObject);
Boolean deleteProtectObject(@Param("proobj_id") Integer protectObjectId);
Boolean changeProtectObjectAuditStatus(@Param("proobj_audit_status") Integer protectObjectAuditStatus);
}

View File

@@ -0,0 +1,53 @@
package com.realtime.protection.server.defense.object;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ProtectObjectService {
private final ProtectObjectMapper protectObjectMapper;
public ProtectObjectService(ProtectObjectMapper protectObjectMapper) {
this.protectObjectMapper = protectObjectMapper;
}
public Integer newProtectObject(ProtectObject protectObject) {
protectObjectMapper.newProtectObject(protectObject);
if (protectObject.getProtectObjectId() == null) {
return 0;
}
return protectObject.getProtectObjectId();
}
@Transactional
public List<Integer> newProtectObjects(List<ProtectObject> protectObjectList) {
protectObjectList.forEach(protectObjectMapper::newProtectObject);
return protectObjectList.stream().map(ProtectObject::getProtectObjectId).collect(Collectors.toList());
}
public List<ProtectObject> queryProtectObjects(String protectObjectName, Integer protectObjectId, Integer page, Integer pageSize) {
return protectObjectMapper.queryProtectObjects(protectObjectName, protectObjectId, page, pageSize);
}
public ProtectObject queryProtectObject(Integer protectObjectId) {
return protectObjectMapper.queryProtectObject(protectObjectId);
}
public Boolean updateProtectObject(ProtectObject protectObject) {
return protectObjectMapper.updateProtectObject(protectObject);
}
public Boolean deleteProtectObject(Integer protectObjectId) {
return protectObjectMapper.deleteProtectObject(protectObjectId);
}
@Transactional
public Boolean deleteProtectObjects(List<Integer> protectObjectIds) {
return protectObjectIds.stream().allMatch(protectObjectMapper::deleteProtectObject);
}
}

View File

@@ -3,6 +3,7 @@ package com.realtime.protection.server.defense.template;
import com.realtime.protection.configuration.entity.defense.template.Template; import com.realtime.protection.configuration.entity.defense.template.Template;
import com.realtime.protection.configuration.response.ResponseResult; import com.realtime.protection.configuration.response.ResponseResult;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@@ -41,16 +42,32 @@ public class TemplateController {
} }
@GetMapping("/query") @GetMapping("/query")
public ResponseResult queryTemplate(@RequestParam(value = "template_name", required = false) String templateName, public ResponseResult queryTemplates(@RequestParam(value = "template_name", required = false) String templateName,
@RequestParam("page") Integer page, @RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") Integer pageSize) { @RequestParam("page_size") @Min(1) Integer pageSize) {
if (page <= 0 || pageSize <= 0) {
return ResponseResult.invalid()
.setData("template_list", null);
}
List<Template> templates = templateService.queryTemplates(templateName, page, pageSize); List<Template> templates = templateService.queryTemplates(templateName, page, pageSize);
return ResponseResult.ok() return ResponseResult.ok()
.setData("templates", templates); .setData("templates", templates);
} }
@PostMapping("/{id}/update")
public ResponseResult updateTemplate(@PathVariable("id") @Min(1) Integer templateId,
@RequestBody @Valid Template template) {
Boolean success = templateService.updateTemplate(templateId, template);
return ResponseResult.ok()
.setData("template_id", templateId)
.setData("success", success);
}
@GetMapping("/{id}/addUsedTimes")
public ResponseResult addTemplateUsedTimes(@PathVariable("id") @Min(1) Integer templateId,
@RequestParam(value = "add_num") @Min(0) Integer addNum) {
Boolean success = templateService.addTemplateUsedTimes(templateId, addNum);
return ResponseResult.ok()
.setData("template_id", templateId)
.setData("success", success);
}
} }

View File

@@ -13,4 +13,10 @@ public interface TemplateMapper {
List<Template> queryTemplates(@Param("template_name") String templateName, List<Template> queryTemplates(@Param("template_name") String templateName,
@Param("page") Integer page, @Param("page") Integer page,
@Param("page_size") Integer pageSize); @Param("page_size") Integer pageSize);
Boolean updateTemplateInformation(@Param("template") Template template);
void countTemplateRunningTasks(@Param("template_id") Integer templateId);
Boolean addTemplateUsedTimes(@Param("template_id") Integer templateId, @Param("add_times") Integer addTimes);
} }

View File

@@ -22,6 +22,28 @@ public class TemplateService {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
setTemplateElements(template);
templateMapper.newTemplate(template);
if (template.getTemplateId() == null) {
return 0;
}
return template.getTemplateId();
}
public List<Template> queryTemplates(String templateName, Integer page, Integer pageSize) {
return templateMapper.queryTemplates(templateName, page, pageSize);
}
public Boolean updateTemplate(Integer templateId, Template template) {
setTemplateElements(template);
template.setTemplateId(templateId);
return templateMapper.updateTemplateInformation(template);
}
private void setTemplateElements(Template template) {
for (String choice : template.getTemplateElements()) { for (String choice : template.getTemplateElements()) {
switch (choice) { switch (choice) {
case "防护对象IP" -> template.setHasProtectObjectIP(true); case "防护对象IP" -> template.setHasProtectObjectIP(true);
@@ -35,16 +57,10 @@ public class TemplateService {
default -> throw new IllegalArgumentException(); default -> throw new IllegalArgumentException();
} }
} }
templateMapper.newTemplate(template);
if (template.getTemplateId() == null) {
return 0;
}
return template.getTemplateId();
} }
public List<Template> queryTemplates(String templateName, Integer page, Integer pageSize) { public Boolean addTemplateUsedTimes(Integer templateId, Integer addTimes) {
return templateMapper.queryTemplates(templateName, page, pageSize); return templateMapper.addTemplateUsedTimes(templateId, addTimes);
} }
} }

View File

@@ -3,7 +3,7 @@ package com.realtime.protection.server.task;
import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.response.ResponseResult; import com.realtime.protection.configuration.response.ResponseResult;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.apache.coyote.Response; import jakarta.validation.constraints.Min;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@@ -40,19 +40,15 @@ public class TaskController {
@RequestParam(value = "task_type", required = false) String taskType, @RequestParam(value = "task_type", required = false) String taskType,
@RequestParam(value = "task_name", required = false) String taskName, @RequestParam(value = "task_name", required = false) String taskName,
@RequestParam(value = "task_creator", required = false) String taskCreator, @RequestParam(value = "task_creator", required = false) String taskCreator,
@RequestParam("page") Integer page, @RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") Integer pageSize) { @RequestParam("page_size") @Min(1) Integer pageSize) {
if (page <= 0 || pageSize <= 0) {
return new ResponseResult(400, "page or page_size is invalid")
.setData("task_list", null);
}
List<Task> tasks = taskService.queryTasks(taskStatus, taskType, taskName, taskCreator, page, pageSize); List<Task> tasks = taskService.queryTasks(taskStatus, taskType, taskName, taskCreator, page, pageSize);
return ResponseResult.ok() return ResponseResult.ok()
.setData("task_list", tasks); .setData("task_list", tasks);
} }
@GetMapping("/{id}/query") @GetMapping("/{id}/query")
public ResponseResult queryTask(@PathVariable("id") Integer id) { public ResponseResult queryTask(@PathVariable("id") @Min(1) Integer id) {
Task task = taskService.queryTask(id); Task task = taskService.queryTask(id);
if (task == null) { if (task == null) {
@@ -74,7 +70,7 @@ public class TaskController {
} }
@PostMapping("/{id}/update") @PostMapping("/{id}/update")
public ResponseResult updateTask(@PathVariable("id") Integer taskId, @RequestBody Task task) { public ResponseResult updateTask(@PathVariable("id") @Min(1) Integer taskId, @RequestBody @Valid Task task) {
task.setTaskId(taskId); task.setTaskId(taskId);
taskService.updateTask(task); taskService.updateTask(task);

View File

@@ -10,7 +10,7 @@ import java.util.List;
public interface TaskMapper { public interface TaskMapper {
void newTask(@Param("task") Task task); void newTask(@Param("task") Task task);
void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_ids") List<Integer> proobjIds); void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_id") Integer proobjId);
void newTaskStaticRuleConcat(@Param("task_id") Integer taskId, void newTaskStaticRuleConcat(@Param("task_id") Integer taskId,
@Param("rule_ids") List<Integer> staticRuleIds); @Param("rule_ids") List<Integer> staticRuleIds);

View File

@@ -4,6 +4,7 @@ import com.realtime.protection.configuration.entity.task.Task;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
@@ -24,9 +25,11 @@ public class TaskService {
try { try {
taskMapper.newTask(task); taskMapper.newTask(task);
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds()); task.getProtectObjectIds().forEach(
proobjId -> taskMapper.newTaskProtectObjectConcat(task.getTaskId(), proobjId));
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds()); taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
// taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
session.commit(); session.commit();
} catch (Exception e) { } catch (Exception e) {
@@ -73,34 +76,17 @@ public class TaskService {
return task; return task;
} }
@Transactional
public void updateTask(Task task) { public void updateTask(Task task) {
SqlSession session = sqlSessionFactory.openSession(false);
TaskMapper taskMapper = session.getMapper(TaskMapper.class);
try {
taskMapper.updateTask(task); taskMapper.updateTask(task);
taskMapper.clearTaskProtectObjectConcat(task.getTaskId()); taskMapper.clearTaskProtectObjectConcat(task.getTaskId());
taskMapper.clearTaskConnectedStaticRule(task.getTaskId()); taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
// taskMapper.clearTaskConnectedDynamicRule(task.getTaskId()); taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
if (task.getProtectObjectIds() != null && !task.getProtectObjectIds().isEmpty()) { task.getProtectObjectIds().forEach(
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds()); proobjId -> taskMapper.newTaskProtectObjectConcat(task.getTaskId(), proobjId));
}
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds()); taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
}
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty()) {
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds()); taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
} }
session.commit();
} catch (Exception e) {
session.rollback();
throw e;
} finally {
session.close();
}
}
} }

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.realtime.protection.server.defense.object.ProtectObjectMapper">
<delete id="deleteProtectObject">
DELETE FROM t_protect_object
WHERE protect_object_id = #{proobj_id}
</delete>
</mapper>

View File

@@ -17,10 +17,7 @@
<insert id="newTaskProtectObjectConcat"> <insert id="newTaskProtectObjectConcat">
INSERT INTO t_task_project_object(task_id, protect_object_id) INSERT INTO t_task_project_object(task_id, protect_object_id)
VALUES VALUES (#{task_id}, #{proobj_id})
<foreach collection="proobj_ids" item="proobj_id" separator="," index="index">
(#{task_id}, #{proobj_id})
</foreach>
</insert> </insert>
<update id="newTaskStaticRuleConcat"> <update id="newTaskStaticRuleConcat">
@@ -28,14 +25,29 @@
<set> <set>
<if test="task_id != null"> static_rule_used_task_id = #{task_id}, </if> <if test="task_id != null"> static_rule_used_task_id = #{task_id}, </if>
</set> </set>
WHERE static_rule_id IN <where>
<if test="rule_ids != null and rule_ids.size() > 0">
AND static_rule_id IN
<foreach collection="rule_ids" item="rule_id" open="(" close=")" separator=","> <foreach collection="rule_ids" item="rule_id" open="(" close=")" separator=",">
#{rule_id} #{rule_id}
</foreach> </foreach>
</if>
</where>
</update> </update>
<update id="newTaskDynamicRuleConcat"> <update id="newTaskDynamicRuleConcat">
<!-- todo: will be written after fulfilling t_dynamic_rule table --> UPDATE t_dynamic_rule
<set>
<if test="task_id != null"> dynamic_rule_used_task_id = #{task_id}, </if>
</set>
<where>
<if test="rule_ids != null and rule_ids.size() > 0">
AND dynamic_rule_id IN
<foreach collection="rule_ids" item="rule_id" open="(" close=")" separator=",">
#{rule_id}
</foreach>
</if>
</where>
</update> </update>
<resultMap id="taskMap" type="com.realtime.protection.configuration.entity.task.Task"> <resultMap id="taskMap" type="com.realtime.protection.configuration.entity.task.Task">

View File

@@ -36,4 +36,33 @@
</where> </where>
LIMIT ${(page - 1) * page_size}, #{page_size} LIMIT ${(page - 1) * page_size}, #{page_size}
</select> </select>
<update id="updateTemplateInformation">
UPDATE t_strategy_template
<set>
<!-- update template name and default op -->
<if test="template.templateName != null">strategy_template_name = #{template.templateName}, </if>
<if test="template.defaultOp != null">default_op = #{template.defaultOp},</if>
<!-- update template elements -->
<if test="template.hasProtectObjectIP != null">has_protect_object_ip = #{template.hasProtectObjectIP},</if>
<if test="template.hasProtectObjectPort != null">
has_protect_object_port = {template.hasProtectObjectPort},
</if>
<if test="template.hasPeerIP != null">has_peer_ip = #{template.hasPeerIP},</if>
<if test="template.hasPeerPort != null">has_peer_port = #{template.hasPeerPort},</if>
<if test="template.hasProtocol != null">has_protocol = #{template.hasProtocol},</if>
<if test="template.hasURL != null">has_url = #{template.hasURL},</if>
<if test="template.hasDNS != null">has_dns = #{template.hasDNS},</if>
</set>
<where>
AND strategy_template_id = #{template.templateId}
</where>
</update>
<update id="addTemplateUsedTimes">
UPDATE t_strategy_template
SET strategy_template_used_times = strategy_template_used_times + #{add_times}
WHERE strategy_template_id = #{template_id}
</update>
</mapper> </mapper>

View File

@@ -0,0 +1,26 @@
package com.realtime.protection.server.defense.object;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
@SpringBootTest
class ProtectObjectServiceTest {
private final ProtectObjectService protectObjectService;
@Autowired
ProtectObjectServiceTest(ProtectObjectService protectObjectService) {
this.protectObjectService = protectObjectService;
}
@Test
void testDeleteProtectObject() {
Boolean success = protectObjectService.deleteProtectObjects(List.of(1, 2, 3, 4));
assertFalse(success);
}
}

View File

@@ -6,6 +6,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest @SpringBootTest
@@ -24,7 +26,7 @@ class TemplateServiceTest {
template = new Template(); template = new Template();
template.setTemplateName("反射型DDOS攻击"); template.setTemplateName("反射型DDOS攻击");
template.setTemplateElements(new String[]{"对端IP", "协议", "URL"}); template.setTemplateElements(List.of("对端IP", "协议", "URL"));
template.setDefaultOp("阻断"); template.setDefaultOp("阻断");
} }
@@ -36,10 +38,26 @@ class TemplateServiceTest {
@Test @Test
void testNewTemplateIllegalArgument() { void testNewTemplateIllegalArgument() {
template.setTemplateElements(new String[]{"DDNS"}); template.setTemplateElements(List.of("DDNS"));
assertThrows(IllegalArgumentException.class, () -> { assertThrows(IllegalArgumentException.class, () -> {
Integer templateId = templateService.newTemplate(template); Integer templateId = templateService.newTemplate(template);
assertTrue(templateId > 0); assertTrue(templateId > 0);
}); });
} }
@Test
void testUpdateTemplateSuccess() {
template.setTemplateName("洪泛型DDOS攻击");
assertTrue(templateService.updateTemplate(3, template));
}
@Test
void testAddTemplateUsedTimes() {
List<Template> templates = templateService.queryTemplates("洪泛型DDOS攻击", 1, 1);
Template originalTemplate = templates.get(0);
Boolean success = templateService.addTemplateUsedTimes(3, 4);
templates = templateService.queryTemplates("洪泛型DDOS攻击", 1, 1);
assertEquals(templates.get(0).getTemplateUsedTimes(), originalTemplate.getTemplateUsedTimes() + 4);
assertTrue(success);
}
} }

View File

@@ -34,12 +34,12 @@ class TaskServiceTest {
task.setTaskEndTime(taskEndTime); task.setTaskEndTime(taskEndTime);
task.setTaskAct("阻断"); task.setTaskAct("阻断");
task.setTaskType("静态任务"); task.setTaskType("静态任务");
task.setStaticRuleIds(new Integer[]{1, 2}); task.setStaticRuleIds(List.of(1, 2));
task.setDynamicRuleIds(new Integer[]{}); task.setDynamicRuleIds(List.of());
task.setTaskCreateUserId(1); task.setTaskCreateUserId(1);
task.setTaskCreateUsername("xxx"); task.setTaskCreateUsername("xxx");
task.setTaskCreateDepart("xxx"); task.setTaskCreateDepart("xxx");
task.setProtectObjectIds(new Integer[]{1}); task.setProtectObjectIds(List.of(1));
} }
@Test @Test