Skip to content

Commit e0d4ee5

Browse files
authored
Fix rule check endpoint (#160)
* Fix check-rule endpoint - Changed query parameters to json in body.
1 parent 5c22967 commit e0d4ee5

File tree

12 files changed

+362
-102
lines changed

12 files changed

+362
-102
lines changed

src/functionaltests/java/com/ericsson/ei/rules/RuleCheckSteps.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ public void file_with_JMESPath_rules_and_file_with_events(String rulesFileName,
5454
events = FileUtils.readFileToString(new File(TEST_RESOURCES_PATH + eventsFileName), "UTF-8");
5555
}
5656

57-
@When("^make a POST request to the REST API \"([^\"]*)\" with request parameter \"([^\"]*)\"$")
58-
public void make_a_POST_request_to_the_REST_API_with_request_parameter(String endpoint, String requestParam) throws Throwable {
57+
@When("^make a POST request to the REST API \"([^\"]*)\" with a single rule")
58+
public void make_a_POST_request_to_the_REST_API_with_request_parameter(String endpoint) throws Throwable {
59+
String requestBody = new JSONObject()
60+
.put("rule", new JSONObject(rules))
61+
.put("event", new JSONObject(events))
62+
.toString();
63+
5964
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
6065
response = postRequest.setPort(applicationPort)
6166
.setHost(hostName)
6267
.addHeader("content-type", "application/json")
6368
.addHeader("Accept", "application/json")
6469
.setEndpoint(endpoint)
65-
.setBody(events)
66-
.addParam(requestParam, rules)
70+
.setBody(requestBody)
6771
.performRequest();
6872
}
6973

src/functionaltests/resources/features/ruleCheck.feature

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Feature: Test Rules Checker
2222
@tag1
2323
Scenario: Execute JMESPath rule on JSON object
2424
Given file with JMESPath rules "/ExtractionRule.txt" and file with events "/EiffelArtifactCreatedEvent.json"
25-
When make a POST request to the REST API "/rules/rule-check" with request parameter "rule"
25+
When make a POST request to the REST API "/rules/rule-check" with a single rule
2626
Then get response code of 200
2727
And get content "/ExtractedContent.json"
2828

@@ -47,11 +47,10 @@ Feature: Test Rules Checker
4747
And file with JMESPath rules "/AggregateListRules.json" and file with events "/AggregateListEvents.json"
4848
When make a POST request to the REST API "/rules/rule-check/aggregation"
4949
Then get response code of 503
50-
50+
5151
#@tag5
5252
Scenario: Check status of test rule page using REST API
5353
When rules checking is enabled
5454
Then get request from REST API "/rules/rule-check/testRulePageEnabled" return response code of 200 and status as "true"
5555
When rules checking is not enabled
56-
Then get request from REST API "/rules/rule-check/testRulePageEnabled" return response code of 200 and status as "false"
57-
56+
Then get request from REST API "/rules/rule-check/testRulePageEnabled" return response code of 200 and status as "false"

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11

22
package com.ericsson.ei.controller;
33

4-
import javax.validation.Valid;
5-
import com.ericsson.ei.controller.model.RuleCheckBody;
64
import org.springframework.http.ResponseEntity;
7-
import org.springframework.web.bind.annotation.RequestBody;
85
import org.springframework.web.bind.annotation.RequestMapping;
96
import org.springframework.web.bind.annotation.RequestMethod;
10-
import org.springframework.web.bind.annotation.RequestParam;
117
import org.springframework.web.bind.annotation.RestController;
128

139

@@ -27,20 +23,19 @@ public interface RuleCheckController {
2723
*/
2824
@RequestMapping(value = "", method = RequestMethod.POST)
2925
public ResponseEntity<?> updateRulesRuleCheck(
30-
@RequestParam
31-
String rule,
32-
@RequestParam
33-
String jsonContent);
26+
@javax.validation.Valid
27+
@org.springframework.web.bind.annotation.RequestBody
28+
RuleCheckBody ruleCheckBody);
3429

3530
/**
3631
* This call for run the jmespath rule objects on the Json array of objects, we get aggregation Object as output
3732
*
3833
*/
3934
@RequestMapping(value = "/aggregation", method = RequestMethod.POST)
4035
public ResponseEntity<?> updateAggregation(
41-
@Valid
42-
@RequestBody
43-
RuleCheckBody ruleCheckBody);
36+
@javax.validation.Valid
37+
@org.springframework.web.bind.annotation.RequestBody
38+
RulesCheckBody rulesCheckBody);
4439

4540
/**
4641
* This call for the current status of test rule entry point, we get status as output

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121

2222
import com.ericsson.ei.controller.model.RuleCheckBody;
23+
import com.ericsson.ei.controller.model.RulesCheckBody;
2324
import com.ericsson.ei.exception.SubscriptionNotFoundException;
2425
import com.ericsson.ei.jmespath.JmesPathInterface;
2526
import com.ericsson.ei.services.IRuleCheckService;
@@ -45,19 +46,19 @@
4546
/**
4647
* This class implements the Interface for JMESPath API, generated by RAML 0.8
4748
* Provides interaction with JmesPathInterface class
48-
*
49+
*
4950
* Usage: 1. If input contains a rule as an argument and json expression in a
5051
* text file, then the following curl command may be used. curl -H
5152
* "Content-type: application/x-www-form-urlencoded" -X POST --data-urlencode
5253
* jsonContent@testjson.txt
5354
* http://localhost:8090/jmespathrule/runRule?rule=data.outcome
54-
*
55+
*
5556
* 2. If input contains rule and json expression as two String arguments, then
5657
* the following curl command may be used. curl -H "Content-type:
5758
* application/x-www-form-urlencoded" -X POST -d
5859
* jsonContent={"data":{"outcome":{"conclusion":"SUCCESSFUL"},"test":"persistentLogs"}}
5960
* http://localhost:8090/jmespathrule/runRule?rule=data.outcome
60-
*
61+
*
6162
*/
6263

6364
@Component
@@ -72,32 +73,35 @@ public class RuleCheckControllerImpl implements RuleCheckController {
7273

7374
@Autowired
7475
private IRuleCheckService ruleCheckService;
75-
76+
7677
@Setter
7778
@Value("${testaggregated.enabled:false}")
7879
private Boolean testEnable;
7980

8081
/**
8182
* This method interacts with JmesPathInterface class method runRuleOnEvent to
8283
* evaluate a rule on JSON object.
83-
*
84+
*
8485
* @param rule-
8586
* takes a String as a rule that need to be evaluated on JSON content
8687
* @param jsonContent-
8788
* takes JSON object as a String
8889
* @return a String object
89-
*
90+
*
9091
*/
9192
@Override
9293
@CrossOrigin
9394
@ApiOperation(value = "To execute rule on JSON", response = String.class)
9495
public ResponseEntity<?> updateRulesRuleCheck(
95-
@ApiParam(value = "JMESPath rule", required = true) @RequestParam String rule,
96-
@ApiParam(value = "JSON object", required = true) @RequestBody String jsonContent) {
96+
@ApiParam(value = "JSON object", required = true) @RequestBody RuleCheckBody body) {
97+
JSONObject rule = new JSONObject(body.getRule().getAdditionalProperties());
98+
JSONObject event = new JSONObject(body.getEvent().getAdditionalProperties());
99+
100+
String ruleString = rule.toString().replaceAll("\"", "");
97101
try {
98-
JSONObject jsonObj = new JSONObject(jsonContent);
99-
String res = jmesPathInterface.runRuleOnEvent(rule, jsonObj.toString()).toString();
100-
LOGGER.debug("Query: " + rule + " executed successfully");
102+
103+
String res = jmesPathInterface.runRuleOnEvent(ruleString, event.toString()).toString();
104+
LOGGER.debug("Query: " + body.getRule()+ " executed successfully");
101105
return new ResponseEntity<>(res, HttpStatus.OK);
102106
} catch (Exception e) {
103107
String errorMessage = "Failed to run rule on event. Error message:\n" + e.getMessage();
@@ -110,7 +114,7 @@ public ResponseEntity<?> updateRulesRuleCheck(
110114
@CrossOrigin
111115
@ApiOperation(value = "To execute the list of rules on list of Eiffel events. Return the aggregated object(s)", response = String.class)
112116
public ResponseEntity<?> updateAggregation(
113-
@ApiParam(value = "Object that include list of rules and list of Eiffel events", required = true) @RequestBody RuleCheckBody body) {
117+
@ApiParam(value = "Object that include list of rules and list of Eiffel events", required = true) @RequestBody RulesCheckBody body) {
114118
if (testEnable) {
115119
try {
116120
String aggregatedObject = ruleCheckService.prepareAggregatedObject(
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
package com.ericsson.ei.controller.model;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7+
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.fasterxml.jackson.annotation.JsonIgnore;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
13+
import org.apache.commons.lang3.builder.ToStringBuilder;
14+
15+
@JsonInclude(JsonInclude.Include.NON_NULL)
16+
@JsonPropertyOrder({
17+
18+
})
19+
public class Event {
20+
21+
@JsonIgnore
22+
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
23+
24+
@Override
25+
public String toString() {
26+
return ToStringBuilder.reflectionToString(this);
27+
}
28+
29+
@JsonAnyGetter
30+
public Map<String, Object> getAdditionalProperties() {
31+
return this.additionalProperties;
32+
}
33+
34+
@JsonAnySetter
35+
public void setAdditionalProperty(String name, Object value) {
36+
this.additionalProperties.put(name, value);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return new HashCodeBuilder().append(additionalProperties).toHashCode();
42+
}
43+
44+
@Override
45+
public boolean equals(Object other) {
46+
if (other == this) {
47+
return true;
48+
}
49+
if ((other instanceof Event) == false) {
50+
return false;
51+
}
52+
Event rhs = ((Event) other);
53+
return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).isEquals();
54+
}
55+
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
package com.ericsson.ei.controller.model;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7+
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.fasterxml.jackson.annotation.JsonIgnore;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
13+
import org.apache.commons.lang3.builder.ToStringBuilder;
14+
15+
@JsonInclude(JsonInclude.Include.NON_NULL)
16+
@JsonPropertyOrder({
17+
18+
})
19+
public class JsonContent {
20+
21+
@JsonIgnore
22+
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
23+
24+
@Override
25+
public String toString() {
26+
return ToStringBuilder.reflectionToString(this);
27+
}
28+
29+
@JsonAnyGetter
30+
public Map<String, Object> getAdditionalProperties() {
31+
return this.additionalProperties;
32+
}
33+
34+
@JsonAnySetter
35+
public void setAdditionalProperty(String name, Object value) {
36+
this.additionalProperties.put(name, value);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return new HashCodeBuilder().append(additionalProperties).toHashCode();
42+
}
43+
44+
@Override
45+
public boolean equals(Object other) {
46+
if (other == this) {
47+
return true;
48+
}
49+
if ((other instanceof JsonContent) == false) {
50+
return false;
51+
}
52+
JsonContent rhs = ((JsonContent) other);
53+
return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).isEquals();
54+
}
55+
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
package com.ericsson.ei.controller.model;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7+
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.fasterxml.jackson.annotation.JsonIgnore;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
13+
import org.apache.commons.lang3.builder.ToStringBuilder;
14+
15+
@JsonInclude(JsonInclude.Include.NON_NULL)
16+
@JsonPropertyOrder({
17+
18+
})
19+
public class Rule {
20+
21+
@JsonIgnore
22+
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
23+
24+
@Override
25+
public String toString() {
26+
return ToStringBuilder.reflectionToString(this);
27+
}
28+
29+
@JsonAnyGetter
30+
public Map<String, Object> getAdditionalProperties() {
31+
return this.additionalProperties;
32+
}
33+
34+
@JsonAnySetter
35+
public void setAdditionalProperty(String name, Object value) {
36+
this.additionalProperties.put(name, value);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return new HashCodeBuilder().append(additionalProperties).toHashCode();
42+
}
43+
44+
@Override
45+
public boolean equals(Object other) {
46+
if (other == this) {
47+
return true;
48+
}
49+
if ((other instanceof Rule) == false) {
50+
return false;
51+
}
52+
Rule rhs = ((Rule) other);
53+
return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).isEquals();
54+
}
55+
56+
}

0 commit comments

Comments
 (0)