Skip to content

Commit 111a6fe

Browse files
authored
Added default sender and subject values for email notification. (#429)
* Added default sender and subject values for email notification.
1 parent 6a3a9b9 commit 111a6fe

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/functionaltests/java/com/ericsson/ei/notifications/trigger/SubscriptionNotificationSteps.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.json.JSONArray;
1818
import org.json.JSONException;
1919
import org.junit.Ignore;
20+
import org.mockito.InjectMocks;
2021
import org.mockserver.client.MockServerClient;
2122
import org.mockserver.integration.ClientAndServer;
2223
import org.mockserver.model.Format;
@@ -34,6 +35,7 @@
3435
import com.dumbster.smtp.SmtpMessage;
3536
import com.ericsson.ei.mongo.MongoCondition;
3637
import com.ericsson.ei.mongo.MongoDBHandler;
38+
import com.ericsson.ei.notifications.EmailSender;
3739
import com.ericsson.ei.utils.FunctionalTestBase;
3840
import com.ericsson.ei.utils.HttpRequest;
3941
import com.fasterxml.jackson.databind.JsonNode;
@@ -70,6 +72,9 @@ public class SubscriptionNotificationSteps extends FunctionalTestBase {
7072
@Value("${email.sender}")
7173
private String sender;
7274

75+
@Value("${email.subject}")
76+
private String subject;
77+
7378
@Value("${aggregations.collection.name}")
7479
private String aggregatedCollectionName;
7580

@@ -88,6 +93,9 @@ public class SubscriptionNotificationSteps extends FunctionalTestBase {
8893
@Autowired
8994
private MongoDBHandler mongoDBHandler;
9095

96+
@InjectMocks
97+
private EmailSender emailSender;
98+
9199
private SimpleSmtpServer smtpServer;
92100
private ClientAndServer restServer;
93101
private MockServerClient mockClient;
@@ -187,6 +195,12 @@ public void send_one_previous_event() throws Throwable {
187195
eventManager.sendEiffelEvents(EIFFEL_EVENTS_JSON_PATH, eventNamesToSend);
188196
}
189197

198+
@Then("^Then Notification email contains ('(.*?)') and ('(.*?)') values")
199+
public void notification_email_contains_expected_values(String sender, String subject) {
200+
assertEquals(sender, emailSender.getSender());
201+
assertEquals(subject, emailSender.getSubject());
202+
}
203+
190204
@Then("^Mail subscriptions were triggered$")
191205
public void mail_subscriptions_were_triggered() {
192206
LOGGER.debug("Verifying received emails.");
@@ -196,6 +210,8 @@ public void mail_subscriptions_were_triggered() {
196210
for (SmtpMessage email : emails) {
197211
// assert correct sender.
198212
assertEquals("Assert correct email sender: ", email.getHeaderValue("From"), sender);
213+
// assert correct subject.
214+
assertEquals("Assert correct email subject: ", email.getHeaderValue("Subject"), subject);
199215
// assert given test case exist in body.
200216
assert (email.getBody().contains("TC5"));
201217
}

src/functionaltests/resources/features/subscriptionNotification.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Feature: Test Subscription Trigger
88
And Subscriptions are created
99
When I send Eiffel events
1010
And Wait for EI to aggregate objects
11+
Then Notification email contains <our expected> values
1112
Then Mail subscriptions were triggered
1213
And Rest subscriptions were triggered
1314
When I send one previous event again
@@ -22,3 +23,14 @@ Feature: Test Subscription Trigger
2223
When I send one previous event again
2324
And Wait for EI to aggregate objects
2425
And Failed notification db should contain 2 objects
26+
27+
@AddedDefaultValuesSenderAndSubject
28+
Scenario: Test default values mail notification
29+
Given The REST API is up and running
30+
And Mail server is up
31+
And Subscriptions are created
32+
When I send Eiffel events
33+
And Wait for EI to aggregate objects
34+
Then Notification email contains 'noreply@domain.com' and 'Email Subscription Notification' values
35+
Then Mail subscriptions were triggered
36+
And Rest subscriptions were triggered

src/main/java/com/ericsson/ei/notifications/EmailSender.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.mail.MessagingException;
2323
import javax.mail.internet.MimeMessage;
2424

25+
import org.apache.commons.lang3.StringUtils;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728
import org.springframework.beans.factory.annotation.Autowired;
@@ -48,17 +49,17 @@ public class EmailSender {
4849
private static final Logger LOGGER = LoggerFactory.getLogger(EmailSender.class);
4950

5051
@Getter
51-
@Value("${email.sender}")
52+
@Value("${email.sender:noreply@domain.com}")
5253
private String sender;
5354

5455
@Getter
55-
@Value("${email.subject}")
56+
@Value("${email.subject:Email Subscription Notification}")
5657
private String subject;
5758

5859
@Autowired
5960
private JavaMailSender emailSender;
6061

61-
/**
62+
/**
6263
* This method sends an email.
6364
*
6465
* @param message The email message to send
@@ -90,6 +91,19 @@ public MimeMessage prepareEmailMessage(String recipients, String mapNotification
9091
return message;
9192
}
9293

94+
/*
95+
* This method assigns the default values for sender and subject of email notification if values
96+
* at application.proporties are empty.
97+
* */
98+
private void setDefaultValuesEmailSenderSubject() {
99+
if (StringUtils.isEmpty(subject)) {
100+
subject = "Email Subscription Notification";
101+
}
102+
if (StringUtils.isEmpty(sender)) {
103+
sender = "noreply@domain.com";
104+
}
105+
}
106+
93107
/**
94108
* This method creates a MimeMessageHelper and prepares the email to send
95109
*
@@ -99,6 +113,7 @@ public MimeMessage prepareEmailMessage(String recipients, String mapNotification
99113
*/
100114
private MimeMessage prepareEmail(String mapNotificationMessage, String emailSubject,
101115
String[] recipients) {
116+
setDefaultValuesEmailSenderSubject();
102117
MimeMessage message = emailSender.createMimeMessage();
103118
try {
104119
MimeMessageHelper helper = new MimeMessageHelper(message, true);

src/test/java/com/ericsson/ei/notifications/EmailSenderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ public void sendEmailThrowsException() throws Exception {
5353
* prepareEmail is not possible to test due too much hidden Spring mumbo jumbo doing things in
5454
* background.
5555
*/
56+
5657
}

0 commit comments

Comments
 (0)