Skip to content

Commit 61dc2c9

Browse files
authored
Add more complex queries in the aggregated object tests (#158)
* Add more query aggregated object tests - Added more complex tests for aggregated object. - Removed GET for /query endpoint - Fix query controller tests
1 parent 0f5387e commit 61dc2c9

File tree

10 files changed

+67
-90
lines changed

10 files changed

+67
-90
lines changed

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import java.io.File;
66
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
710
import org.apache.commons.io.FileUtils;
811
import org.junit.Ignore;
912
import org.slf4j.Logger;
@@ -16,11 +19,7 @@
1619
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1720
import org.springframework.boot.web.server.LocalServerPort;
1821
import org.springframework.http.HttpStatus;
19-
import org.springframework.http.MediaType;
2022
import org.springframework.http.ResponseEntity;
21-
import org.springframework.test.web.servlet.MockMvc;
22-
import org.springframework.test.web.servlet.MvcResult;
23-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2423

2524
import cucumber.api.java.en.And;
2625
import cucumber.api.java.en.Given;
@@ -38,10 +37,10 @@ public class QueryAggregatedObjectsTestSteps extends FunctionalTestBase {
3837

3938
private static final Logger LOGGER = LoggerFactory.getLogger(QueryAggregatedObjectsTestSteps.class);
4039

41-
4240
private static final String AGGREGATED_OBJ_JSON_PATH = "src/test/resources/AggregatedDocument.json";
4341
private static final String MISSED_NOTIFICATION_JSON_PATH = "src/test/resources/MissedNotification.json";
44-
42+
private static final String QUERY_1_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject1.json";
43+
private static final String QUERY_2_FILE_NAME = "src/functionaltests/resources/queryAggregatedObject2.json";
4544

4645
@LocalServerPort
4746
private int applicationPort;
@@ -154,37 +153,46 @@ public void perform_invalid_query_on_created_aggregated_object() throws Throwabl
154153
expectedResponse, responseAsString);
155154
}
156155

