Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/refactor/clean_controller' into …
Browse files Browse the repository at this point in the history
…refactor/clean_controller
  • Loading branch information
Calvin979 committed Jul 25, 2024
2 parents 0edf710 + ac0b35e commit a12f0dd
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 45 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,117 @@

package org.apache.hertzbeat.manager.component.alerter;

import java.util.List;

import org.apache.hertzbeat.alert.AlerterWorkerPool;
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.common.queue.CommonDataQueue;
import org.apache.hertzbeat.manager.service.NoticeConfigService;
import org.apache.hertzbeat.manager.service.PluginService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
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 DispatcherAlarm}
*/

@ExtendWith(MockitoExtension.class)
class DispatcherAlarmTest {

@Mock
private AlerterWorkerPool workerPool;

@Mock
private CommonDataQueue dataQueue;

@Mock
private NoticeConfigService noticeConfigService;

@Mock
private AlertStoreHandler alertStoreHandler;

@Mock
private PluginService pluginService;

@Mock
private AlertNotifyHandler alertNotifyHandler;

private DispatcherAlarm dispatcherAlarm;

private static final int DISPATCH_THREADS = 3;

@BeforeEach
void setUp() {

List<AlertNotifyHandler> alertNotifyHandlerList = List.of(alertNotifyHandler);
dispatcherAlarm = new DispatcherAlarm(
workerPool,
dataQueue,
noticeConfigService,
alertStoreHandler,
alertNotifyHandlerList,
pluginService
);
}

@Test
void testAfterPropertiesSet() {

dispatcherAlarm.afterPropertiesSet();
verify(workerPool, times(DISPATCH_THREADS)).executeJob(any(Runnable.class));
}

@Test
void afterPropertiesSet() {
void testSendNoticeMsg() {

NoticeReceiver receiver = mock(NoticeReceiver.class);
NoticeTemplate noticeTemplate = mock(NoticeTemplate.class);
Alert alert = mock(Alert.class);

assertTrue(dispatcherAlarm.sendNoticeMsg(receiver, noticeTemplate, alert));
verify(alertNotifyHandler).send(receiver, noticeTemplate, alert);
}

@Test
void sendNoticeMsg() {
void testSendNoticeMsgReceiverNull() {

Alert alert = mock(Alert.class);
boolean result = dispatcherAlarm.sendNoticeMsg(null, null, alert);
assertFalse(result);
}

@Test
void testSendNoticeMsgReceiverTypeNull() {

NoticeReceiver receiver = mock(NoticeReceiver.class);
Alert alert = mock(Alert.class);
when(receiver.getType()).thenReturn(null);

boolean result = dispatcherAlarm.sendNoticeMsg(receiver, null, alert);
assertFalse(result);
}

@Test
void testSendNoticeMsgNoHandler() {

NoticeReceiver receiver = mock(NoticeReceiver.class);
Alert alert = mock(Alert.class);
when(receiver.getType()).thenReturn((byte) 2);

assertFalse(dispatcherAlarm.sendNoticeMsg(receiver, null, alert));
}

}
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 a12f0dd

Please sign in to comment.