Skip to content

Commit e25c086

Browse files
Customiseable mail-notification subject (#191)
* customizeable_email_subject * reviewed changes * reviwed changes
1 parent ecb0600 commit e25c086

File tree

5 files changed

+169
-125
lines changed

5 files changed

+169
-125
lines changed

src/functionaltests/java/com/ericsson/ei/notifications/ttl/TestTTLSteps.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package com.ericsson.ei.notifications.ttl;
22

3-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
4-
import com.ericsson.ei.subscriptionhandler.InformSubscription;
5-
import com.ericsson.ei.utils.FunctionalTestBase;
6-
import com.ericsson.ei.utils.HttpRequest;
7-
import com.ericsson.ei.utils.TestContextInitializer;
8-
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
9-
import com.fasterxml.jackson.databind.JsonNode;
10-
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import cucumber.api.java.After;
12-
import cucumber.api.java.Before;
13-
import cucumber.api.java.en.Given;
14-
import cucumber.api.java.en.Then;
15-
import cucumber.api.java.en.When;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.mockserver.model.HttpRequest.request;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
1611
import org.apache.commons.io.FileUtils;
1712
import org.json.JSONArray;
1813
import org.json.JSONException;
@@ -30,12 +25,20 @@
3025
import org.springframework.test.context.TestPropertySource;
3126
import org.springframework.util.SocketUtils;
3227

33-
import java.io.File;
34-
import java.io.IOException;
35-
import java.util.ArrayList;
36-
import java.util.List;
37-
import static org.junit.Assert.assertEquals;
38-
import static org.mockserver.model.HttpRequest.request;
28+
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
29+
import com.ericsson.ei.subscriptionhandler.InformSubscription;
30+
import com.ericsson.ei.utils.FunctionalTestBase;
31+
import com.ericsson.ei.utils.HttpRequest;
32+
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
33+
import com.ericsson.ei.utils.TestContextInitializer;
34+
import com.fasterxml.jackson.databind.JsonNode;
35+
import com.fasterxml.jackson.databind.ObjectMapper;
36+
37+
import cucumber.api.java.After;
38+
import cucumber.api.java.Before;
39+
import cucumber.api.java.en.Given;
40+
import cucumber.api.java.en.Then;
41+
import cucumber.api.java.en.When;
3942

4043
@Ignore
4144
@TestPropertySource(properties = { "notification.ttl.value:1", "aggregated.collection.ttlValue:1",
@@ -46,6 +49,7 @@ public class TestTTLSteps extends FunctionalTestBase {
4649
private static final String BASE_URL = "localhost";
4750
private static final String ENDPOINT = "/missed_notification";
4851
private static final String SUBSCRIPTION_NAME = "Subscription_1";
52+
4953
private static final String SUBSCRIPTION_NAME_3 = "Subscription_Test_3";
5054

5155
@LocalServerPort
@@ -180,6 +184,7 @@ public void i_send_an_Eiffel_event_and_consequently_aggregated_object_and_therea
180184
assertEquals(1, notificationExit.size());
181185
}
182186

187+
183188
@Then("^the Notification document should be deleted from the database according to ttl value$")
184189
public void the_Notification_document_should_be_deleted_from_the_database_according_to_ttl_value()
185190
throws Throwable {

src/functionaltests/resources/features/ttl.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Feature: TestTTL
1616
And I send an Eiffel event and consequently aggregated object and thereafter missed notification is created
1717
Then the Notification document should be deleted from the database according to ttl value
1818
And the Aggregated Object document should be deleted from the database according to ttl value
19+

src/main/java/com/ericsson/ei/handlers/ObjectHandler.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.text.ParseException;
3030
import java.util.ArrayList;
31+
import java.util.Date;
3132
import java.util.List;
3233

3334
import org.bson.Document;
@@ -37,13 +38,24 @@
3738
import org.springframework.beans.factory.annotation.Value;
3839
import org.springframework.stereotype.Component;
3940

41+
import com.ericsson.ei.jmespath.JmesPathInterface;
42+
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
43+
import com.ericsson.ei.rules.RulesObject;
44+
import com.ericsson.ei.subscriptionhandler.SubscriptionHandler;
45+
import com.fasterxml.jackson.databind.JsonNode;
46+
import com.fasterxml.jackson.databind.ObjectMapper;
47+
import com.fasterxml.jackson.databind.node.ObjectNode;
48+
import com.mongodb.BasicDBObject;
49+
import com.mongodb.util.JSON;
50+
4051
import lombok.Getter;
4152
import lombok.Setter;
4253

4354
@Component
4455
public class ObjectHandler {
4556

46-
static Logger log = (Logger) LoggerFactory.getLogger(ObjectHandler.class);
57+
58+
static Logger log = LoggerFactory.getLogger(ObjectHandler.class);
4759

4860
@Getter
4961
@Setter
@@ -101,7 +113,7 @@ public boolean insertObject(JsonNode aggregatedObject, RulesObject rulesObject,
101113
* This method uses previously locked in database aggregatedObject (lock was set
102114
* in lockDocument method) and modifies this document with the new values and
103115
* removes the lock in one query
104-
*
116+
*
105117
* @param aggregatedObject
106118
* String to insert in database
107119
* @param rulesObject
@@ -196,7 +208,7 @@ public String extractObjectId(JsonNode aggregatedDbObject) {
196208
/**
197209
* Locks the document in database to achieve pessimistic locking. Method
198210
* findAndModify is used to optimize the quantity of requests towards database.
199-
*
211+
*
200212
* @param id
201213
* String to search
202214
* @return String aggregated document

src/main/java/com/ericsson/ei/subscriptionhandler/InformSubscription.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
105105
String notificationType = getSubscriptionField("notificationType", subscriptionJson);
106106
String notificationMeta = getSubscriptionField("notificationMeta", subscriptionJson);
107107

108+
String subject = getSubscriptionField("emailSubject", subscriptionJson);
109+
108110
MultiValueMap<String, String> mapNotificationMessage = mapNotificationMessage(aggregatedObject,
109111
subscriptionJson);
110112

@@ -157,7 +159,7 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
157159
} else if (notificationType.trim().equals("MAIL")) {
158160
LOGGER.debug("Notification through EMAIL");
159161
try {
160-
sendMail.sendMail(notificationMeta, String.valueOf((mapNotificationMessage.get("")).get(0)));
162+
sendMail.sendMail(notificationMeta, String.valueOf((mapNotificationMessage.get("")).get(0)), subject);
161163
} catch (MessagingException e) {
162164
e.printStackTrace();
163165
LOGGER.error(e.getMessage());
@@ -234,8 +236,13 @@ private String prepareMissedNotification(String aggregatedObject, String subscri
234236
* @return field value
235237
*/
236238
private String getSubscriptionField(String fieldName, JsonNode subscriptionJson) {
237-
String value = subscriptionJson.get(fieldName).toString().replaceAll(REGEX, "");
238-
LOGGER.debug("Extracted field name and value from subscription json:" + fieldName + " : " + value);
239+
String value;
240+
if (subscriptionJson.get(fieldName) != null) {
241+
value = subscriptionJson.get(fieldName).toString().replaceAll(REGEX, "");
242+
LOGGER.debug("Extracted field name and value from subscription json:" + fieldName + " : " + value);
243+
} else {
244+
value = "";
245+
}
239246
return value;
240247
}
241248

@@ -250,29 +257,29 @@ private MultiValueMap<String, String> mapNotificationMessage(String aggregatedOb
250257
MultiValueMap<String, String> mapNotificationMessage = new LinkedMultiValueMap<>();
251258
ArrayNode arrNode = (ArrayNode) subscriptionJson.get("notificationMessageKeyValues");
252259

253-
if(subscriptionJson.has("authenticationType")) {
254-
String authType = subscriptionJson.get("authenticationType").asText();
255-
256-
if(authType.equals("BASIC_AUTH")) {
257-
String username = subscriptionJson.get("userName").asText();
258-
String password = subscriptionJson.get("password").asText();
259-
String encoding = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
260-
261-
key = "Authorization";
262-
val = "Basic " + encoding;
263-
264-
}
265-
}
266-
267-
if (arrNode.isArray()) {
268-
for (final JsonNode objNode : arrNode) {
269-
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
270-
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
271-
.toString().replaceAll(REGEX, ""));
272-
}
273-
}
274-
return mapNotificationMessage;
275-
}
260+
if (subscriptionJson.has("authenticationType")) {
261+
String authType = subscriptionJson.get("authenticationType").asText();
262+
263+
if (authType.equals("BASIC_AUTH")) {
264+
String username = subscriptionJson.get("userName").asText();
265+
String password = subscriptionJson.get("password").asText();
266+
String encoding = Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
267+
268+
key = "Authorization";
269+
val = "Basic " + encoding;
270+
271+
}
272+
}
273+
274+
if (arrNode.isArray()) {
275+
for (final JsonNode objNode : arrNode) {
276+
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
277+
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
278+
.toString().replaceAll(REGEX, ""));
279+
}
280+
}
281+
return mapNotificationMessage;
282+
}
276283

277284
/**
278285
* This method is responsible to display the configurable application properties

0 commit comments

Comments
 (0)