Skip to content

Commit 535a213

Browse files
refactoring and null pointer fix
refactoring and fix null pointer exception in history extraction
1 parent aac28a7 commit 535a213

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void setHistoryExtractionHandler(final HistoryExtractionHandler historyEx
5555
* Run history extraction rules on all upstream events.
5656
*
5757
* @param aggregatedObjectId
58-
* the aggregated object id
58+
* the aggregated object id
5959
*/
6060
public void runHistoryExtractionRulesOnAllUpstreamEvents(String aggregatedObjectId) {
6161

@@ -98,26 +98,29 @@ public void runHistoryExtractionRulesOnAllUpstreamEvents(String aggregatedObject
9898
* </pre>
9999
*
100100
* @param jsonArray
101-
* the array to traverse
101+
* the array to traverse
102102
* @param aggregatedObjectId
103-
* the id of the aggregated object
103+
* the id of the aggregated object
104104
* @param pathInAggregatedObject
105-
* the current path in the aggregated object
105+
* the current path in the aggregated object
106106
*/
107107
private void traverseTree(final JsonNode jsonArray, final String aggregatedObjectId,
108108
final String pathInAggregatedObject) {
109109

110-
final JsonNode parent = jsonArray.get(0);
111-
JsonNode parentId = parent.at("/meta/id");
112110
String np = pathInAggregatedObject;
113-
if (!aggregatedObjectId.equals(parentId.textValue())) {
114-
// parent event is not the same as the starting event so we can
115-
// start collecting history
116-
RulesObject rules = rulesHandler.getRulesForEvent(parent.toString());
117-
118-
np = historyExtractionHandler.runHistoryExtraction(aggregatedObjectId, rules, parent.toString(),
119-
pathInAggregatedObject);
111+
final JsonNode parent = jsonArray.get(0);
112+
if (parent != null) {
113+
JsonNode parentId = parent.at("/meta/id");
114+
if (!aggregatedObjectId.equals(parentId.textValue())) {
115+
// parent event is not the same as the starting event so we can
116+
// start collecting history
117+
RulesObject rules = rulesHandler.getRulesForEvent(parent.toString());
118+
119+
np = historyExtractionHandler.runHistoryExtraction(aggregatedObjectId, rules, parent.toString(),
120+
pathInAggregatedObject);
121+
}
120122
}
123+
121124
String prevNp = null;
122125
for (int i = 1; i < jsonArray.size(); i++) {
123126
if (jsonArray.get(i).isObject()) {

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,21 @@ public JmesPathInterface() {
4848
jmespath = new JacksonRuntime(customFunctions);
4949
}
5050

51-
public JsonNode runRuleOnEvent(String rule, String input) {
52-
JsonNode event = null;
53-
if (input == null) {
54-
input = "";
51+
public JsonNode runRuleOnEvent(String rule, String event) {
52+
JsonNode result = JsonNodeFactory.instance.nullNode();
53+
String inputs[] = { rule, event };
54+
for (String input : inputs) {
55+
if (input == null || input == "") {
56+
return result;
57+
}
5558
}
5659

5760
ObjectMapper objectMapper = new ObjectMapper();
58-
JsonNode result = JsonNodeFactory.instance.nullNode();
61+
5962
try {
6063
Expression<JsonNode> expression = jmespath.compile(rule);
61-
event = objectMapper.readValue(input, JsonNode.class);
62-
result = expression.search(event);
64+
JsonNode eventJson = objectMapper.readValue(event, JsonNode.class);
65+
result = expression.search(eventJson);
6366
} catch (Exception e) {
6467
log.info(e.getMessage(), e);
6568
}

src/test/java/com/ericsson/ei/jmespath/test/TestJmesPathInterface.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,13 @@ public void testJsonNodeEmpty() throws Exception {
9898

9999
int wait = 0;
100100
}
101+
102+
@Test
103+
public void testEmptyInputs() throws Exception {
104+
JsonNode result1 = unitUnderTest.runRuleOnEvent("", "");
105+
JsonNode result2 = unitUnderTest.runRuleOnEvent("", "{\"test\" :\"test\"}");
106+
JsonNode result3 = unitUnderTest.runRuleOnEvent("{\"test\" :\"test\"}", "");
107+
108+
boolean wait = true;
109+
}
101110
}

0 commit comments

Comments
 (0)