This is an automated email from the ASF dual-hosted git repository.

zhengqiwei 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 7488dfadc [Improve] add SmsAlertNotifyHandlerImpl unit test  (#2384)
7488dfadc is described below

commit 7488dfadc99eb34634f8e07fc56eea11b4f7384e
Author: YuLuo <[email protected]>
AuthorDate: Fri Jul 26 22:03:06 2024 +0800

    [Improve] add SmsAlertNotifyHandlerImpl unit test  (#2384)
    
    Signed-off-by: yuluo-yx <[email protected]>
---
 .../impl/SmsAlertNotifyHandlerImplTest.java        | 95 ++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git 
a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java
 
b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java
index ae908b8db..ce6e38dd4 100644
--- 
a/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java
+++ 
b/manager/src/test/java/org/apache/hertzbeat/manager/component/alerter/impl/SmsAlertNotifyHandlerImplTest.java
@@ -17,9 +17,104 @@
 
 package org.apache.hertzbeat.manager.component.alerter.impl;
 
+import java.util.Map;
+import java.util.ResourceBundle;
+
+import org.apache.hertzbeat.common.constants.CommonConstants;
+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.service.TencentSmsClient;
+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.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 /**
  * test case for {@link SmsAlertNotifyHandlerImpl}
  */
 
 class SmsAlertNotifyHandlerImplTest {
+
+
+       @Mock
+       private TencentSmsClient tencentSmsClient;
+
+       private SmsAlertNotifyHandlerImpl notifyHandler;
+
+       private NoticeTemplate noticeTemplate;
+
+       private NoticeReceiver receiver;
+
+       private ResourceBundle bundle;
+
+       @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.getPhone()).thenReturn("1234567890");
+
+               bundle = mock(ResourceBundle.class);
+               when(bundle.getString(anyString())).thenReturn("High");
+
+               notifyHandler = new SmsAlertNotifyHandlerImpl(tencentSmsClient);
+       }
+
+       @Test
+       public void testSendSuccess() throws AlertNoticeException {
+
+               Alert alert = Alert.builder()
+                               .content("Alert Content")
+                               .priority((byte) 1)
+                               .target("TestTarget")
+                               .tags(Map.of(CommonConstants.TAG_MONITOR_NAME, 
"MonitorName"))
+                               .lastAlarmTime(System.currentTimeMillis())
+                               .id(1L)
+                               .build();
+               when(bundle.getString("alerter.priority.1")).thenReturn("High");
+
+               notifyHandler.send(receiver, noticeTemplate, alert);
+
+               String[] expectedParams = {"MonitorName", "Critical Alert", 
"Alert Content"};
+               verify(tencentSmsClient).sendMessage(expectedParams, new 
String[]{"1234567890"});
+       }
+
+       @Test
+       public void testSendFailed() {
+
+               Alert alert = Alert.builder()
+                               .content("Alert Content")
+                               .priority((byte) 1)
+                               .target("TestTarget")
+                               .tags(Map.of(CommonConstants.TAG_MONITOR_NAME, 
"MonitorName"))
+                               .lastAlarmTime(System.currentTimeMillis())
+                               .id(1L)
+                               .build();
+               
Mockito.when(bundle.getString("alerter.priority.1")).thenReturn("High");
+
+               doThrow(new RuntimeException("[Sms Notify 
Error]")).when(tencentSmsClient).sendMessage(any(), any());
+
+               Exception exception = Assertions.assertThrows(
+                               AlertNoticeException.class,
+                               () -> notifyHandler.send(receiver, 
noticeTemplate, alert)
+               );
+               assertEquals("[Sms Notify Error] [Sms Notify Error]", 
exception.getMessage());
+       }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to