Skip to content

Commit f0502b5

Browse files
Refactor class and function names (#283)
Major refactoring of SubscriptionRepeatDbHandler. Add javadocs in Objecthandler. Improved log messages.
1 parent 865d487 commit f0502b5

39 files changed

+359
-331
lines changed

src/functionaltests/java/com/ericsson/ei/notifications/ttl/TestTTLSteps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.test.context.TestPropertySource;
2626
import org.springframework.util.SocketUtils;
2727

28-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
28+
import com.ericsson.ei.handlers.MongoDBHandler;
2929
import com.ericsson.ei.subscription.InformSubscriber;
3030
import com.ericsson.ei.utils.FunctionalTestBase;
3131
import com.ericsson.ei.utils.HttpRequest;

src/functionaltests/java/com/ericsson/ei/query/QueryAggregatedObjectsTestSteps.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.http.HttpStatus;
1919
import org.springframework.http.ResponseEntity;
2020

21-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
21+
import com.ericsson.ei.handlers.MongoDBHandler;
2222
import com.ericsson.ei.utils.FunctionalTestBase;
2323
import com.ericsson.ei.utils.HttpRequest;
2424
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
@@ -93,17 +93,14 @@ public void aggregated_object_is_created() throws Throwable {
9393

9494
boolean aggregatedObjectExists = aggregatedObject.size() > 0;
9595
if (!aggregatedObjectExists) {
96-
aggregatedObjectExists = createDocumentInMongoDb(eiDatabaseName, aggrCollectionName, aggrObj);
96+
createDocumentInMongoDb(eiDatabaseName, aggrCollectionName, aggrObj);
9797
}
98-
99-
assertEquals(true, aggregatedObjectExists);
10098
}
10199

102100
@Given("^Missed Notification object is created$")
103101
public void missed_notification_object_is_created() throws Throwable {
104102
LOGGER.debug("Missed Notification object has been created in MongoDb");
105-
assertEquals(true, createDocumentInMongoDb(missedNotificationDatabaseName, missedNotificationCollectionName,
106-
missedNotificationObj));
103+
createDocumentInMongoDb(missedNotificationDatabaseName, missedNotificationCollectionName, missedNotificationObj);
107104
}
108105

109106
@Then("^Perform valid query on created Aggregated object")
@@ -474,9 +471,9 @@ public void perform_query_to_retrieve_and_filter_out_confidence_level_informatio
474471
* successfully created in database.
475472
*
476473
*/
477-
private boolean createDocumentInMongoDb(String databaseName, String collectionName, String objToBeInserted) {
474+
private void createDocumentInMongoDb(String databaseName, String collectionName, String objToBeInserted) {
478475
LOGGER.debug("Inserting Object to MongoDb.\nDatabase: " + databaseName + "\nCollection: " + collectionName
479476
+ "\nDocument to be inserted\n: " + objToBeInserted);
480-
return mongoDBHandler.insertDocument(databaseName, collectionName, objToBeInserted);
477+
mongoDBHandler.insertDocument(databaseName, collectionName, objToBeInserted);
481478
}
482479
}

src/functionaltests/java/com/ericsson/ei/subscriptions/repeatHandler/SubscriptionRepeatHandlerSteps.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.springframework.http.ResponseEntity;
2121

2222
import com.ericsson.ei.controller.model.Subscription;
23-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
23+
import com.ericsson.ei.handlers.MongoDBHandler;
2424
import com.ericsson.ei.rules.RulesHandler;
2525
import com.ericsson.ei.services.ISubscriptionService;
2626
import com.ericsson.ei.subscription.RunSubscription;
@@ -82,8 +82,8 @@ public class SubscriptionRepeatHandlerSteps extends FunctionalTestBase {
8282

8383
@Before("@SubscriptionRepeatTrue or @SubscriptionRepeatFalse")
8484
public void beforeScenario() throws IOException, JSONException {
85-
assertTrue(mongoDBHandler.insertDocument(dataBaseName, collectionName,
86-
eventManager.getJSONFromFile(AGGREGATED_OBJECT_FILE_PATH).toString()));
85+
mongoDBHandler.insertDocument(dataBaseName, collectionName,
86+
eventManager.getJSONFromFile(AGGREGATED_OBJECT_FILE_PATH).toString());
8787
subscriptionStrWithOneMatch = FileUtils.readFileToString(
8888
new File(REPEAT_FLAG_SUBSCRIPTION_COLLECTIONS_WITH_ONE_MATCH), "UTF-8");
8989
subscriptionStrWithTwoMatch = FileUtils.readFileToString(
@@ -179,7 +179,7 @@ private String getAggregatedObjectId(List<String> resultRepeatFlagHandler, int i
179179
*/
180180
private void processSubscription(String subscriptionStrValue, JSONObject subscriptionObject) throws IOException {
181181
Subscription subscription = mapper.readValue(subscriptionObject.toString(), Subscription.class);
182-
assertTrue(subscriptionService.addSubscription(subscription));
182+
subscriptionService.addSubscription(subscription);
183183
String expectedSubscriptionName = subscription.getSubscriptionName();
184184
JsonNode subscriptionJson = mapper.readTree(subscriptionStrValue);
185185
ArrayNode requirementNode = (ArrayNode) subscriptionJson.get("requirements");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.springframework.beans.factory.annotation.Autowired;
1010
import org.springframework.stereotype.Component;
1111

12-
import com.ericsson.ei.rmqhandler.RmqHandler;
12+
import com.ericsson.ei.handlers.RmqHandler;
1313
import com.fasterxml.jackson.databind.JsonNode;
1414
import com.fasterxml.jackson.databind.ObjectMapper;
1515

src/integrationtests/java/util/IntegrationTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.springframework.http.ResponseEntity;
3838
import org.springframework.test.context.support.AbstractTestExecutionListener;
3939

40-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
40+
import com.ericsson.ei.handlers.MongoDBHandler;
4141
import com.ericsson.ei.utils.HttpRequest;
4242
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
4343
import com.fasterxml.jackson.databind.JsonNode;

src/main/java/com/ericsson/ei/config/scope/register/SpringBeanScopeRegisterConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SpringBeanScopeRegisterConfig {
1313

1414
@Bean
1515
public static BeanFactoryPostProcessor beanFactoryPostProcessor() {
16-
return new CustomScopeRegisteringBeanFactoryPostProcessor();
16+
return new ThreadScopeConfig();
1717
}
1818

1919
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import org.springframework.context.support.SimpleThreadScope;
77

88
/**
9-
* This class for the extend the "request" bean scope to thread level
10-
* This "thread" scope is implemented to EventHandler and RuleHandler
9+
* This class extends the "request" bean scope to thread level
10+
* This "thread" scope is implemented for EventHandler and RuleHandler
1111
* The spring provided "request" bean scope will work only request level, threads are not access the request bean scope without request
1212
* Using the SimpleThreadScope custom scope(scope name "thread") extend the request scope to threads.
1313
*
1414
*/
15-
public class CustomScopeRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
15+
public class ThreadScopeConfig implements BeanFactoryPostProcessor {
1616
@Override
1717
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
1818
beanFactory.registerScope("thread", new SimpleThreadScope());

src/main/java/com/ericsson/ei/controller/model/ParseInstanceInfoEI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.ericsson.ei.erqueryservice.ERQueryService;
2020
import com.ericsson.ei.handlers.ObjectHandler;
21-
import com.ericsson.ei.rmqhandler.RmqHandler;
21+
import com.ericsson.ei.handlers.RmqHandler;
2222
import com.ericsson.ei.subscription.InformSubscriber;
2323
import com.ericsson.ei.subscription.SendMail;
2424
import com.ericsson.ei.subscription.SubscriptionHandler;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.stereotype.Component;
2727

2828
import com.ericsson.ei.jmespath.JmesPathInterface;
29-
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
3029
import com.ericsson.ei.rules.RulesObject;
3130
import com.fasterxml.jackson.core.type.TypeReference;
3231
import com.fasterxml.jackson.databind.JsonNode;
@@ -136,10 +135,10 @@ public ArrayList<String> getEventToObjectList(String eventId) {
136135
}
137136
return list;
138137
}
139-
138+
140139
/**
141140
* The method is responsible for the delete the EventObjectMap by using the suffix template Name
142-
*
141+
*
143142
* @param templateName
144143
* @return boolean
145144
*/

src/main/java/com/ericsson/ei/mongodbhandler/MongoDBHandler.java renamed to src/main/java/com/ericsson/ei/handlers/MongoDBHandler.java

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
See the License for the specific language governing permissions and
1212
limitations under the License.
1313
*/
14-
package com.ericsson.ei.mongodbhandler;
14+
package com.ericsson.ei.handlers;
1515

1616
import java.util.ArrayList;
1717
import java.util.Collections;
@@ -21,6 +21,7 @@
2121
import javax.annotation.PostConstruct;
2222
import javax.annotation.PreDestroy;
2323

24+
import com.mongodb.*;
2425
import org.apache.commons.lang3.StringUtils;
2526
import org.bson.Document;
2627
import org.slf4j.Logger;
@@ -30,13 +31,6 @@
3031
import org.springframework.stereotype.Component;
3132

3233
import com.fasterxml.jackson.annotation.JsonIgnore;
33-
import com.mongodb.BasicDBObject;
34-
import com.mongodb.Block;
35-
import com.mongodb.MongoClient;
36-
import com.mongodb.MongoCommandException;
37-
import com.mongodb.MongoCredential;
38-
import com.mongodb.MongoWriteException;
39-
import com.mongodb.ServerAddress;
4034
import com.mongodb.client.MongoCollection;
4135
import com.mongodb.client.MongoDatabase;
4236
import com.mongodb.client.model.IndexOptions;
@@ -50,7 +44,7 @@
5044

5145
@Component
5246
public class MongoDBHandler {
53-
static Logger log = LoggerFactory.getLogger(MongoDBHandler.class);
47+
private static Logger LOGGER = LoggerFactory.getLogger(MongoDBHandler.class);
5448

5549
@Autowired
5650
private MongoProperties mongoProperties;
@@ -66,7 +60,7 @@ public class MongoDBHandler {
6660
public void init() {
6761
createConnection();
6862
}
69-
63+
7064
@PreDestroy
7165
public void close() {
7266
mongoClient.close();
@@ -93,21 +87,14 @@ private void createConnection() {
9387
* @param input json String
9488
* @return
9589
*/
96-
public boolean insertDocument(String dataBaseName, String collectionName, String input) {
97-
try {
98-
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
99-
if (collection != null) {
100-
final Document dbObjectInput = Document.parse(input);
101-
collection.insertOne(dbObjectInput);
102-
log.debug("Object : " + input);
103-
log.debug("inserted successfully in ");
104-
log.debug("collection : " + collectionName + "and db : " + dataBaseName);
105-
return true;
106-
}
107-
} catch (MongoWriteException e) {
108-
log.error(e.getMessage(), e);
90+
public void insertDocument(String dataBaseName, String collectionName, String input) throws MongoWriteException {
91+
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
92+
if (collection != null) {
93+
final Document dbObjectInput = Document.parse(input);
94+
collection.insertOne(dbObjectInput);
95+
LOGGER.debug("Object: " + input + "\n was inserted successfully in collection: \n"
96+
+ collectionName + " and database " + dataBaseName + ".");
10997
}
110-
return false;
11198
}
11299

113100
/**
@@ -128,12 +115,12 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
128115
if (result.size() != 0) {
129116
// This will pass about 10 times/second and most of the times DB will be empty,
130117
// this is normal, no need to log
131-
log.debug("getAllDocuments() :: database: " + dataBaseName + " and collection: " + collectionName
118+
LOGGER.debug("getAllDocuments() :: database: " + dataBaseName + " and collection: " + collectionName
132119
+ " fetched No of :" + result.size());
133120
}
134121
}
135122
} catch (Exception e) {
136-
log.error(e.getMessage(), e);
123+
LOGGER.error(e.getMessage(), e);
137124
}
138125
return result;
139126
}
@@ -149,7 +136,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
149136
public ArrayList<String> find(String dataBaseName, String collectionName, String condition) {
150137
ArrayList<String> result = new ArrayList<>();
151138

152-
log.debug("Find and retrieve data from database." + "\nDatabase: " + dataBaseName + "\nCollection: "
139+
LOGGER.debug("Find and retrieve data from database." + "\nDatabase: " + dataBaseName + "\nCollection: "
153140
+ collectionName + "\nCondition/Query: " + condition);
154141

155142
try {
@@ -159,17 +146,17 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
159146
result.add(JSON.serialize(document));
160147
});
161148
if (result.size() != 0) {
162-
log.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
149+
LOGGER.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
163150
+ " fetched No of :" + result.size());
164151
} else {
165-
log.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
152+
LOGGER.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
166153
+ " documents are not found");
167154
}
168155
} else {
169-
log.debug("Collection " + collectionName + " is empty in database " + dataBaseName);
156+
LOGGER.debug("Collection " + collectionName + " is empty in database " + dataBaseName);
170157
}
171158
} catch (Exception e) {
172-
log.error(e.getMessage(), e);
159+
LOGGER.error(e.getMessage(), e);
173160
}
174161

175162
return result;
@@ -192,12 +179,12 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
192179
final Document dbObjectInput = Document.parse(input);
193180
final Document dbObjectUpdateInput = Document.parse(updateInput);
194181
UpdateResult updateMany = collection.replaceOne(dbObjectInput, dbObjectUpdateInput);
195-
log.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
182+
LOGGER.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
196183
+ " is document Updated :" + updateMany.wasAcknowledged());
197184
return updateMany.wasAcknowledged();
198185
}
199186
} catch (Exception e) {
200-
log.error(e.getMessage(), e);
187+
LOGGER.error(e.getMessage(), e);
201188
}
202189

203190
return false;
@@ -221,13 +208,13 @@ public Document findAndModify(String dataBaseName, String collectionName, String
221208
final Document dbObjectUpdateInput = Document.parse(updateInput);
222209
Document result = collection.findOneAndUpdate(dbObjectInput, dbObjectUpdateInput);
223210
if (result != null) {
224-
log.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
211+
LOGGER.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
225212
+ " updated successfully");
226213
return result;
227214
}
228215
}
229216
} catch (Exception e) {
230-
log.error(e.getMessage(), e);
217+
LOGGER.error(e.getMessage(), e);
231218
}
232219
return null;
233220
}
@@ -247,17 +234,17 @@ public boolean dropDocument(String dataBaseName, String collectionName, String c
247234
final Document dbObjectCondition = Document.parse(condition);
248235
DeleteResult deleteMany = collection.deleteMany(dbObjectCondition);
249236
if (deleteMany.getDeletedCount() > 0) {
250-
log.debug("database" + dataBaseName + " and collection: " + collectionName
237+
LOGGER.debug("database" + dataBaseName + " and collection: " + collectionName
251238
+ " deleted No.of records " + deleteMany.getDeletedCount());
252239
return true;
253240
} else {
254-
log.debug("database " + dataBaseName + " and collection: " + collectionName
241+
LOGGER.debug("database " + dataBaseName + " and collection: " + collectionName
255242
+ " No documents found to delete");
256243
return false;
257244
}
258245
}
259246
} catch (Exception e) {
260-
log.error(e.getMessage(), e);
247+
LOGGER.error(e.getMessage(), e);
261248
}
262249
return false;
263250
}
@@ -282,19 +269,19 @@ private MongoCollection<Document> getMongoCollection(String dataBaseName, String
282269
MongoDatabase db = mongoClient.getDatabase(dataBaseName);
283270
List<String> collectionList = db.listCollectionNames().into(new ArrayList<String>());
284271
if (!collectionList.contains(collectionName)) {
285-
log.debug("The requested database(" + dataBaseName + ") / collection(" + collectionName
272+
LOGGER.debug("The requested database(" + dataBaseName + ") / collection(" + collectionName
286273
+ ") not available in mongodb, Creating ........");
287274
try {
288275
db.createCollection(collectionName);
289276
} catch (MongoCommandException e) {
290277
String message = "collection '" + dataBaseName + "." + collectionName + "' already exists";
291278
if (e.getMessage().contains(message)) {
292-
log.warn("A " + message + ".");
279+
LOGGER.warn("A " + message + ".");
293280
} else {
294281
throw e;
295282
}
296283
}
297-
log.debug("done....");
284+
LOGGER.debug("done....");
298285
}
299286
MongoCollection<Document> collection = db.getCollection(collectionName);
300287
return collection;

0 commit comments

Comments
 (0)