diff --git a/common/src/main/java/com/zmops/open/common/entity/manager/NoticeReceiver.java b/common/src/main/java/com/zmops/open/common/entity/manager/NoticeReceiver.java index 3660bcf..0251967 100644 --- a/common/src/main/java/com/zmops/open/common/entity/manager/NoticeReceiver.java +++ b/common/src/main/java/com/zmops/open/common/entity/manager/NoticeReceiver.java @@ -33,6 +33,7 @@ import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import java.time.LocalDateTime; +import java.util.List; import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_ONLY; import static io.swagger.v3.oas.annotations.media.Schema.AccessMode.READ_WRITE; @@ -72,7 +73,8 @@ public class NoticeReceiver { accessMode = READ_WRITE) @Min(0) @NotNull - private Byte type; + @Convert(converter = JsonByteListAttributeConverter.class) + private List type; @Schema(title = "Mobile number: Valid when the notification method is SMS", description = "手机号 : 通知方式为手机短信时有效", diff --git a/common/src/main/java/com/zmops/open/common/entity/manager/NoticeRule.java b/common/src/main/java/com/zmops/open/common/entity/manager/NoticeRule.java index 9f21454..19ac9bf 100644 --- a/common/src/main/java/com/zmops/open/common/entity/manager/NoticeRule.java +++ b/common/src/main/java/com/zmops/open/common/entity/manager/NoticeRule.java @@ -29,6 +29,7 @@ import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.*; +import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.time.LocalDateTime; @@ -100,6 +101,14 @@ public class NoticeRule { @Convert(converter = JsonTagListAttributeConverter.class) private List tags; + @Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot 8-SlackWebHook 9-Discord Bot", + description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人 8-SlackWebHook 9-Discord机器人", + accessMode = READ_WRITE) + @Min(0) + @NotNull + @Convert(converter = JsonByteListAttributeConverter.class) + private List type; + @Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom", accessMode = READ_ONLY) @CreatedBy private String creator; diff --git a/manager/src/main/java/com/zmops/open/manager/component/alerter/DispatcherAlarm.java b/manager/src/main/java/com/zmops/open/manager/component/alerter/DispatcherAlarm.java index 05322e9..1bda699 100644 --- a/manager/src/main/java/com/zmops/open/manager/component/alerter/DispatcherAlarm.java +++ b/manager/src/main/java/com/zmops/open/manager/component/alerter/DispatcherAlarm.java @@ -78,16 +78,17 @@ public void afterPropertiesSet() throws Exception { * @return send success or failed */ public boolean sendNoticeMsg(NoticeReceiver receiver, Alert alert) { - if(receiver == null || receiver.getType() == null){ + if(receiver == null || receiver.getType() == null || receiver.getType().isEmpty()){ log.warn("DispatcherAlarm-sendNoticeMsg params is empty alert:[{}], receiver:[{}]", alert, receiver); return false; } - byte type = receiver.getType(); - if (alertNotifyHandlerMap.containsKey(type)) { - alertNotifyHandlerMap.get(type).send(receiver, alert); - return true; + List types = receiver.getType(); + for (byte type : types) { + if (alertNotifyHandlerMap.containsKey(type)) { + alertNotifyHandlerMap.get(type).send(receiver, alert); + } } - return false; + return true; } private List matchReceiverByNoticeRules(Alert alert) { diff --git a/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java b/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java index 317b03a..1431045 100644 --- a/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java +++ b/manager/src/main/java/com/zmops/open/manager/service/impl/NoticeConfigServiceImpl.java @@ -33,6 +33,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -109,7 +110,7 @@ public List getReceiverFilterRule(Alert alert) { // The temporary rule is to forward all, and then implement more matching rules: alarm status selection, monitoring type selection, etc. // 规则是全部转发, 告警状态选择, 监控类型选择等(按照tags标签和告警级别过滤匹配) - Set filterReceivers = rules.stream() + Map filterReceiverMap = rules.stream() .filter(rule -> { if (rule.isFilterAll()) { return true; @@ -133,9 +134,13 @@ public List getReceiverFilterRule(Alert alert) { } return true; }) - .map(NoticeRule::getReceiverId) - .collect(Collectors.toSet()); - return noticeReceiverDao.findAllById(filterReceivers); + .collect(Collectors.toMap(NoticeRule::getReceiverId, item -> item)); + List receivers = noticeReceiverDao.findAllById(filterReceiverMap.keySet()); + for (NoticeReceiver receiver : receivers) { + NoticeRule rule = filterReceiverMap.get(receiver.getId()); + receiver.setType(rule.getType()); + } + return receivers; } @Override diff --git a/manager/src/test/java/com/zmops/open/manager/controller/NoticeConfigControllerTest.java b/manager/src/test/java/com/zmops/open/manager/controller/NoticeConfigControllerTest.java deleted file mode 100644 index 77a22a2..0000000 --- a/manager/src/test/java/com/zmops/open/manager/controller/NoticeConfigControllerTest.java +++ /dev/null @@ -1,226 +0,0 @@ -package com.zmops.open.manager.controller; - -import com.zmops.open.common.entity.manager.NoticeReceiver; -import com.zmops.open.common.entity.manager.NoticeRule; -import com.zmops.open.common.util.CommonConstants; -import com.zmops.open.common.util.GsonUtil; -import com.zmops.open.manager.service.impl.NoticeConfigServiceImpl; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; - -import java.util.ArrayList; -import java.util.List; - -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -/** - * Test case for {@link NoticeConfigController} - */ -@ExtendWith(MockitoExtension.class) -class NoticeConfigControllerTest { - - private MockMvc mockMvc; - - @Mock - private NoticeConfigServiceImpl noticeConfigService; - - @InjectMocks - private NoticeConfigController noticeConfigController; - - - public NoticeRule getNoticeRule(){ - List tags = new ArrayList<>(); - NoticeRule.TagItem tagItem = new NoticeRule.TagItem(); - tagItem.setName("key1"); - tagItem.setValue("value1"); - tags.add(tagItem); - - NoticeRule noticeRule = new NoticeRule(); - noticeRule.setId(87584674384L); - noticeRule.setName("dispatch-1"); - noticeRule.setReceiverId(4324324L); - noticeRule.setReceiverName("tom"); - noticeRule.setCreator("tom"); - noticeRule.setModifier("tom"); - noticeRule.setTags(tags); - - return noticeRule; - } - - public NoticeReceiver getNoticeReceiver(){ - NoticeReceiver noticeReceiver = new NoticeReceiver(); - noticeReceiver.setName("tom"); - noticeReceiver.setPhone("18923435643"); - noticeReceiver.setEmail("tom@qq.com"); - noticeReceiver.setHookUrl("https://www.tancloud.cn"); - noticeReceiver.setType((byte) 1); - - return noticeReceiver; - - } - - - @BeforeEach - void setUp() { - this.mockMvc = MockMvcBuilders.standaloneSetup(noticeConfigController).build(); - } - - @Test - void addNewNoticeReceiver() throws Exception { - NoticeReceiver noticeReceiver = getNoticeReceiver(); - System.out.println(noticeReceiver); - this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/receiver") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("Add success")) - .andReturn(); - } - - @Test - void editNoticeReceiver() throws Exception { - NoticeReceiver noticeReceiver = getNoticeReceiver(); - System.out.println(noticeReceiver); - this.mockMvc.perform(MockMvcRequestBuilders.put("/api/notice/receiver") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("Edit success")) - .andReturn(); - - } - - @Test - void deleteNoticeReceiver() throws Exception { - NoticeReceiver noticeReceiver = getNoticeReceiver(); - - Mockito.when(noticeConfigService.getReceiverById(7565463543L)) - .thenReturn(noticeReceiver); - Mockito.when(noticeConfigService.getReceiverById(6565463543L)) - .thenReturn(null); - - - this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/notice/receiver/{id}", 6565463543L)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("The relevant information of the recipient could not be found, please check whether the parameters are correct")) - .andReturn(); - - this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/notice/receiver/{id}", 7565463543L)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("Delete success")) - .andReturn(); - - } - - @Test - void getReceivers() throws Exception { - - //Mockito.when(noticeConfigService.getNoticeReceivers()) - this.mockMvc.perform(MockMvcRequestBuilders.get("/api/notice/receivers?name={name}", "tom")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andReturn(); - } - - @Test - void addNewNoticeRule() throws Exception { - NoticeRule noticeRule = getNoticeRule(); - this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/rule") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeRule))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("Add success")) - .andReturn(); - } - - @Test - void editNoticeRule() throws Exception { - NoticeRule noticeRule = getNoticeRule(); - this.mockMvc.perform(MockMvcRequestBuilders.put("/api/notice/rule") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeRule))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("Edit success")) - .andReturn(); - } - - @Test - void deleteNoticeRule() throws Exception { - NoticeRule noticeRule = getNoticeRule(); - - Mockito.when(noticeConfigService.getNoticeRulesById(7565463543L)) - .thenReturn(noticeRule); - Mockito.when(noticeConfigService.getNoticeRulesById(6565463543L)) - .thenReturn(null); - - - this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/notice/rule/{id}", 6565463543L)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("The specified notification rule could not be queried, please check whether the parameters are correct")) - .andReturn(); - - this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/notice/rule/{id}", 7565463543L)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andExpect(jsonPath("$.msg").value("Delete success")) - .andReturn(); - } - - @Test - void getRules() throws Exception { - this.mockMvc.perform(MockMvcRequestBuilders.get("/api/notice/rules")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andReturn(); - - this.mockMvc.perform(MockMvcRequestBuilders.get("/api/notice/rules?name={name}", "tom")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) - .andReturn(); - } - - - @Test - void sendTestMsg() throws Exception { - NoticeReceiver noticeReceiver = getNoticeReceiver(); - Mockito.when(noticeConfigService.sendTestMsg(noticeReceiver)) - .thenReturn(false); - - this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/receiver/send-test-msg") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.FAIL_CODE)) - .andExpect(jsonPath("$.msg").value("Notify service not available, please check config!")) - .andReturn(); - - - Mockito.when(noticeConfigService.sendTestMsg(noticeReceiver)) - .thenReturn(true); - - this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/receiver/send-test-msg") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) -// .andExpect(jsonPath("$.msg").value("Notify service not available, please check config!")) - .andReturn(); - } -} diff --git a/manager/src/test/java/com/zmops/open/manager/dao/NoticeRuleDaoTest.java b/manager/src/test/java/com/zmops/open/manager/dao/NoticeRuleDaoTest.java deleted file mode 100644 index 9ed400e..0000000 --- a/manager/src/test/java/com/zmops/open/manager/dao/NoticeRuleDaoTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.zmops.open.manager.dao; - -import com.zmops.open.common.entity.manager.NoticeRule; -import com.zmops.open.manager.AbstractSpringIntegrationTest; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.transaction.annotation.Transactional; - -import javax.annotation.Resource; -import java.time.LocalDateTime; -import java.util.Collections; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -/** - * Test case for {@link NoticeRuleDao} - */ -@Transactional -class NoticeRuleDaoTest extends AbstractSpringIntegrationTest { - - @Resource - private NoticeRuleDao noticeRuleDao; - - @BeforeEach - void setUp() { - // insert notice rule with enable = true - NoticeRule enabled = NoticeRule.builder() - .name("mock notice rule") - .enable(true) - .filterAll(true) - .gmtCreate(LocalDateTime.now()) - .gmtUpdate(LocalDateTime.now()) - .modifier("mock") - .creator("mock") - .priorities(Collections.emptyList()) - .receiverId(1L) - .receiverName("mock receiver") - .tags(Collections.emptyList()) - .build(); - enabled = noticeRuleDao.saveAndFlush(enabled); - assertNotNull(enabled); - - // insert notice rule with enable = false - NoticeRule disabled = NoticeRule.builder() - .id(2L) - .name("mock notice rule") - .enable(false) - .filterAll(true) - .gmtCreate(LocalDateTime.now()) - .gmtUpdate(LocalDateTime.now()) - .modifier("mock") - .creator("mock") - .priorities(Collections.emptyList()) - .receiverId(1L) - .receiverName("mock receiver") - .tags(Collections.emptyList()) - .build(); - disabled = noticeRuleDao.saveAndFlush(disabled); - assertNotNull(disabled); - } - - @AfterEach - void tearDown() { - noticeRuleDao.deleteAll(); - } - - @Test - void findNoticeRulesByEnableTrue() { - List enabledList = noticeRuleDao.findNoticeRulesByEnableTrue(); - assertNotNull(enabledList); - assertEquals(1, enabledList.size()); - } -}