This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 3d9be220e [Improve] add ServerChanAlertNotifyHandlerImpl unit test
(#2381)
3d9be220e is described below
commit 3d9be220e68654177edec76872f1adc991f3c2cb
Author: YuLuo <[email protected]>
AuthorDate: Thu Jul 25 22:29:37 2024 +0800
[Improve] add ServerChanAlertNotifyHandlerImpl unit test (#2381)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../impl/ServerChanAlertNotifyHandlerImpl.java | 2 +-
.../impl/ServerChanAlertNotifyHandlerImplTest.java | 119 +++++++++++++++++++++
2 files changed, 120 insertions(+), 1 deletion(-)
diff --git
a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java
b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java
index 87056875b..51935b19d 100644
---
a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java
+++
b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java
@@ -79,7 +79,7 @@ public class ServerChanAlertNotifyHandlerImpl extends
AbstractAlertNotifyHandler
}
@Data
- private static class ServerChanWebHookDto {
+ protected static class ServerChanWebHookDto {
private static final String MARKDOWN = "markdown";
/**
diff --git
a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java
b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java
index 1998eb748..5ec08b4c8 100644
---
a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java
+++
b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImplTest.java
@@ -17,9 +17,128 @@
package org.apache.hertzbeat.manager.component.alerter.impl;
+import org.apache.hertzbeat.alert.AlerterProperties;
+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.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.util.ReflectionTestUtils;
+import org.springframework.web.client.RestTemplate;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.anyString;
+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 ServerChanAlertNotifyHandlerImpl}
*/
class ServerChanAlertNotifyHandlerImplTest {
+
+ @Mock
+ private RestTemplate restTemplate;
+
+ @Mock
+ private HttpHeaders headers;
+
+ private ServerChanAlertNotifyHandlerImpl notifyHandler;
+
+ private NoticeTemplate noticeTemplate;
+
+ private NoticeReceiver receiver;
+
+ private AlerterProperties alerterProperties;
+
+ @BeforeEach
+ void setUp() {
+
+ MockitoAnnotations.openMocks(this);
+
+ noticeTemplate = mock(NoticeTemplate.class);
+ when(noticeTemplate.getContent()).thenReturn("This is a test
notice template.");
+
+ receiver = mock(NoticeReceiver.class);
+ when(receiver.getAccessToken()).thenReturn("dummyToken");
+
+ alerterProperties = mock(AlerterProperties.class);
+
when(alerterProperties.getServerChanWebhookUrl()).thenReturn("http://localhost:8080/webhook/%s");
+
+ notifyHandler = new ServerChanAlertNotifyHandlerImpl();
+ ReflectionTestUtils.setField(notifyHandler, "restTemplate",
restTemplate);
+ ReflectionTestUtils.setField(notifyHandler,
"alerterProperties", alerterProperties);
+ }
+
+ @Test
+ void testSendSuccess() {
+
+ Alert alert = Alert.builder()
+ .content("Alert Content")
+ .lastAlarmTime(System.currentTimeMillis())
+ .id(1L)
+ .build();
+
+ ServerChanAlertNotifyHandlerImpl.ServerChanWebHookDto
serverChanWebHookDto =
+ new
ServerChanAlertNotifyHandlerImpl.ServerChanWebHookDto();
+ serverChanWebHookDto.setTitle("Test Title");
+ serverChanWebHookDto.setDesp("Test Message");
+
+ ResponseEntity<CommonRobotNotifyResp> mockResponseEntity =
+ new ResponseEntity<>(new
CommonRobotNotifyResp(), HttpStatus.OK);
+ when(restTemplate.postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class))
+ ).thenReturn(mockResponseEntity);
+
+ notifyHandler.send(receiver, noticeTemplate, alert);
+
+ verify(restTemplate, times(1)).postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class)
+ );
+ }
+
+ @Test
+ void testSendFailed() {
+
+ Alert alert = Alert.builder()
+ .content("Alert Content")
+ .lastAlarmTime(System.currentTimeMillis())
+ .id(1L)
+ .build();
+
+ when(restTemplate.postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class))
+ ).thenThrow(new RuntimeException("Simulated failure"));
+
+ AlertNoticeException exception = assertThrows(
+ AlertNoticeException.class,
+ () -> notifyHandler.send(receiver,
noticeTemplate, alert)
+ );
+ assertTrue(exception.getMessage().contains("[ServerChan Notify
Error]"));
+
+ verify(restTemplate, times(1)).postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class)
+ );
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]