Skip to content

Commit 8610503

Browse files
author
Vasile Baluta
committed
first try to create array aggregation
1 parent 2400389 commit 8610503

File tree

6 files changed

+218
-12
lines changed

6 files changed

+218
-12
lines changed

src/main/java/com/ericsson/ei/jsonmerge/MergePrepare.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@
1616
*/
1717
package com.ericsson.ei.jsonmerge;
1818

19-
import org.slf4j.Logger;
20-
import org.slf4j.LoggerFactory;
21-
import org.apache.commons.lang3.StringUtils;
22-
import org.json.*;
23-
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.stereotype.Component;
25-
2619
import com.ericsson.ei.jmespath.JmesPathInterface;
2720
import com.fasterxml.jackson.databind.JsonNode;
2821
import com.fasterxml.jackson.databind.ObjectMapper;
2922
import com.fasterxml.jackson.databind.node.ArrayNode;
3023
import com.github.wnameless.json.flattener.JsonFlattener;
3124

32-
import lombok.Setter;
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.Map;
28+
29+
import org.apache.commons.lang3.StringUtils;
30+
import org.json.JSONArray;
31+
import org.json.JSONException;
32+
import org.json.JSONObject;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
import org.springframework.beans.factory.annotation.Autowired;
36+
import org.springframework.stereotype.Component;
3337

34-
import java.util.*;
38+
import lombok.Setter;
3539

