Skip to content

Commit 4290636

Browse files
authored
Change rule check endpoints (#388)
- Change rule check endpoints to a new structure according to #344. One change from the specification in that issue is that we changed /rule-test/enabled to /rule-test to be like /auth endpoint. Endpoints will now be: "/rule-test/run-single-rule", "/rule-test/run-full-aggregation" and "/rule-test".
1 parent e88cfa6 commit 4290636

File tree

13 files changed

+258
-210
lines changed

13 files changed

+258
-210
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.ericsson.ei.rules;
22

3+
import org.junit.runner.RunWith;
4+
35
import cucumber.api.CucumberOptions;
46
import cucumber.api.junit.Cucumber;
5-
import org.junit.runner.RunWith;
67

78
@RunWith(Cucumber.class)
8-
@CucumberOptions(features = "src/functionaltests/resources/features/ruleCheck.feature", glue = {
9-
"com.ericsson.ei.rules" }, plugin = { "pretty", "html:target/cucumber-reports/TestRuleCheckRunner" })
10-
public class RuleCheckTestRunner {
9+
@CucumberOptions(features = "src/functionaltests/resources/features/ruleTest.feature", glue = {
10+
"com.ericsson.ei.rules" }, plugin = { "pretty", "html:target/cucumber-reports/RuleTestRunner" })
11+
public class RuleTestRunner {
1112

1213
}

src/functionaltests/java/com/ericsson/ei/rules/RuleCheckSteps.java renamed to src/functionaltests/java/com/ericsson/ei/rules/RuleTestSteps.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.springframework.test.context.TestPropertySource;
1717
import org.springframework.test.util.ReflectionTestUtils;
1818

19-
import com.ericsson.ei.controller.RuleController;
19+
import com.ericsson.ei.controller.RuleTestController;
2020
import com.ericsson.ei.utils.FunctionalTestBase;
2121
import com.ericsson.ei.utils.HttpRequest;
2222
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
@@ -27,16 +27,16 @@
2727

2828
@Ignore
2929
@TestPropertySource(properties = {
30-
"spring.data.mongodb.database: RuleCheckSteps",
31-
"failed.notification.database-name: RuleCheckSteps-failedNotifications",
32-
"rabbitmq.exchange.name: RuleCheckSteps-exchange",
33-
"rabbitmq.consumerName: RuleCheckStepsConsumer" })
34-
public class RuleCheckSteps extends FunctionalTestBase {
30+
"spring.data.mongodb.database: RuleTestSteps",
31+
"failed.notification.database-name: RuleTestSteps-failedNotifications",
32+
"rabbitmq.exchange.name: RuleTestSteps-exchange",
33+
"rabbitmq.consumerName: RuleTestStepsConsumer" })
34+
public class RuleTestSteps extends FunctionalTestBase {
3535

3636
private static final String TEST_RESOURCES_PATH = "src/test/resources";
3737

3838
@Autowired
39-
private RuleController ruleController;
39+
private RuleTestController ruleTestController;
4040

4141
private String rules;
4242
private String events;
@@ -48,12 +48,12 @@ public class RuleCheckSteps extends FunctionalTestBase {
4848

4949
@Given("^rules checking is enabled$")
5050
public void rules_checking_is_enabled() throws Throwable {
51-
ReflectionTestUtils.setField(ruleController, "testEnabled", true);
51+
ReflectionTestUtils.setField(ruleTestController, "testEnabled", true);
5252
}
5353

5454
@Given("^rules checking is not enabled$")
5555
public void rules_checking_is_not_enabled() throws Throwable {
56-
ReflectionTestUtils.setField(ruleController, "testEnabled", false);
56+
ReflectionTestUtils.setField(ruleTestController, "testEnabled", false);
5757
}
5858

5959
@Given("^file with JMESPath rules \"([^\"]*)\" and file with events \"([^\"]*)\"$")

src/functionaltests/resources/features/restEndpoints.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Feature: Test Rest Endpoints
33

44
# Note: /query is not tested since it has its own test.
5-
# /rules/rule-check/aggregation is not tested since it performs an aggregation.
5+
# /rule-test/run-full-aggregation is not tested since it performs an aggregation.
66

77
Scenario Outline: Perform GET request on <endpoint> and expect response code <responsecode>
88
Given A GET request is prepared
@@ -24,7 +24,7 @@ Feature: Test Rest Endpoints
2424
| 404 | /queryMissedNotifications/subs_name |
2525
| 404 | /queryAggregatedObject/id |
2626
| 200 | /rules |
27-
| 200 | /rules/rule-check/testRulePageEnabled |
27+
| 200 | /rule-test |
2828

2929
@Test_Post_Put_Get_Delete_and_Get_(not_found)_subscription
3030
Scenario Outline: Perform <type> request on <endpoint> and expect response code <responsecode>
@@ -41,8 +41,8 @@ Feature: Test Rest Endpoints
4141
| 200 | DELETE | do not | /subscriptions/test_subscription |
4242
| 404 | GET | do not | /subscriptions/test_subscription |
4343

44-
Scenario: Test POST on /rules/rule-check endpoint
44+
Scenario: Test POST on /rule-test/run-single-rule endpoint
4545
Given A POST request is prepared
4646
And Event rule json data is added as body
47-
When Perform request on endpoint "/rules/rule-check"
47+
When Perform request on endpoint "/rule-test/run-single-rule"
4848
Then Request should get response code 200
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
#Author: valentin.tyhonov@ericsson.com
22

33
@RuleCheck
4-
Feature: Test Rules Checker
4+
Feature: Test rule test feature
55

66
@ExecuteRuleSingle
77
Scenario: Execute JMESPath rule on JSON object
88
Given file with JMESPath rules "/ExtractionRule.txt" and file with events "/EiffelArtifactCreatedEvent.json"
9-
When make a POST request to the REST API "/rules/rule-check" with a single rule
9+
When make a POST request to the REST API "/rule-test/run-single-rule" with a single rule
1010
Then get response code of 200
1111
And get content "/ExtractedContent.json"
1212

1313
@ExecuteRuleMultiple
1414
Scenario: Execute list of JMESPath rules on list of JSON objects
1515
Given rules checking is enabled
1616
And file with JMESPath rules "/AggregateListRules.json" and file with events "/AggregateListEvents.json"
17-
When make a POST request to the REST API "/rules/rule-check/aggregation"
17+
When make a POST request to the REST API "/rule-test/run-full-aggregation"
1818
Then get response code of 200
1919
And get content "/AggregateResultObject.json"
2020

2121
@ExecuteIncorrectRule
2222
Scenario: Execute incorrect list of JMESPath rules on list of JSON objects
2323
Given rules checking is enabled
2424
And file with JMESPath rules "/AggregateListRules.json" and file with events "/subscription_single.json"
25-
When make a POST request to the REST API "/rules/rule-check/aggregation"
25+
When make a POST request to the REST API "/rule-test/run-full-aggregation"
2626
Then get response code of 400
2727

2828
@ExecuteRuleDisabled
2929
Scenario: Execute list of JMESPath rules on list of JSON objects, when rules checking is not enabled
3030
Given rules checking is not enabled
3131
And file with JMESPath rules "/AggregateListRules.json" and file with events "/AggregateListEvents.json"
32-
When make a POST request to the REST API "/rules/rule-check/aggregation"
32+
When make a POST request to the REST API "/rule-test/run-full-aggregation"
3333
Then get response code of 503
3434

35-
@RuleCheckStatus
35+
@RuleTestStatus
3636
Scenario: Check status of test rule page using REST API
3737
When rules checking is enabled
38-
Then get request from REST API "/rules/rule-check/testRulePageEnabled" return response code of 200 and status as "true"
38+
Then get request from REST API "/rule-test" return response code of 200 and status as "true"
3939
When rules checking is not enabled
40-
Then get request from REST API "/rules/rule-check/testRulePageEnabled" return response code of 200 and status as "false"
40+
Then get request from REST API "/rule-test" return response code of 200 and status as "false"

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

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
package com.ericsson.ei.controller;
33

44
import javax.servlet.http.HttpServletRequest;
5-
import com.ericsson.ei.controller.model.RuleCheckBody;
6-
import com.ericsson.ei.controller.model.RulesCheckBody;
75
import org.springframework.http.ResponseEntity;
86
import org.springframework.validation.annotation.Validated;
97
import org.springframework.web.bind.annotation.RequestMapping;
@@ -12,7 +10,7 @@
1210

1311

1412
/**
15-
* This class is used for anything rules related. It's currently used to test rules on Eiffel events and to fetch the active rules file content. Test rules must be enabled in Eiffel Intelligence for the /rule-check endpoints to work.
13+
* No description
1614
* (Generated with springmvc-raml-parser v.2.0.5)
1715
*
1816
*/
@@ -29,31 +27,4 @@ public interface RuleController {
2927
@RequestMapping(value = "", method = RequestMethod.GET)
3028
public ResponseEntity<?> getRules(HttpServletRequest httpRequest);
3129

32-
/**
33-
* This method extracts data from a single Eiffel event based on the given JMESPath expression.
34-
*
35-
*/
36-
@RequestMapping(value = "/rule-check", method = RequestMethod.POST)
37-
public ResponseEntity<?> createRulesRuleCheck(
38-
@javax.validation.Valid
39-
@org.springframework.web.bind.annotation.RequestBody
40-
RuleCheckBody ruleCheckBody, HttpServletRequest httpRequest);
41-
42-
/**
43-
* This method extracts data from the given list of Eiffel events, based on a set of rules and returns an aggregated object.
44-
*
45-
*/
46-
@RequestMapping(value = "/rule-check/aggregation", method = RequestMethod.POST)
47-
public ResponseEntity<?> createRuleCheckAggregation(
48-
@javax.validation.Valid
49-
@org.springframework.web.bind.annotation.RequestBody
50-
RulesCheckBody rulesCheckBody, HttpServletRequest httpRequest);
51-
52-
/**
53-
* This method checks if the possibility to test rules has been enabled in Eiffel Intelligence.
54-
*
55-
*/
56-
@RequestMapping(value = "/rule-check/testRulePageEnabled", method = RequestMethod.GET)
57-
public ResponseEntity<?> getRuleCheckTestRulePageEnabled(HttpServletRequest httpRequest);
58-
5930
}

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

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

19-
import java.io.IOException;
20-
2119
import javax.servlet.http.HttpServletRequest;
2220

23-
import org.json.JSONArray;
24-
import org.json.JSONException;
2521
import org.json.JSONObject;
2622
import org.slf4j.Logger;
2723
import org.slf4j.LoggerFactory;
@@ -31,22 +27,15 @@
3127
import org.springframework.http.ResponseEntity;
3228
import org.springframework.stereotype.Component;
3329
import org.springframework.web.bind.annotation.CrossOrigin;
34-
import org.springframework.web.bind.annotation.RequestBody;
3530

36-
import com.ericsson.ei.controller.model.RuleCheckBody;
37-
import com.ericsson.ei.controller.model.RulesCheckBody;
38-
import com.ericsson.ei.jmespath.JmesPathInterface;
3931
import com.ericsson.ei.rules.RulesHandler;
40-
import com.ericsson.ei.services.IRuleCheckService;
4132
import com.ericsson.ei.utils.ResponseMessage;
4233
import com.fasterxml.jackson.core.JsonProcessingException;
4334
import com.fasterxml.jackson.databind.JsonNode;
4435
import com.fasterxml.jackson.databind.ObjectMapper;
4536

4637
import io.swagger.annotations.Api;
4738
import io.swagger.annotations.ApiOperation;
48-
import io.swagger.annotations.ApiParam;
49-
import lombok.Setter;
5039

5140
@Component
5241
@CrossOrigin
@@ -55,19 +44,9 @@ public class RuleControllerImpl implements RuleController{
5544

5645
private static final Logger LOGGER = LoggerFactory.getLogger(RuleControllerImpl.class);
5746

58-
@Autowired
59-
private JmesPathInterface jmesPathInterface;
60-
61-
@Autowired
62-
private IRuleCheckService ruleCheckService;
63-
6447
@Autowired
6548
private RulesHandler rulesHandler;
6649

67-
@Setter
68-
@Value("${testaggregated.enabled:false}")
69-
private Boolean testEnabled;
70-
7150
@Value("${rules.path}")
7251
private String rulesPath;
7352

@@ -91,85 +70,4 @@ public ResponseEntity<?> getRules(final HttpServletRequest httpRequest) {
9170
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
9271
}
9372
}
94-
95-
/**
96-
* This method interacts with JmesPathInterface class method runRuleOnEvent to
97-
* evaluate a rule on JSON object.
98-
*
99-
* @param rule-
100-
* takes a String as a rule that need to be evaluated on JSON content
101-
* @param jsonContent-
102-
* takes JSON object as a String
103-
* @return a String object
104-
*
105-
*/
106-
@Override
107-
@CrossOrigin
108-
@ApiOperation(value = "To execute rule on one Eiffel event", response = String.class)
109-
public ResponseEntity<?> createRulesRuleCheck(
110-
@ApiParam(value = "JSON object", required = true) @RequestBody RuleCheckBody body, final HttpServletRequest httpRequest) {
111-
JSONObject rule = new JSONObject(body.getRule().getAdditionalProperties());
112-
JSONObject event = new JSONObject(body.getEvent().getAdditionalProperties());
113-
114-
String ruleString = rule.toString().replaceAll("\"", "");
115-
try {
116-
String res = jmesPathInterface.runRuleOnEvent(ruleString, event.toString()).toString();
117-
LOGGER.debug("Query: {} executed successfully", body.getRule());
118-
return new ResponseEntity<>(res, HttpStatus.OK);
119-
} catch (Exception e) {
120-
String errorMessage = "Failed to run rule on event. Error message:\n" + e.getMessage();
121-
LOGGER.error(errorMessage, e);
122-
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
123-
return new ResponseEntity<>(errorJsonAsString, HttpStatus.BAD_REQUEST);
124-
}
125-
}
126-
127-
@Override
128-
@CrossOrigin
129-
@ApiOperation(value = "To execute the list of rules on list of Eiffel events. Returns the aggregated object(s)", response = String.class)
130-
public ResponseEntity<?> createRuleCheckAggregation(
131-
@ApiParam(value = "Object that include list of rules and list of Eiffel events", required = true) @RequestBody RulesCheckBody body,
132-
final HttpServletRequest httpRequest) {
133-
if (testEnabled) {
134-
try {
135-
String aggregatedObject = ruleCheckService.prepareAggregatedObject(
136-
new JSONArray(body.getListRulesJson()), new JSONArray(body.getListEventsJson()));
137-
if (aggregatedObject != null && !aggregatedObject.equals("[]")) {
138-
return new ResponseEntity<>(aggregatedObject, HttpStatus.OK);
139-
} else {
140-
String errorMessage = "Failed to generate aggregated object. List of rules or list of events are not correct";
141-
LOGGER.error(errorMessage);
142-
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
143-
return new ResponseEntity<>(errorJsonAsString, HttpStatus.BAD_REQUEST);
144-
}
145-
} catch (JSONException | IOException e) {
146-
String errorMessage = "Internal Server Error: Failed to generate aggregated object.";
147-
LOGGER.error(errorMessage, e);
148-
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
149-
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
150-
}
151-
} else {
152-
String errorMessage = "Test rules functionality is disabled in backend server. "
153-
+ "Configure \"testaggregated.enabled\" setting in backend servers properties "
154-
+ "to enable this functionality. This should normally only be enabled in backend test servers.";
155-
LOGGER.error(errorMessage);
156-
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
157-
return new ResponseEntity<>(errorJsonAsString, HttpStatus.SERVICE_UNAVAILABLE);
158-
}
159-
}
160-
161-
@Override
162-
@CrossOrigin
163-
@ApiOperation(value = "Check if rules check service is enabled", response = String.class)
164-
public ResponseEntity<?> getRuleCheckTestRulePageEnabled(final HttpServletRequest httpRequest) {
165-
LOGGER.debug("Getting Enabling Status of Rules Check Service");
166-
try {
167-
return new ResponseEntity<>(new JSONObject().put("status", testEnabled).toString(), HttpStatus.OK);
168-
} catch (Exception e) {
169-
String errorMessage = "Internal Server Error: Failed to get status.";
170-
LOGGER.error(errorMessage, e);
171-
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
172-
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
173-
}
174-
}
17573
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
package com.ericsson.ei.controller;
3+
4+
import javax.servlet.http.HttpServletRequest;
5+
import com.ericsson.ei.controller.model.RuleCheckBody;
6+
import com.ericsson.ei.controller.model.RulesCheckBody;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
14+
/**
15+
* This class is used for anything rule test related. It's currently used to test rules on Eiffel events. Test rules must be enabled in Eiffel Intelligence for the /rule-test endpoints to work.
16+
* (Generated with springmvc-raml-parser v.2.0.5)
17+
*
18+
*/
19+
@RestController
20+
@Validated
21+
@RequestMapping(value = "/rule-test", produces = "application/json")
22+
public interface RuleTestController {
23+
24+
25+
/**
26+
* This method checks if the possibility to test rules has been enabled in Eiffel Intelligence.
27+
*
28+
*/
29+
@RequestMapping(value = "", method = RequestMethod.GET)
30+
public ResponseEntity<?> getRuleTest(HttpServletRequest httpRequest);
31+
32+
/**
33+
* This method extracts data from a single Eiffel event based on the given JMESPath expression.
34+
*
35+
*/
36+
@RequestMapping(value = "/run-single-rule", method = RequestMethod.POST)
37+
public ResponseEntity<?> createRuleTestRunSingleRule(
38+
@javax.validation.Valid
39+
@org.springframework.web.bind.annotation.RequestBody
40+
RuleCheckBody ruleCheckBody, HttpServletRequest httpRequest);
41+
42+
/**
43+
* This method extracts data from the given list of Eiffel events, based on a set of rules and returns an aggregated object.
44+
*
45+
*/
46+
@RequestMapping(value = "/run-full-aggregation", method = RequestMethod.POST)
47+
public ResponseEntity<?> createRuleTestRunFullAggregation(
48+
@javax.validation.Valid
49+
@org.springframework.web.bind.annotation.RequestBody
50+
RulesCheckBody rulesCheckBody, HttpServletRequest httpRequest);
51+
52+
}

0 commit comments

Comments
 (0)