157-
@And("^Perform valid freestyle query on created Aggregated object$")
158-
public void perform_valid_freestyle_query_on_created_aggregated_object() throws Throwable {
156+
@Then("^Perform several valid freestyle queries on created Aggregated objects$")
157+
public void perform_several_valid_freestyle_queries_on_created_Aggregated_objects() throws Throwable {
158+
159159
final String expectedAggrId = "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43";
160160
final String entryPoint = "/query";
161-
final String queryAggrObj = "{\"criteria\" :{\"aggregatedObject.id\" : \"" + expectedAggrId + "\", \"aggregatedObject.gav.groupId\" : \"com.mycompany.myproduct\"}}";
162161

163-
LOGGER.debug("Freestyle querying for the AggregatedObject with criteria: " + queryAggrObj);
162+
String query1 = FileUtils.readFileToString(new File(QUERY_1_FILE_NAME), "UTF-8");
163+
String query2 = FileUtils.readFileToString(new File(QUERY_2_FILE_NAME), "UTF-8");
164164

165-
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
166-
response = getRequest.setPort(applicationPort)
167-
.setHost(hostName)
168-
.addHeader("content-type", "application/json")
169-
.addHeader("Accept", "application/json")
170-
.setEndpoint(entryPoint)
171-
.addParam("request", queryAggrObj)
172-
.performRequest();
165+
List<String> queries = new ArrayList<>();
166+
queries.add(query1);
167+
queries.add(query2);
173168

174-
LOGGER.debug("Response of /query RestApi, Status Code: " + response.getStatusCodeValue() +
175-
"\nResponse: " + response.getBody().toString());
169+
for(String query: queries) {
170+
LOGGER.debug("Freestyle querying for the AggregatedObject with criteria: " + query);
176171

177-
JsonNode jsonNodeResult = objMapper.readValue(response.getBody().toString(), JsonNode.class);
178-
JsonNode aggrObjResponse = objMapper.readValue(jsonNodeResult.get(0).get("aggregatedObject").toString(), JsonNode.class);
172+
HttpRequest postRequest = new HttpRequest(HttpMethod.POST);
173+
response = postRequest.setPort(applicationPort)
174+
.setHost(hostName)
175+
.addHeader("content-type", "application/json")
176+
.addHeader("Accept", "application/json")
177+
.setEndpoint(entryPoint)
178+
.addParam("request", query)
179+
.performRequest();
179180

180-
String actualAggrObjId = aggrObjResponse.get("id").asText();
181-
LOGGER.debug("AggregatedObject id from Response: " + actualAggrObjId);
181+
LOGGER.debug("Response of /query RestApi, Status Code: " + response.getStatusCodeValue() +
182+
"\nResponse: " + response.getBody().toString());
182183

184+
JsonNode jsonNodeResult = objMapper.readValue(response.getBody().toString(), JsonNode.class);
185+
JsonNode aggrObjResponse = objMapper.readValue(jsonNodeResult.get(0).get("aggregatedObject").toString(), JsonNode.class);
183186

184-
assertEquals(HttpStatus.OK.toString(), Integer.toString(response.getStatusCodeValue()));
185-
assertEquals("Failed to compare actual Aggregated Object Id:\n" + actualAggrObjId
186-
+ "\nwith expected Aggregated Object Id:\n" + expectedAggrId,
187-
expectedAggrId, actualAggrObjId);
187+
String actualAggrObjId = aggrObjResponse.get("id").asText();
188+
LOGGER.debug("AggregatedObject id from Response: " + actualAggrObjId);
189+
190+
191+
assertEquals(HttpStatus.OK.toString(), Integer.toString(response.getStatusCodeValue()));
192+
assertEquals("Failed to compare actual Aggregated Object Id:\n" + actualAggrObjId
193+
+ "\nwith expected Aggregated Object Id:\n" + expectedAggrId,
194+
expectedAggrId, actualAggrObjId);
195+
}
188196
}
189197

190198
@And("^Perform an invalid freesyle query on Aggregated object$")
@@ -196,7 +204,7 @@ public void perform_invalid_freestyle_query_on_created_aggregated_object() throw
196204

197205
LOGGER.debug("Trying an invalid query on /query RestApi with invalid criteria query: " + queryAggrObj);
198206

199-
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
207+
HttpRequest getRequest = new HttpRequest(HttpMethod.POST);
200208
response = getRequest.setPort(applicationPort)
201209
.setHost(hostName)
202210
.addHeader("content-type", "application/json")
@@ -309,4 +317,4 @@ private boolean createDocumentInMongoDb(String databaseName, String collectionNa
309317
return mongoDBHandler.insertDocument(databaseName, collectionName, objToBeInserted);
310318
}
311319

312-
}
320+
}

