Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[imporve] Adjustment of parameter verification #2635

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 "
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> receiverId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -78,7 +78,7 @@ public class Tag {
accessMode = READ_WRITE)
@Min(0)
@Max(3)
private byte type;
private Byte type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this change cause null data conversion exceptions? when type is null in db, but use the Byte

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this change cause null data conversion exceptions? when type is null in db, but use the Byte

I think it is ok. Byte is compatible with null but byte cannot convert null as a primitive data type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, we need make sure Byte type can not null, due here will throws a NullPointerException since null cannot be converted to a primitive value.

List<Tag> tags = monitor.getTags().stream().filter(tag -> tag.getType() == (byte) 0).collect(Collectors.toList());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, we need make sure Byte type can not null, due here will throws a NullPointerException since null cannot be converted to a primitive value.

List<Tag> tags = monitor.getTags().stream().filter(tag -> tag.getType() == (byte) 0).collect(Collectors.toList());

Okay, i will check it.


@Schema(title = "The creator of this record", example = "tom", accessMode = READ_ONLY)
@CreatedBy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,11 +51,6 @@ public class TagController {
@PostMapping
@Operation(summary = "Add Tag", description = "Add Tag")
public ResponseEntity<Message<Void>> addNewTags(@Valid @RequestBody List<Tag> 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"));
}
Expand All @@ -65,8 +59,8 @@ public ResponseEntity<Message<Void>> addNewTags(@Valid @RequestBody List<Tag> ta
@Operation(summary = "Modify an existing tag", description = "Modify an existing tag")
public ResponseEntity<Message<Void>> 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"));
Expand All @@ -79,18 +73,14 @@ public ResponseEntity<Message<Page<Tag>>> 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<Tag> alertPage = tagService.getTags(search, type, pageIndex, pageSize);
Message<Page<Tag>> 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<Message<Void>> deleteTags(
@Parameter(description = "TAG IDs ", example = "6565463543") @RequestParam(required = false) List<Long> ids) {
if (ids != null && !ids.isEmpty()) {
tagService.deleteTags(new HashSet<>(ids));
}
tagService.deleteTags(new HashSet<>(ids));
return ResponseEntity.ok(Message.success("Delete success"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +41,7 @@ public class MonitorDto {
private Monitor monitor;

@Schema(description = "Monitor Params", accessMode = READ_WRITE)
@NotNull
@NotEmpty
@Valid
private List<Param> params;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class TagServiceImpl implements TagService {

@Override
public void addTags(List<Tag> tags) {
// Verify request data
tags = tags.stream().peek(tag -> {
tag.setType((byte) 1);
tag.setId(null);
}).distinct().collect(Collectors.toList());
tagDao.saveAll(tags);
}

Expand Down Expand Up @@ -108,6 +113,9 @@ public Page<Tag> getTags(String search, Byte type, int pageIndex, int pageSize)

@Override
public void deleteTags(HashSet<Long> ids) {
if (CollectionUtils.isEmpty(ids)){
return;
}
if (tagMonitorBindDao.countByTagIdIn(ids) != 0) {
throw new CommonException("The tag is in use and cannot be deleted.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,9 @@ void getTags() throws Exception {

@Test
void deleteTags() throws Exception {
List<Long> 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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> set = new HashSet<>(1);
set.add(1L);
assertThrows(CommonException.class, () -> tagService.deleteTags(set));
}
}
Loading