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 a427099db [Improve] add GotifyAlertNotifyHandlerImpl unit test (#2380)
a427099db is described below
commit a427099db7ee34881ddb8df4ea93c4ea1f6ca6e8
Author: YuLuo <[email protected]>
AuthorDate: Thu Jul 25 21:17:29 2024 +0800
[Improve] add GotifyAlertNotifyHandlerImpl unit test (#2380)
Signed-off-by: yuluo-yx <[email protected]>
---
.../alerter/impl/GotifyAlertNotifyHandlerImpl.java | 2 +-
.../impl/GotifyAlertNotifyHandlerImplTest.java | 120 ++++++++++++++++++++-
2 files changed, 120 insertions(+), 2 deletions(-)
diff --git
a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImpl.java
b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImpl.java
index 763e8fdd2..aea5dcc40 100644
---
a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImpl.java
+++
b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImpl.java
@@ -87,7 +87,7 @@ public class GotifyAlertNotifyHandlerImpl extends
AbstractAlertNotifyHandlerImpl
}
@Data
- private static class GotifyWebHookDto {
+ protected static class GotifyWebHookDto {
private String title;
private String message;
private Extras extras;
diff --git
a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java
b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java
index 0f48da286..1d5dc380b 100644
---
a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java
+++
b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/GotifyAlertNotifyHandlerImplTest.java
@@ -17,10 +17,128 @@
package org.apache.hertzbeat.manager.component.alerter.impl;
+import java.util.ResourceBundle;
+
+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.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.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+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.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
/**
- * Test case for {@link AliYunAlertNotifyHandlerImpl}
+ * Test case for {@link GotifyAlertNotifyHandlerImpl}
*/
class GotifyAlertNotifyHandlerImplTest {
+
+ @Mock
+ private RestTemplate restTemplate;
+
+ @Mock
+ private ResourceBundle bundle;
+
+ private AlerterProperties alerterProperties;
+
+ private GotifyAlertNotifyHandlerImpl notifyHandler;
+
+ private NoticeTemplate noticeTemplate;
+
+ private NoticeReceiver receiver;
+
+ @BeforeEach
+ public 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.getGotifyWebhookUrl()).thenReturn("http://localhost:8080/gotify/webhook/%s");
+
+ notifyHandler = new GotifyAlertNotifyHandlerImpl();
+ ReflectionTestUtils.setField(notifyHandler,
"alerterProperties", alerterProperties);
+ ReflectionTestUtils.setField(notifyHandler, "restTemplate",
restTemplate);
+ }
+
+ @Test
+ public void testSendSuccess() throws AlertNoticeException {
+
+ Alert alert = Alert.builder()
+ .content("Alert Content")
+ .lastAlarmTime(System.currentTimeMillis())
+ .id(1L)
+ .build();
+
+ when(restTemplate.postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class))
+ ).thenReturn(new ResponseEntity<>(new CommonRobotNotifyResp(),
HttpStatus.OK));
+
+ assertDoesNotThrow(() -> notifyHandler.send(receiver,
noticeTemplate, alert));
+
+ verify(restTemplate).postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class)
+ );
+ verify(restTemplate, times(1)).postForEntity(
+ anyString(),
+ any(HttpEntity.class),
+ eq(CommonRobotNotifyResp.class)
+ );
+ verifyNoMoreInteractions(bundle, restTemplate);
+ }
+
+ @Test
+ public 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))
+ ).thenReturn(new
ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
+
+ AlertNoticeException exception = assertThrows(
+ AlertNoticeException.class,
+ () -> notifyHandler.send(receiver,
noticeTemplate, alert)
+ );
+
+ assertEquals("[Gotify Notify Error] Http StatusCode 500
INTERNAL_SERVER_ERROR", exception.getMessage());
+ verify(restTemplate).postForEntity(anyString(),
any(HttpEntity.class), eq(CommonRobotNotifyResp.class));
+ verifyNoMoreInteractions(bundle, restTemplate);
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]