src/functionaltests/resources/features/queryAggregatedObjects.feature

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Feature: QueryAggregatedObjectsTestSteps
77
And Missed Notification object is created
88
Then Perform valid query on created Aggregated object
99
And Perform an invalid query on same Aggregated object
10-
And Perform valid freestyle query on created Aggregated object
10+
And Perform several valid freestyle queries on created Aggregated objects
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-
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"criteria": {
3+
"$or": [
4+
{
5+
"aggregatedObject.testCaseExecutions.outcome.id":"TC5"
6+
}, {
7+
"aggregatedObject.testCaseExecutions.outcome.id":"TC6"
8+
}
9+
],
10+
"aggregatedObject.gav.groupId":"com.mycompany.myproduct"
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.ericsson.ei.controller;
33

4-
import com.ericsson.ei.controller.model.QueryResponse;
54
import org.springframework.http.ResponseEntity;
65
import org.springframework.web.bind.annotation.RequestMapping;
76
import org.springframework.web.bind.annotation.RequestMethod;

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
public interface QueryController {
1919

2020

21-
/**
22-
* The REST GET method is used to query Aggregated Objects with the criterias requested, which are present as query parameters in the request URL.
23-
*
24-
*/
25-
@RequestMapping(value = "", method = RequestMethod.GET)
26-
public ResponseEntity<?> getQuery(
27-
@RequestParam
28-
String request);
29-
3021
/**
3122
* The REST POST method is used to query Aggregated Objects with the criterias requested, which are present in the request body of the request URL.
3223
*

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,4 @@ public ResponseEntity<?> updateQuery(@RequestParam(value = "request") String req
5757
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
5858
}
5959
}
60-
61-
@Override
62-
@CrossOrigin
63-
@ApiOperation(value = "")
64-
public ResponseEntity<?> getQuery(@RequestParam(value = "request") final String request) {
65-
try {
66-
JSONArray result = processQueryParams.filterQueryParam(request);
67-
LOGGER.debug("Final Output : " + result.toString());
68-
return new ResponseEntity<>(result.toString(), HttpStatus.OK);
69-
} catch (Exception e) {
70-
String errorMessage = "Failed to extract data from the Aggregated Object using freestyle query. Error message:\n" + e.getMessage();
71-
LOGGER.error(errorMessage, e);
72-
return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
73-
}
74-
}
7560
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public JSONArray filterFormParam(JsonNode request) {
6060
if (options == null || options.toString().equals("{}")) {
6161
resultAggregatedObject = processAggregatedObject.processQueryAggregatedObject(criteria.toString(), databaseName, aggregationCollectionName);
6262
} else {
63+
LOGGER.debug("The options is : " + options.toString());
6364
String result = "{ \"$and\" : [ " + criteria.toString() + "," + options.toString() + " ] }";
6465
resultAggregatedObject = processAggregatedObject.processQueryAggregatedObject(result, databaseName, aggregationCollectionName);
6566
}

src/main/resources/public/raml/query.raml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,3 @@ post:
1313
200:
1414
body:
1515
application/json:
16-
get:
17-
description: The REST GET method is used to query Aggregated Objects with the
18-
criterias requested, which are present as query parameters in the request
19-
URL.
20-
queryParameters:
21-
request:
22-
displayName: request
23-
type: string
24-
description: Query to MongoDB
25-
example: request=testCaseExecutions.testCase.verdict:PASSED
26-
required: true
27-
responses:
28-
200:
29-
body:
30-
application/json:

src/test/java/com/ericsson/ei/controller/TestQueryControllerImpl.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,16 @@
4040

4141
import static org.junit.Assert.assertEquals;
4242
import static org.mockito.Matchers.any;
43-
import static org.mockito.Matchers.anyString;
4443
import static org.mockito.Mockito.when;
45-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
46-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4744

4845
@RunWith(SpringJUnit4ClassRunner.class)
4946
@SpringBootTest(classes = {
50-
App.class,
47+
App.class,
5148
EmbeddedMongoAutoConfiguration.class // <--- Don't forget THIS
5249
})
5350
@AutoConfigureMockMvc
5451
public class TestQueryControllerImpl {
5552
private static final String inputPath = "src/test/resources/AggregatedObject.json";
56-
private static final String REQUEST = "testCaseExecutions.testCase.verdict:PASSED,testCaseExecutions.testCase.id:TC5,id:6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43";
5753
private static final String QUERY = "{\"criteria\" :{\"testCaseExecutions.testCase.verdict\":\"PASSED\", \"testCaseExecutions.testCase.id\":\"TC5\" }, \"options\" :{ \"id\": \"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\"} }";
5854
private static String input;
5955

@@ -62,7 +58,7 @@ public class TestQueryControllerImpl {
6258

6359
@Autowired
6460
private MockMvc mockMvc;
65-
61+
6662
@BeforeClass
6763
public static void init() {
6864
int port = SocketUtils.findAvailableTcpPort();
@@ -83,16 +79,4 @@ public void filterFormParamTest() throws Exception {
8379
.andReturn();
8480
assertEquals(inputObj.toString(), result.getResponse().getContentAsString());
8581
}
86-
87-
@Test
88-
public void filterQueryParamTest() throws Exception {
89-
JSONArray inputObj = new JSONArray("[" + input + "]");
90-
when(unitUnderTest.filterQueryParam(anyString())).thenReturn(inputObj);
91-
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/query")
92-
.param("request", REQUEST))
93-
.andExpect(status().isOk())
94-
.andDo(print())
95-
.andReturn();
96-
assertEquals(inputObj.toString(), result.getResponse().getContentAsString());
97-
}
9882
}

0 commit comments

Comments
 (0)