Skip to content

Commit 6a3a9b9

Browse files
Improve /information endpoint response (#434)
* Throw mocked response instead of null to avoid multiple NullPointerExceptions in tests * /information endpoint response now reflects name changes in application.properties * Update java doc + spelling in some places * Add constants for keys used in MongoDB document
1 parent 06bd1ef commit 6a3a9b9

File tree

17 files changed

+94
-91
lines changed

17 files changed

+94
-91
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@
4141
import lombok.Getter;
4242

4343
/**
44-
* Parsing all classes which contains value annotation in eiffel-intelligence plugin. Needed for
45-
* generate Json file with information about backend instance.
44+
* Parsing all classes which contains value annotation in Eiffel Intelligence. Needed for
45+
* generating JSON file with information about backend instance.
4646
*/
4747
@Component
4848
public class ParseInstanceInfoEI {
4949
@Autowired
5050
private Environment environment;
5151

52-
@Getter
53-
@Value("${build.version:#{null}}")
54-
private String applicationPropertiesVersion;
55-
5652
@Getter
5753
private String version;
5854

@@ -65,7 +61,7 @@ public class ParseInstanceInfoEI {
6561

6662
@Getter
6763
@Value("${test.aggregation.enabled:false}")
68-
private String testRulesEnabled;
64+
private String testAggregationEnabled;
6965

7066
@Getter
7167
@Autowired
@@ -109,7 +105,7 @@ public class ParseInstanceInfoEI {
109105

110106
@Getter
111107
@Autowired
112-
private ERQueryService erUrl;
108+
private ERQueryService eventRepository;
113109

114110
@PostConstruct
115111
public void init() throws IOException {
@@ -164,7 +160,7 @@ public void init() throws IOException {
164160
private class LdapValues {
165161
@Getter
166162
@Value("${ldap.enabled}")
167-
private String enabled;
163+
private String ldapEnabled;
168164

169165
@Getter
170166
private String ldapServerList;

src/main/java/com/ericsson/ei/erqueryservice/ERQueryService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class ERQueryService {
5151

5252
@Getter
5353
@Value("${event.repository.url}")
54-
private String erBaseUrl;
54+
private String eventRepositoryUrl;
5555

5656
public ERQueryService() {
5757
this.request = new HttpRequest();
@@ -90,7 +90,7 @@ public ResponseEntity getEventStreamDataById(String eventId, SearchOption search
9090
private ResponseEntity sendRequestToER(String eventId, SearchOption searchOption, int limit,
9191
int levels, boolean tree) throws IOException, URISyntaxException,
9292
ClientProtocolException, PropertyNotFoundException {
93-
if (StringUtils.isBlank(erBaseUrl)) {
93+
if (StringUtils.isBlank(eventRepositoryUrl)) {
9494
throw new PropertyNotFoundException("The URL to ER is not provided");
9595
}
9696

@@ -103,7 +103,7 @@ private void prepareRequest(String eventId, SearchOption searchOption, int limit
103103
final SearchParameters searchParameters = getSearchParameters(searchOption);
104104
request
105105
.setHttpMethod(HttpMethod.POST)
106-
.setBaseUrl(erBaseUrl)
106+
.setBaseUrl(eventRepositoryUrl)
107107
.setEndpoint(eventId)
108108
.addParam("limit", Integer.toString(limit))
109109
.addParam("levels", Integer.toString(levels))

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public class DateUtils {
1414

1515
/**
1616
* This method creates the date object with the current date for appending
17-
* in the document object before inserting it into mongoDB.
17+
* in the document object before inserting it into Mongo DB.
1818
*/
1919
public static Date getDate() throws ParseException {
20-
Date date = new Date();
21-
try {
22-
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
23-
String time = dateFormat.format(date);
24-
date = dateFormat.parse(time);
25-
} catch (Exception e) {
26-
LOGGER.error("Failed to create date/time object.", e);
27-
}
28-
return date;
20+
Date date = new Date();
21+
try {
22+
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
23+
String time = dateFormat.format(date);
24+
date = dateFormat.parse(time);
25+
} catch (Exception e) {
26+
LOGGER.error("Failed to create date/time object.", e);
27+
}
28+
return date;
2929
}
3030
}

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23+
import com.ericsson.ei.mongo.*;
24+
import org.apache.commons.lang3.StringUtils;
2325
import org.bson.Document;
2426
import org.slf4j.Logger;
2527
import org.slf4j.LoggerFactory;
@@ -28,10 +30,6 @@
2830
import org.springframework.stereotype.Component;
2931

3032
import com.ericsson.ei.jmespath.JmesPathInterface;
31-
import com.ericsson.ei.mongo.MongoCondition;
32-
import com.ericsson.ei.mongo.MongoDBHandler;
33-
import com.ericsson.ei.mongo.MongoQuery;
34-
import com.ericsson.ei.mongo.MongoQueryBuilder;
3533
import com.ericsson.ei.rules.RulesObject;
3634
import com.ericsson.ei.subscription.SubscriptionHandler;
3735
import com.fasterxml.jackson.databind.JsonNode;
@@ -46,22 +44,24 @@
4644
@Component
4745
public class ObjectHandler {
4846

49-
private static final String NOT_LOCKED = "0";
50-
5147
private static final int MAX_RETRY_COUNT = 1000;
5248

5349
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectHandler.class);
5450

5551
@Getter
5652
@Setter
5753
@Value("${aggregations.collection.name}")
58-
private String collectionName;
54+
private String aggregationsCollectionName;
5955

6056
@Getter
6157
@Setter
6258
@Value("${spring.data.mongodb.database}")
6359
private String databaseName;
6460

61+
@Getter
62+
@Value("${aggregations.collection.ttl}")
63+
private String aggregationsTtl;
64+
6565
@Setter
6666
@Autowired
6767
private MongoDBHandler mongoDbHandler;
@@ -78,10 +78,6 @@ public class ObjectHandler {
7878
@Autowired
7979
private SubscriptionHandler subscriptionHandler;
8080

81-
@Getter
82-
@Value("${aggregations.collection.ttl}")
83-
private String ttlValue;
84-
8581
/**
8682
* This method is responsible for inserting an aggregated object in to the database.
8783
*
@@ -103,10 +99,11 @@ public void insertObject(String aggregatedObject, RulesObject rulesObject, Strin
10399
document.toString());
104100

105101
if (getTtl() > 0) {
106-
mongoDbHandler.createTTLIndex(databaseName, collectionName, "Time", getTtl());
102+
mongoDbHandler.createTTLIndex(databaseName, aggregationsCollectionName,
103+
MongoConstants.TIME, getTtl());
107104
}
108105

109-
mongoDbHandler.insertDocument(databaseName, collectionName, document.toString());
106+
mongoDbHandler.insertDocument(databaseName, aggregationsCollectionName, document.toString());
110107
postInsertActions(aggregatedObject, rulesObject, event, id);
111108
}
112109

@@ -138,7 +135,7 @@ public void updateObject(String aggregatedObject, RulesObject rulesObject, Strin
138135
BasicDBObject document = prepareDocumentForInsertion(id, aggregatedObject);
139136
final MongoCondition condition = MongoCondition.idCondition(id);
140137
String documentStr = document.toString();
141-
mongoDbHandler.updateDocument(databaseName, collectionName, condition, documentStr);
138+
mongoDbHandler.updateDocument(databaseName, aggregationsCollectionName, condition, documentStr);
142139
postInsertActions(aggregatedObject, rulesObject, event, id);
143140
}
144141

@@ -154,7 +151,7 @@ public void updateObject(JsonNode aggregatedObject, RulesObject rulesObject, Str
154151
* @return List of documents
155152
*/
156153
public List<String> findObjectsByCondition(MongoQuery query) throws MongoClientException {
157-
return mongoDbHandler.find(databaseName, collectionName, query);
154+
return mongoDbHandler.find(databaseName, aggregationsCollectionName, query);
158155
}
159156

160157
/**
@@ -198,10 +195,10 @@ public List<String> findObjectsByIds(List<String> ids) {
198195
*/
199196
public BasicDBObject prepareDocumentForInsertion(String id, String object) {
200197
BasicDBObject document = BasicDBObject.parse(object);
201-
document.put("_id", id);
198+
document.put(MongoConstants.ID, id);
202199
try {
203200
if (getTtl() > 0) {
204-
document.put("Time", DateUtils.getDate());
201+
document.put(MongoConstants.TIME, DateUtils.getDate());
205202
}
206203
} catch (ParseException e) {
207204
LOGGER.error("Failed to attach date to document.", e);
@@ -216,7 +213,7 @@ public BasicDBObject prepareDocumentForInsertion(String id, String object) {
216213
* @return id
217214
*/
218215
public String extractObjectId(JsonNode aggregatedDbObject) {
219-
return aggregatedDbObject.get("_id").textValue();
216+
return aggregatedDbObject.get(MongoConstants.ID).textValue();
220217
}
221218

222219
/**
@@ -233,7 +230,7 @@ public String lockDocument(String id) {
233230
String setLock = "{ \"$set\" : { \"lock\" : \"1\"}}";
234231
ObjectMapper mapper = new ObjectMapper();
235232

236-
final MongoCondition lockNotSet = MongoCondition.lockCondition(NOT_LOCKED);
233+
final MongoCondition lockNotSet = MongoCondition.lockCondition(MongoConstants.NOT_LOCKED);
237234
final MongoCondition noLock = MongoCondition.lockNullCondition();
238235
MongoQuery idAndNoLockCondition = MongoQueryBuilder.buildOr(lockNotSet, noLock)
239236
.append(MongoCondition.idCondition(id));
@@ -251,7 +248,8 @@ public String lockDocument(String id) {
251248
while (documentLocked && retryCounter < MAX_RETRY_COUNT) {
252249
try {
253250
JsonNode documentJson = mapper.readValue(setLock, JsonNode.class);
254-
Document result = mongoDbHandler.findAndModify(databaseName, collectionName,
251+
Document result = mongoDbHandler.findAndModify(databaseName,
252+
aggregationsCollectionName,
255253
idAndNoLockCondition,
256254
documentJson.toString());
257255
if (result != null) {
@@ -278,9 +276,9 @@ public String lockDocument(String id) {
278276
*/
279277
public int getTtl() {
280278
int ttl = 0;
281-
if (ttlValue != null && !ttlValue.isEmpty()) {
279+
if (StringUtils.isNotEmpty(aggregationsTtl)) {
282280
try {
283-
ttl = Integer.parseInt(ttlValue);
281+
ttl = Integer.parseInt(aggregationsTtl);
284282
} catch (NumberFormatException e) {
285283
LOGGER.error("Failed to parse TTL value.", e);
286284
}
@@ -290,7 +288,8 @@ public int getTtl() {
290288

291289
private boolean isInvalidId(String id) {
292290
final MongoCondition idCondition = MongoCondition.idCondition(id);
293-
ArrayList<String> documentExistsCheck = mongoDbHandler.find(databaseName, collectionName,
291+
ArrayList<String> documentExistsCheck = mongoDbHandler.find(databaseName,
292+
aggregationsCollectionName,
294293
idCondition);
295294

296295
return documentExistsCheck.isEmpty();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class RMQProperties {
7676
@Getter
7777
@Setter
7878
@Value("${rabbitmq.waitlist.queue.suffix}")
79-
private String waitlistSuffix;
79+
private String waitlistQueueSuffix;
8080

8181
@Getter
8282
@Setter
@@ -96,6 +96,6 @@ public String getQueueName() {
9696
public String getWaitlistQueueName() {
9797
final String durableName = this.queueDurable ? "durable" : "transient";
9898
return this.domainId + "." + this.componentName + "." + this.queueSuffix + "." + durableName + "."
99-
+ this.waitlistSuffix;
99+
+ this.waitlistQueueSuffix;
100100
}
101101
}

src/main/java/com/ericsson/ei/mongo/MongoCondition.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
public class MongoCondition implements MongoQuery {
2121

22-
private static final String ID = "_id";
2322
private static final String LOCK = "lock";
2423
private static final String SUBSCRIPTION_ID = "subscriptionId";
2524
private static final String SUBSCRIPTION_NAME = "subscriptionName";
@@ -37,7 +36,7 @@ public class MongoCondition implements MongoQuery {
3736
* @return A MongoCondition with id set
3837
*/
3938
public static MongoCondition idCondition(String documentId) {
40-
return condition(ID, documentId);
39+
return condition(MongoConstants.ID, documentId);
4140
}
4241

4342
/**
@@ -96,7 +95,7 @@ public static MongoCondition ldapUserNameCondition(String ldapUserName) {
9695
* <p>
9796
* <code>{"lock":"0"}
9897
*
99-
* @param lockValue
98+
* @param lock
10099
* @return A MongoCondition with lock set
101100
*/
102101
public static MongoCondition lockCondition(String lock) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.ericsson.ei.mongo;
2+
3+
public class MongoConstants {
4+
public static final String ID = "_id";
5+
public static final String EVENT = "Event";
6+
public static final String TIME = "Time";
7+
public static final String NOT_LOCKED = "0";
8+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public void dropCollection(String dataBaseName, String collectionName) {
241241
/**
242242
* This method is used to drop a database. For example after testing.
243243
*
244-
* @param dataBaseName to know which database to remove
244+
* @param databaseName to know which database to remove
245245
*/
246246
public void dropDatabase(String databaseName) {
247247
MongoDatabase db = mongoClient.getDatabase(databaseName);

src/main/java/com/ericsson/ei/notifications/InformSubscriber.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.mail.internet.MimeMessage;
2222

23+
import com.ericsson.ei.mongo.MongoConstants;
2324
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
2425
import org.slf4j.Logger;
2526
import org.slf4j.LoggerFactory;
@@ -60,7 +61,7 @@ public class InformSubscriber {
6061
@Setter
6162
@Getter
6263
@Value("${notification.retry:#{0}}")
63-
private int failAttempt;
64+
private int notificationRetry;
6465

6566
@Getter
6667
@Value("${failed.notifications.collection.name}")
@@ -72,7 +73,7 @@ public class InformSubscriber {
7273

7374
@Getter
7475
@Value("${failed.notifications.collection.ttl}")
75-
private int ttlValue;
76+
private int failedNotificationsTtl;
7677

7778
@Autowired
7879
private JmesPathInterface jmespath;
@@ -132,7 +133,7 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
132133
"Failed to inform subscriber '{}'\nPrepared 'failed notification' document : {}",
133134
e.getMessage(), failedNotification);
134135
mongoDBHandler.createTTLIndex(database,
135-
failedNotificationCollectionName, "time", ttlValue);
136+
failedNotificationCollectionName, MongoConstants.TIME, failedNotificationsTtl);
136137
saveFailedNotificationToDB(failedNotification);
137138
}
138139
}
@@ -161,7 +162,7 @@ private void makeHTTPRequests(HttpRequest request)
161162
}
162163
LOGGER.debug("After trying for {} time(s), the result is : {}", requestTries,
163164
exception != null);
164-
} while (exception != null && requestTries <= failAttempt);
165+
} while (exception != null && requestTries <= notificationRetry);
165166

166167
if (exception != null) {
167168
String errorMessage = "Failed to send REST/POST notification!";
@@ -197,7 +198,7 @@ private String prepareFailedNotification(String aggregatedObject, String subscri
197198
document.put("subscriptionName", subscriptionName);
198199
document.put("notificationMeta", notificationMeta);
199200
try {
200-
document.put("time", DateUtils.getDate());
201+
document.put(MongoConstants.TIME, DateUtils.getDate());
201202
} catch (ParseException e) {
202203
LOGGER.error("Failed to get date object.", e);
203204
}

src/main/java/com/ericsson/ei/queryservice/ProcessQueryParams.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package com.ericsson.ei.queryservice;
1515

16+
import com.ericsson.ei.mongo.MongoConstants;
1617
import org.json.JSONArray;
1718
import org.json.JSONException;
1819
import org.json.JSONObject;
@@ -102,7 +103,8 @@ private JSONArray filterResult(String filter, JSONArray resultAggregatedObjectAr
102103
JmesPathInterface jmesPathInterface = new JmesPathInterface();
103104
try {
104105
for (int i = 0; i < resultAggregatedObjectArray.length(); i++) {
105-
String objectId = ((JSONObject) resultAggregatedObjectArray.get(i)).get("_id").toString();
106+
String objectId =
107+
((JSONObject) resultAggregatedObjectArray.get(i)).get(MongoConstants.ID).toString();
106108
JsonNode filteredData = jmesPathInterface.runRuleOnEvent(filter, resultAggregatedObjectArray.get(i).toString());
107109
JSONObject tempJson = new JSONObject();
108110
tempJson.put(objectId, filteredData);

0 commit comments

Comments
 (0)