3640
@Component
3741
public class MergePrepare {
@@ -320,13 +324,14 @@ public String getMergePath(String originObject, String mergeRule, boolean skipPa
320324
Object value = parsedJson.at(mergePath);
321325
if (value instanceof ArrayNode) {
322326
int arraySize = ((ArrayNode) value).size();
323-
mergePath += "." + arraySize++ + "." + ruleKeyLast;
324-
} else {
327+
mergePath += "." + arraySize++;
328+
}
329+
if (!StringUtils.isAllBlank(ruleKeyLast)) {
325330
mergePath += "." + ruleKeyLast;
326331
}
332+
327333
mergePath = mergePath.replaceFirst("\\/", "");
328334
mergePath = mergePath.replaceAll("\\/", "\\.");
329-
330335
} catch (Exception e) {
331336
log.error(e.getMessage(), e);
332337
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.ericsson.ei.flowtests;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.anyBoolean;
5+
import static org.mockito.ArgumentMatchers.anyInt;
6+
import static org.mockito.ArgumentMatchers.anyString;
7+
import static org.mockito.Mockito.when;
8+
9+
import com.ericsson.ei.App;
10+
import com.ericsson.ei.erqueryservice.ERQueryService;
11+
import com.ericsson.ei.erqueryservice.SearchOption;
12+
import com.ericsson.ei.handlers.UpStreamEventsHandler;
13+
import com.fasterxml.jackson.databind.JsonNode;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
import com.fasterxml.jackson.databind.node.ObjectNode;
16+
17+
import java.io.IOException;
18+
import java.net.URL;
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.junit.Before;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Mock;
27+
import org.mockito.MockitoAnnotations;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.http.HttpStatus;
31+
import org.springframework.http.ResponseEntity;
32+
import org.springframework.test.context.TestExecutionListeners;
33+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34+
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
35+
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class, ArrayAggregationTest.class })
38+
@SpringBootTest(classes = App.class)
39+
public class ArrayAggregationTest extends FlowTestBase {
40+
41+
private static final String UPSTREAM_RESULT_FILE = "arrayAggregationUpstreamResult.json";
42+
private static final String EVENTS_FILE_PATH = "src/test/resources/arrayAggregationEvents.json";
43+
private static final String RULES_FILE_PATH = "src/test/resources/arrayAggregationRules.json";
44+
private static final String AGGREGATED_OBJECT_FILE_PATH = "src/test/resources/arrayAggregatedObject.json";
45+
private static final String AGGREGATED_OBJECT_ID = "175f08ff-1e4b-4265-a0d4-36e744297dc3";
46+
47+
@Autowired
48+
private UpStreamEventsHandler upStreamEventsHandler;
49+
50+
@Mock
51+
private ERQueryService erQueryService;
52+
53+
@Before
54+
public void before() throws IOException {
55+
MockitoAnnotations.initMocks(this);
56+
upStreamEventsHandler.setEventRepositoryQueryService(erQueryService);
57+
58+
final URL upStreamResult = this.getClass().getClassLoader().getResource(UPSTREAM_RESULT_FILE);
59+
ObjectMapper objectMapper = new ObjectMapper();
60+
ObjectNode objectNode = objectMapper.createObjectNode();
61+
objectNode.set("upstreamLinkObjects", objectMapper.readTree(upStreamResult));
62+
objectNode.set("downstreamLinkObjects", objectMapper.createArrayNode());
63+
64+
when(erQueryService.getEventStreamDataById(anyString(), any(SearchOption.class), anyInt(), anyInt(),
65+
anyBoolean())).thenReturn(new ResponseEntity<>(objectNode, HttpStatus.OK));
66+
}
67+
68+
@Override
69+
String getRulesFilePath() {
70+
return RULES_FILE_PATH;
71+
}
72+
73+
@Override
74+
String getEventsFilePath() {
75+
return EVENTS_FILE_PATH;
76+
}
77+
78+
@Override
79+
List<String> getEventNamesToSend() {
80+
List<String> eventNames = new ArrayList<>();
81+
// eventNames.add("EiffelArtifactPublishedEvent_1");
82+
// eventNames.add("EiffelArtifactPublishedEvent_2");
83+
eventNames.add("EiffelCompositionDefinedEvent");
84+
// eventNames.add("EiffelArtifactPublishedEvent_3");
85+
// eventNames.add("EiffelArtifactPublishedEvent_4");
86+
return eventNames;
87+
}
88+
89+
@Override
90+
Map<String, JsonNode> getCheckData() throws IOException {
91+
JsonNode expectedJSON = getJSONFromFile(AGGREGATED_OBJECT_FILE_PATH);
92+
Map<String, JsonNode> checkData = new HashMap<>();
93+
checkData.put(AGGREGATED_OBJECT_ID, expectedJSON);
94+
return checkData;
95+
}
96+
97+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"events": [
3+
{
4+
"data":{"customData":[],"gav":{"artifactId":"artifact id","groupId":"group id","version":"0.0.0a"}},
5+
"links":[],
6+
"meta":{"id":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","time":1529405139319,"type":"EiffelArtifactCreatedEvent","version":"1.1.0"}
7+
},
8+
{
9+
"data":{"customData":[],"gav":{"artifactId":"artifact id","groupId":"group id","version":"0.0.0a"}},
10+
"links":[],
11+
"meta":{"id":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","time":1529405139319,"type":"EiffelArtifactCreatedEvent","version":"1.1.0"}
12+
},
13+
{
14+
"data":{"customData":[],"locations":[{"type":"PLAIN","uri":"https://external.example.com/84e9dfe1-09f7-4002-83b9-1aff4b7844a2"}]},
15+
"links":[{"target":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","type":"ARTIFACT"}],
16+
"meta":{"id":"cc78087b-dc58-4da6-a009-51cea15bfa46","time":1529405139327,"type":"EiffelArtifactPublishedEvent","version":"1.1.0"}
17+
},
18+
{
19+
"data":{"customData":[],"locations":[{"type":"PLAIN","uri":"https://external.example.com/cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf"}]},
20+
"links":[{"target":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","type":"ARTIFACT"}],
21+
"meta":{"id":"d7cf357d-9c05-4b51-84e7-af36b73d42df","time":1529405139330,"type":"EiffelArtifactPublishedEvent","version":"1.1.0"}
22+
},
23+
{
24+
"data":{"customData":[{"key":"Type","value":"Source"}],"name":"TestCompositionName","version":"TestCompositionVersion"},
25+
"links":[{"target":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","type":"ELEMENT"}, {"target":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","type":"ELEMENT"}],
26+
"meta":{"id":"175f08ff-1e4b-4265-a0d4-36e744297dc3","time":1529405139338,"type":"EiffelCompositionDefinedEvent","version":"1.1.0"}
27+
}
28+
]
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"EiffelArtifactPublishedEvent_1" : {
3+
"data":{"customData":[],"locations":[{"type":"PLAIN","uri":"https://external.example.com/84e9dfe1-09f7-4002-83b9-1aff4b7844a2"}]},
4+
"links":[{"target":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","type":"ARTIFACT"}],
5+
"meta":{"id":"cc78087b-dc58-4da6-a009-51cea15bfa46","time":1529405139327,"type":"EiffelArtifactPublishedEvent","version":"1.1.0"}
6+
},
7+
"EiffelArtifactPublishedEvent_2" :{
8+
"data":{"customData":[],"locations":[{"type":"PLAIN","uri":"https://external.example.com/cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf"}]},
9+
"links":[{"target":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","type":"ARTIFACT"}],
10+
"meta":{"id":"d7cf357d-9c05-4b51-84e7-af36b73d42df","time":1529405139330,"type":"EiffelArtifactPublishedEvent","version":"1.1.0"}
11+
},
12+
"EiffelCompositionDefinedEvent" :{
13+
"data":{"customData":[{"key":"Type","value":"Source"}],"name":"TestCompositionName","version":"TestCompositionVersion"},
14+
"links":[{"target":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","type":"ELEMENT"}, {"target":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","type":"ELEMENT"}],
15+
"meta":{"id":"175f08ff-1e4b-4265-a0d4-36e744297dc3","time":1529405139338,"type":"EiffelCompositionDefinedEvent","version":"1.1.0"}
16+
},
17+
"EiffelArtifactPublishedEvent_3" : {
18+
"data":{"customData":[],"locations":[{"type":"PLAIN","uri":"https://subdomain.example.com/84e9dfe1-09f7-4002-83b9-1aff4b7844a2"}]},
19+
"links":[{"target":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","type":"ARTIFACT"}],
20+
"meta":{"id":"908ff42d-786c-4a45-bf94-b43883d29784","time":1529405139639,"type":"EiffelArtifactPublishedEvent","version":"1.1.0"}
21+
},
22+
"EiffelArtifactPublishedEvent_4" :{
23+
"data":{"customData":[],"locations":[{"type":"PLAIN","uri":"https://subdomain.example.com/cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf"}]},
24+
"links":[{"target":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","type":"ARTIFACT"}],
25+
"meta":{"id":"908ff42d-786c-4a45-bf94-b43883d49784","time":1529405139701,"type":"EiffelArtifactPublishedEvent","version":"1.1.0"}
26+
}
27+
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[
2+
{
3+
"TemplateName":"ARRAY_AGGREGATION_1",
4+
"Type":"EiffelCompositionDefinedEvent",
5+
"TypeRule": "meta.type",
6+
"IdRule": "meta.id",
7+
"StartEvent": "YES",
8+
"IdentifyRules" : "[meta.id]",
9+
"MatchIdRules": {"_id": "%IdentifyRules_objid%"},
10+
"ExtractionRules" : "{events:[@]}"
11+
},
12+
{
13+
"TemplateName":"ARRAY_AGGREGATION_1",
14+
"Type":"EiffelArtifactPublishedEvent",
15+
"TypeRule": "meta.type",
16+
"IdRule": "meta.id",
17+
"StartEvent": "NO",
18+
"IdentifyRules" : "links | [?type=='ARTIFACT'].target",
19+
"MatchIdRules": {"_id": "%IdentifyRules_objid%"},
20+
"ExtractionRules" : "@",
21+
"MergeResolverRules": "[ {NONEPATH:NONE}, {events: []} ]"
22+
},
23+
{
24+
"TemplateName":"ARRAY_AGGREGATION_1",
25+
"Type":"EiffelArtifactCreatedEvent",
26+
"TypeRule": "meta.type",
27+
"IdRule": "meta.id",
28+
"StartEvent": "NO",
29+
"IdentifyRules" : "",
30+
"MatchIdRules": {},
31+
"ExtractionRules" : "",
32+
"HistoryExtractionRules":"@",
33+
"HistoryPathRules": "{events: []}"
34+
}
35+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"data":{"customData":[],"gav":{"artifactId":"artifact id","groupId":"group id","version":"0.0.0a"}},
4+
"links":[],
5+
"meta":{"id":"84e9dfe1-09f7-4002-83b9-1aff4b7844a2","time":1529405139319,"type":"EiffelArtifactCreatedEvent","version":"1.1.0"}
6+
},
7+
{
8+
"data":{"customData":[],"gav":{"artifactId":"artifact id","groupId":"group id","version":"0.0.0a"}},
9+
"links":[],
10+
"meta":{"id":"cdffc8e9-5ee5-45c5-9f0e-0e89cea51dcf","time":1529405139319,"type":"EiffelArtifactCreatedEvent","version":"1.1.0"}
11+
}
12+
]

0 commit comments

Comments
 (0)