forked from apache/hertzbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve] add EmailAlertNotifyHandlerImpl unit test (apache#2370)
Signed-off-by: yuluo-yx <[email protected]> Co-authored-by: tomsun28 <[email protected]>
- Loading branch information
Showing
1 changed file
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
} |