Skip to content

Commit c6b4317

Browse files
authored
Escape Character issues in Aggregated objects in MongoDb solved (#34)
* Added some debug logging. * Solved some potential Escape character bugs around handling of aggregated objects. * Added missing quotation in getEventToObjectList() for eventId in condition variable. * Solved more issue extra quotation in some cases when merging, extracting and macthing objects id. * Cleaned up more debug and test prinout code. Improved debug logs. * Solved some failing unit tests. * Solved some issues related to flowtest. * add back template name in object * add back put properties * Fixed Flowtest tests. * Solved escape character issues in FlowTest2 and in AggregatedDocument2.json.
1 parent caa4285 commit c6b4317

File tree

13 files changed

+62
-28
lines changed

13 files changed

+62
-28
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class DownstreamExtractionHandler {
3939
public void runExtraction(RulesObject rulesObject, String mergeId, String event, String aggregatedDbObject) {
4040
try {
4141
ObjectMapper mapper = new ObjectMapper();
42+
log.debug("Start extraction of Aggregated Object:\n" + aggregatedDbObject +
43+
"\nwith Event:\n" + event);
4244
JsonNode aggregatedJsonObject = mapper.readValue(aggregatedDbObject, JsonNode.class);
4345
runExtraction(rulesObject, mergeId, event, aggregatedJsonObject);
4446
} catch (Exception e) {
@@ -49,6 +51,9 @@ public void runExtraction(RulesObject rulesObject, String mergeId, String event,
4951
public void runExtraction(RulesObject rulesObject, String mergeId, String event, JsonNode aggregatedDbObject) {
5052
JsonNode extractedContent;
5153
extractedContent = extractContent(rulesObject, event);
54+
log.debug("Start extraction of Aggregated Object:\n" + aggregatedDbObject.toString() +
55+
"\nwith Event:\n" + event);
56+
5257

5358
if(aggregatedDbObject != null) {
5459
String objectId = objectHandler.extractObjectId(aggregatedDbObject);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ public void updateEventToObjectMapInMemoryDB(RulesObject rulesObject, String eve
100100
try {
101101
entry = new ObjectMapper().readValue(condition, JsonNode.class);
102102
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
103-
((ObjectNode) entry).put(listPropertyName, jsonNode);
103+
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
104104
String mapStr = entry.toString();
105+
log.debug("MongoDbHandler Insert/Update Event: " + mapStr +
106+
"\nto database: " + databaseName + " and to Collection: " + collectionName);
105107
if (firstTime) {
106108
mongodbhandler.insertDocument(databaseName, collectionName, mapStr);
107109
} else {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void setObjectHandler(ObjectHandler objectHandler) {
6262
public void runExtraction(RulesObject rulesObject, String id, String event, String aggregatedDbObject) {
6363
try {
6464
ObjectMapper mapper = new ObjectMapper();
65-
JsonNode aggregatedJsonObject = mapper.readValue(aggregatedDbObject, JsonNode.class);
65+
JsonNode aggregatedJsonObject = mapper.readTree(aggregatedDbObject);
6666
runExtraction(rulesObject, id, event, aggregatedJsonObject);
6767
} catch (Exception e) {
6868
log.info(e.getMessage(),e);
@@ -74,11 +74,15 @@ public void runExtraction(RulesObject rulesObject, String mergeId, String event,
7474
extractedContent = extractContent(rulesObject, event);
7575

7676
if(aggregatedDbObject != null) {
77+
log.debug("ExtractionHandler: Merging Aggregated Object:\n" + aggregatedDbObject.toString() +
78+
"\nwith extracted content:\n" + extractedContent.toString() +
79+
"\nfrom event:\n" + event);
7780
String objectId = objectHandler.extractObjectId(aggregatedDbObject);
7881
String mergedContent = mergeHandler.mergeObject(objectId, mergeId, rulesObject, event, extractedContent);
7982
mergedContent = processRulesHandler.runProcessRules(event, rulesObject, mergedContent, objectId, mergeId);
8083
//historyIdRulesHandler.runHistoryIdRules(aggregationObject, rulesObject, event);
8184
} else {
85+
ObjectMapper mapper = new ObjectMapper();
8286
ObjectNode objectNode = (ObjectNode) extractedContent;
8387
objectNode.put("TemplateName", rulesObject.getTemplateName());
8488
mergeHandler.addNewObject(event, extractedContent, rulesObject);

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public boolean insertObject(String aggregatedObject, RulesObject rulesObject, St
8686
id = idNode.textValue();
8787
}
8888
JsonNode document = prepareDocumentForInsertion(id, aggregatedObject);
89-
String documentStr = document.toString();
90-
boolean result = mongoDbHandler.insertDocument(databaseName, collectionName, documentStr);
89+
log.debug("ObjectHandler: Aggregated Object document to be inserted: " + document.toString());
90+
boolean result = mongoDbHandler.insertDocument(databaseName, collectionName, document.toString());
9191
if (result)
9292
eventToObjectMap.updateEventToObjectMapInMemoryDB(rulesObject, event, id);
9393
subscriptionHandler.checkSubscriptionForObject(aggregatedObject);
@@ -113,6 +113,8 @@ public boolean updateObject(String aggregatedObject, RulesObject rulesObject, St
113113
JsonNode idNode = jmespathInterface.runRuleOnEvent(idRules, event);
114114
id = idNode.textValue();
115115
}
116+
log.debug("ObjectHandler: Updating Aggregated Object:\n" + aggregatedObject +
117+
"\nEvent:\n" + event);
116118
JsonNode document = prepareDocumentForInsertion(id, aggregatedObject);
117119
String condition = "{\"_id\" : \"" + id + "\"}";
118120
String documentStr = document.toString();
@@ -148,18 +150,22 @@ public ArrayList<String> findObjectsByIds(ArrayList<String> ids) {
148150
return objects;
149151
}
150152

151-
private JsonNode prepareDocumentForInsertion(String id, String object) {
152-
ObjectMapper mapper = new ObjectMapper();
153-
try {
154-
String docStr = "{\"_id\":\"" + id +"\"}";
155-
JsonNode document = mapper.readValue(docStr, JsonNode.class);
156-
((ObjectNode) document).put("aggregatedObject", object);
157-
return document;
158-
} catch (Exception e) {
159-
log.info(e.getMessage(),e);
160-
}
161-
return null;
162-
}
153+
public JsonNode prepareDocumentForInsertion(String id, String object) {
154+
ObjectMapper mapper = new ObjectMapper();
155+
try {
156+
String docStr = "{\"_id\": \"" + id + "\"}";
157+
JsonNode jsonNodeNew = mapper.readValue(docStr, JsonNode.class);
158+
159+
JsonNode jsonNode = mapper.readValue(jsonNodeNew.toString(), JsonNode.class);
160+
ObjectNode objNode = (ObjectNode) jsonNode;
161+
objNode.set("aggregatedObject", mapper.readTree(object));
162+
163+
return jsonNode;
164+
} catch (Exception e) {
165+
log.info(e.getMessage(),e);
166+
}
167+
return null;
168+
}
163169

164170
public JsonNode getAggregatedObject(String dbDocument) {
165171
ObjectMapper mapper = new ObjectMapper();
@@ -174,7 +180,7 @@ public JsonNode getAggregatedObject(String dbDocument) {
174180
}
175181

176182
public String extractObjectId(JsonNode aggregatedDbObject) {
177-
return aggregatedDbObject.get("_id").asText();
183+
return aggregatedDbObject.get("_id").textValue();
178184
}
179185

180186
/**

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public String mergeObject(String id, String mergeId, RulesObject rules, String e
8484
}catch (Exception e){
8585
log.info(e.getMessage(),e);
8686
}
87+
log.debug("Merged Aggregated Object:\n" + mergedObject);
8788
// unlocking of document will be performed, when mergedObject will be inserted to database
8889
objectHandler.updateObject(mergedObject, rules, event, id);
8990
return mergedObject;
@@ -94,9 +95,9 @@ protected String getMergeRules(RulesObject rules) {
9495
}
9596

9697
public String replaceIdMarkerInRules(String rule, String id){
97-
String literal = "\"" + id + "\"";
98+
9899
if (rule.contains(mergeIdMarker)) {
99-
String updatedRule = rule.replaceAll(mergeIdMarker, literal);
100+
String updatedRule = rule.replaceAll(mergeIdMarker, "\"" + id + "\"");
100101
updatedRule = "`" + updatedRule + "`";
101102
return updatedRule;
102103
}
@@ -172,7 +173,7 @@ public String getAggregatedObject(String id){
172173
String document = objectHandler.lockDocument(id);
173174
JsonNode result = objectHandler.getAggregatedObject(document);
174175
if (result != null)
175-
return result.asText();
176+
return result.toString();
176177
}catch (Exception e){
177178
log.info(e.getMessage(),e);
178179
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
107107
//Retrieve data from the collection based on condition
108108
public ArrayList<String> find(String dataBaseName, String collectionName, String condition){
109109
ArrayList<String> result = new ArrayList<>();
110+
log.debug("Find and retrieve data from database: " + dataBaseName + " Collection: " + collectionName +
111+
"\nwith Condition: " + condition);
110112
try{
111113
DB db = mongoClient.getDB(dataBaseName);
112114
DBCollection table = db.getCollection(collectionName);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class SubscriptionValidator {
3939
public void validateSubscription(Subscription subscription) throws SubscriptionValidationException {
4040

4141
LOG.info("Validation of subscription " + subscription.getSubscriptionName() + " Started.");
42-
4342
this.validateSubscriptionName(subscription.getSubscriptionName());
4443
this.validateNotificationMessage(subscription.getNotificationMessage());
4544
this.validateNotificationMeta(subscription.getNotificationMeta());

src/main/java/com/ericsson/ei/waitlist/WaitListWorker.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import org.slf4j.LoggerFactory;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.scheduling.TaskScheduler;
2527
import org.springframework.scheduling.annotation.Scheduled;
28+
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
2629
import org.springframework.stereotype.Component;
2730

2831
import com.ericsson.ei.handlers.MatchIdRulesHandler;
@@ -54,6 +57,11 @@ public class WaitListWorker {
5457
private MatchIdRulesHandler matchIdRulesHandler;
5558

5659
static Logger log = (Logger) LoggerFactory.getLogger(WaitListWorker.class);
60+
61+
@Bean
62+
public TaskScheduler taskScheduler() {
63+
return new ConcurrentTaskScheduler();
64+
}
5765

5866
@Scheduled(initialDelayString = "${waitlist.initialDelayResend}", fixedRateString = "${waitlist.fixedRateResend}")
5967
public void run() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected void checkResult() {
235235
JsonNode expectedJson = objectmapper.readTree(expectedDocument);
236236
JsonNode actualJson = objectmapper.readTree(document);
237237
String breakString = "breakHere";
238-
assertEquals(expectedJson.toString().length(), actualJson.toString().length());
238+
assertEquals("\nExpectedJson:\n" + expectedJson.toString() + "\nActual:\n" + actualJson.toString() + "\n", expectedJson.toString().length(), actualJson.toString().length());
239239
} catch (IOException e) {
240240
log.info(e.getMessage(),e);
241241
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,18 @@ public void trafficGeneratedTest() {
233233
* @return list of ready to send events.
234234
*/
235235
private List<String> getPreparedEventsToSend(List<String> eventNames) throws IOException {
236+
ObjectMapper mapper = new ObjectMapper();
236237
List<String> events = new ArrayList<>();
237238
String newID;
238239
for (int i = 0; i < EVENT_PACKAGES; i++) {
239240
for(String eventName : eventNames) {
240241
JsonNode eventJson = parsedJason.get(eventName);
241242
newID = eventJson.at("/meta/id").textValue().substring(0, 30).concat(String.format("%06d", i));;
242-
((ObjectNode) eventJson.path("meta")).put("id", newID);
243+
((ObjectNode) eventJson.path("meta")).set("id", mapper.readValue(newID, JsonNode.class));
243244
for (JsonNode link : eventJson.path("links")) {
244245
if (link.has("target")) {
245246
newID = link.path("target").textValue().substring(0, 30).concat(String.format("%06d", i));
246-
((ObjectNode) link).put("target", newID);
247+
((ObjectNode) link).set("target", mapper.readValue(newID, JsonNode.class));
247248
}
248249
}
249250
events.add(eventJson.toString());

0 commit comments

Comments
 (0)