Skip to content

Commit 225157a

Browse files
authored
Merge branch 'master' into xml_path_23_8
2 parents afc8926 + 6c36d1d commit 225157a

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

manager/src/main/java/org/apache/hertzbeat/manager/service/impl/TagServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public class TagServiceImpl implements TagService {
6060
public void addTags(List<Tag> tags) {
6161
// Verify request data
6262
tags = tags.stream().peek(tag -> {
63+
Optional<Tag> tagOptional = tagDao.findTagByNameAndTagValue(tag.getName(), tag.getTagValue());
64+
if (tagOptional.isPresent()) {
65+
throw new IllegalArgumentException("The tag already exists.");
66+
}
6367
tag.setType((byte) 1);
6468
tag.setId(null);
6569
}).distinct().collect(Collectors.toList());
@@ -70,6 +74,11 @@ public void addTags(List<Tag> tags) {
7074
public void modifyTag(Tag tag) {
7175
Optional<Tag> tagOptional = tagDao.findById(tag.getId());
7276
if (tagOptional.isPresent()) {
77+
78+
Optional<Tag> tagExistOptional = tagDao.findTagByNameAndTagValue(tag.getName(), tag.getTagValue());
79+
if (tagExistOptional.isPresent() && !tagExistOptional.get().getId().equals(tag.getId())) {
80+
throw new IllegalArgumentException("The tag with same key and value already exists.");
81+
}
7382
tag.setTagValue(StringUtils.isEmpty(tag.getTagValue()) ? null : tag.getTagValue());
7483
tagDao.save(tag);
7584
} else {

manager/src/test/java/org/apache/hertzbeat/manager/service/TagServiceTest.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2121
import static org.junit.jupiter.api.Assertions.assertNotNull;
2222
import static org.junit.jupiter.api.Assertions.assertThrows;
23-
import static org.mockito.ArgumentMatchers.anyList;
2423
import static org.mockito.ArgumentMatchers.anySet;
24+
import static org.mockito.ArgumentMatchers.anyString;
2525
import static org.mockito.Mockito.any;
2626
import static org.mockito.Mockito.reset;
27+
import static org.mockito.Mockito.verify;
2728
import static org.mockito.Mockito.when;
29+
2830
import java.util.Collections;
2931
import java.util.HashSet;
32+
import java.util.List;
3033
import java.util.Optional;
34+
3135
import org.apache.hertzbeat.common.entity.manager.Tag;
3236
import org.apache.hertzbeat.common.support.exception.CommonException;
3337
import org.apache.hertzbeat.manager.dao.TagDao;
@@ -47,22 +51,30 @@
4751
*/
4852
@ExtendWith(MockitoExtension.class)
4953
class TagServiceTest {
50-
54+
5155
@InjectMocks
5256
private TagServiceImpl tagService;
53-
57+
5458
@Mock
5559
private TagDao tagDao;
56-
60+
5761
@Mock
5862
private TagMonitorBindDao tagMonitorBindDao;
59-
63+
6064
@Test
6165
void addTags() {
62-
when(tagDao.saveAll(anyList())).thenReturn(anyList());
63-
assertDoesNotThrow(() -> tagService.addTags(Collections.singletonList(new Tag())));
66+
// Prepare test data
67+
List<Tag> tags = Collections.singletonList(
68+
Tag.builder().id(1L).name("tagname").tagValue("tagvalue").build()
69+
);
70+
when(tagDao.findTagByNameAndTagValue(anyString(), anyString())).thenReturn(Optional.empty());
71+
72+
tagService.addTags(tags);
73+
74+
verify(tagDao).saveAll(tags);
75+
6476
}
65-
77+
6678
@Test
6779
void modifyTag() {
6880
Tag tag = Tag.builder().id(1L).build();
@@ -73,18 +85,18 @@ void modifyTag() {
7385
when(tagDao.findById(1L)).thenReturn(Optional.empty());
7486
assertThrows(IllegalArgumentException.class, () -> tagService.modifyTag(tag));
7587
}
76-
88+
7789
@Test
7890
void getTags() {
7991
when(tagDao.findAll(any(Specification.class), any(PageRequest.class))).thenReturn(Page.empty());
8092
assertNotNull(tagService.getTags(null, null, 1, 10));
8193
}
82-
94+
8395
@Test
8496
void deleteTags() {
8597
assertDoesNotThrow(() -> tagService.deleteTags(new HashSet<>(1)));
8698
}
87-
99+
88100
@Test
89101
void deleteUsingTags() {
90102
when(tagMonitorBindDao.countByTagIdIn(anySet())).thenReturn(1L);

web-app/src/app/routes/monitor/monitor-form/monitor-form.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
<nz-upload
178178
nzName="file"
179179
nzListType="text"
180-
[nzShowUploadList]="true"
180+
[nzShowUploadList]="false"
181181
[nzMultiple]="false"
182182
[nzLimit]="1"
183183
[nzCustomRequest]="handleTemplateInput"

0 commit comments

Comments
 (0)