Skip to content

Commit 239d153

Browse files
authored
Subscription handler function tests (#78)
* ADD: code refactoring, delete unused values, change loggers to debug and ect. * ADD: integration test for subscription * fixes * FIX: mail trigger * CHANGE: LOG to LOGGER * FIX: review comments * FIX: review comments * fixes * fixes * FIX: review comments * fixes
1 parent 51e2198 commit 239d153

File tree

11 files changed

+755
-805
lines changed

11 files changed

+755
-805
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,18 @@
172172
<version>6.1.3</version>
173173
<scope>test</scope>
174174
</dependency>
175-
176175
<dependency>
177176
<groupId>de.flapdoodle.embed</groupId>
178177
<artifactId>de.flapdoodle.embed.mongo</artifactId>
179178
<version>2.0.0</version>
180179
<scope>test</scope>
181180
</dependency>
181+
<dependency>
182+
<groupId>org.powermock</groupId>
183+
<artifactId>powermock-module-junit4</artifactId>
184+
<version>1.7.3</version>
185+
<scope>test</scope>
186+
</dependency>
182187
<dependency>
183188
<groupId>org.projectlombok</groupId>
184189
<artifactId>lombok</artifactId>

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

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,43 @@
1616
*/
1717
package com.ericsson.ei.subscriptionhandler;
1818

19-
import java.text.DateFormat;
20-
import java.text.SimpleDateFormat;
21-
import java.util.Date;
22-
import java.util.List;
23-
24-
import javax.annotation.PostConstruct;
25-
import javax.mail.MessagingException;
26-
27-
import com.ericsson.ei.exception.SubscriptionValidationException;
2819
import com.ericsson.ei.jmespath.JmesPathInterface;
20+
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
21+
import com.fasterxml.jackson.databind.JsonNode;
2922
import com.fasterxml.jackson.databind.node.ArrayNode;
23+
import com.mongodb.BasicDBObject;
24+
import com.mongodb.util.JSON;
3025
import lombok.Getter;
3126
import org.slf4j.Logger;
3227
import org.slf4j.LoggerFactory;
3328
import org.springframework.beans.factory.annotation.Autowired;
3429
import org.springframework.beans.factory.annotation.Value;
3530
import org.springframework.http.HttpStatus;
3631
import org.springframework.stereotype.Component;
37-
38-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
39-
import com.fasterxml.jackson.databind.JsonNode;
40-
import com.mongodb.BasicDBObject;
41-
import com.mongodb.util.JSON;
4232
import org.springframework.util.LinkedMultiValueMap;
4333
import org.springframework.util.MultiValueMap;
4434

35+
import javax.annotation.PostConstruct;
36+
import javax.mail.MessagingException;
37+
import java.text.DateFormat;
38+
import java.text.SimpleDateFormat;
39+
import java.util.Date;
40+
4541
/**
4642
* This class represents the REST POST notification mechanism and the alternate
4743
* way to save the aggregatedObject details in the database when the
4844
* notification fails.
49-
*
45+
*
5046
* @author xjibbal
51-
*
5247
*/
5348

5449
@Component
5550
public class InformSubscription {
5651

52+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(InformSubscription.class);
53+
//Regular expression for replacement unexpected character like \"|
54+
private static final String REGEX = "^\"|\"$";
55+
5756
@Getter
5857
@Value("${notification.failAttempt}")
5958
private int failAttempt;
@@ -74,108 +73,100 @@ public class InformSubscription {
7473
private JmesPathInterface jmespath;
7574

7675
@Autowired
77-
SpringRestTemplate restTemplate;
76+
private SpringRestTemplate restTemplate;
7877

7978
@Autowired
80-
MongoDBHandler mongoDBHandler;
79+
private MongoDBHandler mongoDBHandler;
8180

8281
@Autowired
83-
SendMail sendMail;
84-
85-
static Logger log = (Logger) LoggerFactory.getLogger(InformSubscription.class);
82+
private SendMail sendMail;
8683

8784
/**
8885
* This method extracts the mode of notification through which the subscriber
8986
* should be notified, from the subscription Object. And if the notification
9087
* fails, then it saved in the database.
91-
*
88+
*
9289
* @param aggregatedObject
9390
* @param subscriptionJson
9491
*/
9592
public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson) {
96-
String subscriptionName = subscriptionJson.get("subscriptionName").toString().replaceAll("^\"|\"$", "");
97-
log.info("SubscriptionName : " + subscriptionName);
98-
String notificationType = subscriptionJson.get("notificationType").toString().replaceAll("^\"|\"$", "");
99-
log.info("NotificationType : " + notificationType);
100-
String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll("^\"|\"$", "");
101-
log.info("NotificationMeta : " + notificationMeta);
102-
MultiValueMap<String, String> mapNotificationMessage = new LinkedMultiValueMap<String, String>();
93+
String subscriptionName = subscriptionJson.get("subscriptionName").toString().replaceAll(REGEX, "");
94+
LOGGER.debug("SubscriptionName : " + subscriptionName);
95+
String notificationType = subscriptionJson.get("notificationType").toString().replaceAll(REGEX, "");
96+
LOGGER.debug("NotificationType : " + notificationType);
97+
String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll(REGEX, "");
98+
LOGGER.debug("NotificationMeta : " + notificationMeta);
99+
MultiValueMap<String, String> mapNotificationMessage = new LinkedMultiValueMap<>();
103100
ArrayNode arrNode = (ArrayNode) subscriptionJson.get("notificationMessageKeyValues");
104101
if (arrNode.isArray()) {
105102
for (final JsonNode objNode : arrNode) {
106-
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll("^\"|\"$", ""), jmespath
107-
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll("^\"|\"$", ""), aggregatedObject)
108-
.toString().toString().replaceAll("^\"|\"$", ""));
103+
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll(REGEX, ""), jmespath
104+
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll(REGEX, ""), aggregatedObject)
105+
.toString().replaceAll(REGEX, ""));
109106
}
110107
}
111108
if (notificationType.trim().equals("REST_POST")) {
112-
log.info("Notification through REST_POST");
113-
int result = -1;
109+
LOGGER.debug("Notification through REST_POST");
110+
int result;
114111
String headerContentMediaType = subscriptionJson.get("restPostBodyMediaType").toString()
115-
.replaceAll("^\"|\"$", "");
116-
log.info("headerContentMediaType : " + headerContentMediaType);
112+
.replaceAll(REGEX, "");
113+
LOGGER.debug("headerContentMediaType : " + headerContentMediaType);
117114
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage, headerContentMediaType);
118115
if (result == HttpStatus.OK.value() || result == HttpStatus.CREATED.value()
119116
|| result == HttpStatus.NO_CONTENT.value()) {
120-
log.info("The result is : " + result);
117+
LOGGER.debug("The result is : " + result);
121118
} else {
122119
for (int i = 0; i < failAttempt; i++) {
123120
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage,
124121
headerContentMediaType);
125-
log.info("After trying for " + (i + 1) + " times, the result is : " + result);
122+
LOGGER.debug("After trying for " + (i + 1) + " times, the result is : " + result);
126123
if (result == HttpStatus.OK.value())
127124
break;
128125
}
129126
if (result != HttpStatus.OK.value() && result != HttpStatus.CREATED.value()
130127
&& result != HttpStatus.NO_CONTENT.value()) {
131128
String input = prepareMissedNotification(aggregatedObject, subscriptionName, notificationMeta);
132-
log.info("Input missed Notification document : " + input);
129+
LOGGER.debug("Input missed Notification document : " + input);
133130
mongoDBHandler.createTTLIndex(missedNotificationDataBaseName, missedNotificationCollectionName,
134131
"Time", ttlValue);
135132
boolean output = mongoDBHandler.insertDocument(missedNotificationDataBaseName,
136133
missedNotificationCollectionName, input);
137-
log.info("The output of insertion of missed Notification : " + output);
138-
if (output == false) {
139-
log.info("failed to insert the notification into database");
134+
LOGGER.debug("The output of insertion of missed Notification : " + output);
135+
if (!output) {
136+
LOGGER.debug("failed to insert the notification into database");
140137
} else
141-
log.info("Notification saved in the database");
138+
LOGGER.debug("Notification saved in the database");
142139
}
143140
}
144141
} else if (notificationType.trim().equals("MAIL")) {
145-
log.info("Notification through EMAIL");
142+
LOGGER.debug("Notification through EMAIL");
146143
try {
147144
sendMail.sendMail(notificationMeta,
148-
String.valueOf(((List<String>) mapNotificationMessage.get("")).get(0)));
145+
String.valueOf((mapNotificationMessage.get("")).get(0)));
149146
} catch (MessagingException e) {
150147
e.printStackTrace();
151-
log.error(e.getMessage());
152-
} catch (SubscriptionValidationException e) {
153-
e.printStackTrace();
154-
log.error(e.getMessage());
148+
LOGGER.error(e.getMessage());
155149
}
156150
}
157151
}
158152

