Skip to content

Commit d2b4137

Browse files
Add GET /rules endpoint (#300)
* /rules is now an endpoint that fetches the active rules content and path
1 parent 4ef18b6 commit d2b4137

File tree

7 files changed

+84
-46
lines changed

7 files changed

+84
-46
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ericsson.ei.rules;
22

3-
import com.ericsson.ei.controller.RuleCheckController;
3+
import com.ericsson.ei.controller.RuleController;
44
import com.ericsson.ei.utils.FunctionalTestBase;
55
import com.ericsson.ei.utils.HttpRequest;
66
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
@@ -28,7 +28,7 @@ public class RuleCheckSteps extends FunctionalTestBase {
2828
private static final String TEST_RESOURCES_PATH = "src/test/resources";
2929

3030
@Autowired
31-
private RuleCheckController ruleCheckController;
31+
private RuleController ruleController;
3232

3333
private String rules;
3434
private String events;
@@ -40,12 +40,12 @@ public class RuleCheckSteps extends FunctionalTestBase {
4040

4141
@Given("^rules checking is enabled$")
4242
public void rules_checking_is_enabled() throws Throwable {
43-
ReflectionTestUtils.setField(ruleCheckController, "testEnable", true);
43+
ReflectionTestUtils.setField(ruleController, "testEnabled", true);
4444
}
4545

4646
@Given("^rules checking is not enabled$")
4747
public void rules_checking_is_not_enabled() throws Throwable {
48-
ReflectionTestUtils.setField(ruleCheckController, "testEnable", false);
48+
ReflectionTestUtils.setField(ruleController, "testEnabled", false);
4949
}
5050

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

src/main/java/com/ericsson/ei/controller/RuleCheckController.java renamed to src/main/java/com/ericsson/ei/controller/RuleController.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@
1111

1212

1313
/**
14-
* No description
14+
* 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.
1515
* (Generated with springmvc-raml-parser v.2.0.4)
1616
*
1717
*/
1818
@RestController
1919
@Validated
20-
@RequestMapping(value = "/rules/rule-check", produces = "application/json")
21-
public interface RuleCheckController {
20+
@RequestMapping(value = "/rules", produces = "application/json")
21+
public interface RuleController {
2222

2323

24+
/**
25+
* This method returns the active rules file content.
26+
*
27+
*/
28+
@RequestMapping(value = "", method = RequestMethod.GET)
29+
public ResponseEntity<?> getRules();
30+
2431
/**
2532
* This method extracts data from a single Eiffel event based on the given JMESPath expression.
2633
*
2734
*/
28-
@RequestMapping(value = "", method = RequestMethod.POST)
35+
@RequestMapping(value = "/rule-check", method = RequestMethod.POST)
2936
public ResponseEntity<?> createRulesRuleCheck(
3037
@javax.validation.Valid
3138
@org.springframework.web.bind.annotation.RequestBody
@@ -35,7 +42,7 @@ public ResponseEntity<?> createRulesRuleCheck(
3542
* This method extracts data from the given list of Eiffel events, based on a set of rules and returns an aggregated object.
3643
*
3744
*/
38-
@RequestMapping(value = "/aggregation", method = RequestMethod.POST)
45+
@RequestMapping(value = "/rule-check/aggregation", method = RequestMethod.POST)
3946
public ResponseEntity<?> createRuleCheckAggregation(
4047
@javax.validation.Valid
4148
@org.springframework.web.bind.annotation.RequestBody
@@ -45,7 +52,7 @@ public ResponseEntity<?> createRuleCheckAggregation(
4552
* This method checks if the possibility to test rules has been enabled in Eiffel Intelligence.
4653
*
4754
*/
48-
@RequestMapping(value = "/testRulePageEnabled", method = RequestMethod.GET)
55+
@RequestMapping(value = "/rule-check/testRulePageEnabled", method = RequestMethod.GET)
4956
public ResponseEntity<?> getRuleCheckTestRulePageEnabled();
5057

5158
}

src/main/java/com/ericsson/ei/controller/RuleCheckControllerImpl.java renamed to src/main/java/com/ericsson/ei/controller/RuleControllerImpl.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,48 +34,61 @@
3434
import com.ericsson.ei.controller.model.RuleCheckBody;
3535
import com.ericsson.ei.controller.model.RulesCheckBody;
3636
import com.ericsson.ei.jmespath.JmesPathInterface;
37+
import com.ericsson.ei.rules.RulesHandler;
3738
import com.ericsson.ei.services.IRuleCheckService;
3839
import com.ericsson.ei.utils.ResponseMessage;
40+
import com.fasterxml.jackson.core.JsonProcessingException;
41+
import com.fasterxml.jackson.databind.JsonNode;
42+
import com.fasterxml.jackson.databind.ObjectMapper;
3943

4044
import io.swagger.annotations.Api;
4145
import io.swagger.annotations.ApiOperation;
4246
import io.swagger.annotations.ApiParam;
4347
import lombok.Setter;
4448

45-
/**
46-
* This class implements the Interface for JMESPath API, generated by RAML 0.8
47-
* Provides interaction with JmesPathInterface class
48-
*
49-
* Usage: 1. If input contains a rule as an argument and json expression in a
50-
* text file, then the following curl command may be used. curl -H
51-
* "Content-type: application/x-www-form-urlencoded" -X POST --data-urlencode
52-
* jsonContent@testjson.txt
53-
* http://localhost:8090/jmespathrule/runRule?rule=data.outcome
54-
*
55-
* 2. If input contains rule and json expression as two String arguments, then
56-
* the following curl command may be used. curl -H "Content-type:
57-
* application/x-www-form-urlencoded" -X POST -d
58-
* jsonContent={"data":{"outcome":{"conclusion":"SUCCESSFUL"},"test":"persistentLogs"}}
59-
* http://localhost:8090/jmespathrule/runRule?rule=data.outcome
60-
*
61-
*/
62-
6349
@Component
6450
@CrossOrigin
65-
@Api(value = "checkRules", tags = {"Check rules"})
66-
public class RuleCheckControllerImpl implements RuleCheckController {
51+
@Api(value = "Rules", tags = {"Rules"})
52+
public class RuleControllerImpl implements RuleController{
6753

68-
private static final Logger LOGGER = LoggerFactory.getLogger(RuleCheckControllerImpl.class);
54+
private static final Logger LOGGER = LoggerFactory.getLogger(RuleControllerImpl.class);
6955

7056
@Autowired
7157
private JmesPathInterface jmesPathInterface;
7258

7359
@Autowired
7460
private IRuleCheckService ruleCheckService;
7561

62+
@Autowired
63+
private RulesHandler rulesHandler;
64+
7665
@Setter
7766
@Value("${testaggregated.enabled:false}")
78-
private Boolean testEnable;
67+
private Boolean testEnabled;
68+
69+
@Value("${rules.path}")
70+
private String rulesPath;
71+
72+
@Override
73+
@CrossOrigin
74+
@ApiOperation(value = "Get the active rules from Eiffel Intelligence", response = String.class)
75+
public ResponseEntity<?> getRules() {
76+
JsonNode rulesContent = rulesHandler.getRulesContent();
77+
ObjectMapper objectmapper = new ObjectMapper();
78+
try {
79+
String contentAsString = objectmapper.writeValueAsString(rulesContent);
80+
JSONObject jsonResult = new JSONObject();
81+
jsonResult.put("path", rulesPath);
82+
jsonResult.put("content", contentAsString);
83+
String result = jsonResult.toString();
84+
return new ResponseEntity<>(result, HttpStatus.OK);
85+
} catch (JsonProcessingException e) {
86+
String errorMessage = "Internal Server Error: Failed to parse the rules content.";
87+
LOGGER.error(errorMessage, e);
88+
String errorJsonAsString = ResponseMessage.createJsonMessage(errorMessage);
89+
return new ResponseEntity<>(errorJsonAsString, HttpStatus.INTERNAL_SERVER_ERROR);
90+
}
91+
}
7992

8093
/**
8194
* This method interacts with JmesPathInterface class method runRuleOnEvent to
@@ -114,7 +127,7 @@ public ResponseEntity<?> createRulesRuleCheck(
114127
@ApiOperation(value = "To execute the list of rules on list of Eiffel events. Returns the aggregated object(s)", response = String.class)
115128
public ResponseEntity<?> createRuleCheckAggregation(
116129
@ApiParam(value = "Object that include list of rules and list of Eiffel events", required = true) @RequestBody RulesCheckBody body) {
117-
if (testEnable) {
130+
if (testEnabled) {
118131
try {
119132
String aggregatedObject = ruleCheckService.prepareAggregatedObject(
120133
new JSONArray(body.getListRulesJson()), new JSONArray(body.getListEventsJson()));
@@ -148,7 +161,7 @@ public ResponseEntity<?> createRuleCheckAggregation(
148161
public ResponseEntity<?> getRuleCheckTestRulePageEnabled() {
149162
LOGGER.debug("Getting Enabling Status of Rules Check Service");
150163
try {
151-
return new ResponseEntity<>(new JSONObject().put("status", testEnable).toString(), HttpStatus.OK);
164+
return new ResponseEntity<>(new JSONObject().put("status", testEnabled).toString(), HttpStatus.OK);
152165
} catch (Exception e) {
153166
String errorMessage = "Internal Server Error: Failed to get status.";
154167
LOGGER.error(errorMessage, e);

src/main/java/com/ericsson/ei/controller/model/QueryBody.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
public class QueryBody {
2525

2626
/**
27-
*
27+
*
2828
* (Required)
29-
*
29+
*
3030
*/
3131
@JsonProperty("criteria")
3232
@Valid
@@ -42,19 +42,19 @@ public class QueryBody {
4242
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
4343

4444
/**
45-
*
45+
*
4646
* (Required)
47-
*
47+
*
4848
*/
4949
@JsonProperty("criteria")
5050
public Criteria getCriteria() {
5151
return criteria;
5252
}
5353

5454
/**
55-
*
55+
*
5656
* (Required)
57-
*
57+
*
5858
*/
5959
@JsonProperty("criteria")
6060
public void setCriteria(Criteria criteria) {

src/main/java/com/ericsson/ei/rules/RulesHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public RulesObject getRulesForEvent(String event) {
103103
return null;
104104
}
105105

106+
/**
107+
* Returns the active rules used by Eiffel Intelligence.
108+
*
109+
* @return rules content
110+
*/
111+
public JsonNode getRulesContent() {
112+
return parsedJson;
113+
}
114+
106115
/**
107116
* Reads the content of a given rules file path or URL.
108117
*

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
description: This class is used to test rules on Eiffel events. Testrules must be enabled in Eiffel Intelligence.
1+
description: This class is used for anything rules related. It's currently used to test rules on Eiffel events
2+
and to fetch the active rules file content. Test rules must be enabled in Eiffel Intelligence
3+
for the /rule-check endpoints to work.
4+
get:
5+
description: This method returns the active rules content.
6+
responses:
7+
200:
8+
body:
9+
application/string:
10+
211
/rule-check:
312
post:
413
description: This method extracts data from a single Eiffel event based on the given JMESPath expression.

src/test/java/com/ericsson/ei/rules/test/TestRulesRestAPI.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.springframework.util.SocketUtils;
4949

5050
import com.ericsson.ei.App;
51-
import com.ericsson.ei.controller.RuleCheckControllerImpl;
51+
import com.ericsson.ei.controller.RuleControllerImpl;
5252
import com.ericsson.ei.services.IRuleCheckService;
5353

5454
@RunWith(SpringJUnit4ClassRunner.class)
@@ -158,8 +158,8 @@ public void testGetTestRulePageEnabledAPI_ensurePropertyFalse() throws Exception
158158
@Test
159159
public void testGetTestRulePageEnabledAPI_setPropertyTrue() throws Exception {
160160
String responseBody = new JSONObject().put("status", true).toString();
161-
RuleCheckControllerImpl ruleCheckControllerImpl = new RuleCheckControllerImpl();
162-
ruleCheckControllerImpl.setTestEnable(true);
161+
RuleControllerImpl ruleCheckControllerImpl = new RuleControllerImpl();
162+
ruleCheckControllerImpl.setTestEnabled(true);
163163
ResponseEntity<?> responseEntity = ruleCheckControllerImpl.getRuleCheckTestRulePageEnabled();
164164
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
165165
assertEquals(responseBody, responseEntity.getBody().toString());
@@ -169,8 +169,8 @@ public void testGetTestRulePageEnabledAPI_setPropertyTrue() throws Exception {
169169
@Test
170170
public void testGetTestRulePageEnabledAPI_setPropertyFalse() throws Exception {
171171
String responseBody = new JSONObject().put("status", false).toString();
172-
RuleCheckControllerImpl ruleCheckControllerImpl = new RuleCheckControllerImpl();
173-
ruleCheckControllerImpl.setTestEnable(false);
172+
RuleControllerImpl ruleCheckControllerImpl = new RuleControllerImpl();
173+
ruleCheckControllerImpl.setTestEnabled(false);
174174
ResponseEntity<?> responseEntity = ruleCheckControllerImpl.getRuleCheckTestRulePageEnabled();
175175
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
176176
assertEquals(responseBody, responseEntity.getBody().toString());

0 commit comments

Comments
 (0)