Skip to content

Commit ec73074

Browse files
Improved responses (#125)
1 parent e538b9e commit ec73074

17 files changed

+131
-114
lines changed

src/functionaltests/resources/features/ruleCheck.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Feature: Test Rules Checker
3939
Given rules checking is enabled
4040
And file with JMESPath rules "/AggregateListRules.json" and file with events "/subscription_single.json"
4141
When make a POST request to the REST API "/rules/rule-check/aggregation"
42-
Then get response code of 400
42+
Then get response code of 500
4343

4444
#@tag4
4545
Scenario: Execute list of JMESPath rules on list of JSON objects, when rules checking is not enabled

src/main/java/com/ericsson/ei/controller/AuthControllerImpl.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ public class AuthControllerImpl implements AuthController {
4444

4545
@Override
4646
@CrossOrigin
47-
@ApiOperation(value = "To check is security enabled", response = String.class)
47+
@ApiOperation(value = "To check if security is enabled", response = String.class)
4848
public ResponseEntity<?> getAuth() {
4949
try {
5050
return new ResponseEntity<>(new JSONObject().put("security", ldapEnabled).toString(), HttpStatus.OK);
5151
} catch (Exception e) {
52-
LOGGER.error(e.getMessage(), e);
53-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
52+
String errorMessage = "Failed to check if security is enabled. Error message:\n" + e.getMessage();
53+
LOGGER.error(errorMessage, e);
54+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
5455
}
5556
}
5657

@@ -62,8 +63,9 @@ public ResponseEntity<?> getLogin() {
6263
String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
6364
return new ResponseEntity<>(new JSONObject().put("user", currentUser).toString(), HttpStatus.OK);
6465
} catch (Exception e) {
65-
LOGGER.error(e.getMessage(), e);
66-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
66+
String errorMessage = "Failed to log in user. Error message:\n" + e.getMessage();
67+
LOGGER.error(errorMessage, e);
68+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
6769
}
6870
}
6971

@@ -72,10 +74,11 @@ public ResponseEntity<?> getLogin() {
7274
@ApiOperation(value = "To check backend status", response = String.class)
7375
public ResponseEntity<?> getCheckStatus() {
7476
try {
75-
return new ResponseEntity<>(HttpStatus.OK);
77+
return new ResponseEntity<>("Backend server is up and running", HttpStatus.OK);
7678
} catch (Exception e) {
77-
LOGGER.error(e.getMessage(), e);
78-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
79+
String errorMessage = "Failed to check backend status. Error message:\n" + e.getMessage();
80+
LOGGER.error(errorMessage, e);
81+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
7982
}
8083
}
8184
}

src/main/java/com/ericsson/ei/controller/DownloadControllerImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public ResponseEntity<?> getDownload() {
4545
response.put("events", "/download/eventsTemplate");
4646
return new ResponseEntity<>(response.toString(), HttpStatus.OK);
4747
} catch (Exception e) {
48-
LOGGER.error(e.getMessage(), e);
49-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
48+
String errorMessage = "Failed to get information about download endpoints. Error message:\n" + e.getMessage();
49+
LOGGER.error(errorMessage, e);
50+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
5051
}
5152
}
5253

@@ -59,8 +60,9 @@ public ResponseEntity<?> getSubscriptionsTemplate() {
5960
LOGGER.error(e.getMessage(), e);
6061
return new ResponseEntity<>("Subscriptions template file is not found", HttpStatus.NOT_FOUND);
6162
} catch (Exception e) {
63+
String errorMessage = "Failed to download subscriptions template file. Error message:\n" + e.getMessage();
6264
LOGGER.error(e.getMessage(), e);
63-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
65+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
6466
}
6567
}
6668

@@ -73,8 +75,9 @@ public ResponseEntity<?> getRulesTemplate() {
7375
LOGGER.error(e.getMessage(), e);
7476
return new ResponseEntity<>("Rules template file is not found", HttpStatus.NOT_FOUND);
7577
} catch (Exception e) {
76-
LOGGER.error(e.getMessage(), e);
77-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
78+
String errorMessage = "Failed to download rules template file. Error message:\n" + e.getMessage();
79+
LOGGER.error(errorMessage, e);
80+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
7881
}
7982
}
8083

