diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImpl.java index 62e6ff38dca..6300b718eab 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImpl.java @@ -123,7 +123,7 @@ public byte type() { @Data @AllArgsConstructor @NoArgsConstructor - private static class WeChatAppReq { + protected static class WeChatAppReq { @JsonProperty(value = "errcode") private Integer errCode; @@ -139,7 +139,7 @@ private static class WeChatAppReq { @Builder @AllArgsConstructor @NoArgsConstructor - private static class WeChatAppDTO { + protected static class WeChatAppDTO { /** * markdown format @@ -172,7 +172,7 @@ private static class WeChatAppDTO { private MarkdownDTO markdown; @Data - private static class MarkdownDTO { + protected static class MarkdownDTO { /** * message content */ @@ -180,7 +180,7 @@ private static class MarkdownDTO { } @Data - private static class TextDTO { + protected static class TextDTO { /** * message content */ diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/AlertNotifyHandlerTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/AlertNotifyHandlerTest.java deleted file mode 100644 index f258c069d70..00000000000 --- a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/AlertNotifyHandlerTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hertzbeat.manager.component.alerter; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * Test case for {@link AlertNotifyHandler} - */ -class AlertNotifyHandlerTest { - - @BeforeEach - void setUp() { - } - - @Test - void send() { - } - - @Test - void type() { - } -} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarmTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarmTest.java index 22d94420ac6..59f0cfc19d3 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarmTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/DispatcherAlarmTest.java @@ -17,23 +17,117 @@ package org.apache.hertzbeat.manager.component.alerter; +import java.util.List; + +import org.apache.hertzbeat.alert.AlerterWorkerPool; +import org.apache.hertzbeat.common.entity.alerter.Alert; +import org.apache.hertzbeat.common.entity.manager.NoticeReceiver; +import org.apache.hertzbeat.common.entity.manager.NoticeTemplate; +import org.apache.hertzbeat.common.queue.CommonDataQueue; +import org.apache.hertzbeat.manager.service.NoticeConfigService; +import org.apache.hertzbeat.manager.service.PluginService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; /** * Test case for {@link DispatcherAlarm} */ + +@ExtendWith(MockitoExtension.class) class DispatcherAlarmTest { + @Mock + private AlerterWorkerPool workerPool; + + @Mock + private CommonDataQueue dataQueue; + + @Mock + private NoticeConfigService noticeConfigService; + + @Mock + private AlertStoreHandler alertStoreHandler; + + @Mock + private PluginService pluginService; + + @Mock + private AlertNotifyHandler alertNotifyHandler; + + private DispatcherAlarm dispatcherAlarm; + + private static final int DISPATCH_THREADS = 3; + @BeforeEach void setUp() { + + List alertNotifyHandlerList = List.of(alertNotifyHandler); + dispatcherAlarm = new DispatcherAlarm( + workerPool, + dataQueue, + noticeConfigService, + alertStoreHandler, + alertNotifyHandlerList, + pluginService + ); + } + + @Test + void testAfterPropertiesSet() { + + dispatcherAlarm.afterPropertiesSet(); + verify(workerPool, times(DISPATCH_THREADS)).executeJob(any(Runnable.class)); } @Test - void afterPropertiesSet() { + void testSendNoticeMsg() { + + NoticeReceiver receiver = mock(NoticeReceiver.class); + NoticeTemplate noticeTemplate = mock(NoticeTemplate.class); + Alert alert = mock(Alert.class); + + assertTrue(dispatcherAlarm.sendNoticeMsg(receiver, noticeTemplate, alert)); + verify(alertNotifyHandler).send(receiver, noticeTemplate, alert); } @Test - void sendNoticeMsg() { + void testSendNoticeMsgReceiverNull() { + + Alert alert = mock(Alert.class); + boolean result = dispatcherAlarm.sendNoticeMsg(null, null, alert); + assertFalse(result); } + + @Test + void testSendNoticeMsgReceiverTypeNull() { + + NoticeReceiver receiver = mock(NoticeReceiver.class); + Alert alert = mock(Alert.class); + when(receiver.getType()).thenReturn(null); + + boolean result = dispatcherAlarm.sendNoticeMsg(receiver, null, alert); + assertFalse(result); + } + + @Test + void testSendNoticeMsgNoHandler() { + + NoticeReceiver receiver = mock(NoticeReceiver.class); + Alert alert = mock(Alert.class); + when(receiver.getType()).thenReturn((byte) 2); + + assertFalse(dispatcherAlarm.sendNoticeMsg(receiver, null, alert)); + } + } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java index ae66e568803..cd1954719e7 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/WeComAppAlertNotifyHandlerImplTest.java @@ -17,9 +17,119 @@ package org.apache.hertzbeat.manager.component.alerter.impl; +import java.net.URI; + +import org.apache.hertzbeat.common.entity.alerter.Alert; +import org.apache.hertzbeat.common.entity.manager.NoticeReceiver; +import org.apache.hertzbeat.common.entity.manager.NoticeTemplate; +import org.apache.hertzbeat.manager.support.exception.AlertNoticeException; +import org.junit.jupiter.api.Assertions; +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.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + /** * test case for {@link WeComAppAlertNotifyHandlerImpl} */ +@ExtendWith(MockitoExtension.class) class WeComAppAlertNotifyHandlerImplTest { + + @InjectMocks + private WeComAppAlertNotifyHandlerImpl weComAppAlertNotifyHandler; + + @Mock + private RestTemplate restTemplate; + + private NoticeReceiver receiver; + + private NoticeTemplate noticeTemplate; + + private Alert alert; + + @BeforeEach + void setUp() { + + receiver = new NoticeReceiver(); + receiver.setCorpId("testCorpId"); + receiver.setAgentId(1000001); + receiver.setAppSecret("testAppSecret"); + receiver.setUserId("testUserId"); + receiver.setPartyId("testPartyId"); + receiver.setTagId("testTagId"); + + noticeTemplate = mock(NoticeTemplate.class); + when(noticeTemplate.getContent()).thenReturn("This is a test notice template."); + + alert = new Alert(); + alert.setId(1L); + alert.setLastAlarmTime(System.currentTimeMillis()); + alert.setContent("This is a test alert."); + + weComAppAlertNotifyHandler = new WeComAppAlertNotifyHandlerImpl(restTemplate); + } + + @Test + void testSendSuccess() throws AlertNoticeException { + + WeComAppAlertNotifyHandlerImpl.WeChatAppReq tokenResponse = new WeComAppAlertNotifyHandlerImpl.WeChatAppReq(); + tokenResponse.setAccessToken("testAccessToken"); + when(restTemplate.getForEntity( + anyString(), + eq(WeComAppAlertNotifyHandlerImpl.WeChatAppReq.class) + )).thenReturn(ResponseEntity.ok(tokenResponse)); + + WeComAppAlertNotifyHandlerImpl.WeChatAppReq sendResponse = new WeComAppAlertNotifyHandlerImpl.WeChatAppReq(); + sendResponse.setErrCode(0); + sendResponse.setErrMsg("ok"); + when(restTemplate.postForEntity( + anyString(), + any(HttpEntity.class), + eq(WeComAppAlertNotifyHandlerImpl.WeChatAppReq.class) + )).thenReturn(ResponseEntity.ok(sendResponse)); + + weComAppAlertNotifyHandler.send(receiver, noticeTemplate, alert); + + verify(restTemplate, times(1)).getForEntity(anyString(), eq(WeComAppAlertNotifyHandlerImpl.WeChatAppReq.class)); + verify(restTemplate, times(1)).postForEntity(anyString(), any(HttpEntity.class), eq(WeComAppAlertNotifyHandlerImpl.WeChatAppReq.class)); + } + + @Test + void testSendFail() { + + WeComAppAlertNotifyHandlerImpl.WeChatAppReq tokenResponse = new WeComAppAlertNotifyHandlerImpl.WeChatAppReq(); + tokenResponse.setErrCode(40013); + tokenResponse.setErrMsg("invalid corpid"); + when(restTemplate.getForEntity( + anyString(), + eq(WeComAppAlertNotifyHandlerImpl.WeChatAppReq.class) + )).thenReturn(ResponseEntity.ok(tokenResponse)); + + Assertions.assertThrows( + AlertNoticeException.class, + () -> weComAppAlertNotifyHandler.send(receiver, noticeTemplate, alert) + ); + + verify(restTemplate, never()).postForEntity( + any(URI.class), + any(HttpEntity.class), + eq(WeComAppAlertNotifyHandlerImpl.WeChatAppReq.class) + ); + } + }