Skip to content

Commit 68ff28d

Browse files
ValentinTyhonovemichaf
authored andcommitted
Fixed rule-check enpoint (#83)
* Update fork (#6) * Fixed rule-check endpoints * Fixed rule-check endpoints * Fixed rule-check endpoints * Fixed error response * Fixed error response
1 parent 966c1af commit 68ff28d

File tree

12 files changed

+227
-172
lines changed

12 files changed

+227
-172
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
package com.ericsson.ei.controller;
33

4+
import javax.validation.Valid;
5+
import com.ericsson.ei.controller.model.RuleCheckBody;
46
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.RequestBody;
58
import org.springframework.web.bind.annotation.RequestMapping;
69
import org.springframework.web.bind.annotation.RequestMethod;
710
import org.springframework.web.bind.annotation.RequestParam;
@@ -35,9 +38,8 @@ public ResponseEntity<?> updateRulesRuleCheck(
3538
*/
3639
@RequestMapping(value = "/aggregation", method = RequestMethod.POST)
3740
public ResponseEntity<?> updateAggregation(
38-
@RequestParam
39-
String listRulesJson,
40-
@RequestParam
41-
String listEventsJson);
41+
@Valid
42+
@RequestBody
43+
RuleCheckBody ruleCheckBody);
4244

4345
}

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

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import java.io.IOException;
44

5+
import com.ericsson.ei.controller.model.RuleCheckBody;
6+
import com.ericsson.ei.jmespath.JmesPathInterface;
7+
import com.ericsson.ei.services.IRuleCheckService;
8+
import io.swagger.annotations.Api;
9+
import io.swagger.annotations.ApiOperation;
10+
import io.swagger.annotations.ApiParam;
11+
import org.json.JSONArray;
512
import org.json.JSONException;
613
import org.json.JSONObject;
714
import org.slf4j.Logger;
@@ -12,12 +19,8 @@
1219
import org.springframework.http.ResponseEntity;
1320
import org.springframework.stereotype.Component;
1421
import org.springframework.web.bind.annotation.CrossOrigin;
15-
16-
import com.ericsson.ei.jmespath.JmesPathInterface;
17-
import com.ericsson.ei.services.IRuleCheckService;
18-
19-
import io.swagger.annotations.Api;
20-
import io.swagger.annotations.ApiOperation;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
import org.springframework.web.bind.annotation.RequestBody;
2124

2225
/**
2326
* This class implements the Interface for JMESPath API, generated by RAML 0.8
@@ -39,16 +42,20 @@
3942

4043
@Component
4144
@CrossOrigin
42-
@Api(value = "Check Rules", description = "This rest call for the execute the rule or rules(Rule object) on the Json, for checking output of rule")
45+
@Api(value = "Check Rules", description = "REST endpoints for executing rule(s) on the JSON")
4346
public class RuleCheckControllerImpl implements RuleCheckController {
4447

45-
private static final Logger LOG = LoggerFactory.getLogger(SubscriptionControllerImpl.class);
48+
private static final Logger LOGGER = LoggerFactory.getLogger(SubscriptionControllerImpl.class);
49+
private static final String BAD_REQUEST = "{\"message\": \"Bad request\"}";
50+
private static final String INVALID_JSON = "{\"message\": \"Invalid JSON content\"}";
51+
private static final String ENVIRONMENT_DISABLED = "{\"message\": \"Test environment is not enabled. "
52+
+ "Please use the test environment for this execution\"}";
4653

4754
@Autowired
48-
JmesPathInterface jmesPathInterface;
55+
private JmesPathInterface jmesPathInterface;
4956

5057
@Autowired
51-
IRuleCheckService ruleCheckService;
58+
private IRuleCheckService ruleCheckService;
5259

5360
@Value("${testaggregated.enabled:false}")
5461
private Boolean testEnable;
@@ -62,50 +69,45 @@ public class RuleCheckControllerImpl implements RuleCheckController {
6269
* content
6370
* @param jsonContent-
6471
* takes JSON object as a String
65-
* @return return a String object
72+
* @return a String object
6673
*
6774
*/
6875
@Override
6976
@CrossOrigin
70-
@ApiOperation(value = "run rule on event")
71-
public ResponseEntity<?> updateRulesRuleCheck(String rule, String jsonContent) {
72-
String res = new String("[]");
73-
77+
@ApiOperation(value = "To execute rule on JSON", response = String.class)
78+
public ResponseEntity<?> updateRulesRuleCheck(@ApiParam(value = "JMESPath rule", required = true) @RequestParam String rule,
79+
@ApiParam(value = "JSON object", required = true) @RequestBody String jsonContent) {
7480
try {
7581
JSONObject jsonObj = new JSONObject(jsonContent);
76-
77-
String jsonString = jsonObj.toString();
78-
res = jmesPathInterface.runRuleOnEvent(rule, jsonString).toString();
79-
LOG.debug("Query :" + rule + " executed Successfully");
80-
return new ResponseEntity<String>(res, HttpStatus.OK);
81-
82+
String res = jmesPathInterface.runRuleOnEvent(rule, jsonObj.toString()).toString();
83+
LOGGER.debug("Query: " + rule + " executed successfully");
84+
return new ResponseEntity<>(res, HttpStatus.OK);
8285
} catch (Exception e) {
83-
LOG.error(e.getMessage(), e);
84-
return new ResponseEntity<String>(res, HttpStatus.BAD_REQUEST);
86+
LOGGER.error(e.getMessage(), e);
87+
return new ResponseEntity<>(BAD_REQUEST, HttpStatus.BAD_REQUEST);
8588
}
8689
}
8790

8891
@Override
8992
@CrossOrigin
90-
@ApiOperation(value = "Run the list of rules on list of events and prepare the aggregation object. This endpoint for executing the rules on list of objects and return the aggregated objects.")
91-
public ResponseEntity<?> updateAggregation(String listRulesJson, String listEventsJson) {
92-
93+
@ApiOperation(value = "To execute the list of rules on list of Eiffel events. Return the aggregated object(s)", response = String.class)
94+
public ResponseEntity<?> updateAggregation(@ApiParam(value = "Object that include list of rules and list of Eiffel events", required = true)
95+
@RequestBody RuleCheckBody body) {
9396
if (testEnable) {
9497
try {
95-
String aggeObject = ruleCheckService.prepareAggregatedObject(listRulesJson, listEventsJson);
96-
if (aggeObject != null) {
97-
return new ResponseEntity<String>(aggeObject, HttpStatus.OK);
98+
String aggregatedObject = ruleCheckService.prepareAggregatedObject(new JSONArray(body.getListRulesJson()), new JSONArray(body.getListEventsJson()));
99+
if (aggregatedObject != null) {
100+
return new ResponseEntity<>(aggregatedObject, HttpStatus.OK);
98101
} else {
99-
return new ResponseEntity<String>("invalid json content", HttpStatus.BAD_REQUEST);
102+
return new ResponseEntity<>(INVALID_JSON, HttpStatus.BAD_REQUEST);
100103
}
101-
102104
} catch (JSONException | IOException e) {
103-
LOG.error(e.getMessage(), e);
104-
return new ResponseEntity<String>("invalid json content", HttpStatus.BAD_REQUEST);
105+
LOGGER.error(e.getMessage(), e);
106+
return new ResponseEntity<>(INVALID_JSON, HttpStatus.BAD_REQUEST);
105107
}
106108
} else {
107-
LOG.debug("testaggregated.controller.enabled is not enabled in application.properties file, Unable to test the rules on list of events");
108-
return new ResponseEntity<String>("Please use the test environment for this execution", HttpStatus.BAD_REQUEST);
109+
LOGGER.debug("testaggregated.controller.enabled is not enabled in application.properties file, Unable to test the rules on list of events");
110+
return new ResponseEntity<>(ENVIRONMENT_DISABLED, HttpStatus.SERVICE_UNAVAILABLE);
109111
}
110112
}
111113

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
package com.ericsson.ei.controller.model;
3+
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
9+
import com.fasterxml.jackson.annotation.JsonAnySetter;
10+
import com.fasterxml.jackson.annotation.JsonIgnore;
11+
import com.fasterxml.jackson.annotation.JsonInclude;
12+
import com.fasterxml.jackson.annotation.JsonProperty;
13+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
14+
import org.apache.commons.lang3.builder.EqualsBuilder;
15+
import org.apache.commons.lang3.builder.HashCodeBuilder;
16+
import org.apache.commons.lang3.builder.ToStringBuilder;
17+
18+
@JsonInclude(JsonInclude.Include.NON_NULL)
19+
@JsonPropertyOrder({
20+
"listRulesJson",
21+
"listEventsJson"
22+
})
23+
public class RuleCheckBody {
24+
25+
@JsonProperty("listRulesJson")
26+
private List<Object> listRulesJson = new ArrayList<Object>();
27+
@JsonProperty("listEventsJson")
28+
private List<Object> listEventsJson = new ArrayList<Object>();
29+
@JsonIgnore
30+
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
31+
32+
@JsonProperty("listRulesJson")
33+
public List<Object> getListRulesJson() {
34+
return listRulesJson;
35+
}
36+
37+
@JsonProperty("listRulesJson")
38+
public void setListRulesJson(List<Object> listRulesJson) {
39+
this.listRulesJson = listRulesJson;
40+
}
41+
42+
@JsonProperty("listEventsJson")
43+
public List<Object> getListEventsJson() {
44+
return listEventsJson;
45+
}
46+
47+
@JsonProperty("listEventsJson")
48+
public void setListEventsJson(List<Object> listEventsJson) {
49+
this.listEventsJson = listEventsJson;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return ToStringBuilder.reflectionToString(this);
55+
}
56+
57+
@JsonAnyGetter
58+
public Map<String, Object> getAdditionalProperties() {
59+
return this.additionalProperties;
60+
}
61+
62+
@JsonAnySetter
63+
public void setAdditionalProperty(String name, Object value) {
64+
this.additionalProperties.put(name, value);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return new HashCodeBuilder().append(listRulesJson).append(listEventsJson).append(additionalProperties).toHashCode();
70+
}
71+
72+
@Override
73+
public boolean equals(Object other) {
74+
if (other == this) {
75+
return true;
76+
}
77+
if ((other instanceof RuleCheckBody) == false) {
78+
return false;
79+
}
80+
RuleCheckBody rhs = ((RuleCheckBody) other);
81+
return new EqualsBuilder().append(listRulesJson, rhs.listRulesJson).append(listEventsJson, rhs.listEventsJson).append(additionalProperties, rhs.additionalProperties).isEquals();
82+
}
83+
84+
}

src/main/java/com/ericsson/ei/services/IRuleCheckService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44

5+
import org.json.JSONArray;
56
import org.json.JSONException;
67

78
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -24,7 +25,7 @@ public interface IRuleCheckService {
2425
* @throws JsonProcessingException
2526
* @throws IOException
2627
*/
27-
String prepareAggregatedObject(String listRulesJson, String listEventsJson)
28+
String prepareAggregatedObject(JSONArray listRulesJson, JSONArray listEventsJson)
2829
throws JSONException, JsonProcessingException, IOException;
2930

3031
}

src/main/java/com/ericsson/ei/services/RuleCheckService.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.util.ArrayList;
55
import java.util.HashSet;
6-
import java.util.Random;
76

87
import org.json.JSONArray;
98
import org.json.JSONException;
@@ -17,52 +16,47 @@
1716
import com.ericsson.ei.handlers.EventToObjectMapHandler;
1817
import com.ericsson.ei.jmespath.JmesPathInterface;
1918
import com.ericsson.ei.queryservice.ProcessAggregatedObject;
20-
import com.fasterxml.jackson.core.JsonProcessingException;
2119

2220
@Component
2321
public class RuleCheckService implements IRuleCheckService {
22+
private static final Logger LOGGER = LoggerFactory.getLogger(RuleCheckService.class);
2423

2524
@Autowired
26-
JmesPathInterface jmesPathInterface;
25+
private JmesPathInterface jmesPathInterface;
2726

2827
@Autowired
29-
EventHandler eventHandler;
28+
private EventHandler eventHandler;
3029

3130
@Autowired
3231
private ProcessAggregatedObject processAggregatedObject;
3332

3433
@Autowired
35-
EventToObjectMapHandler eventToObjectMapHandler;
36-
37-
private static final Logger LOGGER = LoggerFactory.getLogger(RuleCheckService.class);
34+
private EventToObjectMapHandler eventToObjectMapHandler;
3835

3936
@Override
40-
public String prepareAggregatedObject(String listRulesJson, String listEventsJson)
41-
throws JSONException, JsonProcessingException, IOException {
42-
JSONArray rulesList = new JSONArray(listRulesJson);
43-
JSONArray eventsList = new JSONArray(listEventsJson);
44-
eventHandler.getRulesHandler().setParsedJson(rulesList.toString());
45-
String response = null;
37+
public String prepareAggregatedObject(JSONArray listRulesJson, JSONArray listEventsJson)
38+
throws JSONException, IOException {
39+
eventHandler.getRulesHandler().setParsedJson(listRulesJson.toString());
40+
String response;
4641
// Looping all events and add suffix template name to id and links, For
4742
// identifying the test aggregated events.
4843
HashSet<String> templateNames = new HashSet<>();
49-
for (int i = 0; i < eventsList.length(); i++) {
44+
for (int i = 0; i < listEventsJson.length(); i++) {
5045
String templateName = jmesPathInterface
51-
.runRuleOnEvent("TemplateName", rulesList.getJSONObject(i).toString()).asText("TEST");
46+
.runRuleOnEvent("TemplateName", listRulesJson.getJSONObject(i).toString()).asText("TEST");
5247
templateNames.add(templateName);
5348
if (templateNames.size() == 1) {
54-
addTemplateNameToIds(eventsList.getJSONObject(i), templateName);
55-
LOGGER.debug("event to prepare aggregated object :: " + eventsList.getJSONObject(i).toString());
56-
eventHandler.eventReceived(eventsList.getJSONObject(i).toString());
49+
addTemplateNameToIds(listEventsJson.getJSONObject(i), templateName);
50+
LOGGER.debug("event to prepare aggregated object :: " + listEventsJson.getJSONObject(i).toString());
51+
eventHandler.eventReceived(listEventsJson.getJSONObject(i).toString());
5752
}
5853
}
59-
6054
String templateName = templateNames.iterator().next();
6155
if (templateNames.size() == 1) {
6256
ArrayList<String> responseList = processAggregatedObject.getAggregatedObjectByTemplateName(templateName);
6357
response = responseList.toString();
6458
} else {
65-
response = "Multipul template names are not allowed, Please use single name for all rules.";
59+
response = "Multiple template names are not allowed, Please use single name for all rules.";
6660
}
6761
// Delete the aggregated object
6862
processAggregatedObject.deleteAggregatedObject(templateName);

src/main/resources/public/raml/common-schema.raml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,18 @@ stringServiceResponse: |
158158
}
159159
},
160160
"type":"object"
161+
}
162+
ruleCheckBody: |
163+
{
164+
"$schema":"http://json-schema.org/draft-04/schema#",
165+
"javaType":"com.ericsson.ei.controller.model.RuleCheckBody",
166+
"properties":{
167+
"listRulesJson":{
168+
"type":"array"
169+
},
170+
"listEventsJson":{
171+
"type":"array"
172+
}
173+
},
174+
"type":"object"
161175
}

src/main/resources/public/raml/rules.raml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
type: string
2020
description: jmespath rule expression
2121
example: a.b
22-
required: true
22+
required: true
2323
responses:
2424
200:
2525
body:
@@ -30,18 +30,8 @@
3030
description: This call for run the jmespath rule objects on the Json array of objects, we get aggregation Object as output
3131
displayName: findAggregation
3232
body:
33-
multipart/form-data:
34-
formParameters:
35-
listRulesJson:
36-
description: This variable for the list of rules in json format, for find the aggregation object
37-
displayName: listRulesJson
38-
type: string
39-
required: true
40-
listEventsJson:
41-
description: This variable for the list of Events in json format
42-
displayName: listEventsJson
43-
type: string
44-
required: true
33+
application/json:
34+
schema: ruleCheckBody
4535
responses:
4636
200:
4737
body:

0 commit comments

Comments
 (0)