Skip to content

Commit 9d5e698

Browse files
authored
ADD: test for ProcessQueryParams (#152)
* ADD: test for ProcessQueryParams * ADD: a few scenarios
1 parent 179fc6c commit 9d5e698

File tree

6 files changed

+113
-27
lines changed

6 files changed

+113
-27
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import com.ericsson.ei.utils.FunctionalTestBase;
55
import com.ericsson.ei.utils.HttpRequest;
66
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
7-
87
import cucumber.api.java.en.Given;
98
import cucumber.api.java.en.Then;
109
import cucumber.api.java.en.When;
11-
1210
import org.apache.commons.io.FileUtils;
1311
import org.json.JSONArray;
1412
import org.json.JSONObject;
@@ -18,7 +16,9 @@
1816
import org.springframework.boot.web.server.LocalServerPort;
1917
import org.springframework.http.ResponseEntity;
2018
import org.springframework.test.util.ReflectionTestUtils;
19+
2120
import java.io.File;
21+
2222
import static org.junit.Assert.assertEquals;
2323
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
2424

@@ -70,9 +70,9 @@ public void make_a_POST_request_to_the_REST_API_with_request_parameter(String en
7070
@When("^make a POST request to the REST API \"([^\"]*)\"$")
7171
public void make_a_POST_request_to_the_REST_API(String endpoint) throws Throwable {
7272
String requestBody = new JSONObject()
73-
.put("listRulesJson", new JSONArray(rules))
74-
.put("listEventsJson", new JSONArray(events))
75-
.toString();
73+
.put("listRulesJson", new JSONArray(rules))
74+
.put("listEventsJson", new JSONArray(events))
75+
.toString();
7676

7777
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
7878
response = postRequest.setPort(applicationPort)
@@ -104,7 +104,7 @@ public void get_content(String contentFileName) throws Throwable {
104104
assertEquals(expectedObject, responseObject, true);
105105
}
106106
}
107-
107+
108108
@Then("^get request from REST API \"([^\"]*)\" return response code of (\\d+) and status as \"([^\"]*)\"$")
109109
public void get_request_from_REST_API_return_response_code_of_and_status_as(String endpoint, int statusCode, String status) throws Throwable {
110110
String responseBody = new JSONObject().put("status", Boolean.valueOf(status)).toString();
@@ -115,7 +115,7 @@ public void get_request_from_REST_API_return_response_code_of_and_status_as(Stri
115115
.addHeader("Accept", "application/json")
116116
.setEndpoint(endpoint)
117117
.performRequest();
118-
118+
119119
assertEquals(statusCode, apiResponse.getStatusCodeValue());
120120
assertEquals(responseBody, apiResponse.getBody());
121121
}

src/functionaltests/java/com/ericsson/ei/subscriptions/trigger/SubscriptionTriggerSteps.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void check_rest_subscriptions_were_triggered() throws Throwable {
168168

169169
/**
170170
* Assemble subscription names in a list.
171-
*
171+
*
172172
* @param jsonDataAsString
173173
* JSON string containing subscriptions
174174
* @throws Throwable
@@ -182,7 +182,7 @@ private void readSubscriptionNames(String jsonDataAsString) throws Throwable {
182182

183183
/**
184184
* POST subscriptions to endpoint.
185-
*
185+
*
186186
* @param jsonDataAsString
187187
* JSON string containing subscriptions
188188
* @param endPoint
@@ -202,7 +202,7 @@ private void postSubscriptions(String jsonDataAsString, String endPoint) throws
202202

203203
/**
204204
* Verify that subscriptions were successfully posted.
205-
*
205+
*
206206
* @param endPoint
207207
* endpoint to use in GET
208208
* @throws Exception
@@ -223,7 +223,7 @@ private void validateSubscriptionsSuccessfullyAdded(String endPoint) throws Exce
223223

224224
/**
225225
* Checks that an enpoint got at least the number of calls as expected.
226-
*
226+
*
227227
* @param endpoints
228228
* List of endpoints to check.
229229
* @param expectedCalls
@@ -251,7 +251,7 @@ private boolean allEndpointsGotAtLeastXCalls(final List<String> endpoints, int e
251251

252252
/**
253253
* Verify that request made to endpoint contains the correct information.
254-
*
254+
*
255255
* @param endpoint
256256
* endpoint to check
257257
* @return true if verification was successful, false otherwise
@@ -298,7 +298,7 @@ private void setupRestEndpoints() {
298298

299299
/**
300300
* Setup and start SMTP mock server.
301-
*
301+
*
302302
* @throws IOException
303303
*/
304304
private void setupSMTPServer() throws IOException {
@@ -322,7 +322,7 @@ protected List<String> getEventNamesToSend() {
322322

323323
/**
324324
* Replaces tags in the subscription JSON string with valid information.
325-
*
325+
*
326326
* @param text
327327
* JSON string containing replaceable tags
328328
* @return Processed content

src/functionaltests/resources/features/subscriptionTrigger.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ Feature: Subscription trigger test
2626
When I send Eiffel events
2727
And Wait for EI to aggregate objects and trigger subscriptions
2828
Then Mail subscriptions were triggered
29-
And Rest subscriptions were triggered
29+
And Rest subscriptions were triggered

src/main/java/com/ericsson/ei/queryservice/ProcessAggregatedObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public ArrayList<String> processQueryAggregatedObject(String id) {
5858
ObjectMapper mapper = new ObjectMapper();
5959
String query = "{\"aggregatedObject.id\": \"" + id + "\"}";
6060

61-
LOGGER.debug("The condition is : " + query.toString());
61+
LOGGER.debug("The condition is : " + query);
6262
JsonNode jsonCondition = null;
6363
try {
64-
jsonCondition = mapper.readTree(query.toString());
64+
jsonCondition = mapper.readTree(query);
6565
} catch (Exception e) {
6666
LOGGER.error(e.getMessage(), e);
6767
}

src/main/java/com/ericsson/ei/queryservice/ProcessQueryParams.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import com.ericsson.ei.controller.QueryControllerImpl;
1717
import com.fasterxml.jackson.databind.JsonNode;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19-
2019
import org.json.JSONArray;
2120
import org.slf4j.Logger;
2221
import org.slf4j.LoggerFactory;
@@ -35,7 +34,7 @@
3534
@Component
3635
public class ProcessQueryParams {
3736

38-
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(QueryControllerImpl.class);
37+
private static final Logger LOGGER = LoggerFactory.getLogger(QueryControllerImpl.class);
3938

4039
@Value("${aggregated.collection.name}")
4140
private String aggregationCollectionName;
@@ -54,13 +53,11 @@ public class ProcessQueryParams {
5453
* @return JSONArray
5554
* @throws IOException
5655
*/
57-
public JSONArray filterFormParam(JsonNode request) throws IOException {
56+
public JSONArray filterFormParam(JsonNode request) {
5857
JsonNode criteria = request.get("criteria");
5958
JsonNode options = request.get("options");
60-
LOGGER.debug("The criteria is : " + criteria.toString());
61-
LOGGER.debug("The options is : " + options.toString());
6259
JSONArray resultAggregatedObject;
63-
if (options.toString().equals("{}") || options.isNull()) {
60+
if (options == null || options.toString().equals("{}")) {
6461
resultAggregatedObject = processAggregatedObject.processQueryAggregatedObject(criteria.toString(), databaseName, aggregationCollectionName);
6562
} else {
6663
String result = "{ \"$and\" : [ " + criteria.toString() + "," + options.toString() + " ] }";
@@ -80,11 +77,10 @@ public JSONArray filterFormParam(JsonNode request) throws IOException {
8077
public JSONArray filterQueryParam(String request) {
8178
LOGGER.debug("The query string is : " + request);
8279
ObjectMapper mapper = new ObjectMapper();
83-
JsonNode criteriasJsonNode = null;
80+
JsonNode criteriasJsonNode;
8481
try {
8582
criteriasJsonNode = mapper.readValue(request, JsonNode.class).get("criteria");
86-
}
87-
catch (IOException e) {
83+
} catch (IOException e) {
8884
LOGGER.error("Failed to parse FreeStyle query critera field from request:\n" + request);
8985
return new JSONArray();
9086
}
@@ -95,6 +91,6 @@ public JSONArray filterQueryParam(String request) {
9591
@PostConstruct
9692
public void print() {
9793
LOGGER.debug("Aggregation Database : " + databaseName
98-
+ "\nAggregation Collection is : " + aggregationCollectionName);
94+
+ "\nAggregation Collection is : " + aggregationCollectionName);
9995
}
10096
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.ericsson.ei.queryservice.test;
2+
3+
import com.ericsson.ei.App;
4+
import com.ericsson.ei.queryservice.ProcessAggregatedObject;
5+
import com.ericsson.ei.queryservice.ProcessQueryParams;
6+
import com.fasterxml.jackson.core.io.JsonEOFException;
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.apache.qpid.util.FileUtils;
10+
import org.json.JSONArray;
11+
import org.json.JSONException;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
17+
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.boot.test.mock.mockito.MockBean;
19+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
20+
import org.springframework.util.SocketUtils;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.mockito.Mockito.when;
27+
28+
@RunWith(SpringJUnit4ClassRunner.class)
29+
@SpringBootTest(classes = {
30+
App.class,
31+
EmbeddedMongoAutoConfiguration.class
32+
})
33+
public class TestProcessQueryParams {
34+
35+
private static final String inputPath = "src/test/resources/AggregatedObject.json";
36+
private static final String REQUEST = "{\"criteria\":{\"testCaseExecutions.testCase.verdict\":\"PASSED\"}}";
37+
private static final String QUERY_WITH_CRITERIA_AND_OPTIONS = "{\"criteria\" :{\"testCaseExecutions.testCase.verdict\":\"PASSED\", \"testCaseExecutions.testCase.id\":\"TC5\" }, \"options\" :{ \"id\": \"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\"} }";
38+
private static final String QUERY_WITH_CRITERIA = "{\"criteria\" :{\"testCaseExecutions.testCase.verdict\":\"PASSED\", \"testCaseExecutions.testCase.id\":\"TC5\" }}";
39+
private static final String QUERY_WITH_WRONG_FORMAT = "{\"criteria\" :{\"testCaseExecutions.testCase.verdict\":\"PASSED\", \"testCaseExecutions.testCase.id\":\"TC5\" }, \"options\" :{ \"id\": \"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\" }";
40+
private static final String DATA_BASE_NAME = "eiffel_intelligence";
41+
private static final String AGGREGATION_COLLECTION_NAME = "aggregated_objects";
42+
private static JSONArray expected;
43+
private ObjectMapper mapper = new ObjectMapper();
44+
45+
@Autowired
46+
private ProcessQueryParams processQueryParams;
47+
48+
@MockBean
49+
private ProcessAggregatedObject processAggregatedObject;
50+
51+
@BeforeClass
52+
public static void setUp() throws JSONException {
53+
String input = FileUtils.readFileAsString(new File(inputPath));
54+
int port = SocketUtils.findAvailableTcpPort();
55+
System.setProperty("spring.data.mongodb.port", "" + port);
56+
expected = new JSONArray("[" + input + "]");
57+
}
58+
59+
@Test
60+
public void testFilterFormParam() throws IOException {
61+
JsonNode criteria = mapper.readTree(QUERY_WITH_CRITERIA_AND_OPTIONS).get("criteria");
62+
JsonNode options = mapper.readTree(QUERY_WITH_CRITERIA_AND_OPTIONS).get("options");
63+
String request = "{ \"$and\" : [ " + criteria.toString() + "," + options.toString() + " ] }";
64+
when(processAggregatedObject.processQueryAggregatedObject(request, DATA_BASE_NAME, AGGREGATION_COLLECTION_NAME)).thenReturn(expected);
65+
JSONArray result = processQueryParams.filterFormParam(new ObjectMapper().readTree(QUERY_WITH_CRITERIA_AND_OPTIONS));
66+
assertEquals(expected, result);
67+
}
68+
69+
@Test
70+
public void testFilterFormParamWithOnlyCriteria() throws IOException {
71+
JsonNode criteria = mapper.readTree(QUERY_WITH_CRITERIA).get("criteria");
72+
when(processAggregatedObject.processQueryAggregatedObject(criteria.toString(), DATA_BASE_NAME, AGGREGATION_COLLECTION_NAME)).thenReturn(expected);
73+
JSONArray result = processQueryParams.filterFormParam(new ObjectMapper().readTree(QUERY_WITH_CRITERIA));
74+
assertEquals(expected, result);
75+
}
76+
77+
@Test(expected = JsonEOFException.class)
78+
public void testFilterFormParamWithWrongFormat() throws IOException {
79+
JSONArray result = processQueryParams.filterFormParam(new ObjectMapper().readTree(QUERY_WITH_WRONG_FORMAT));
80+
assertEquals(expected, result);
81+
}
82+
83+
@Test
84+
public void testFilterQueryParam() throws IOException {
85+
JsonNode criteria = mapper.readValue(REQUEST, JsonNode.class).get("criteria");
86+
when(processAggregatedObject.processQueryAggregatedObject(criteria.toString(), DATA_BASE_NAME, AGGREGATION_COLLECTION_NAME)).thenReturn(expected);
87+
JSONArray result = processQueryParams.filterQueryParam(REQUEST);
88+
assertEquals(expected, result);
89+
}
90+
}

0 commit comments

Comments
 (0)