@@ -87,8 +90,9 @@ public ResponseEntity<?> getEventsTemplate() {
8790
LOGGER.error(e.getMessage(), e);
8891
return new ResponseEntity<>("Events template file is not found", HttpStatus.NOT_FOUND);
8992
} catch (Exception e) {
90-
LOGGER.error(e.getMessage(), e);
91-
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
93+
String errorMessage = "Failed to download events template file. Error message:\n" + e.getMessage();
94+
LOGGER.error(errorMessage, e);
95+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
9296
}
9397
}
9498
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
package com.ericsson.ei.controller;
3+
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
10+
/**
11+
* Provides interaction with InstanceInfo resource
12+
*
13+
* (Generated with springmvc-raml-parser v.0.10.11)
14+
*
15+
*/
16+
@RestController
17+
@RequestMapping(value = "/information", produces = "application/json")
18+
public interface InformationController {
19+
20+
21+
/**
22+
* List of all instance information
23+
*
24+
*
25+
*/
26+
@RequestMapping(value = "", method = RequestMethod.GET)
27+
public ResponseEntity<?> getInformation();
28+
29+
}

src/main/java/com/ericsson/ei/controller/info/InstanceInfoControllerImpl.java renamed to src/main/java/com/ericsson/ei/controller/InformationControllerImpl.java

Lines changed: 13 additions & 8 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.controller.info;
14+
package com.ericsson.ei.controller;
1515

1616
import com.ericsson.ei.controller.model.ParseInstanceInfoEI;
1717
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -20,27 +20,32 @@
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
2222
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.ResponseEntity;
2325
import org.springframework.stereotype.Component;
2426
import org.springframework.web.bind.annotation.CrossOrigin;
2527

2628
@Component
2729
@CrossOrigin
2830
@Api(value = "information", description = "The Information about Eiffel Intelligence Backend instance")
29-
public class InstanceInfoControllerImpl implements InstanceInfoController {
30-
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(InstanceInfoControllerImpl.class);
31+
public class InformationControllerImpl implements InformationController {
32+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(InformationControllerImpl.class);
3133

3234
@Autowired
33-
private ParseInstanceInfoEI istanceInfo;
35+
private ParseInstanceInfoEI instanceInfo;
3436

3537
@Override
3638
@CrossOrigin
3739
@ApiOperation(value = "Parse information")
38-
public String parseInfo() {
40+
public ResponseEntity<?> getInformation() {
3941
try {
40-
return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(istanceInfo);
42+
String info = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(instanceInfo);
43+
LOGGER.debug("EI backend information is parsed successfully");
44+
return new ResponseEntity<>(info, HttpStatus.OK);
4145
} catch (Exception e) {
42-
LOGGER.error("Serialization has failed " + e.getMessage());
46+
String errorMessage = "Failed to parse EI backend information. Error message:\n" + e.getMessage();
47+
LOGGER.error(errorMessage);
48+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
4349
}
44-
return null;
4550
}
4651
}

src/main/java/com/ericsson/ei/controller/QueryAggregatedObjectControllerImpl.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,18 @@ public class QueryAggregatedObjectControllerImpl implements QueryAggregatedObjec
4747
* @return ResponseEntity
4848
*/
4949
public ResponseEntity<QueryResponse> getQueryAggregatedObject(@RequestParam("ID") final String id) {
50-
QueryResponse queryResponse= new QueryResponse();
51-
List<String> response = processAggregatedObject.processQueryAggregatedObject(id);
52-
queryResponse.setResponseEntity(response.toString());
53-
LOGGER.debug("The response is : " + response.toString());
54-
return new ResponseEntity<>(queryResponse, HttpStatus.OK);
50+
QueryResponse queryResponse = new QueryResponse();
51+
try {
52+
List<String> response = processAggregatedObject.processQueryAggregatedObject(id);
53+
queryResponse.setResponseEntity(response.toString());
54+
LOGGER.debug("The response is: " + response.toString());
55+
return new ResponseEntity<>(queryResponse, HttpStatus.OK);
56+
} catch (Exception e) {
57+
String errorMessage = "Failed to extract the aggregated data from the Aggregated Object based on ID "
58+
+ id + ". Error message:\n" + e.getMessage();
59+
LOGGER.error(errorMessage, e);
60+
queryResponse.setResponseEntity(errorMessage);
61+
return new ResponseEntity<>(queryResponse, HttpStatus.INTERNAL_SERVER_ERROR);
62+
}
5563
}
5664
}

src/main/java/com/ericsson/ei/controller/QueryControllerImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package com.ericsson.ei.controller;
1818

19-
import com.ericsson.ei.controller.model.QueryResponse;
2019
import com.ericsson.ei.queryservice.ProcessQueryParams;
2120
import com.fasterxml.jackson.databind.ObjectMapper;
2221
import io.swagger.annotations.Api;
@@ -53,8 +52,9 @@ public ResponseEntity<?> updateQuery(@RequestParam(value = "request") String req
5352
JSONArray result = processQueryParams.filterFormParam(new ObjectMapper().readTree(request));
5453
return new ResponseEntity<>(result.toString(), HttpStatus.OK);
5554
} catch (Exception e) {
56-
LOGGER.error(e.getMessage());
57-
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
55+
String errorMessage = "Failed to extract data from the Aggregated Object using freestyle query. Error message:\n" + e.getMessage();
56+
LOGGER.error(errorMessage, e);
57+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
5858
}
5959
}
6060

