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/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) + ); + } + }