Skip to content

Commit 604b2e9

Browse files
Result filter (#170)
* Implemented a new JMESPath extension that takes one object and a key (key can be a whole path, partial path, or only simple key). It search through the object and finds all values that has this key. Then it returns a value or a list of values. Added extra query parameter to filterFormParam. The new parameter calls filter and it is a string that is send to JMESPath function. filterFormParam can now return a JSONArray with elements that contains key: objectId and value: value or list of found values. Added four functional tests for filterFormParam and six for JMESPath extension.
1 parent 9d5b473 commit 604b2e9

File tree

17 files changed

+590
-82
lines changed

17 files changed

+590
-82
lines changed

src/functionaltests/java/com/ericsson/ei/query/QueryAggregatedObjectsTestSteps.java

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ public class QueryAggregatedObjectsTestSteps extends FunctionalTestBase {
3737

3838
private static final Logger LOGGER = LoggerFactory.getLogger(QueryAggregatedObjectsTestSteps.class);
3939

40-
private static final String AGGREGATED_OBJ_JSON_PATH = "src/test/resources/AggregatedDocument.json";
40+
private static final String AGGREGATED_OBJ_JSON_PATH = "src/test/resources/AggregatedDocumentInternalCompositionLatest.json";
4141
private static final String MISSED_NOTIFICATION_JSON_PATH = "src/test/resources/MissedNotification.json";
4242
private static final String QUERY_1_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject1.json";
4343
private static final String QUERY_2_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject2.json";
44+
private static final String QUERY_3_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject3.json";
45+
private static final String QUERY_4_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject4.json";
46+
private static final String QUERY_5_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject5.json";
47+
private static final String QUERY_6_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject6.json";
4448

4549
@LocalServerPort
4650
private int applicationPort;
@@ -300,6 +304,86 @@ public void check_missed_notification_has_been_returned() throws Throwable {
300304
expectedResponse, responseAsString);
301305
}
302306

307+
@And("^Perform a query on created Aggregated object with filter$")
308+
public void perform_valid_query_and_filter_on_aggregated_object() throws Throwable {
309+
final String expectedResponse = "[{\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\":\"\\\"33d05e6f-9bd9-4138-83b6-e20cc74680a3\\\"\"}]";
310+
final String entryPoint = "/query";
311+
312+
String query1 = FileUtils.readFileToString(new File(QUERY_3_FILE_NAME), "UTF-8");
313+
314+
List<String> queries = new ArrayList<>();
315+
queries.add(query1);
316+
317+
for (String query : queries) {
318+
LOGGER.debug("Freestyle querying for the AggregatedObject with criteria: " + query);
319+
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
320+
response = postRequest.setPort(applicationPort)
321+
.setHost(hostName)
322+
.addHeader("content-type", "application/json")
323+
.addHeader("Accept", "application/json")
324+
.setEndpoint(entryPoint)
325+
.setBody(query)
326+
.performRequest();
327+
328+
LOGGER.debug(
329+
"Response of /query RestApi, Status Code: " + response.getStatusCodeValue() + "\nResponse: " + response.getBody().toString());
330+
331+
String responseAsString = response.getBody().toString();
332+
int reponseStatusCode = response.getStatusCodeValue();
333+
334+
assertEquals(HttpStatus.OK.toString(), Integer.toString(reponseStatusCode));
335+
assertEquals("Failed to compare actual response:\n" + responseAsString + "\nwith expected response:\n" + expectedResponse,
336+
expectedResponse, responseAsString);
337+
}
338+
}
339+
340+
@And("^Perform a query and filter with part of path$")
341+
public void perform__query_and_filter_with_part_of_path() throws Throwable {
342+
final String expectedResponse = "[{\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\":\"\\\"[1481875921843, 1481875988767, 1481875921763, 1481875944272, 5005, 1481875891763, 2000]\\\"\"}]";
343+
final String expectedResponse2 = "[{\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\":\"\\\"null\\\"\"}]";
344+
final String expectedResponse3 = "[{\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\":\"\\\"[33d05e6f-9bd9-4138-83b6-e20cc74680a3, 33d05e6f-9bd9-4138-83b6-e20cc74681b5]\\\"\"}]";
345+
final String entryPoint = "/query";
346+
347+
List<String> expectedResponses = new ArrayList<String>();
348+
expectedResponses.add(expectedResponse);
349+
expectedResponses.add(expectedResponse2);
350+
expectedResponses.add(expectedResponse3);
351+
352+
String query1 = FileUtils.readFileToString(new File(QUERY_4_FILE_NAME), "UTF-8");
353+
String query2 = FileUtils.readFileToString(new File(QUERY_5_FILE_NAME), "UTF-8");
354+
String query3 = FileUtils.readFileToString(new File(QUERY_6_FILE_NAME), "UTF-8");
355+
356+
List<String> queries = new ArrayList<>();
357+
queries.add(query1);
358+
queries.add(query2);
359+
queries.add(query3);
360+
361+
int pos = 0;
362+
for (String query : queries) {
363+
LOGGER.debug("Freestyle querying for the AggregatedObject with criteria: " + query);
364+
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
365+
response = postRequest.setPort(applicationPort)
366+
.setHost(hostName)
367+
.addHeader("content-type", "application/json")
368+
.addHeader("Accept", "application/json")
369+
.setEndpoint(entryPoint)
370+
.setBody(query)
371+
.performRequest();
372+
373+
LOGGER.debug(
374+
"Response of /query RestApi, Status Code: " + response.getStatusCodeValue() + "\nResponse: " + response.getBody().toString());
375+
376+
String responseAsString = response.getBody().toString();
377+
int reponseStatusCode = response.getStatusCodeValue();
378+
379+
assertEquals(HttpStatus.OK.toString(), Integer.toString(reponseStatusCode));
380+
assertEquals("Failed to compare actual response:\n" + responseAsString + "\nwith expected response:\n" + expectedResponses.get(pos),
381+
expectedResponses.get(pos), responseAsString);
382+
383+
pos++;
384+
}
385+
}
386+
303387
/**
304388
* Method that creates a document in MongoDb database.
305389
*

src/functionaltests/resources/features/queryAggregatedObjects.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ Feature: QueryAggregatedObjectsTestSteps
1111
And Perform an invalid freesyle query on Aggregated object
1212
And Perform a query for missed notification
1313
And Check missed notification has been returned
14+
And Perform a query on created Aggregated object with filter
15+
And Perform a query and filter with part of path
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"criteria": {
3+
"aggregatedObject.testCaseExecutions": {
4+
"$elemMatch": {
5+
"outcome.conclusion": "SUCCESSFUL",
6+
"outcome.id": "TC5"
7+
}
8+
}
9+
},
10+
"options": {
11+
"aggregatedObject.gav.groupId": "com.mycompany.myproduct"
12+
},
13+
"filter" : "incomplete_path_filter(@, 'aggregatedObject.publications[0].eventId')"
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"criteria": {
3+
"aggregatedObject.testCaseExecutions": {
4+
"$elemMatch": {
5+
"outcome.conclusion": "SUCCESSFUL",
6+
"outcome.id": "TC5"
7+
}
8+
}
9+
},
10+
"options": {
11+
"aggregatedObject.gav.groupId": "com.mycompany.myproduct"
12+
},
13+
"filter" : "incomplete_path_filter(@, 'time')"
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"criteria": {
3+
"aggregatedObject.gav.groupId":"com.mycompany.myproduct",
4+
"aggregatedObject.gav.artifactId":"sub-system",
5+
"aggregatedObject.gav.version":"1.1.0"
6+
},
7+
"filter" : "incomplete_path_filter(@, 'svnIdentifier')"
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"criteria": {
3+
"aggregatedObject.testCaseExecutions": {
4+
"$elemMatch": {
5+
"outcome.conclusion": "SUCCESSFUL",
6+
"outcome.id": "TC5"
7+
}
8+
}
9+
},
10+
"options": {
11+
"aggregatedObject.gav.groupId": "com.mycompany.myproduct"
12+
},
13+
"filter" : "incomplete_path_filter(@, 'aggregatedObject.publications.eventId')"
14+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ public ResponseEntity<?> updateQuery(@RequestBody final QueryBody body) {
5252
try {
5353
JSONObject criteria = new JSONObject(body.getCriteria().getAdditionalProperties());
5454
JSONObject options = null;
55+
String filter = "";
5556
if (body.getOptions() != null) {
5657
options = new JSONObject(body.getOptions().getAdditionalProperties());
5758
}
59+
if (body.getFilter() != null) {
60+
filter = body.getFilter();
61+
}
5862

59-
JSONArray result = processQueryParams.filterFormParam(criteria, options);
63+
JSONArray result = processQueryParams.filterFormParam(criteria, options, filter);
6064
return new ResponseEntity<>(result.toString(), HttpStatus.OK);
6165
} catch (Exception e) {
6266
String errorMessage = "Failed to extract data from the Aggregated Object using freestyle query. Error message:\n" + e.getMessage();

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
@JsonInclude(JsonInclude.Include.NON_NULL)
1717
@JsonPropertyOrder({
1818
"criteria",
19-
"options"
19+
"options",
20+
"filter"
2021
})
2122
public class QueryBody {
2223

2324
@JsonProperty("criteria")
2425
private Criteria criteria;
2526
@JsonProperty("options")
2627
private Options options;
28+
@JsonProperty("filter")
29+
private String filter;
2730
@JsonIgnore
2831
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
2932

@@ -47,6 +50,16 @@ public void setOptions(Options options) {
4750
this.options = options;
4851
}
4952

53+
@JsonProperty("filter")
54+
public String getFilter() {
55+
return filter;
56+
}
57+
58+
@JsonProperty("filter")
59+
public void setFilter(String filter) {
60+
this.filter = filter;
61+
}
62+
5063
@Override
5164
public String toString() {
5265
return ToStringBuilder.reflectionToString(this);
@@ -64,7 +77,7 @@ public void setAdditionalProperty(String name, Object value) {
6477

6578
@Override
6679
public int hashCode() {
67-
return new HashCodeBuilder().append(criteria).append(options).append(additionalProperties).toHashCode();
80+
return new HashCodeBuilder().append(criteria).append(options).append(filter).append(additionalProperties).toHashCode();
6881
}
6982

7083
@Override
@@ -76,7 +89,7 @@ public boolean equals(Object other) {
7689
return false;
7790
}
7891
QueryBody rhs = ((QueryBody) other);
79-
return new EqualsBuilder().append(criteria, rhs.criteria).append(options, rhs.options).append(additionalProperties, rhs.additionalProperties).isEquals();
92+
return new EqualsBuilder().append(criteria, rhs.criteria).append(options, rhs.options).append(filter, rhs.filter).append(additionalProperties, rhs.additionalProperties).isEquals();
8093
}
8194

8295
}

0 commit comments

Comments
 (0)