Skip to content

Commit e68e673

Browse files
Added functional tests for Rules Checker (#118)
1 parent 38f0030 commit e68e673

File tree

7 files changed

+202
-35
lines changed

7 files changed

+202
-35
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.ericsson.ei.rules;
2+
3+
import com.ericsson.ei.controller.RuleCheckController;
4+
import com.ericsson.ei.utils.FunctionalTestBase;
5+
import cucumber.api.java.en.Given;
6+
import cucumber.api.java.en.Then;
7+
import cucumber.api.java.en.When;
8+
import org.apache.commons.io.FileUtils;
9+
import org.json.JSONArray;
10+
import org.json.JSONObject;
11+
import org.junit.Ignore;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.test.util.ReflectionTestUtils;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.MvcResult;
18+
19+
import java.io.File;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
23+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
24+
25+
@Ignore
26+
@AutoConfigureMockMvc
27+
public class RuleCheckSteps extends FunctionalTestBase {
28+
29+
private static final String TEST_RESOURCES_PATH = "src/test/resources";
30+
31+
@Autowired
32+
private RuleCheckController ruleCheckController;
33+
34+
@Autowired
35+
private MockMvc mockMvc;
36+
37+
private MvcResult mvcResult;
38+
39+
private String rules;
40+
private String events;
41+
42+
@Given("^rules checking is enabled$")
43+
public void rules_checking_is_enabled() throws Throwable {
44+
ReflectionTestUtils.setField(ruleCheckController, "testEnable", true);
45+
}
46+
47+
@Given("^rules checking is not enabled$")
48+
public void rules_checking_is_not_enabled() throws Throwable {
49+
ReflectionTestUtils.setField(ruleCheckController, "testEnable", false);
50+
}
51+
52+
@Given("^file with JMESPath rules \"([^\"]*)\" and file with events \"([^\"]*)\"$")
53+
public void file_with_JMESPath_rules_and_file_with_events(String rulesFileName, String eventsFileName) throws Throwable {
54+
rules = FileUtils.readFileToString(new File(TEST_RESOURCES_PATH + rulesFileName), "UTF-8");
55+
events = FileUtils.readFileToString(new File(TEST_RESOURCES_PATH + eventsFileName), "UTF-8");
56+
}
57+
58+
@When("^make a POST request to the REST API \"([^\"]*)\" with request parameter \"([^\"]*)\"$")
59+
public void make_a_POST_request_to_the_REST_API_with_request_parameter(String endpoint, String requestParam) throws Throwable {
60+
mvcResult = mockMvc.perform(post(endpoint)
61+
.param(requestParam, rules)
62+
.accept(MediaType.APPLICATION_JSON)
63+
.content(events)
64+
.contentType(MediaType.APPLICATION_JSON))
65+
.andReturn();
66+
}
67+
68+
@When("^make a POST request to the REST API \"([^\"]*)\"$")
69+
public void make_a_POST_request_to_the_REST_API(String endpoint) throws Throwable {
70+
String requestBody = new JSONObject()
71+
.put("listRulesJson", new JSONArray(rules))
72+
.put("listEventsJson", new JSONArray(events))
73+
.toString();
74+
mvcResult = mockMvc.perform(post(endpoint)
75+
.accept(MediaType.APPLICATION_JSON)
76+
.content(requestBody)
77+
.contentType(MediaType.APPLICATION_JSON))
78+
.andReturn();
79+
}
80+
81+
@Then("^get response code of (\\d+)$")
82+
public void get_response_code_of(int statusCode) throws Throwable {
83+
assertEquals(statusCode, mvcResult.getResponse().getStatus());
84+
}
85+
86+
@Then("^get content \"([^\"]*)\"$")
87+
public void get_content(String contentFileName) throws Throwable {
88+
String responseBody = FileUtils.readFileToString(new File(TEST_RESOURCES_PATH + contentFileName), "UTF-8");
89+
assertEquals(responseBody, mvcResult.getResponse().getContentAsString(), true);
90+
}
91+
92+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ericsson.ei.rules;
2+
3+
import cucumber.api.CucumberOptions;
4+
import cucumber.api.junit.Cucumber;
5+
import org.junit.runner.RunWith;
6+
7+
@RunWith(Cucumber.class)
8+
@CucumberOptions(features = "src/functionaltests/resources/features/ruleCheck.feature", glue = {
9+
"com.ericsson.ei.rules" }, plugin = { "pretty",
10+
"html:target/cucumber-reports/TestRuleCheckRunner" }, monochrome = false)
11+
public class TestRuleCheckRunner {
12+
13+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#Author: valentin.tyhonov@ericsson.com
2+
#Keywords Summary :
3+
#Feature: List of scenarios.
4+
#Scenario: Business rule through list of steps with arguments.
5+
#Given: Some precondition step
6+
#When: Some key actions
7+
#Then: To observe outcomes or validation
8+
#And,But: To enumerate more Given,When,Then steps
9+
#Scenario Outline: List of steps for data-driven as an Examples and <placeholder>
10+
#Examples: Container for s table
11+
#Background: List of steps run before each of the scenarios
12+
#""" (Doc Strings)
13+
#| (Data Tables)
14+
#@ (Tags/Labels):To group Scenarios
15+
#<> (placeholder)
16+
#""
17+
## (Comments)
18+
#Sample Feature Definition Template
19+
@tag
20+
Feature: Test Rules Checker
21+
22+
@tag1
23+
Scenario: Execute JMESPath rule on JSON object
24+
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"
26+
Then get response code of 200
27+
And get content "/ExtractedContent.json"
28+
29+
#@tag2
30+
Scenario: Execute list of JMESPath rules on list of JSON objects
31+
Given rules checking is enabled
32+
And file with JMESPath rules "/AggregateListRules.json" and file with events "/AggregateListEvents.json"
33+
When make a POST request to the REST API "/rules/rule-check/aggregation"
34+
Then get response code of 200
35+
And get content "/AggregateResultObject.json"
36+
37+
#@tag3
38+
Scenario: Execute incorrect list of JMESPath rules on list of JSON objects
39+
Given rules checking is enabled
40+
And file with JMESPath rules "/AggregateListRules.json" and file with events "/subscription_single.json"
41+
When make a POST request to the REST API "/rules/rule-check/aggregation"
42+
Then get response code of 400
43+
44+
#@tag4
45+
Scenario: Execute list of JMESPath rules on list of JSON objects, when rules checking is not enabled
46+
Given rules checking is not enabled
47+
And file with JMESPath rules "/AggregateListRules.json" and file with events "/AggregateListEvents.json"
48+
When make a POST request to the REST API "/rules/rule-check/aggregation"
49+
Then get response code of 503

src/main/java/com/ericsson/ei/erqueryservice/ERQueryService.java

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import lombok.Getter;
22+
import org.apache.commons.lang3.StringUtils;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425
import org.springframework.beans.factory.annotation.Value;
@@ -47,8 +48,10 @@
4748
@Component
4849
public class ERQueryService {
4950

50-
static Logger log = (Logger) LoggerFactory.getLogger(ERQueryService.class);
51+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(ERQueryService.class);
52+
5153
private RestOperations rest;
54+
5255
@Getter
5356
@Value("${er.url}")
5457
private String url;
@@ -70,16 +73,20 @@ public void setRest(RestOperations rest) {
7073
* @return ResponseEntity
7174
*/
7275
public ResponseEntity<String> getEventDataById(String eventId) {
73-
final String erUrl = URI.create(url.trim() + "/" + "{id}").normalize().toString();
74-
log.debug("The URL to ER is: " + erUrl);
75-
76-
final Map<String, String> params = Collections.singletonMap("id", eventId);
77-
log.trace("The ID parameter is set");
78-
try {
79-
final ResponseEntity<String> response = rest.getForEntity(erUrl, String.class, params);
80-
log.trace("The response is : " + response.toString());
81-
} catch (RestClientException e) {
82-
log.error("Error occurred while executing REST GET to: " + erUrl + " for " + eventId, e);
76+
if(StringUtils.isNotBlank(url)) {
77+
final String erUrl = URI.create(url.trim() + "/" + "{id}").normalize().toString();
78+
LOGGER.debug("The URL to ER is: " + erUrl);
79+
80+
final Map<String, String> params = Collections.singletonMap("id", eventId);
81+
LOGGER.trace("The ID parameter is set");
82+
try {
83+
final ResponseEntity<String> response = rest.getForEntity(erUrl, String.class, params);
84+
LOGGER.trace("The response is : " + response.toString());
85+
} catch (RestClientException e) {
86+
LOGGER.error("Error occurred while executing REST GET to: " + erUrl + " for " + eventId, e);
87+
}
88+
} else {
89+
LOGGER.info("The URL to ER is not provided");
8390
}
8491

8592
return null;
@@ -108,26 +115,30 @@ public ResponseEntity<String> getEventDataById(String eventId) {
108115
public ResponseEntity<JsonNode> getEventStreamDataById(String eventId, SearchOption searchOption, int limit,
109116
int levels, boolean tree) {
110117

111-
final String erUrl = URI.create(url.trim() + "/" + eventId).normalize().toString();
112-
log.debug("The URL to ER is: " + erUrl);
113-
114-
// Request Body parameters
115-
final SearchParameters searchParameters = getSearchParameters(searchOption);
116-
117-
// Build query parameters
118-
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(erUrl).queryParam("limit", limit)
119-
.queryParam("levels", levels).queryParam("tree", tree);
120-
final HttpHeaders headers = new HttpHeaders();
121-
headers.setContentType(MediaType.APPLICATION_JSON);
122-
123-
final HttpEntity<SearchParameters> requestEntity = new HttpEntity<>(searchParameters, headers);
124-
final UriComponents uriComponents = builder.buildAndExpand(searchParameters);
125-
log.debug("The request is : " + uriComponents.toUri().toString());
126-
127-
try {
128-
return rest.exchange(uriComponents.toUri(), HttpMethod.POST, requestEntity, JsonNode.class);
129-
} catch (RestClientException e) {
130-
log.error("Error occurred while executing REST POST to: " + erUrl + " for\n" + requestEntity, e);
118+
if(StringUtils.isNotBlank(url)) {
119+
final String erUrl = URI.create(url.trim() + "/" + eventId).normalize().toString();
120+
LOGGER.debug("The URL to ER is: " + erUrl);
121+
122+
// Request Body parameters
123+
final SearchParameters searchParameters = getSearchParameters(searchOption);
124+
125+
// Build query parameters
126+
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(erUrl).queryParam("limit", limit)
127+
.queryParam("levels", levels).queryParam("tree", tree);
128+
final HttpHeaders headers = new HttpHeaders();
129+
headers.setContentType(MediaType.APPLICATION_JSON);
130+
131+
final HttpEntity<SearchParameters> requestEntity = new HttpEntity<>(searchParameters, headers);
132+
final UriComponents uriComponents = builder.buildAndExpand(searchParameters);
133+
LOGGER.debug("The request is : " + uriComponents.toUri().toString());
134+
135+
try {
136+
return rest.exchange(uriComponents.toUri(), HttpMethod.POST, requestEntity, JsonNode.class);
137+
} catch (RestClientException e) {
138+
LOGGER.error("Error occurred while executing REST POST to: " + erUrl + " for\n" + requestEntity, e);
139+
}
140+
} else {
141+
LOGGER.info("The URL to ER is not provided");
131142
}
132143

133144
return null;
@@ -164,6 +175,6 @@ private SearchParameters getSearchParameters(SearchOption searchOption) {
164175
@PostConstruct
165176
public void init() {
166177
// TODO: is this needed?
167-
log.debug("The url parameter is : " + url);
178+
LOGGER.debug("The url parameter is : " + url);
168179
}
169180
}

src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ spring.mail.password:
7575
spring.mail.properties.mail.smtp.auth: false
7676
spring.mail.properties.mail.smtp.starttls.enable: false
7777

78-
er.url: http://localhost:8080/search/
78+
er.url:
79+
#er.url: http://localhost:8080/search/
7980

8081
ldap.enabled: false
8182
ldap.url:

src/test/java/com/ericsson/ei/erqueryservice/test/ERQueryServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class ERQueryServiceTest {
7777
public static void init() {
7878
int port = SocketUtils.findAvailableTcpPort();
7979
System.setProperty("spring.data.mongodb.port", "" + port);
80+
System.setProperty("er.url", "http://localhost:8080/search/");
8081
}
8182

8283
@Before public void setUp() throws Exception {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class TestRulesRestAPI {
6161
private static final String EXTRACTION_RULE_FILE_PATH = "src/test/resources/ExtractionRule.txt";
6262
private static final String EVENTS = "src/test/resources/AggregateListEvents.json";
6363
private static final String RULES = "src/test/resources/AggregateListRules.json";
64-
private static final String AGGREATED_RESULT_OBJECT = "src/test/resources/AggregateResultObject.json";
64+
private static final String AGGREGATED_RESULT_OBJECT = "src/test/resources/AggregateResultObject.json";
6565

6666
private static final String ENVIRONMENT_DISABLED = "{\"message\": \"Test environment is not enabled. "
6767
+ "Please use the test environment for this execution\"}";
@@ -114,7 +114,7 @@ public void testAggregationRestApi() throws Exception {
114114
try {
115115
jsonInput = new JSONArray(FileUtils.readFileToString(new File(EVENTS), "UTF-8"));
116116
extractionRules_test = new JSONArray(FileUtils.readFileToString(new File(RULES), "UTF-8"));
117-
aggregatedResult = FileUtils.readFileToString(new File(AGGREATED_RESULT_OBJECT), "UTF-8");
117+
aggregatedResult = FileUtils.readFileToString(new File(AGGREGATED_RESULT_OBJECT), "UTF-8");
118118
expectedAggObject = new JSONArray(aggregatedResult);
119119
} catch (Exception e) {
120120
LOGGER.error(e.getMessage(), e);

0 commit comments

Comments
 (0)