Skip to content

Commit ce574ff

Browse files
SantoshNC68Durga Vasaadi
and
Durga Vasaadi
authored
Fix for MongoDBHandling on ObjectToEvent map handling (#472)
* Added fixes to MongoDB connections * Updated the Mongo query handling to check the object mapping * Updated java doc for the newly created methods * Updated DatabaseManager to check expectedEventIds in objects * Updated DatabaseManager to check expectedEventIds Co-authored-by: Durga Vasaadi <durga.vasaadi@ericsson.com>
1 parent c373473 commit ce574ff

File tree

7 files changed

+94
-22
lines changed

7 files changed

+94
-22
lines changed

src/functionaltests/java/com/ericsson/ei/utils/DataBaseManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ private List<String> compareSentEventsWithEventsInDB(List<String> checklist) {
149149
List<Document> documents = collection.find().into(new ArrayList<>());
150150
for (Document document : documents) {
151151
for (String expectedID : new ArrayList<>(checklist)) {
152-
if (expectedID.equals(document.get("_id").toString())) {
152+
if (document.get("objects").toString().contains(expectedID)) {
153153
checklist.remove(expectedID);
154-
}
154+
}
155155
}
156156
}
157157
return checklist;

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,36 @@ public ArrayList<String> getObjectsForEventId(String eventId) {
8181
return getEventToObjectList(eventId);
8282
}
8383

84+
/**
85+
* To check and save the eventIds to the objectId in the mapped database.
86+
* @param rulesObject
87+
* @param event
88+
* @param objectId aggregated event object Id
89+
*/
8490
public void updateEventToObjectMapInMemoryDB(RulesObject rulesObject, String event, String objectId) {
8591
String eventId = getEventId(rulesObject, event);
86-
String condition = "{\"_id\" : \"" + eventId + "\"}";
87-
ArrayList<String> list = getEventToObjectList(eventId);
88-
boolean firstTime = list.isEmpty();
89-
list = updateList(list, eventId, objectId);
90-
ObjectMapper mapper = new ObjectMapper();
91-
JsonNode entry = null;
92-
92+
String condition = "{\"_id\" : \"" + objectId + "\"}";
93+
LOGGER.debug("Checking document exists in the collection with condition : {}\n EventId : {}", condition, eventId);
94+
boolean docExists = mongodbhandler.checkDocumentExists(databaseName, collectionName, condition);
9395
try {
94-
entry = new ObjectMapper().readValue(condition, JsonNode.class);
95-
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
96-
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
97-
String mapStr = entry.toString();
98-
LOGGER.debug("MongoDbHandler Insert/Update Event: {}\nto database: {} and to Collection: {}", mapStr, databaseName, collectionName);
99-
if (firstTime) {
100-
mongodbhandler.insertDocument(databaseName, collectionName, mapStr);
96+
if (!docExists) {
97+
ArrayList<String> list = new ArrayList<String>();
98+
list.add(eventId);
99+
final ObjectMapper mapper = new ObjectMapper();
100+
JsonNode entry = new ObjectMapper().readValue(condition, JsonNode.class);
101+
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
102+
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
103+
final String mapStr = entry.toString();
104+
LOGGER.debug("MongoDbHandler Insert/Update Event: {}\nto database: {} and to Collection: {}", mapStr, databaseName, collectionName);
105+
mongodbhandler.insertDocument(databaseName, collectionName, mapStr );
101106
} else {
102-
mongodbhandler.updateDocument(databaseName, collectionName, condition, mapStr);
107+
mongodbhandler.updateDocumentAddToSet(databaseName, collectionName, condition, eventId);
103108
}
104109
} catch (Exception e) {
105-
LOGGER.info("Failed to update event object list.", e);
110+
LOGGER.error("Failed to update event object list.", e);
106111
}
107112
}
113+
108114

109115
public String getEventId(RulesObject rulesObject, String event) {
110116
String idRule = rulesObject.getIdRule();
@@ -148,7 +154,7 @@ public boolean deleteEventObjectMap(String templateName) {
148154
}
149155

150156
public boolean isEventInEventObjectMap(String eventId) {
151-
String condition = "{\"_id\" : \"" + eventId + "\"}";
157+
String condition = "{\"objects\": { \"$in\" : [\"" + eventId + "\"]} }";
152158
List<String> documents = mongodbhandler.find(databaseName, collectionName, condition);
153159
return !documents.isEmpty();
154160
}

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.mongodb.client.MongoDatabase;
4444
import com.mongodb.client.model.IndexOptions;
4545
import com.mongodb.client.model.Indexes;
46+
import com.mongodb.client.model.Updates;
4647
import com.mongodb.client.result.DeleteResult;
4748
import com.mongodb.client.result.UpdateResult;
4849
import com.mongodb.util.JSON;
@@ -137,7 +138,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
137138
*
138139
* @param dataBaseName
139140
* @param collectionName
140-
* @param condition string json
141+
* @param condition a condition to find a requested object in the database
141142
* @return
142143
*/
143144
public ArrayList<String> find(String dataBaseName, String collectionName, String condition) {
@@ -342,4 +343,47 @@ public void dropDatabase(String databaseName) {
342343
MongoDatabase db = mongoClient.getDatabase(databaseName);
343344
db.drop();
344345
}
346+
347+
/**
348+
* Check if the document exists
349+
* @param databaseName
350+
* @param collectionName
351+
* @param condition a condition to find a requested object in the database
352+
* @return
353+
*/
354+
public boolean checkDocumentExists(String databaseName, String collectionName, String condition) {
355+
MongoDatabase db = mongoClient.getDatabase(databaseName);
356+
MongoCollection<Document> mongoCollection = db.getCollection(collectionName);
357+
Document doc = mongoCollection.find(BasicDBObject.parse(condition)).first();
358+
if (doc == null || doc.isEmpty()) {
359+
return false;
360+
}
361+
return true;
362+
}
363+
364+
365+
/**
366+
* Update the existing documents with unique objects list
367+
* Used only in EventToObjectMapHandler.java
368+
* @param dataBaseName
369+
* @param collectionName
370+
* @param condition a condition to find a requested object in the database
371+
* @param eventId eventId to update in the mapper collection
372+
* @return
373+
*/
374+
public boolean updateDocumentAddToSet(String dataBaseName, String collectionName, String condition, String eventId) {
375+
try {
376+
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
377+
if (collection != null) {
378+
final Document dbObjectInput = Document.parse(condition);
379+
UpdateResult updateMany = collection.updateOne(dbObjectInput, Updates.addToSet("objects", eventId));
380+
LOGGER.debug("updateDocument() :: database: {} and collection: {} is document Updated : {}", dataBaseName, collectionName, updateMany.wasAcknowledged());
381+
return updateMany.wasAcknowledged();
382+
}
383+
} catch (Exception e) {
384+
LOGGER.error("Failed to update document.", e);
385+
}
386+
387+
return false;
388+
}
345389
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public JsonNode runRuleOnEvent(String rule, String event) {
7474
Expression<JsonNode> expression = jmespath.compile(rule);
7575
JsonNode eventJson = objectMapper.readValue(event, JsonNode.class);
7676
result = expression.search(eventJson);
77+
LOGGER.debug("Expression : {} \n RESULT VALUE FROM JMESPATH : {}" , expression, result);
7778
} catch (Exception e) {
7879
LOGGER.error("Failed to run rule on event.\nRule: {}\nEvent: {}", rule, event, e);
7980
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public String mergeObject(String id, String mergeId, RulesObject rules, String e
6767
try {
6868
// lock and get the AggregatedObject
6969
String aggregatedObject = getAggregatedObject(id, true);
70+
LOGGER.debug("AGGREGATED OBJECT : " + aggregatedObject);
7071
String mergeRule = getMergeRules(rules);
7172
if (mergeRule != null && !mergeRule.isEmpty()) {
7273
String updatedRule = replaceIdMarkerInRules(mergeRule, mergeId);
7374
String ruleForMerge = jmesPathInterface.runRuleOnEvent(updatedRule, event).toString();
7475
String mergePath = prepareMergePrepareObject.getMergePath(aggregatedObject, ruleForMerge, false);
7576
preparedToMergeObject = prepareMergePrepareObject.addMissingLevels(aggregatedObject,
7677
objectToMerge.toString(), ruleForMerge, mergePath);
78+
LOGGER.debug("PREPARE TO MERGE OBJECT : " + preparedToMergeObject);
7779
} else {
7880
preparedToMergeObject = objectToMerge.toString();
7981
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class WaitListStorageHandler {
7171
public void addEventToWaitListIfNotExisting(String event, RulesObject rulesObject) {
7272
try {
7373
JsonNode id = extractIdFromEventUsingRules(event, rulesObject);
74-
String foundEvent = findEventInWaitList(id);
74+
String foundEvent = findEventInWaitList(id.textValue());
7575
if (foundEvent.isEmpty()) {
7676
Date date = createCurrentTimeStamp();
7777
BasicDBObject document = createWaitListDocument(event, id, date);
@@ -82,7 +82,7 @@ public void addEventToWaitListIfNotExisting(String event, RulesObject rulesObjec
8282
}
8383
}
8484

85-
private String findEventInWaitList(JsonNode id) {
85+
private String findEventInWaitList(String id) {
8686
String condition = "{\"_id\" : \"" + id + "\"}";
8787
List<String> foundEventsInWaitList = mongoDbHandler.find(databaseName, collectionName, condition);
8888
if (foundEventsInWaitList.isEmpty()) {

src/test/java/com/ericsson/ei/handlers/test/MongoDBHandlerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ public class MongoDBHandlerTest {
3737

3838
private String dataBaseName = "MongoDBHandlerTestDB";
3939
private String collectionName = "SampleEvents";
40+
private String mapCollectionName = "SampleEventObjectMap";
4041
private String input = "{\"id\":\"eventId\",\"type\":\"eventType11\",\"test_cases\" : [{\"event_id\" : \"testcaseid1\", \"test_data\" : \"testcase1data\"},{\"event_id\" : \"testcaseid2\", \"test_data\" : \"testcase2data\"}]}";
4142
private String updateInput = "{\"id\":\"eventId\",\"type\":\"eventType11\",\"test_cases\" : [{\"event_id\" : \"testcaseid1\", \"test_data\" : \"testcase2data\"},{\"event_id\" : \"testcaseid3\", \"test_data\" : \"testcase3data\"}]}";
4243
private String condition = "{\"test_cases.event_id\" : \"testcaseid1\"}";
44+
45+
//Added to test new functionality for EventToObjectMapHandler
46+
private String conditionForEventToObjectMap = "{\"_id\" : \"testid1\"}";
47+
private String inputForEventToObjectMap = "{\"_id\" : \"testid1\", \"objects\" : [\"eventid1\", \"eventid2\"]}";
48+
private String updateInputForEventToObjectMap = "\"eventid3\"";
4349

4450
@Before
4551
public void init() throws Exception {
4652
TestConfigs.init();
4753
mongoDBHandler = new MongoDBHandler();
4854
mongoDBHandler.setMongoClient(TestConfigs.getMongoClient());
4955
mongoDBHandler.insertDocument(dataBaseName, collectionName, input);
56+
mongoDBHandler.insertDocument(dataBaseName, mapCollectionName, inputForEventToObjectMap);
5057
}
5158

5259
@Test
@@ -69,5 +76,17 @@ public void testUpdateDocument() {
6976
@After
7077
public void dropCollection() {
7178
assertTrue(mongoDBHandler.dropDocument(dataBaseName, collectionName, condition));
79+
mongoDBHandler.dropCollection(dataBaseName, mapCollectionName);
80+
}
81+
82+
//Added test cases for EventToObjectMapHandler
83+
@Test
84+
public void checkDocument() {
85+
assertTrue(mongoDBHandler.checkDocumentExists(dataBaseName, mapCollectionName, conditionForEventToObjectMap));
86+
}
87+
88+
@Test
89+
public void updateEventToObjectMap() {
90+
assertTrue(mongoDBHandler.updateDocumentAddToSet(dataBaseName, mapCollectionName, conditionForEventToObjectMap, updateInputForEventToObjectMap));
7291
}
7392
}

0 commit comments

Comments
 (0)