Skip to content

Commit caa4285

Browse files
authored
subscription bugfix, and 'AND' between conditions 'OR' between requirements added (#33)
1 parent 13e39ef commit caa4285

File tree

5 files changed

+36
-65
lines changed

5 files changed

+36
-65
lines changed

src/main/java/com/ericsson/ei/jmespath/JmesPathInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public JsonNode runRuleOnEvent(String rule, String input) {
5454
log.info(e.getMessage(), e);
5555
}
5656
JsonNode result = expression.search(event);
57+
5758
return result;
5859
}
5960
}

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

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.fasterxml.jackson.databind.ObjectMapper;
2929
import com.fasterxml.jackson.databind.node.ArrayNode;
3030

31+
3132
/**
3233
* This class represents the mechanism to fetch the rule conditions from the
3334
* Subscription Object and match it with the aggregatedObject to check if it is
@@ -49,67 +50,55 @@ public class RunSubscription {
4950
* This method matches every condition specified in the subscription Object
5051
* and if all conditions are matched then only the aggregatedObject is
5152
* eligible for notification via e-mail or REST POST.
53+
*
54+
* (AND between conditions in requirements, "OR" between requirements with conditions)
5255
*
5356
* @param aggregatedObject
5457
* @param requirement
5558
* @param subscriptionJson
5659
* @return boolean
5760
*/
58-
public boolean runSubscriptionOnObject(String aggregatedObject, ArrayNode fulfilledRequirements,
59-
JsonNode subscriptionJson) {
60-
Iterator<JsonNode> requirementIterator = fulfilledRequirements.elements();
61+
62+
public boolean runSubscriptionOnObject(String aggregatedObject, Iterator<JsonNode> requirementIterator,
63+
JsonNode subscriptionJson) {
6164
boolean conditionFulfilled = false;
65+
int count_condition_fulfillment = 0;
66+
int count_conditions = 0;
67+
68+
6269
while (requirementIterator.hasNext()) {
6370
JsonNode requirement = requirementIterator.next();
6471
log.info("The fulfilled requirement which will condition checked is : " + requirement.toString());
6572
ArrayNode conditions = (ArrayNode) requirement.get("conditions");
73+
74+
count_condition_fulfillment = 0;
75+
count_conditions = conditions.size();
76+
6677
log.info("Conditions of the subscription : " + conditions.toString());
6778
Iterator<JsonNode> conditionIterator = conditions.elements();
68-
// boolean conditionFulfilled = false;
6979
while (conditionIterator.hasNext()) {
7080
String rule = conditionIterator.next().get("jmespath").toString().replaceAll("^\"|\"$", "");
7181
String new_Rule = rule.replace("'", "\"");
7282
log.info("Rule : " + rule);
7383
log.info("New Rule after replacing single quote : " + new_Rule);
7484
JsonNode result = jmespath.runRuleOnEvent(rule, aggregatedObject);
7585
log.info("Result : " + result.toString());
76-
if (result.toString() != null) {
77-
conditionFulfilled = true;
86+
int test = result.toString().length();
87+
if (result.toString() != null && result.toString() != "false" && !result.toString().equals("[]")){
88+
count_condition_fulfillment++;
7889
}
7990
}
80-
}
81-
log.info("The final value of conditionFulfilled is : " + conditionFulfilled);
82-
return conditionFulfilled;
8391

84-
}
92+
if(count_conditions != 0 && count_condition_fulfillment == count_conditions){
8593

86-
/**
87-
* This method check if the subscription requirement type and the
88-
* aggregatedObject TemplateName are same.
89-
*
90-
* @param requirementIterator
91-
* @param aggregatedObject
92-
* @return JsonNode
93-
*/
94-
public ArrayNode checkRequirementType(Iterator<JsonNode> requirementIterator, String aggregatedObject) {
95-
ArrayNode fulfilledRequirements = new ObjectMapper().createArrayNode();
96-
;
97-
JsonNode requirement = null;
98-
JsonNode aggregatedJson = null;
99-
boolean condition = false;
100-
try {
101-
aggregatedJson = new ObjectMapper().readTree(aggregatedObject);
102-
log.info("AggregatedJson : " + aggregatedJson.toString());
103-
} catch (Exception e) {
104-
log.error(e.getMessage(), e);
105-
}
106-
while (requirementIterator.hasNext()) {
107-
requirement = requirementIterator.next();
108-
log.info("Requirements : " + requirement.toString());
109-
fulfilledRequirements.add(requirement);
94+
conditionFulfilled = true;
95+
}
11096
}
11197

112-
return fulfilledRequirements;
98+
log.info("The final value of conditionFulfilled is : " + conditionFulfilled);
99+
100+
return conditionFulfilled;
101+
113102
}
114103

115104
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434
/**
3535
* This class is responsible to take a aggregatedObject and match it with all
36-
* the Subscription Object, to check if any of them is eligible for
37-
* notification.
36+
* the Subscription Object, to check ALL Conditions/requirement for
37+
* notification. (AND between conditions in requirements, "OR" between requirements with conditions)
3838
*
3939
* @author xjibbal
4040
*
@@ -109,15 +109,12 @@ public void extractConditions(String aggregatedObject, String subscriptionData)
109109
ArrayNode requirementNode = (ArrayNode) subscriptionJson.get("requirements");
110110
log.info("RequirementNode : " + requirementNode.toString());
111111
Iterator<JsonNode> requirementIterator = requirementNode.elements();
112-
ArrayNode fulfilledRequirements = runSubscription.checkRequirementType(requirementIterator, aggregatedObject);
113-
if (fulfilledRequirements != null) {
114-
log.info("The fulfilled requirements are : " + fulfilledRequirements.toString());
115-
if (runSubscription.runSubscriptionOnObject(aggregatedObject, fulfilledRequirements, subscriptionJson)) {
112+
113+
if (runSubscription.runSubscriptionOnObject(aggregatedObject, requirementIterator, subscriptionJson)) {
116114
log.info("The subscription conditions match for the aggregatedObject");
117115
informSubscription.informSubscriber(aggregatedObject, subscriptionJson);
118116
}
119-
} else
120-
log.info("The subscription conditions did not match for the aggregatedObject");
117+
121118
}
122119

123120
/**

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ logging.level.root: INFO
99
logging.level.org.springframework.web: DEBUG
1010
logging.level.com.ericsson.ei: DEBUG
1111

12-
rabbitmq.host: 127.0.0.1
12+
rabbitmq.host: localhost
1313
rabbitmq.port: 5672
1414
rabbitmq.user:
1515
rabbitmq.password:

src/test/java/com/ericsson/ei/subscriptionhandler/test/SubscriptionHandlerTest.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,21 @@ public void initMocks() {
9797
System.out.println("Database connected");
9898
}
9999

100-
@Test
101-
public void checkRequirementTypeTest() {
102-
ObjectMapper mapper = new ObjectMapper();
103-
JsonNode subscriptionJson = null;
104-
JsonNode aggregatedJson = null;
105-
try {
106-
subscriptionJson = mapper.readTree(subscriptionData);
107-
aggregatedJson = mapper.readTree(aggregatedObject);
108-
} catch (Exception e) {
109-
log.error(e.getMessage(), e);
110-
}
111-
ArrayNode requirementNode = (ArrayNode) subscriptionJson.get("requirements");
112-
JsonNode expectedOutput = (JsonNode) requirementNode;
113-
log.info("RequirementNode : " + requirementNode.toString());
114-
Iterator<JsonNode> requirementIterator = requirementNode.elements();
115-
JsonNode output = runSubscription.checkRequirementType(requirementIterator, aggregatedObject);
116-
assertEquals(output.toString(), expectedOutput.toString());
117-
}
118100

119101
@Test
120102
public void runSubscriptionOnObjectTest() {
121103
ObjectMapper mapper = new ObjectMapper();
122104
JsonNode subscriptionJson = null;
123-
ArrayNode fulfilledRequirements = null;
105+
ArrayNode requirementNode = null;
106+
Iterator<JsonNode> requirementIterator = null;
124107
try {
125108
subscriptionJson = mapper.readTree(subscriptionData);
126-
fulfilledRequirements = (ArrayNode) subscriptionJson.get("requirements");
109+
requirementNode = (ArrayNode) subscriptionJson.get("requirements");
110+
requirementIterator = requirementNode.elements();
127111
} catch (Exception e) {
128112
log.error(e.getMessage(), e);
129113
}
130-
boolean output = runSubscription.runSubscriptionOnObject(aggregatedObject, fulfilledRequirements,
114+
boolean output = runSubscription.runSubscriptionOnObject(aggregatedObject, requirementIterator,
131115
subscriptionJson);
132116
assertEquals(output, true);
133117
}

0 commit comments

Comments
 (0)