Skip to content

Commit

Permalink
[Improve] add EmailAlertNotifyHandlerImpl unit test (apache#2370)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
yuluo-yx and tomsun28 authored Jul 25, 2024
1 parent ba70b2f commit d1d28e6
Showing 1 changed file with 76 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,95 @@

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

import java.util.Date;
import java.util.Properties;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.mail.internet.MimeMessage;
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.dao.GeneralConfigDao;
import org.apache.hertzbeat.manager.service.MailService;
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.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.util.ReflectionTestUtils;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Test case for {@link EmailAlertNotifyHandlerImpl}
*/

@ExtendWith(MockitoExtension.class)
class EmailAlertNotifyHandlerImplTest {

@Mock
private JavaMailSenderImpl javaMailSender;

@Mock
private MailService mailService;

@Mock
private GeneralConfigDao generalConfigDao;

@Mock
private ObjectMapper objectMapper;

private JavaMailSenderImpl sender;

@InjectMocks
private EmailAlertNotifyHandlerImpl emailAlertNotifyHandler;

@BeforeEach
void setUp() {
}
public void setUp() {

@Test
void send() {
ReflectionTestUtils.setField(emailAlertNotifyHandler, "host", "smtp.demo.com");
ReflectionTestUtils.setField(emailAlertNotifyHandler, "username", "demo");
ReflectionTestUtils.setField(emailAlertNotifyHandler, "password", "demo");
ReflectionTestUtils.setField(emailAlertNotifyHandler, "port", 465);
ReflectionTestUtils.setField(emailAlertNotifyHandler, "sslEnable", true);
}

@Test
void type() {
void testSend() throws Exception {

NoticeReceiver receiver = new NoticeReceiver();
receiver.setEmail("[email protected]");

NoticeTemplate noticeTemplate = new NoticeTemplate();
Alert alert = Alert.builder().build();

MimeMessage mimeMessage = mock(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);

Properties properties = new Properties();
when(javaMailSender.getJavaMailProperties()).thenReturn(properties);

when(mailService.buildAlertHtmlTemplate(
any(Alert.class),
any(NoticeTemplate.class))
).thenReturn("HTML Content");

emailAlertNotifyHandler.send(receiver, noticeTemplate, alert);

verify(javaMailSender).send(mimeMessage);

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom("demo");
helper.setTo("[email protected]");
helper.setSubject("Email Alert Notification");
helper.setSentDate(new Date());
helper.setText("HTML Content", true);
}

}

0 comments on commit d1d28e6

Please sign in to comment.