@@ -67,8 +67,9 @@ public ResponseEntity<?> getQuery(@RequestParam(value = "request") final String
6767
LOGGER.debug("Final Output : " + result.toString());
6868
return new ResponseEntity<>(result.toString(), HttpStatus.OK);
6969
} catch (Exception e) {
70-
LOGGER.error(e.getMessage());
71-
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
70+
String errorMessage = "Failed to extract data from the Aggregated Object using freestyle query. Error message:\n" + e.getMessage();
71+
LOGGER.error(errorMessage, e);
72+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
7273
}
7374
}
7475
}

src/main/java/com/ericsson/ei/controller/QueryMissedNotificationControllerImpl.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@ public class QueryMissedNotificationControllerImpl implements QueryMissedNotific
4848
*/
4949
public ResponseEntity<QueryResponse> getQueryMissedNotifications(@RequestParam("SubscriptionName") final String subscriptionName) {
5050
QueryResponse queryResponse = new QueryResponse();
51-
List<String> response = processMissedNotification.processQueryMissedNotification(subscriptionName);
52-
queryResponse.setResponseEntity(response.toString());
53-
LOGGER.debug("The response is : " + response.toString());
54-
if(processMissedNotification.deleteMissedNotification(subscriptionName)) {
55-
LOGGER.debug("Missed notification with subscription name " + subscriptionName + " was successfully removed from database");
51+
try {
52+
List<String> response = processMissedNotification.processQueryMissedNotification(subscriptionName);
53+
queryResponse.setResponseEntity(response.toString());
54+
LOGGER.debug("The response is : " + response.toString());
55+
if (processMissedNotification.deleteMissedNotification(subscriptionName)) {
56+
LOGGER.debug("Missed notification with subscription name " + subscriptionName + " was successfully removed from database");
57+
}
58+
return new ResponseEntity<>(queryResponse, HttpStatus.OK);
59+
} catch (Exception e) {
60+
String errorMessage = "Failed to extract the data from the Missed Notification Object based on subscription name "
61+
+ subscriptionName + ". Error message:\n" + e.getMessage();
62+
LOGGER.error(errorMessage, e);
63+
queryResponse.setResponseEntity(errorMessage);
64+
return new ResponseEntity<>(queryResponse, HttpStatus.INTERNAL_SERVER_ERROR);
5665
}
57-
return new ResponseEntity<>(queryResponse, HttpStatus.OK);
5866
}
5967

6068
}

src/main/java/com/ericsson/ei/controller/RuleCheckControllerImpl.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@
6262
public class RuleCheckControllerImpl implements RuleCheckController {
6363

6464
private static final Logger LOGGER = LoggerFactory.getLogger(RuleCheckControllerImpl.class);
65-
private static final String BAD_REQUEST = "{\"message\": \"Bad request\"}";
66-
private static final String INVALID_JSON = "{\"message\": \"Invalid JSON content\"}";
67-
private static final String ENVIRONMENT_DISABLED = "{\"message\": \"Test environment is not enabled. "
68-
+ "Please use the test environment for this execution\"}";
6965

7066
@Autowired
7167
private JmesPathInterface jmesPathInterface;
@@ -99,8 +95,9 @@ public ResponseEntity<?> updateRulesRuleCheck(@ApiParam(value = "JMESPath rule",
9995
LOGGER.debug("Query: " + rule + " executed successfully");
10096
return new ResponseEntity<>(res, HttpStatus.OK);
10197
} catch (Exception e) {
102-
LOGGER.error(e.getMessage(), e);
103-
return new ResponseEntity<>(BAD_REQUEST, HttpStatus.BAD_REQUEST);
98+
String errorMessage = "Failed to run rule on event. Error message:\n" + e.getMessage();
99+
LOGGER.error(errorMessage, e);
100+
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
104101
}
105102
}
106103

@@ -115,15 +112,21 @@ public ResponseEntity<?> updateAggregation(@ApiParam(value = "Object that includ
115112
if (aggregatedObject != null) {
116113
return new ResponseEntity<>(aggregatedObject, HttpStatus.OK);
117114
} else {
118-
return new ResponseEntity<>(INVALID_JSON, HttpStatus.BAD_REQUEST);
115+
String errorMessage = "Aggregated event is not generated. List of rules or list of events are not correct";
116+
LOGGER.error(errorMessage);
117+
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
119118
}
120119
} catch (JSONException | IOException e) {
121-
LOGGER.error(e.getMessage(), e);
122-
return new ResponseEntity<>(INVALID_JSON, HttpStatus.BAD_REQUEST);
120+
String errorMessage = "Failed to generate aggregated object. Error message:\n" + e.getMessage();
121+
LOGGER.error(errorMessage, e);
122+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
123123
}
124124
} else {
125-
LOGGER.debug("testaggregated.controller.enabled is not enabled in application.properties file, Unable to test the rules on list of events");
126-
return new ResponseEntity<>(ENVIRONMENT_DISABLED, HttpStatus.SERVICE_UNAVAILABLE);
125+
String errorMessage = "Test Rules functionality is disabled in backend server. "
126+
+ "Configure \"testaggregated.controller.enabled\" setting in backend servers properties "
127+
+ "to enable this functionality. This should normally only be enabled in backend test servers.";
128+
LOGGER.error(errorMessage);
129+
return new ResponseEntity<>(errorMessage, HttpStatus.SERVICE_UNAVAILABLE);
127130
}
128131
}
129132

