Skip to content

Commit

Permalink
Merge branch 'master' into refactor/clean_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 authored Jul 25, 2024
2 parents b2d42e4 + ba70b2f commit ac0b35e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public byte type() {
@Data
@AllArgsConstructor
@NoArgsConstructor
private static class WeChatAppReq {
protected static class WeChatAppReq {

@JsonProperty(value = "errcode")
private Integer errCode;
Expand All @@ -139,7 +139,7 @@ private static class WeChatAppReq {
@Builder
@AllArgsConstructor
@NoArgsConstructor
private static class WeChatAppDTO {
protected static class WeChatAppDTO {

/**
* markdown format
Expand Down Expand Up @@ -172,15 +172,15 @@ private static class WeChatAppDTO {
private MarkdownDTO markdown;

@Data
private static class MarkdownDTO {
protected static class MarkdownDTO {
/**
* message content
*/
private String content;
}

@Data
private static class TextDTO {
protected static class TextDTO {
/**
* message content
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

}

0 comments on commit ac0b35e

Please sign in to comment.