From 89be682a88fc136218a1fb390668016f058c99b6 Mon Sep 17 00:00:00 2001 From: b_mountain <49973336+MRgenial@users.noreply.github.com> Date: Fri, 30 Aug 2024 00:06:53 +0800 Subject: [PATCH] [imporve] Adjustment of parameter verification (#2635) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 刘进山 Co-authored-by: Calvin Co-authored-by: shown --- .../common/entity/manager/Collector.java | 6 +++--- .../common/entity/manager/GeneralConfig.java | 4 ++-- .../common/entity/manager/NoticeReceiver.java | 5 +++-- .../common/entity/manager/NoticeRule.java | 7 ++++--- .../hertzbeat/common/entity/manager/Param.java | 4 ++-- .../hertzbeat/common/entity/manager/Tag.java | 6 +++--- .../manager/controller/TagController.java | 18 ++++-------------- .../hertzbeat/manager/pojo/dto/MonitorDto.java | 3 ++- .../manager/service/impl/TagServiceImpl.java | 8 ++++++++ .../manager/controller/TagControllerTest.java | 7 ++----- .../manager/service/TagServiceTest.java | 7 +++---- 11 files changed, 36 insertions(+), 39 deletions(-) diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java index 78c5eb51ab1..c692bab629c 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Collector.java @@ -26,7 +26,7 @@ import jakarta.persistence.Table; import jakarta.persistence.UniqueConstraint; import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; import java.time.LocalDateTime; import lombok.AllArgsConstructor; import lombok.Builder; @@ -57,11 +57,11 @@ public class Collector { private Long id; @Schema(title = "collector identity name", description = "collector identity name") - @NotNull + @NotBlank(message = "name can not null") private String name; @Schema(title = "collector ip", description = "collector remote ip") - @NotNull + @NotBlank(message = "ip can not null") private String ip; @Schema(title = "collector version", description = "collector version") diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/GeneralConfig.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/GeneralConfig.java index 4f65da6806b..08325e79bf3 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/GeneralConfig.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/GeneralConfig.java @@ -25,7 +25,7 @@ import jakarta.persistence.EntityListeners; import jakarta.persistence.Id; import jakarta.persistence.Table; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; import java.time.LocalDateTime; import lombok.AllArgsConstructor; import lombok.Builder; @@ -53,7 +53,7 @@ public class GeneralConfig { @Id @Schema(title = "Config type: email sms, primary key ", description = "Config type: email sms, primary key ", accessMode = READ_WRITE) - @NotNull + @NotBlank(message = "ip can not null") private String type; @Schema(title = "Config content", description = "Config content,formatjson", accessMode = READ_WRITE) diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeReceiver.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeReceiver.java index b794fd0d969..af03830b279 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeReceiver.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeReceiver.java @@ -28,6 +28,7 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import java.time.LocalDateTime; @@ -63,7 +64,7 @@ public class NoticeReceiver { @Schema(title = "Recipient name", description = "Recipient name", example = "tom", accessMode = READ_WRITE) @Size(max = 100) - @NotNull + @NotBlank(message = "name can not null") private String name; @Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot " @@ -75,7 +76,7 @@ public class NoticeReceiver { + "WeChat app message", accessMode = READ_WRITE) @Min(0) - @NotNull + @NotNull(message = "type can not null") private Byte type; @Schema(title = "Mobile number: Valid when the notification method is SMS", diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeRule.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeRule.java index 0e24e2d7a94..8f20554b94b 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeRule.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/NoticeRule.java @@ -28,7 +28,8 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Size; import java.time.LocalDateTime; import java.time.ZonedDateTime; @@ -67,13 +68,13 @@ public class NoticeRule { description = "Policy name", example = "dispatch-1", accessMode = READ_WRITE) @Size(max = 100) - @NotNull + @NotBlank(message = "name can not null") private String name; @Schema(title = "Recipient ID", description = "Recipient ID", example = "4324324", accessMode = READ_WRITE) - @NotNull + @NotEmpty(message = "receiverId can not empty") @Convert(converter = JsonLongListAttributeConverter.class) private List receiverId; diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Param.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Param.java index bb331495d62..80810908341 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Param.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Param.java @@ -30,7 +30,7 @@ import jakarta.persistence.Table; import jakarta.persistence.UniqueConstraint; import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import java.time.LocalDateTime; import lombok.AllArgsConstructor; @@ -74,7 +74,7 @@ public class Param { */ @Schema(title = "Parameter identifier field", example = "port", accessMode = READ_WRITE) @Size(max = 100) - @NotNull + @NotBlank(message = "field can not null") private String field; /** diff --git a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Tag.java b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Tag.java index 8d887015c3e..a0f6ff6de18 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Tag.java +++ b/common/src/main/java/org/apache/hertzbeat/common/entity/manager/Tag.java @@ -29,7 +29,7 @@ import jakarta.persistence.Table; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; import java.time.LocalDateTime; import java.util.Objects; import lombok.AllArgsConstructor; @@ -61,7 +61,7 @@ public class Tag { private Long id; @Schema(title = "Tag Field", example = "app", accessMode = READ_WRITE) - @NotNull + @NotBlank(message = "name can not null") private String name; @Schema(title = "Tag Value", example = "23", accessMode = READ_WRITE) @@ -78,7 +78,7 @@ public class Tag { accessMode = READ_WRITE) @Min(0) @Max(3) - private byte type; + private Byte type; @Schema(title = "The creator of this record", example = "tom", accessMode = READ_ONLY) @CreatedBy diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/TagController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/TagController.java index e7f793239d8..43276f788d4 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/TagController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/TagController.java @@ -23,7 +23,6 @@ import jakarta.validation.Valid; import java.util.HashSet; import java.util.List; -import java.util.stream.Collectors; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.manager.Tag; import org.apache.hertzbeat.manager.service.TagService; @@ -52,11 +51,6 @@ public class TagController { @PostMapping @Operation(summary = "Add Tag", description = "Add Tag") public ResponseEntity> addNewTags(@Valid @RequestBody List tags) { - // Verify request data - tags = tags.stream().peek(tag -> { - tag.setType((byte) 1); - tag.setId(null); - }).distinct().collect(Collectors.toList()); tagService.addTags(tags); return ResponseEntity.ok(Message.success("Add success")); } @@ -65,8 +59,8 @@ public ResponseEntity> addNewTags(@Valid @RequestBody List ta @Operation(summary = "Modify an existing tag", description = "Modify an existing tag") public ResponseEntity> modifyMonitor(@Valid @RequestBody Tag tag) { // Verify request data - if (tag.getId() == null || tag.getName() == null) { - throw new IllegalArgumentException("The Tag not exist."); + if (tag.getId() == null) { + throw new IllegalArgumentException("ID cannot be null."); } tagService.modifyTag(tag); return ResponseEntity.ok(Message.success("Modify success")); @@ -79,18 +73,14 @@ public ResponseEntity>> getTags( @Parameter(description = "Tag type", example = "0") @RequestParam(required = false) Byte type, @Parameter(description = "List current page", example = "0") @RequestParam(defaultValue = "0") int pageIndex, @Parameter(description = "Number of list pagination", example = "8") @RequestParam(defaultValue = "8") int pageSize) { - Page alertPage = tagService.getTags(search, type, pageIndex, pageSize); - Message> message = Message.success(alertPage); - return ResponseEntity.ok(message); + return ResponseEntity.ok(Message.success(tagService.getTags(search, type, pageIndex, pageSize))); } @DeleteMapping() @Operation(summary = "Delete tags based on ID", description = "Delete tags based on ID") public ResponseEntity> deleteTags( @Parameter(description = "TAG IDs ", example = "6565463543") @RequestParam(required = false) List ids) { - if (ids != null && !ids.isEmpty()) { - tagService.deleteTags(new HashSet<>(ids)); - } + tagService.deleteTags(new HashSet<>(ids)); return ResponseEntity.ok(Message.success("Delete success")); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/MonitorDto.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/MonitorDto.java index d52e7b8dda5..9c71eb1adf9 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/MonitorDto.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/MonitorDto.java @@ -21,6 +21,7 @@ import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.Valid; +import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import java.util.List; import lombok.Data; @@ -40,7 +41,7 @@ public class MonitorDto { private Monitor monitor; @Schema(description = "Monitor Params", accessMode = READ_WRITE) - @NotNull + @NotEmpty @Valid private List params; diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TagServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TagServiceImpl.java index 2340c26f807..02ac69febf6 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TagServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TagServiceImpl.java @@ -56,6 +56,11 @@ public class TagServiceImpl implements TagService { @Override public void addTags(List tags) { + // Verify request data + tags = tags.stream().peek(tag -> { + tag.setType((byte) 1); + tag.setId(null); + }).distinct().collect(Collectors.toList()); tagDao.saveAll(tags); } @@ -108,6 +113,9 @@ public Page getTags(String search, Byte type, int pageIndex, int pageSize) @Override public void deleteTags(HashSet ids) { + if (CollectionUtils.isEmpty(ids)){ + return; + } if (tagMonitorBindDao.countByTagIdIn(ids) != 0) { throw new CommonException("The tag is in use and cannot be deleted."); } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/controller/TagControllerTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/controller/TagControllerTest.java index bb79c1bb71f..3e29badb9be 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/controller/TagControllerTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/controller/TagControllerTest.java @@ -110,12 +110,9 @@ void getTags() throws Exception { @Test void deleteTags() throws Exception { - List ids = new ArrayList<>(); - ids.add(6565463543L); - this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/tag") - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtil.toJson(ids))) + .contentType(MediaType.MULTIPART_FORM_DATA) + .param("ids", "6565463543")) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Delete success")) diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/TagServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/TagServiceTest.java index 23e29e25708..ada1c65956a 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/TagServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/TagServiceTest.java @@ -23,7 +23,6 @@ import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anySet; import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.when; import java.util.Collections; @@ -83,14 +82,14 @@ void getTags() { @Test void deleteTags() { - doNothing().when(tagDao).deleteTagsByIdIn(anySet()); - when(tagMonitorBindDao.countByTagIdIn(anySet())).thenReturn(0L); assertDoesNotThrow(() -> tagService.deleteTags(new HashSet<>(1))); } @Test void deleteUsingTags() { when(tagMonitorBindDao.countByTagIdIn(anySet())).thenReturn(1L); - assertThrows(CommonException.class, () -> tagService.deleteTags(new HashSet<>(1))); + HashSet set = new HashSet<>(1); + set.add(1L); + assertThrows(CommonException.class, () -> tagService.deleteTags(set)); } }