src/main/java/com/ericsson/ei/controller/SubscriptionController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface SubscriptionController {
2727
*
2828
*/
2929
@RequestMapping(value = "", method = RequestMethod.GET)
30-
public ResponseEntity<List<com.ericsson.ei.controller.model.Subscription>> getSubscriptions();
30+
public ResponseEntity<?> getSubscriptions();
3131

3232
/**
3333
* Takes the subscription rules, the name for subscription and the user name of the person registering this subscription and saves the subscription in subscription database. The name needs to be unique.

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public ResponseEntity<List<SubscriptionResponse>> createSubscription(@RequestBod
8282
errorMap.put(subscriptionName, SUBSCRIPTION_ALREADY_EXISTS);
8383
}
8484
} catch (Exception e) {
85-
LOG.error("Failed to create subscription " + subscriptionName + "\nError message: " + e.getMessage());
85+
LOG.error("Failed to create subscription " + subscriptionName + "\nError message: " + e.getMessage(), e);
8686
errorMap.put(subscriptionName, e.getMessage());
8787
}
8888
});
@@ -106,6 +106,9 @@ public ResponseEntity<GetSubscriptionResponse> getSubscriptionById(@PathVariable
106106
} catch (SubscriptionNotFoundException e) {
107107
LOG.error("Subscription is not found: " + subscriptionName);
108108
notFoundSubscriptionList.add(subscriptionName);
109+
} catch (Exception e) {
110+
LOG.error("Failed to fetch subscription " + subscriptionName + "\nError message: " + e.getMessage(), e);
111+
notFoundSubscriptionList.add(subscriptionName);
109112
}
110113
});
111114
GetSubscriptionResponse response = new GetSubscriptionResponse();
@@ -138,7 +141,7 @@ public ResponseEntity<List<SubscriptionResponse>> updateSubscriptions(@RequestBo
138141
errorMap.put(subscriptionName, SUBSCRIPTION_NOT_FOUND);
139142
}
140143
} catch (Exception e) {
141-
LOG.error("Failed to update subscription " + subscriptionName + "\nError message: " + e.getMessage());
144+
LOG.error("Failed to update subscription " + subscriptionName + "\nError message: " + e.getMessage(), e);
142145
errorMap.put(subscriptionName, e.getMessage());
143146
}
144147
});
@@ -168,13 +171,17 @@ public ResponseEntity<List<SubscriptionResponse>> deleteSubscriptionById(@PathVa
168171
@Override
169172
@CrossOrigin
170173
@ApiOperation(value = "Retrieves all the subscriptions")
171-
public ResponseEntity<List<Subscription>> getSubscriptions() {
174+
public ResponseEntity<?> getSubscriptions() {
172175
LOG.debug("Subscriptions fetching all has been started");
173176
try {
174177
return new ResponseEntity<>(subscriptionService.getSubscriptions(), HttpStatus.OK);
175178
} catch (SubscriptionNotFoundException e) {
176-
LOG.info(e.getLocalizedMessage());
179+
LOG.info(e.getMessage(), e);
177180
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
181+
} catch (Exception e) {
182+
String errorMessage = "Failed to fetch all subscriptions. Error message:\n" + e.getMessage();
183+
LOG.error(errorMessage, e);
184+
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
178185
}
179186
}
180187

0 commit comments

Comments
 (0)