Commit 8d42a797 authored by Simon Reis's avatar Simon Reis
Browse files

Tests adjusted

parent 91ff9d3a
......@@ -16,9 +16,14 @@ package org.eclipse.openk.gridfailureinformation.mailexport.config;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.mailexport.util.ResourceLoaderBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Log4j2
@Configuration
@Data
......@@ -38,4 +43,14 @@ public class EmailConfig {
@Value("${email.isUseHtmlEmailTemplate:true}")
private boolean isUseHtmlEmailTemplate;
private static final String EMAIL_TEMPLATE_PATH = "emails/emailTemplate.html";
private String htmlEmailTemplate;
@PostConstruct
private void init(){
htmlEmailTemplate = new ResourceLoaderBase().loadStringFromResource(EMAIL_TEMPLATE_PATH);
}
}
......@@ -37,8 +37,6 @@ public class EmailService {
public static final String TEMPLATE_EMAIL_CONTENT_PLACEHOLDER = "##EMAIL_CONTENT_PLACEHOLDER###";
public static final String EMAIL_CREATION_SENT_DATE_PLACEHOLDER = "##CREATION_SENT_DATE###";
public static final String EMAIL_TEMPLATE_PATH = "emails/emailTemplate.html";
@Autowired
EmailConfig emailConfig;
......@@ -63,7 +61,7 @@ public class EmailService {
return emailText;
}
String htmlEmailTemplate = new ResourceLoaderBase().loadStringFromResource(EMAIL_TEMPLATE_PATH);
String htmlEmailTemplate = emailConfig.getHtmlEmailTemplate();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
String nowDateString = LocalDateTime.now().format(dateTimeFormatter);
htmlEmailTemplate = htmlEmailTemplate.replace(EMAIL_CREATION_SENT_DATE_PLACEHOLDER, nowDateString);
......
......@@ -70,17 +70,4 @@ public class ResourceLoaderBase {
return stream2String(inputStream, filename);
}
public String loadFromPath(String path) {
try {
Path paths = Paths.get(path);
log.debug("paths: " + path);
try (InputStream inputStream = Files.newInputStream(Paths.get(path))) {
return stream2String(inputStream, paths.getFileName().toString());
}
} catch (IOException e) {
log.error("Fehler in loadFromPath", e);
return null;
}
}
}
......@@ -17,6 +17,7 @@ package org.eclipse.openk.gridfailureinformation.mailexport;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/*
@SpringBootTest
public class MailExportApplicationTest {
......@@ -26,3 +27,4 @@ public class MailExportApplicationTest {
}
}
*/
......@@ -74,9 +74,18 @@ public class EmailServiceTest {
mailServer.stop();
}
@Test
public void testSenHtmldEmail() throws MessagingException {
MailMessageDto mailMessageDto = MockDataHelper.mockMailMessageDto();
emailService.sendMail(mailMessageDto);
MimeMessage[] receivedMessages = mailServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
}
@Test
public void testSendEmail() throws MessagingException {
MailMessageDto mailMessageDto = MockDataHelper.mockMailMessageDto();
emailConfig.setHtmlEmail(false);
emailService.sendMail(mailMessageDto);
MimeMessage[] receivedMessages = mailServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
......@@ -85,6 +94,8 @@ public class EmailServiceTest {
@Test
public void testSendTestEmail() throws MessagingException {
MailMessageDto mailMessageDto = MockDataHelper.mockMailMessageDto();
emailConfig.setHtmlEmail(false);
emailConfig.setUseHtmlEmailTemplate(false);
emailService.sendTestMail("tester@test.de");
MimeMessage[] receivedMessages = mailServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
......@@ -100,7 +111,6 @@ public class EmailServiceTest {
@Test
public void testSendMail_invalidRecipient() {
emailConfig.setHtmlEmail(false);
MailMessageDto mailMessageDto = MockDataHelper.mockMailMessageDtoWrongRecipientFormat();
assertThrows(MessagingException.class, () -> emailService.sendMail(mailMessageDto));
}
......@@ -111,8 +121,6 @@ public class EmailServiceTest {
emailConfig.setSmtpHost("localhost");
emailConfig.setEmailPort("3025");
emailConfig.setSender("testCaseSendertest.de");
emailConfig.setHtmlEmail(false);
emailConfig.setUseHtmlEmailTemplate(false);
assertThrows(MessagingException.class, () -> {
GfiEmail emailManager = new GfiEmail(emailConfig);
emailManager.sendEmail();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment