Skip to content

Commit 75472fd

Browse files
Improve internal composition
Improve internal composition
1 parent 6800686 commit 75472fd

File tree

11 files changed

+227
-165
lines changed

11 files changed

+227
-165
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
<groupId>org.springframework.boot</groupId>
5151
<artifactId>spring-boot-starter-web</artifactId>
5252
<exclusions>
53-
<exclusion>
54-
<groupId>org.springframework.boot</groupId>
55-
<artifactId>spring-boot-starter-tomcat</artifactId>
56-
</exclusion>
57-
</exclusions>
53+
<exclusion>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-tomcat</artifactId>
56+
</exclusion>
57+
</exclusions>
5858
</dependency>
5959

6060
<dependency>
@@ -338,4 +338,4 @@
338338

339339
</plugins>
340340
</build>
341-
</project>
341+
</project>

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.beans.factory.annotation.Autowired;
2727
import org.springframework.stereotype.Component;
2828

29-
// TODO: Auto-generated Javadoc
3029
/**
3130
* The Class HistoryExtractionHandler.
3231
*/
@@ -71,17 +70,21 @@ public String runHistoryExtraction(String aggregatedObjectId, RulesObject rules,
7170
String objAtPathStr = "";
7271
String pathTrimmed = mergePrepare.trimLastInPath(pathInAggregatedObject, ".");
7372
try {
74-
pathTrimmed = mergePrepare.makeJmespathArrayIndexes(pathTrimmed);
75-
JsonNode objAtPath = jmesPathInterface.runRuleOnEvent(pathTrimmed, aggregatedObject);
76-
objAtPathStr = objAtPath.toString();
73+
if (pathTrimmed.isEmpty()) {
74+
objAtPathStr = aggregatedObject;
75+
} else {
76+
pathTrimmed = mergePrepare.makeJmespathArrayIndexes(pathTrimmed);
77+
JsonNode objAtPath = jmesPathInterface.runRuleOnEvent(pathTrimmed, aggregatedObject);
78+
objAtPathStr = objAtPath.toString();
79+
}
7780
} catch (Exception e) {
7881
log.error(e.getMessage(), e);
7982
}
8083
String array_path = getPathFromExtractedContent(objAtPathStr, ruleString);
8184

8285
if (!array_path.isEmpty()) {
83-
// pathInAggregatedObject = array_path;
84-
pathInAggregatedObject = pathTrimmed + "." + array_path;
86+
String pathPrefix = pathTrimmed.isEmpty() ? "" : pathTrimmed + ".";
87+
pathInAggregatedObject = pathPrefix + array_path;
8588
pathInAggregatedObject = MergePrepare.destringify(pathInAggregatedObject);
8689
} else {
8790
String ruleKey = getRulePath(ruleString);
@@ -123,7 +126,7 @@ private String getRulePath(String stringRule) {
123126
* @return the path from given content
124127
*/
125128
private String getPathFromExtractedContent(String content, String mergeRules) {
126-
return mergePrepare.getMergePath(content, mergeRules);
129+
return mergePrepare.getMergePath(content, mergeRules, true);
127130
}
128131

129132
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public String mergeObject(String id, String mergeId, RulesObject rules, String e
7070
if (mergeRule != null && !mergeRule.isEmpty()) {
7171
String updatedRule = replaceIdMarkerInRules(mergeRule, mergeId);
7272
String ruleForMerge = jmesPathInterface.runRuleOnEvent(updatedRule, event).toString();
73-
String mergePath = prepareMergePrepareObject.getMergePath(aggregatedObject, ruleForMerge);
73+
String mergePath = prepareMergePrepareObject.getMergePath(aggregatedObject, ruleForMerge, false);
7474
preparedToMergeObject = prepareMergePrepareObject.addMissingLevels(aggregatedObject,
7575
objectToMerge.toString(), ruleForMerge, mergePath);
7676
} else {

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public String getMergePathFromArrayMergeRules(String originObject, String mergeR
130130
JSONArray ruleJSONArray = new JSONArray(mergeRule);
131131
String firstRule = ruleJSONArray.getString(0);
132132
String secondRule = ruleJSONArray.getString(1);
133-
String firstPath = getMergePath(originObject, firstRule);
133+
String firstPath = getMergePath(originObject, firstRule, false);
134134
String firstPathTrimmed = trimLastInPath(firstPath, ".");
135135

136136
if (propertyExist(stringObject, firstPathTrimmed, secondRule)) {
@@ -145,9 +145,9 @@ public String getMergePathFromArrayMergeRules(String originObject, String mergeR
145145
secondRuleComplete += "}";
146146
}
147147

148-
return getMergePath(originObject, secondRuleComplete);
148+
return getMergePath(originObject, secondRuleComplete, false);
149149
} else {
150-
return getMergePath(originObject, secondRule);
150+
return getMergePath(originObject, secondRule, false);
151151
}
152152
} else {
153153
String flattenRule = JsonFlattener.flatten(secondRule);
@@ -199,7 +199,7 @@ public String makeJmespathArrayIndexes(String path) {
199199
return path;
200200
}
201201

202-
public String getMergePath(String originObject, String mergeRule) {
202+
public String getMergePath(String originObject, String mergeRule, boolean skipPathSearch) {
203203
String mergePath = "";
204204
String stringObject = "";
205205
String stringRule = "";
@@ -236,20 +236,33 @@ public String getMergePath(String originObject, String mergeRule) {
236236
ArrayList<String> pathsWithValue = new ArrayList<String>();
237237
ArrayList<String> pathsContainingRule = new ArrayList<String>();
238238

239-
for (Map.Entry<String, Object> entry : flattenJson.entrySet()) {
240-
String entryKey = entry.getKey();
241-
Object entryValue = entry.getValue();
242-
if (entryValue != null && entryValue.equals(ruleValue)) {
243-
pathsWithValue.add(destringify(entryKey));
239+
if (skipPathSearch) {
240+
int pos = ruleKey.lastIndexOf(".");
241+
if (pos > 0)
242+
ruleKey = ruleKey.substring(0, pos);
243+
try {
244+
Object object = objectJSONObject.get(ruleKey);
245+
if (object != null)
246+
mergePath = ruleKey;
247+
} catch (JSONException e) {
248+
log.error(e.getMessage(), e);
244249
}
245-
246-
int factorCount = 0;
247-
for (String factor : ruleKeyFactors) {
248-
if (entryKey.contains(factor)) {
249-
factorCount++;
250+
} else {
251+
for (Map.Entry<String, Object> entry : flattenJson.entrySet()) {
252+
String entryKey = entry.getKey();
253+
Object entryValue = entry.getValue();
254+
if (entryValue != null && entryValue.equals(ruleValue)) {
255+
pathsWithValue.add(destringify(entryKey));
250256
}
251-
if (factorCount == ruleKeyFactors.length) {
252-
pathsContainingRule.add(destringify(entryKey));
257+
258+
int factorCount = 0;
259+
for (String factor : ruleKeyFactors) {
260+
if (entryKey.contains(factor)) {
261+
factorCount++;
262+
}
263+
if (factorCount == ruleKeyFactors.length) {
264+
pathsContainingRule.add(destringify(entryKey));
265+
}
253266
}
254267
}
255268
}

src/main/java/com/ericsson/ei/mongodbhandler/MongoDBHandler.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/*
22
Copyright 2017 Ericsson AB.
33
For a full list of individual contributors, please see the commit history.
4-
54
Licensed under the Apache License, Version 2.0 (the "License");
65
you may not use this file except in compliance with the License.
76
You may obtain a copy of the License at
8-
97
http://www.apache.org/licenses/LICENSE-2.0
10-
118
Unless required by applicable law or agreed to in writing, software
129
distributed under the License is distributed on an "AS IS" BASIS,
1310
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -42,7 +39,23 @@
4239
import com.mongodb.client.result.UpdateResult;
4340
import com.mongodb.util.JSON;
4441

42+
import java.util.ArrayList;
43+
import java.util.Collections;
44+
import java.util.List;
45+
import java.util.concurrent.TimeUnit;
46+
47+
import javax.annotation.PostConstruct;
48+
49+
import org.apache.commons.lang3.StringUtils;
50+
import org.bson.Document;
51+
import org.slf4j.Logger;
52+
import org.slf4j.LoggerFactory;
53+
import org.springframework.beans.factory.annotation.Autowired;
54+
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
55+
import org.springframework.stereotype.Component;
56+
4557
import lombok.Getter;
58+
import lombok.Setter;
4659

4760
@Component
4861
public class MongoDBHandler {
@@ -65,10 +78,11 @@ public void init() {
6578

6679
// Establishing the connection to mongodb and creating a collection
6780
private void createConnection() {
68-
if (!StringUtils.isBlank(mongoProperties.getUsername()) && !StringUtils.isBlank(new String(mongoProperties.getPassword()))) {
81+
if (!StringUtils.isBlank(mongoProperties.getUsername())
82+
&& !StringUtils.isBlank(new String(mongoProperties.getPassword()))) {
6983
ServerAddress address = new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort());
7084
MongoCredential credential = MongoCredential.createCredential(mongoProperties.getUsername(),
71-
mongoProperties.getDatabase(), mongoProperties.getPassword());
85+
mongoProperties.getDatabase(), mongoProperties.getPassword());
7286
mongoClient = new MongoClient(address, Collections.singletonList(credential));
7387
} else {
7488
mongoClient = new MongoClient(mongoProperties.getHost(), mongoProperties.getPort());
@@ -102,7 +116,8 @@ public boolean insertDocument(String dataBaseName, String collectionName, String
102116
}
103117

104118
/**
105-
* This method is used for the retrieve the all documents from the collection
119+
* This method is used for the retrieve the all documents from the
120+
* collection
106121
*
107122
* @param dataBaseName
108123
* @param collectionName
@@ -166,8 +181,8 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
166181
}
167182

168183
/**
169-
* This method is used for update the document in collection and remove the lock
170-
* in one query. Lock is needed for multi process execution
184+
* This method is used for update the document in collection and remove the
185+
* lock in one query. Lock is needed for multi process execution
171186
*
172187
* @param dataBaseName
173188
* @param collectionName
@@ -196,9 +211,9 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
196211
}
197212

198213
/**
199-
* This method is used for lock and return the document that matches the input
200-
* condition in one query. Lock is needed for multi process execution. This
201-
* method is executed in a loop.
214+
* This method is used for lock and return the document that matches the
215+
* input condition in one query. Lock is needed for multi process execution.
216+
* This method is executed in a loop.
202217
*
203218
* @param dataBaseName
204219
* @param collectionName
@@ -289,11 +304,11 @@ private MongoCollection<Document> getMongoCollection(String dataBaseName, String
289304
MongoCollection<Document> collection = db.getCollection(collectionName);
290305
return collection;
291306
}
292-
307+
293308
public void dropCollection(String dataBaseName, String collectionName) {
294-
MongoDatabase db = mongoClient.getDatabase(dataBaseName);
295-
MongoCollection<Document> mongoCollection = db.getCollection(collectionName);
296-
mongoCollection.drop();
309+
MongoDatabase db = mongoClient.getDatabase(dataBaseName);
310+
MongoCollection<Document> mongoCollection = db.getCollection(collectionName);
311+
mongoCollection.drop();
297312
}
298313

299314
}

src/test/java/com/ericsson/ei/flowtests/FlowSourceChangeObject.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,45 @@
1313
*/
1414
package com.ericsson.ei.flowtests;
1515

16+
import com.ericsson.ei.App;
17+
import com.ericsson.ei.erqueryservice.ERQueryService;
18+
import com.ericsson.ei.erqueryservice.SearchOption;
19+
import com.ericsson.ei.handlers.UpStreamEventsHandler;
1620
import com.fasterxml.jackson.databind.JsonNode;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.node.ObjectNode;
23+
import org.junit.Before;
1724
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.MockitoAnnotations;
27+
import org.springframework.beans.factory.annotation.Autowired;
1828
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.http.HttpStatus;
30+
import org.springframework.http.ResponseEntity;
1931
import org.springframework.test.context.TestExecutionListeners;
2032
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2133
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
2234

2335
import java.io.IOException;
36+
import java.net.URL;
2437
import java.util.ArrayList;
2538
import java.util.HashMap;
2639
import java.util.List;
2740
import java.util.Map;
2841

42+
import static org.mockito.ArgumentMatchers.*;
43+
import static org.mockito.Mockito.when;
44+
2945
@RunWith(SpringJUnit4ClassRunner.class)
30-
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class, FlowSourceChangeObject.class })
31-
@SpringBootTest
46+
@TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class, FlowSourceChangeObject.class})
47+
@SpringBootTest(classes = App.class)
3248
public class FlowSourceChangeObject extends FlowTestBase {
3349

3450
private static final String RULES_FILE_PATH = "src/test/resources/TestSourceChangeObjectRules.json";
3551
private static final String EVENTS_FILE_PATH = "src/test/resources/TestSourceChangeObject.json";
3652
private static final String AGGREGATED_OBJECT_FILE_PATH = "src/test/resources/aggregatedSourceChangeObject.json";
3753
private static final String AGGREGATED_OBJECT_ID = "fb6efi4n-25fb-4d77-b9fd-5f2xrrefe66de47";
54+
private static final String UPSTREAM_FILE = "UpstreamEventsForSourceChange.json";
3855

3956
@Override
4057
String getRulesFilePath() {
@@ -46,12 +63,29 @@ String getEventsFilePath() {
4663
return EVENTS_FILE_PATH;
4764
}
4865

66+
@Autowired
67+
private UpStreamEventsHandler upStreamEventsHandler;
68+
69+
@Mock
70+
private ERQueryService erQueryService;
71+
72+
@Before
73+
public void before() throws IOException {
74+
MockitoAnnotations.initMocks(this);
75+
upStreamEventsHandler.setEventRepositoryQueryService(erQueryService);
76+
final URL upStreamResult = this.getClass().getClassLoader().getResource(UPSTREAM_FILE);
77+
ObjectMapper objectMapper = new ObjectMapper();
78+
ObjectNode objectNode = objectMapper.createObjectNode();
79+
objectNode.set("upstreamLinkObjects", objectMapper.readTree(upStreamResult));
80+
objectNode.set("downstreamLinkObjects", objectMapper.createArrayNode());
81+
when(erQueryService.getEventStreamDataById(anyString(), any(SearchOption.class), anyInt(), anyInt(),
82+
anyBoolean())).thenReturn(new ResponseEntity<>(objectNode, HttpStatus.OK));
83+
}
84+
4985
@Override
5086
List<String> getEventNamesToSend() {
5187
List<String> eventNames = new ArrayList<>();
5288
eventNames.add("event_EiffelSourceChangeSubmittedEvent_3");
53-
eventNames.add("event_EiffelSourceChangeCreatedEvent_3");
54-
eventNames.add("event_EiffelSourceChangeCreatedEvent_3_2");
5589
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3");
5690
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
5791
eventNames.add("event_EiffelActivityTriggeredEvent_3");

src/test/java/com/ericsson/ei/jsonmerge/test/TestMergePrepare.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void getValueFromRule() {
7575

7676
@Test
7777
public void getMergePath() {
78-
String result = mergePrepareObject.getMergePath(originObject, mergeRule);
78+
String result = mergePrepareObject.getMergePath(originObject, mergeRule, false);
7979
assertEquals(mergePath, result);
8080
}
8181

0 commit comments

Comments
 (0)