Skip to content

Commit e845839

Browse files
author
Vali (Vasile Baluta)
committed
integrate process rules handler
Change-Id: I2072ea5a9b3773a7bbd9e381b85499a431d5586b
1 parent 2592443 commit e845839

File tree

10 files changed

+191
-12
lines changed

10 files changed

+191
-12
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ExtractionHandler {
2525
@Autowired private JmesPathInterface jmesPathInterface;
2626
@Autowired private MergeHandler mergeHandler;
2727
@Autowired private ObjectHandler objectHandler;
28-
//TODO:@Autowired private ProcessRulesHandler processRulesHandler;
28+
@Autowired private ProcessRulesHandler processRulesHandler;
2929
//TODO:@Autowired private HistoryIdRulesHandler historyIdRulesHandler;
3030

3131
public void setJmesPathInterface(JmesPathInterface jmesPathInterface) {
@@ -63,9 +63,9 @@ public void runExtraction(RulesObject rulesObject, String id, String event, Json
6363
extractedContent = extractContent(rulesObject, event);
6464

6565
if(aggregatedDbObject != null) {
66-
String object_id = objectHandler.extractObjectId(aggregatedDbObject);
67-
String mergedContent = mergeHandler.mergeObject(object_id, rulesObject, event, extractedContent);
68-
//aggregationObject = processRulesHandler.runProcessRules(aggregationObject, rulesObject);
66+
String objectId = objectHandler.extractObjectId(aggregatedDbObject);
67+
String mergedContent = mergeHandler.mergeObject(objectId, rulesObject, event, extractedContent);
68+
mergedContent = processRulesHandler.runProcessRules(event, rulesObject, mergedContent, objectId);
6969
//historyIdRulesHandler.runHistoryIdRules(aggregationObject, rulesObject, event);
7070
} else {
7171
mergeHandler.addNewObject(event, extractedContent, rulesObject);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ericsson.ei.handlers;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.ericsson.ei.jmespath.JmesPathInterface;
7+
import com.ericsson.ei.jsonmerge.MergeHandler;
8+
import com.ericsson.ei.rules.RulesObject;
9+
import com.fasterxml.jackson.databind.JsonNode;
10+
11+
@Component
12+
public class ProcessRulesHandler {
13+
14+
@Autowired
15+
JmesPathInterface jmespath;
16+
17+
@Autowired
18+
MergeHandler mergeHandler;
19+
20+
public void setJmesPathInterface(JmesPathInterface jmesPathInterface) {
21+
this.jmespath = jmesPathInterface;
22+
}
23+
24+
public void setMergeHandler(MergeHandler mergeHandler) {
25+
this.mergeHandler = mergeHandler;
26+
}
27+
28+
public String runProcessRules(String event, RulesObject rulesObject, String aggregationObject, String objectId) {
29+
String processRules = rulesObject.fetchProcessRules();
30+
JsonNode ruleResult = jmespath.runRuleOnEvent(processRules, aggregationObject);
31+
String aggregatedObject = mergeHandler.mergeObject(objectId, rulesObject, event, ruleResult);
32+
return aggregatedObject;
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.ericsson.ei.jmespath;
2+
3+
import java.util.List;
4+
5+
import io.burt.jmespath.Adapter;
6+
import io.burt.jmespath.JmesPathType;
7+
import io.burt.jmespath.function.ArgumentConstraints;
8+
import io.burt.jmespath.function.BaseFunction;
9+
import io.burt.jmespath.function.FunctionArgument;
10+
11+
public class DiffFunction extends BaseFunction {
12+
public DiffFunction() {
13+
super(ArgumentConstraints.listOf(2, Integer.MAX_VALUE, ArgumentConstraints.typeOf(JmesPathType.NUMBER)));
14+
}
15+
16+
@Override
17+
protected <T> T callFunction(Adapter<T> runtime, List<FunctionArgument<T>> arguments) {
18+
double diff = 0;
19+
T value1 = arguments.get(0).value();
20+
T value2 = arguments.get(1).value();
21+
double num1 = runtime.toNumber(value1).doubleValue();
22+
double num2 = runtime.toNumber(value2).doubleValue();
23+
diff = num1 - num2;
24+
return runtime.createNumber(diff);
25+
}
26+
27+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@
99

1010
import io.burt.jmespath.Expression;
1111
import io.burt.jmespath.JmesPath;
12+
import io.burt.jmespath.function.FunctionRegistry;
1213
import io.burt.jmespath.jackson.JacksonRuntime;
1314

1415
@Component
1516
public class JmesPathInterface {
1617

1718
static Logger log = (Logger) LoggerFactory.getLogger(JmesPathInterface.class);
1819

20+
private JmesPath<JsonNode> jmespath;
21+
22+
public JmesPathInterface() {
23+
FunctionRegistry defaultFunctions = FunctionRegistry.defaultRegistry();
24+
FunctionRegistry customFunctions = defaultFunctions.extend(new DiffFunction());
25+
jmespath = new JacksonRuntime(customFunctions);
26+
}
27+
28+
1929
public JsonNode runRuleOnEvent(String rule, String input) {
2030
JsonNode event = null;
21-
JmesPath<JsonNode> jmespath = new JacksonRuntime();
2231
Expression<JsonNode> expression = jmespath.compile(rule);
2332
ObjectMapper objectMapper = new ObjectMapper();
2433
try {
2534
event = objectMapper.readValue(input, JsonNode.class);
2635
} catch (Exception e) {
27-
log.info(e.getMessage(),e);
36+
log.info(e.getMessage(), e);
2837
}
2938
JsonNode result = expression.search(event);
3039
return result;

src/main/java/com/ericsson/ei/rules/RulesObject.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ public String getMergeRules() {
3434
return rulesObject.get("MergeResolverRules").textValue();
3535
}
3636

37-
public boolean equals(Object other){
37+
public String fetchProcessRules() {
38+
return rulesObject.get("ProcessRules").textValue();
39+
}
40+
41+
public boolean equals(Object other) {
3842
if (other instanceof RulesObject) {
39-
return rulesObject.equals(((RulesObject)other).getJsonRulesObject());
43+
return rulesObject.equals(((RulesObject) other).getJsonRulesObject());
4044
}
4145

4246
return (this == other);

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TestJmesPathInterface {
1717
private JmesPathInterface unitUnderTest;
1818
private final String inputFilePath = "src/test/resources/EiffelArtifactCreatedEvent.json";
1919
private final String outputFilePath = "src/test/resources/JmesPathInterfaceOutput.json";
20-
20+
private final String inputDiffpath = "src/test/resources/DiffFunctionInput.json";
2121

2222
static Logger log = (Logger) LoggerFactory.getLogger(TestJmesPathInterface.class);
2323

@@ -40,4 +40,21 @@ public void testRunRuleOnEvent() {
4040
assertEquals(result, output);
4141
}
4242

43+
@Test
44+
public void testDiffFunction() {
45+
unitUnderTest = new JmesPathInterface();
46+
String jsonInput = null;
47+
JsonNode expectedResult = null;
48+
try {
49+
jsonInput = FileUtils.readFileToString(new File(inputDiffpath));
50+
ObjectMapper mapper = new ObjectMapper();
51+
expectedResult = mapper.readTree("{\"testCaseExecutions\":[{\"testCaseDuration\":6.67}]}");
52+
} catch (Exception e) {
53+
log.error(e.getMessage(), e);
54+
}
55+
String processRule = "{testCaseExecutions :[{testCaseDuration : diff(testCaseExecutions[0].testCaseFinishedTime, testCaseExecutions[0].testCaseStartedTime)}]}";
56+
JsonNode result = unitUnderTest.runRuleOnEvent(processRule, jsonInput);
57+
assertEquals(result, expectedResult);
58+
}
59+
4360
}

src/test/java/com/ericsson/ei/rules/test/TestRulesObject.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
public class TestRulesObject {
1717
private RulesObject unitUnderTest;
1818
private final String inputFilePath = "src/test/resources/RulesHandlerOutput2.json";
19+
private final String inputRulesPath = "src/test/resources/ProcessRules.json";
1920
private JsonNode rulesJson;
2021

2122
static Logger log = (Logger) LoggerFactory.getLogger(TestRulesObject.class);
2223

2324
@Test
2425
public void testPrintJson() {
25-
String expectedOutput = "{ id : meta.id, type : meta.type, time : meta.time, gav : data.gav, fileInformation " +
26-
": data.fileInformation, buildCommand : data.buildCommand }";
26+
String expectedOutput = "{ id : meta.id, type : meta.type, time : meta.time, gav : data.gav, fileInformation "
27+
+ ": data.fileInformation, buildCommand : data.buildCommand }";
2728

2829
String result;
2930

@@ -41,4 +42,21 @@ public void testPrintJson() {
4142
assertEquals(result, expectedOutput);
4243
}
4344

45+
@Test
46+
public void fetchProcessRulesTest() {
47+
String expectedOutput = "{testCaseExecutions :[{testCaseDuration : diff(testCaseExecutions[0].testCaseFinishedTime, testCaseExecutions[0].testCaseStartedTime)}]}";
48+
String result;
49+
50+
try {
51+
String ruleString = FileUtils.readFileToString(new File(inputRulesPath));
52+
ObjectMapper objectMapper = new ObjectMapper();
53+
rulesJson = objectMapper.readTree(ruleString);
54+
} catch (Exception e) {
55+
log.error(e.getMessage(), e);
56+
}
57+
unitUnderTest = new RulesObject(rulesJson);
58+
result = unitUnderTest.fetchProcessRules();
59+
assertEquals(result, expectedOutput);
60+
}
61+
4462
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "_id" : "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43" , "aggregatedObject" : "{\"fileInformation\":[{\"extension\":\"jar\",\"classifier\":\"\"}],\"buildCommand\":null,\"confidenceLevels\":[{\"eventId\":\"f37d59a3-069e-4f4c-8cc5-a52e73501a75\",\"name\":\"readyForDelivery\",\"time\":1481875944272,\"value\":\"SUCCESS\"}],\"testCaseExecutions\":[{\"testCaseFinishEventId\":\"11109351-41e0-474a-bc1c-f6e81e58a1c9\",\"testCaseStartedTime\":1481875925916,\"testCaseStartedEventId\":\"cb9d64b0-a6e9-4419-8b5d-a650c27c59ca\",\"testCaseFinishedTime\":1481875935919,\"testCase\":{\"conclusion\":\"SUCCESSFUL\",\"verdict\":\"PASSED\",\"tracker\":\"My Other Test Management System\",\"id\":\"TC5\",\"uri\":\"https:\\/\\/other-tm.company.com\\/testCase\\/TC5\"}}],\"id\":\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\",\"time\":1481875891763,\"type\":\"EiffelArtifactCreatedEvent\",\"gav\":{\"groupId\":\"com.mycompany.myproduct\",\"artifactId\":\"sub-system\",\"version\":\"1.1.0\"},\"publications\":[{\"eventId\":\"33d05e6f-9bd9-4138-83b6-e20cc74680a3\",\"locations\":[{\"type\":\"PLAIN\",\"uri\":\"https:\\/\\/myrepository.com\\/mySubSystemArtifact\"}],\"time\":1481875921763}]}"}
1+
{ "_id" : "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43" , "aggregatedObject" : "{\"fileInformation\":[{\"extension\":\"jar\",\"classifier\":\"\"}],\"buildCommand\":null,\"confidenceLevels\":[{\"eventId\":\"f37d59a3-069e-4f4c-8cc5-a52e73501a75\",\"name\":\"readyForDelivery\",\"time\":1481875944272,\"value\":\"SUCCESS\"}],\"testCaseExecutions\":[{\"testCaseFinishEventId\":\"11109351-41e0-474a-bc1c-f6e81e58a1c9\",\"testCaseStartedTime\":1481875925916,\"testCaseStartedEventId\":\"cb9d64b0-a6e9-4419-8b5d-a650c27c59ca\",\"testCaseFinishedTime\":1481875935919,\"testCaseDuration\":10003,\"testCase\":{\"conclusion\":\"SUCCESSFUL\",\"verdict\":\"PASSED\",\"tracker\":\"My Other Test Management System\",\"id\":\"TC5\",\"uri\":\"https:\\/\\/other-tm.company.com\\/testCase\\/TC5\"}}],\"id\":\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\",\"time\":1481875891763,\"type\":\"EiffelArtifactCreatedEvent\",\"gav\":{\"groupId\":\"com.mycompany.myproduct\",\"artifactId\":\"sub-system\",\"version\":\"1.1.0\"},\"publications\":[{\"eventId\":\"33d05e6f-9bd9-4138-83b6-e20cc74680a3\",\"locations\":[{\"type\":\"PLAIN\",\"uri\":\"https:\\/\\/myrepository.com\\/mySubSystemArtifact\"}],\"time\":1481875921763}]}"}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"links": [
3+
{
4+
"target": "1921bc9f-59e1-45b7-8bb1-26602ed667b1",
5+
"type": "COMPOSITION"
6+
},
7+
{
8+
"target": "1921bc9f-59e1-45b7-8bb1-26602ed667b1",
9+
"type": "CAUSE"
10+
},
11+
{
12+
"target": "55da5d47-fb10-43c8-97d2-cbd5eb9a2676",
13+
"type": "PREVIOUS_VERSION"
14+
}
15+
],
16+
"meta": {
17+
"id": "e90daae3-bf3f-4b0a-b899-67834fd5ebd0",
18+
"source": {
19+
"domainId": "example.domain"
20+
},
21+
"time": 1484061386383,
22+
"type": "EiffelArtifactCreatedEvent",
23+
"version": "1.0.0"
24+
},
25+
"data": {
26+
"customData": [
27+
{
28+
"value": "ArtCC1",
29+
"key": "name"
30+
},
31+
{
32+
"value": 5000,
33+
"key": "iteration"
34+
}
35+
],
36+
"fileInformation": [
37+
{
38+
"classifier": "",
39+
"extension": "jar"
40+
}
41+
],
42+
"gav": {
43+
"artifactId": "component-1",
44+
"version": "1.5000.0",
45+
"groupId": "com.mycompany.myproduct"
46+
}
47+
},
48+
"testCaseExecutions": [
49+
{
50+
"testCaseStartedTime": 3.56,
51+
"testCaseFinishedTime": 10.23
52+
}
53+
]
54+
}

src/test/resources/ProcessRules.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"TemplateName":"ARTIFACT_1",
3+
"Type":"EiffelTestCaseFinishedEvent",
4+
"TypeRule": "meta.type",
5+
"IdRule": "meta.id",
6+
"StartEvent": "NO",
7+
"IdentifyRules" : "event[?meta.type=='EiffelTestCaseFinishedEvent'] | [0].[links] | [] | [?type=='TEST_CASE_EXECUTION'].target",
8+
"MatchIdRules": { "$and": [{"_event_object.testCaseExecutions.testCaseStartedEventId": "%IdentifyRules%"},{"_event_object.TemplateName": "ARTIFACT_1"}]},
9+
"ExtractionRules" : "{testCaseExecutions :[{ testCaseFinishEventId:meta.id, testCaseFinishedTime:meta.time, testCase:data.outcome}]}",
10+
"MergeResolverRules" : {"testCaseStartedEventId":"%IdentifyRules%"},
11+
"ArrayMergeOptions": "",
12+
"HistoryIdentifyRules": "",
13+
"HistoryExtractionRules": "",
14+
"ProcessRules":"{testCaseExecutions :[{testCaseDuration : diff(testCaseExecutions[0].testCaseFinishedTime, testCaseExecutions[0].testCaseStartedTime)}]}",
15+
"ProcessFunction" : "difference"
16+
}

0 commit comments

Comments
 (0)