Skip to content

Commit e88cfa6

Browse files
Refactor error handling in ERQueryService (#369)
* Clean up of ERQueryService
1 parent 8abea1e commit e88cfa6

File tree

12 files changed

+100
-114
lines changed

12 files changed

+100
-114
lines changed

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

Lines changed: 48 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,28 @@
1717
*/
1818
package com.ericsson.ei.erqueryservice;
1919

20-
import java.net.MalformedURLException;
20+
import java.io.IOException;
21+
import java.net.URISyntaxException;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.List;
2425

25-
import javax.annotation.PostConstruct;
26-
2726
import org.apache.commons.lang3.StringUtils;
28-
import org.apache.commons.lang3.exception.ExceptionUtils;
27+
import org.apache.http.client.ClientProtocolException;
2928
import org.apache.http.entity.ContentType;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
3231
import org.springframework.beans.factory.annotation.Value;
3332
import org.springframework.stereotype.Component;
3433

34+
import com.ericsson.ei.exception.HttpRequestFailedException;
35+
import com.ericsson.ei.exception.PropertyNotFoundException;
3536
import com.ericsson.eiffelcommons.utils.HttpRequest;
3637
import com.ericsson.eiffelcommons.utils.HttpRequest.HttpMethod;
3738
import com.ericsson.eiffelcommons.utils.ResponseEntity;
3839

3940
import lombok.Getter;
41+
4042
/**
4143
* @author evasiba
4244
*/
@@ -60,97 +62,62 @@ public void setHttpRequest(HttpRequest request) {
6062
}
6163

6264
/**
63-
* This method only extracts the event information from ER2.0 based on the
64-
* eventID.
65+
* This method is used to fetch only the upstream or downstream or both event information for
66+
* ER based on the eventID and searchOption conditions.
6567
*
66-
* @param eventId
67-
* the id of the event.
68+
* @param eventId the id of the event.
69+
* @param searchOption the SearchOption to indicate whether to search up, down or both ways from
70+
* the eventId.
71+
* @param limit sets the limit of how many events up and/or down stream from the eventId
72+
* to include in the result.
73+
* @param levels sets the limit of how many levels up and/or down stream from the eventId
74+
* to include in the result.
75+
* @param tree whether or not to retain the tree structure in the result.
6876
* @return ResponseEntity
77+
* @throws PropertyNotFoundException
6978
*/
70-
public ResponseEntity getEventDataById(String eventId) {
71-
String erUrl = null;
72-
79+
public ResponseEntity getEventStreamDataById(String eventId, SearchOption searchOption,
80+
int limit, int levels, boolean tree) throws PropertyNotFoundException, Exception {
7381
try {
74-
if(StringUtils.isNotBlank(erBaseUrl)) {
75-
request
76-
.setHttpMethod(HttpMethod.GET)
77-
.setBaseUrl(erBaseUrl)
78-
.setEndpoint("{id}")
79-
.addParam("id", eventId);
80-
81-
erUrl= request.getURI().toString();
82-
LOGGER.debug("The URL to ER is: {}", erUrl);
83-
ResponseEntity response = request.performRequest();
84-
LOGGER.trace("The response is : {}", response.toString());
85-
} else {
86-
LOGGER.info("The URL to ER is not provided");
87-
}
88-
} catch (MalformedURLException e) {
89-
LOGGER.error("Error while building the ER url. Stacktrace: {}", ExceptionUtils.getStackTrace(e));
90-
} catch (Exception e) {
91-
LOGGER.error("Error occurred while executing REST GET to: {} for {}", erUrl, eventId, e);
82+
ResponseEntity responseFromEr = sendRequestToER(eventId, searchOption, limit, levels,
83+
tree);
84+
return responseFromEr;
85+
} catch (IOException | URISyntaxException e) {
86+
throw new HttpRequestFailedException("Error occurred while executing REST POST", e);
9287
}
93-
94-
return null;
9588
}
9689

97-
/**
98-
* This method is used to fetch only the upstream or downstream or both
99-
* event information from ER2.0 based on the eventID and searchOption
100-
* conditions.
101-
*
102-
* @param eventId
103-
* the id of the event.
104-
* @param searchOption
105-
* the SearchOption to indicate whether to search up, down or
106-
* both ways from the eventId.
107-
* @param limit
108-
* sets the limit of how many events up and/or down stream from
109-
* the eventId to include in the result.
110-
* @param levels
111-
* sets the limit of how many levels up and/or down stream from
112-
* the eventId to include in the result.
113-
* @param tree
114-
* whether or not to retain the tree structure in the result.
115-
* @return ResponseEntity
116-
*/
117-
public ResponseEntity getEventStreamDataById(String eventId, SearchOption searchOption, int limit,
118-
int levels, boolean tree) {
119-
120-
String uri = null;
121-
try {
122-
if(StringUtils.isNotBlank(erBaseUrl)) {
123-
// Request Body parameters
124-
final SearchParameters searchParameters = getSearchParameters(searchOption);
125-
request
126-
.setHttpMethod(HttpMethod.POST)
127-
.setBaseUrl(erBaseUrl)
128-
.setEndpoint(eventId)
129-
.addParam("limit", Integer.toString(limit))
130-
.addParam("levels", Integer.toString(levels))
131-
.addParam("tree", Boolean.toString(tree))
132-
.setBody(searchParameters.getAsJsonString(), ContentType.APPLICATION_JSON);
133-
134-
uri = request.getURI().toString();
135-
LOGGER.debug("The URL to ER is: {}", uri);
136-
137-
return request.performRequest();
138-
} else {
139-
LOGGER.info("The URL to ER is not provided");
140-
}
141-
}
142-
catch (Exception e) {
143-
LOGGER.error("Error occurred while executing REST POST to {}, stacktrace: {}", uri, e);
90+
private ResponseEntity sendRequestToER(String eventId, SearchOption searchOption, int limit,
91+
int levels, boolean tree) throws IOException, URISyntaxException,
92+
ClientProtocolException, PropertyNotFoundException {
93+
if (StringUtils.isBlank(erBaseUrl)) {
94+
throw new PropertyNotFoundException("The URL to ER is not provided");
14495
}
14596

146-
return null;
97+
prepareRequest(eventId, searchOption, limit, levels, tree);
98+
return request.performRequest();
99+
}
100+
101+
private void prepareRequest(String eventId, SearchOption searchOption, int limit,
102+
int levels, boolean tree) throws IOException, URISyntaxException {
103+
final SearchParameters searchParameters = getSearchParameters(searchOption);
104+
request
105+
.setHttpMethod(HttpMethod.POST)
106+
.setBaseUrl(erBaseUrl)
107+
.setEndpoint(eventId)
108+
.addParam("limit", Integer.toString(limit))
109+
.addParam("levels", Integer.toString(levels))
110+
.addParam("tree", Boolean.toString(tree))
111+
.setBody(searchParameters.getAsJsonString(), ContentType.APPLICATION_JSON);
112+
113+
String uri = request.getURI().toString();
114+
LOGGER.debug("The URL to ER is: {}", uri);
147115
}
148116

149117
/**
150118
* Build the search parameters to be used to query ER.
151119
*
152-
* @param searchOption
153-
* one of UP_STREAM, DOWN_STREAM or UP_AND_DOWN_STREAM
120+
* @param searchOption one of UP_STREAM, DOWN_STREAM or UP_AND_DOWN_STREAM
154121
* @return the search parameters to be used
155122
*/
156123
private SearchParameters getSearchParameters(SearchOption searchOption) {
@@ -173,10 +140,4 @@ private SearchParameters getSearchParameters(SearchOption searchOption) {
173140

174141
return searchParameters;
175142
}
176-
177-
@PostConstruct
178-
public void init() {
179-
// TODO: is this needed?
180-
LOGGER.debug("The url parameter is : {}", erBaseUrl);
181-
}
182143
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ericsson.ei.exception;
2+
3+
public class HttpRequestFailedException extends Exception {
4+
private static final long serialVersionUID = 1L;
5+
6+
public HttpRequestFailedException(final String message, final Throwable e) {
7+
super(message, e);
8+
}
9+
10+
public HttpRequestFailedException(final String message) {
11+
super(message);
12+
}
13+
14+
public HttpRequestFailedException(final Throwable e) {
15+
super(e);
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ericsson.ei.exception;
2+
3+
public class PropertyNotFoundException extends Exception {
4+
5+
private static final long serialVersionUID = 1L;
6+
7+
public PropertyNotFoundException(String message) {
8+
super(message);
9+
}
10+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
*/
1717
package com.ericsson.ei.handlers;
1818

19-
import org.apache.commons.lang3.exception.ExceptionUtils;
2019
import org.slf4j.Logger;
2120
import org.slf4j.LoggerFactory;
2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.stereotype.Component;
2423

24+
import com.ericsson.ei.exception.PropertyNotFoundException;
2525
import com.ericsson.ei.jmespath.JmesPathInterface;
2626
import com.ericsson.ei.jsonmerge.MergeHandler;
2727
import com.ericsson.ei.rules.RulesObject;
@@ -88,8 +88,10 @@ public void runExtraction(RulesObject rulesObject, String mergeId, String event,
8888
mergeHandler.addNewObject(event, extractedContent, rulesObject);
8989
upStreamEventsHandler.runHistoryExtractionRulesOnAllUpstreamEvents(mergeId);
9090
}
91+
} catch (PropertyNotFoundException e) {
92+
LOGGER.debug("Did not run history extraction on upstream events.", e);
9193
} catch (Exception e) {
92-
LOGGER.error("Failed to run extraction for event {} , stacktrace {}", event, ExceptionUtils.getStackTrace(e));
94+
LOGGER.error("Failed to run extraction for event {}", event, e);
9395
}
9496
}
9597

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.ericsson.ei.handlers;
1616

17-
import java.io.IOException;
1817
import java.net.URISyntaxException;
1918

2019
import org.slf4j.Logger;
@@ -24,6 +23,7 @@
2423

2524
import com.ericsson.ei.erqueryservice.ERQueryService;
2625
import com.ericsson.ei.erqueryservice.SearchOption;
26+
import com.ericsson.ei.exception.PropertyNotFoundException;
2727
import com.ericsson.ei.rules.RulesHandler;
2828
import com.ericsson.ei.rules.RulesObject;
2929
import com.ericsson.eiffelcommons.utils.ResponseEntity;
@@ -63,18 +63,15 @@ public void setHistoryExtractionHandler(final HistoryExtractionHandler historyEx
6363
*
6464
* @param aggregatedObjectId
6565
* the aggregated object id
66-
* @throws IOException
66+
* @throws Exception
67+
* @throws PropertyNotFoundException
6768
*/
68-
public void runHistoryExtractionRulesOnAllUpstreamEvents(String aggregatedObjectId) throws IOException {
69+
public void runHistoryExtractionRulesOnAllUpstreamEvents(String aggregatedObjectId) throws PropertyNotFoundException, Exception {
6970

7071
// Use aggregatedObjectId as eventId since they are the same for start
7172
// events.
7273
final ResponseEntity responseEntity = eventRepositoryQueryService
7374
.getEventStreamDataById(aggregatedObjectId, SearchOption.UP_STREAM, -1, -1, true);
74-
if (responseEntity == null) {
75-
LOGGER.warn("Asked for upstream from {} but got null response entity back!", aggregatedObjectId);
76-
return;
77-
}
7875

7976
final String searchResultString = responseEntity.getBody();
8077
ObjectMapper mapper = new ObjectMapper();

src/test/java/com/ericsson/ei/erqueryservice/test/ERQueryServiceTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515

1616
import static org.junit.Assert.assertEquals;
1717

18-
import java.io.IOException;
1918
import java.io.InputStream;
2019
import java.io.InputStreamReader;
21-
import java.net.URISyntaxException;
2220

2321
import org.apache.http.HttpEntity;
2422
import org.apache.http.HttpResponse;
2523
import org.apache.http.ProtocolVersion;
26-
import org.apache.http.client.ClientProtocolException;
2724
import org.apache.http.client.methods.HttpPost;
2825
import org.apache.http.client.methods.HttpRequestBase;
2926
import org.apache.http.impl.DefaultHttpResponseFactory;
@@ -44,6 +41,7 @@
4441
import com.ericsson.ei.App;
4542
import com.ericsson.ei.erqueryservice.ERQueryService;
4643
import com.ericsson.ei.erqueryservice.SearchOption;
44+
import com.ericsson.ei.exception.PropertyNotFoundException;
4745
import com.ericsson.ei.utils.TestContextInitializer;
4846
import com.ericsson.eiffelcommons.utils.HttpExecutor;
4947
import com.ericsson.eiffelcommons.utils.HttpRequest;
@@ -81,8 +79,8 @@ public void setUp() throws Exception {
8179
erQueryService.setHttpRequest(httpRequest);
8280
}
8381

84-
@Test
85-
public void testErQueryUpstream() throws ClientProtocolException, URISyntaxException, IOException {
82+
@Test(expected = Test.None.class)
83+
public void testErQueryUpstream() throws PropertyNotFoundException, Exception {
8684
BDDMockito.given(httpExecutor.executeRequest(any(HttpRequestBase.class))).willAnswer(validateRequest(Mockito.any(HttpRequestBase.class)));
8785
erQueryService.getEventStreamDataById(eventId, searchOption, limitParam, levels, isTree);
8886
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.ericsson.ei.App;
3131
import com.ericsson.ei.erqueryservice.ERQueryService;
3232
import com.ericsson.ei.erqueryservice.SearchOption;
33+
import com.ericsson.ei.exception.PropertyNotFoundException;
3334
import com.ericsson.ei.handlers.UpStreamEventsHandler;
3435
import com.ericsson.ei.utils.TestContextInitializer;
3536
import com.ericsson.eiffelcommons.utils.ResponseEntity;
@@ -61,7 +62,7 @@ public class ArrayAggregationTest extends FlowTestBase {
6162
private ERQueryService erQueryService;
6263

6364
@Before
64-
public void before() throws IOException {
65+
public void before() throws PropertyNotFoundException, Exception {
6566
MockitoAnnotations.initMocks(this);
6667
upStreamEventsHandler.setEventRepositoryQueryService(erQueryService);
6768

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.ericsson.ei.App;
4444
import com.ericsson.ei.erqueryservice.ERQueryService;
4545
import com.ericsson.ei.erqueryservice.SearchOption;
46+
import com.ericsson.ei.exception.PropertyNotFoundException;
4647
import com.ericsson.ei.handlers.UpStreamEventsHandler;
4748
import com.ericsson.ei.utils.TestContextInitializer;
4849
import com.ericsson.eiffelcommons.utils.ResponseEntity;
@@ -78,7 +79,7 @@ String getEventsFilePath() {
7879
private ERQueryService erQueryService;
7980

8081
@Before
81-
public void before() throws IOException {
82+
public void before() throws PropertyNotFoundException, Exception {
8283
MockitoAnnotations.initMocks(this);
8384
upStreamEventsHandler.setEventRepositoryQueryService(erQueryService);
8485
final URL upStreamResult = this.getClass().getClassLoader().getResource(UPSTREAM_FILE);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.ericsson.ei.App;
4848
import com.ericsson.ei.erqueryservice.ERQueryService;
4949
import com.ericsson.ei.erqueryservice.SearchOption;
50+
import com.ericsson.ei.exception.PropertyNotFoundException;
5051
import com.ericsson.ei.handlers.UpStreamEventsHandler;
5152
import com.ericsson.ei.utils.TestContextInitializer;
5253
import com.ericsson.eiffelcommons.utils.ResponseEntity;
@@ -83,7 +84,7 @@ public class FlowTest extends FlowTestBase {
8384
private RabbitTemplate rabbitMqTemplate;
8485

8586
@Before
86-
public void before() throws IOException {
87+
public void before() throws PropertyNotFoundException, Exception {
8788
ObjectMapper objectMapper = new ObjectMapper();
8889

8990
if (!systemTest) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.ericsson.ei.controller.model.Subscription;
5151
import com.ericsson.ei.erqueryservice.ERQueryService;
5252
import com.ericsson.ei.erqueryservice.SearchOption;
53+
import com.ericsson.ei.exception.PropertyNotFoundException;
5354
import com.ericsson.ei.handlers.ObjectHandler;
5455
import com.ericsson.ei.handlers.UpStreamEventsHandler;
5556
import com.ericsson.ei.services.ISubscriptionService;
@@ -91,7 +92,7 @@ String getEventsFilePath() {
9192
}
9293

9394
@Before
94-
public void before() {
95+
public void before() throws PropertyNotFoundException, Exception {
9596

9697
MockitoAnnotations.initMocks(this);
9798
upStreamEventsHandler.setEventRepositoryQueryService(erQueryService);

0 commit comments

Comments
 (0)