Skip to content

Commit

Permalink
[Improve] add GotifyAlertNotifyHandlerImpl unit test (#2380)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
  • Loading branch information
yuluo-yx authored Jul 25, 2024
1 parent d1d28e6 commit a427099
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public byte type() {
}

@Data
private static class GotifyWebHookDto {
protected static class GotifyWebHookDto {
private String title;
private String message;
private Extras extras;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit a427099

Please sign in to comment.