159153
/**
160154
* This method saves the missed Notification into a single document along with
161155
* Subscription name, notification meta and time period.
162-
*
156+
*
163157
* @param aggregatedObject
164158
* @param subscriptionName
165159
* @param notificationMeta
166-
*
167160
* @return String
168161
*/
169-
public String prepareMissedNotification(String aggregatedObject, String subscriptionName, String notificationMeta) {
170-
String time = null;
171-
Date date = null;
162+
private String prepareMissedNotification(String aggregatedObject, String subscriptionName, String notificationMeta) {
163+
Date date = new Date();
172164
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
173-
date = new Date();
174-
time = dateFormat.format(date);
165+
String time = dateFormat.format(date);
175166
try {
176167
date = dateFormat.parse(time);
177168
} catch (Exception e) {
178-
log.info(e.getMessage(), e);
169+
LOGGER.error(e.getMessage(), e);
179170
}
180171
BasicDBObject document = new BasicDBObject();
181172
document.put("subscriptionName", subscriptionName);
@@ -191,10 +182,9 @@ public String prepareMissedNotification(String aggregatedObject, String subscrip
191182
*/
192183
@PostConstruct
193184
public void init() {
194-
log.debug("missedNotificationCollectionName : " + missedNotificationCollectionName);
195-
log.debug("missedNotificationDataBaseName : " + missedNotificationDataBaseName);
196-
log.debug("notification.failAttempt : " + failAttempt);
197-
log.debug("Missed Notification TTL value : " + ttlValue);
185+
LOGGER.debug("missedNotificationCollectionName : " + missedNotificationCollectionName);
186+
LOGGER.debug("missedNotificationDataBaseName : " + missedNotificationDataBaseName);
187+
LOGGER.debug("notification.failAttempt : " + failAttempt);
188+
LOGGER.debug("Missed Notification TTL value : " + ttlValue);
198189
}
199-
200-
}
190+
}

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

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,87 +16,72 @@
1616
*/
1717
package com.ericsson.ei.subscriptionhandler;
1818

19-
import java.util.Iterator;
20-
19+
import com.ericsson.ei.jmespath.JmesPathInterface;
20+
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.node.ArrayNode;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324
import org.springframework.beans.factory.annotation.Autowired;
2425
import org.springframework.stereotype.Component;
2526

26-
import com.ericsson.ei.jmespath.JmesPathInterface;
27-
import com.fasterxml.jackson.databind.JsonNode;
28-
import com.fasterxml.jackson.databind.node.ArrayNode;
27+
import java.util.Iterator;
2928

3029

3130
/**
3231
* This class represents the mechanism to fetch the rule conditions from the
3332
* Subscription Object and match it with the aggregatedObject to check if it is
3433
* true.
35-
*
36-
* @author xjibbal
3734
*
35+
* @author xjibbal
3836
*/
3937

4038
@Component
4139
public class RunSubscription {
4240

41+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(RunSubscription.class);
42+
4343
@Autowired
4444
private JmesPathInterface jmespath;
4545

46-
static Logger log = (Logger) LoggerFactory.getLogger(RunSubscription.class);
47-
4846
/**
4947
* This method matches every condition specified in the subscription Object
5048
* and if all conditions are matched then only the aggregatedObject is
5149
* eligible for notification via e-mail or REST POST.
5250
*
5351
* (AND between conditions in requirements, "OR" between requirements with conditions)
54-
*
52+
*
5553
* @param aggregatedObject
56-
* @param requirement
57-
* @param subscriptionJson
54+
* @param requirementIterator
5855
* @return boolean
5956
*/
60-
61-
public boolean runSubscriptionOnObject(String aggregatedObject, Iterator<JsonNode> requirementIterator,
62-
JsonNode subscriptionJson) {
57+
public boolean runSubscriptionOnObject(String aggregatedObject, Iterator<JsonNode> requirementIterator) {
6358
boolean conditionFulfilled = false;
64-
int count_condition_fulfillment = 0;
65-
int count_conditions = 0;
66-
67-
59+
int countConditionFulfillment;
60+
int countConditions;
6861
while (requirementIterator.hasNext()) {
6962
JsonNode requirement = requirementIterator.next();
70-
log.info("The fulfilled requirement which will condition checked is : " + requirement.toString());
63+
LOGGER.debug("The fulfilled requirement which will condition checked is : " + requirement.toString());
7164
ArrayNode conditions = (ArrayNode) requirement.get("conditions");
72-
73-
count_condition_fulfillment = 0;
74-
count_conditions = conditions.size();
75-
76-
log.info("Conditions of the subscription : " + conditions.toString());
65+
countConditionFulfillment = 0;
66+
countConditions = conditions.size();
67+
LOGGER.debug("Conditions of the subscription : " + conditions.toString());
7768
Iterator<JsonNode> conditionIterator = conditions.elements();
7869
while (conditionIterator.hasNext()) {
7970
String rule = conditionIterator.next().get("jmespath").toString().replaceAll("^\"|\"$", "");
80-
String new_Rule = rule.replace("'", "\"");
81-
log.info("Rule : " + rule);
82-
log.info("New Rule after replacing single quote : " + new_Rule);
71+
String newRule = rule.replace("'", "\"");
72+
LOGGER.debug("Rule : " + rule);
73+
LOGGER.debug("New Rule after replacing single quote : " + newRule);
8374
JsonNode result = jmespath.runRuleOnEvent(rule, aggregatedObject);
84-
log.info("Result : " + result.toString());
85-
if (result.toString() != null && result.toString() != "false" && !result.toString().equals("[]")){
86-
count_condition_fulfillment++;
75+
LOGGER.debug("Result : " + result.toString());
76+
if (result.toString() != null && !result.toString().equals("false") && !result.toString().equals("[]")) {
77+
countConditionFulfillment++;
8778
}
8879
}
89-
90-
if(count_conditions != 0 && count_condition_fulfillment == count_conditions){
91-
80+
if (countConditions != 0 && countConditionFulfillment == countConditions) {
9281
conditionFulfilled = true;
9382
}
9483
}
95-
96-
log.info("The final value of conditionFulfilled is : " + conditionFulfilled);
97-
84+
LOGGER.debug("The final value of conditionFulfilled is : " + conditionFulfilled);
9885
return conditionFulfilled;
99-
10086
}
101-
102-
}
87+
}

0 commit comments

Comments
 (0)