Skip to content

Commit 61e29e3

Browse files
authored
Replace mockmvc in QueryAggregateObjects (#150)
* Replace mockmvc in QueryAggregateObjects
1 parent 642e5e5 commit 61e29e3

File tree

1 file changed

+123
-91
lines changed

1 file changed

+123
-91
lines changed

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

Lines changed: 123 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import org.springframework.beans.factory.annotation.Autowired;
1515
import org.springframework.beans.factory.annotation.Value;
1616
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
17+
import org.springframework.boot.web.server.LocalServerPort;
1718
import org.springframework.http.HttpStatus;
1819
import org.springframework.http.MediaType;
20+
import org.springframework.http.ResponseEntity;
1921
import org.springframework.test.web.servlet.MockMvc;
2022
import org.springframework.test.web.servlet.MvcResult;
2123
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -26,6 +28,8 @@
2628

2729
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
2830
import com.ericsson.ei.utils.FunctionalTestBase;
31+
import com.ericsson.ei.utils.HttpRequest;
32+
import com.ericsson.ei.utils.HttpRequest.HttpMethod;
2933

3034

3135
@Ignore
@@ -34,35 +38,36 @@ public class QueryAggregatedObjectsTestSteps extends FunctionalTestBase {
3438

3539
private static final Logger LOGGER = LoggerFactory.getLogger(QueryAggregatedObjectsTestSteps.class);
3640

37-
41+
3842
private static final String AGGREGATED_OBJ_JSON_PATH = "src/test/resources/AggregatedDocument.json";
3943
private static final String MISSED_NOTIFICATION_JSON_PATH = "src/test/resources/MissedNotification.json";
4044

41-
42-
@Autowired
43-
private MockMvc mockMvc;
44-
private MvcResult mvcResult;
45-
45+
46+
@LocalServerPort
47+
private int applicationPort;
48+
private String hostName = getHostName();
49+
private ResponseEntity response;
50+
4651
@Autowired
4752
private MongoDBHandler mongoDBHandler;
48-
53+
4954
@Value("${spring.data.mongodb.database}")
5055
private String eiDatabaseName;
51-
56+
5257
@Value("${aggregated.collection.name}")
5358
private String aggrCollectionName;
54-
59+
5560
@Value("${missedNotificationCollectionName}")
5661
private String missedNotificationCollectionName;
57-
62+
5863
@Value("${missedNotificationDataBaseName}")
5964
private String missedNotificationDatabaseName;
6065

6166
private String aggrObj;
6267
private String missedNotificationObj;
63-
68+
6469
private ObjectMapper objMapper;
65-
70+
6671
public QueryAggregatedObjectsTestSteps() {
6772
objMapper = new ObjectMapper();
6873

@@ -75,7 +80,7 @@ public QueryAggregatedObjectsTestSteps() {
7580
e.printStackTrace();
7681
}
7782
}
78-
83+
7984
@Given("^Aggregated object is created$")
8085
public void aggregated_object_is_created() throws Throwable {
8186
LOGGER.debug("Creating aggregated object in MongoDb");
@@ -94,181 +99,208 @@ public void perform_valid_query_on_newly_created_aggregated_object() throws Thro
9499
final String entryPoint = "/queryAggregatedObject";
95100
final String documentId = "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43";
96101
LOGGER.debug("Got AggregateObject actual DocumentId after querying MongoDB: " + documentId);
97-
mvcResult = mockMvc.perform(get(entryPoint)
98-
.param("ID", documentId)
99-
.accept(MediaType.APPLICATION_JSON)
100-
.contentType(MediaType.APPLICATION_JSON))
101-
.andReturn();
102-
LOGGER.debug("Response of /queryAggregatedObject RestApi, Status Code: " + mvcResult.getResponse().getStatus() +
103-
"\nResponse: " + mvcResult.getResponse().getContentAsString());
104-
105-
JsonNode jsonNodeResult = objMapper.readValue(mvcResult.getResponse().getContentAsString(), JsonNode.class);
106-
JsonNode responseEntityNode = objMapper.readValue(jsonNodeResult.get("responseEntity").asText(), JsonNode.class);
102+
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
103+
response = getRequest.setPort(applicationPort)
104+
.setHost(hostName)
105+
.setHeaders("content-type", "application/json")
106+
.setHeaders("Accept", "application/json")
107+
.setEndpoint(entryPoint)
108+
.setParam("ID", documentId)
109+
.performRequest();
110+
111+
LOGGER.debug("Response of /queryAggregatedObject RestApi, Status Code: " + response.getStatusCodeValue() +
112+
"\nResponse: " + response.getBody().toString());
113+
114+
JsonNode jsonNodeResult = objMapper.readValue(response.getBody().toString(), JsonNode.class);
115+
JsonNode responseEntityNode = objMapper.readValue(jsonNodeResult.get("responseEntity").asText(), JsonNode.class);
107116
String responseEntityFormattedString = responseEntityNode.toString().
108117
substring(1, responseEntityNode.toString().length()-1);
109-
118+
110119
LOGGER.debug("AggregatedObject from Response: " + responseEntityFormattedString);
111120
JsonNode responseEntityFormattedJsonNode = objMapper.readValue(responseEntityFormattedString, JsonNode.class);
112121
String actualTestCaseFinishedEventId = responseEntityFormattedJsonNode.get("aggregatedObject")
113122
.get("testCaseExecutions").get(0).get("testCaseFinishedEventId").asText();
114-
115-
assertEquals(HttpStatus.OK.toString(), Integer.toString(mvcResult.getResponse().getStatus()));
123+
124+
assertEquals(HttpStatus.OK.toString(), Integer.toString(response.getStatusCodeValue()));
116125
assertEquals("Failed to compare actual Aggregated Object TestCaseFinishedEventId:\n" + actualTestCaseFinishedEventId
117126
+ "\nwith expected Aggregated Object TestCaseFinishedEventId:\n" + expectedTestCaseFinishedEventId,
118127
expectedTestCaseFinishedEventId, actualTestCaseFinishedEventId);
119128
}
120-
129+
121130
@And("^Perform an invalid query on same Aggregated object$")
122131
public void perform_invalid_query_on_created_aggregated_object() throws Throwable {
123132
final String invalidDocumentId = "6acc3c87-75e0-4aaa-88f5-b1a5d4e6cccc";
124133
final String entryPoint = "/queryAggregatedObject";
125134
final String expectedResponse = "{\"responseEntity\":\"[]\"}";
126-
135+
127136
LOGGER.debug("Trying an invalid query on /queryAggregatedObject RestApi with invalid documentId: " + invalidDocumentId);
128-
mvcResult = mockMvc.perform(get(entryPoint)
129-
.param("ID", invalidDocumentId)
130-
.accept(MediaType.APPLICATION_JSON)
131-
.contentType(MediaType.APPLICATION_JSON))
132-
.andReturn();
133-
String responseAsString = mvcResult.getResponse().getContentAsString();
134-
int reponseStatusCode = mvcResult.getResponse().getStatus();
137+
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
138+
response = getRequest.setPort(applicationPort)
139+
.setHost(hostName)
140+
.setHeaders("content-type", "application/json")
141+
.setHeaders("Accept", "application/json")
142+
.setEndpoint(entryPoint)
143+
.setParam("ID", invalidDocumentId)
144+
.performRequest();
145+
146+
String responseAsString = response.getBody().toString();
147+
int reponseStatusCode = response.getStatusCodeValue();
135148
LOGGER.debug("Response of /queryAggregatedObject RestApi, Status Code: " + reponseStatusCode +
136149
"\nResponse: " + responseAsString);
137-
150+
138151
assertEquals(HttpStatus.OK.toString(), Integer.toString(reponseStatusCode));
139152
assertEquals("Diffences between actual Aggregated Object:\n" + responseAsString
140153
+ "\nand expected Aggregated Object:\n" + expectedResponse,
141154
expectedResponse, responseAsString);
142155
}
143-
156+
144157
@And("^Perform valid freestyle query on created Aggregated object$")
145158
public void perform_valid_freestyle_query_on_created_aggregated_object() throws Throwable {
146159
final String expectedAggrId = "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43";
147160
final String entryPoint = "/query";
148161
final String queryAggrObj = "{\"criteria\" :{\"aggregatedObject.id\" : \"" + expectedAggrId + "\", \"aggregatedObject.gav.groupId\" : \"com.mycompany.myproduct\"}}";
149162

150163
LOGGER.debug("Freestyle querying for the AggregatedObject with criteria: " + queryAggrObj);
151-
mvcResult = mockMvc.perform(get(entryPoint)
152-
.param("request", queryAggrObj)
153-
.accept(MediaType.APPLICATION_JSON)
154-
.contentType(MediaType.APPLICATION_JSON))
155-
.andReturn();
156-
LOGGER.debug("Response of /query RestApi, Status Code: " + mvcResult.getResponse().getStatus() +
157-
"\nResponse: " + mvcResult.getResponse().getContentAsString());
158-
159-
JsonNode jsonNodeResult = objMapper.readValue(mvcResult.getResponse().getContentAsString(), JsonNode.class);
160-
JsonNode aggrObjResponse = objMapper.readValue(jsonNodeResult.get(0).get("aggregatedObject").toString(), JsonNode.class);
164+
165+
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
166+
response = getRequest.setPort(applicationPort)
167+
.setHost(hostName)
168+
.setHeaders("content-type", "application/json")
169+
.setHeaders("Accept", "application/json")
170+
.setEndpoint(entryPoint)
171+
.setParam("request", queryAggrObj)
172+
.performRequest();
173+
174+
LOGGER.debug("Response of /query RestApi, Status Code: " + response.getStatusCodeValue() +
175+
"\nResponse: " + response.getBody().toString());
176+
177+
JsonNode jsonNodeResult = objMapper.readValue(response.getBody().toString(), JsonNode.class);
178+
JsonNode aggrObjResponse = objMapper.readValue(jsonNodeResult.get(0).get("aggregatedObject").toString(), JsonNode.class);
161179

162180
String actualAggrObjId = aggrObjResponse.get("id").asText();
163181
LOGGER.debug("AggregatedObject id from Response: " + actualAggrObjId);
164-
165-
166-
assertEquals(HttpStatus.OK.toString(), Integer.toString(mvcResult.getResponse().getStatus()));
182+
183+
184+
assertEquals(HttpStatus.OK.toString(), Integer.toString(response.getStatusCodeValue()));
167185
assertEquals("Failed to compare actual Aggregated Object Id:\n" + actualAggrObjId
168186
+ "\nwith expected Aggregated Object Id:\n" + expectedAggrId,
169187
expectedAggrId, actualAggrObjId);
170188
}
171-
189+
172190
@And("^Perform an invalid freesyle query on Aggregated object$")
173191
public void perform_invalid_freestyle_query_on_created_aggregated_object() throws Throwable {
174192
final String invalidAggrId = "6acc3c87-75e0-4b6d-88f5-b1aee4e62b43";
175193
final String entryPoint = "/query";
176194
final String queryAggrObj = "{\"criteria\" :{\"aggregatedObject.id\" : \"" + invalidAggrId + "\" }}";
177195
final String expectedResponse = "[]";
178-
196+
179197
LOGGER.debug("Trying an invalid query on /query RestApi with invalid criteria query: " + queryAggrObj);
180-
mvcResult = mockMvc.perform(get(entryPoint)
181-
.param("request", queryAggrObj)
182-
.accept(MediaType.APPLICATION_JSON)
183-
.contentType(MediaType.APPLICATION_JSON))
184-
.andReturn();
185-
String responseAsString = mvcResult.getResponse().getContentAsString();
186-
int reponseStatusCode = mvcResult.getResponse().getStatus();
198+
199+
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
200+
response = getRequest.setPort(applicationPort)
201+
.setHost(hostName)
202+
.setHeaders("content-type", "application/json")
203+
.setHeaders("Accept", "application/json")
204+
.setEndpoint(entryPoint)
205+
.setParam("request", queryAggrObj)
206+
.performRequest();
207+
208+
String responseAsString = response.getBody().toString();
209+
int reponseStatusCode = response.getStatusCodeValue();
187210
LOGGER.debug("Response of /query RestApi, Status Code: " + reponseStatusCode +
188211
"\nResponse: " + responseAsString);
189-
212+
190213
assertEquals(HttpStatus.OK.toString(), Integer.toString(reponseStatusCode));
191214
assertEquals("Diffences between actual Aggregated Object:\n" + responseAsString
192215
+ "\nand expected Aggregated Object:\n" + expectedResponse,
193216
expectedResponse, responseAsString);
194217
}
195-
218+
196219
@And("^Perform a query for missed notification$")
197220
public void perform_a_query_for_missed_notification() throws Throwable {
198-
221+
199222
final String subscriptionName = "Subscription_1";
200223
final String entryPoint = "/queryMissedNotifications";
201224
final String expectedTestCaseStartedEventId = "cb9d64b0-a6e9-4419-8b5d-a650c27c59ca";
202-
225+
203226
LOGGER.debug("Check if MissedNotification and " + subscriptionName + " exist in Database");
204227
final String queryRequest = "{\"subscriptionName\":\"" + subscriptionName + "\"}";
205228
String subscriptionNameCheck = objMapper.readValue(mongoDBHandler
206229
.find(missedNotificationDatabaseName, missedNotificationCollectionName, queryRequest).get(0),
207230
JsonNode.class).get("subscriptionName").asText();
208231
assertEquals("Expected subscriptionName in missed notification in Database is not as expected.",
209232
subscriptionName, subscriptionNameCheck);
210-
233+
211234
LOGGER.debug("Trying to query /queryMissedNotifications RestApi with subscriptionName: " + subscriptionName);
212-
mvcResult = mockMvc.perform(get(entryPoint)
213-
.param("SubscriptionName", subscriptionName)
214-
.accept(MediaType.APPLICATION_JSON)
215-
.contentType(MediaType.APPLICATION_JSON))
216-
.andReturn();
217-
218-
String responseAsString = mvcResult.getResponse().getContentAsString();
219-
int reponseStatusCode = mvcResult.getResponse().getStatus();
235+
236+
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
237+
response = getRequest.setPort(applicationPort)
238+
.setHost(hostName)
239+
.setHeaders("content-type", "application/json")
240+
.setHeaders("Accept", "application/json")
241+
.setEndpoint(entryPoint)
242+
.setParam("SubscriptionName", subscriptionName)
243+
.performRequest();
244+
245+
String responseAsString = response.getBody().toString();
246+
int reponseStatusCode = response.getStatusCodeValue();
220247
LOGGER.debug("Response of /queryMissedNotifications RestApi, Status Code: " + reponseStatusCode +
221248
"\nResponse: " + responseAsString);
222-
223-
JsonNode jsonNodeResult = objMapper.readValue(mvcResult.getResponse().getContentAsString(), JsonNode.class);
249+
250+
JsonNode jsonNodeResult = objMapper.readValue(response.getBody().toString(), JsonNode.class);
224251
JsonNode responseEntityNode = jsonNodeResult.get("responseEntity");
225252
String responseEntityNodeFormatted = responseEntityNode.asText().replace("[", "").replace("]", "");
226253
JsonNode responseEntityFormattedJsonNode = objMapper.readValue(responseEntityNodeFormatted, JsonNode.class);
227-
254+
228255
LOGGER.debug("AggregatedObject from Response: " + responseEntityFormattedJsonNode.toString());
229-
256+
230257
String actualTestCaseStartedEventId = responseEntityFormattedJsonNode
231258
.get("testCaseExecutions").get("testCaseStartedEventId").asText();
232-
assertEquals(HttpStatus.OK.toString(), Integer.toString(mvcResult.getResponse().getStatus()));
259+
assertEquals(HttpStatus.OK.toString(), Integer.toString(response.getStatusCodeValue()));
233260
assertEquals("Diffences between actual Missed Notification response TestCaseStartedEventId:\n" + actualTestCaseStartedEventId
234261
+ "\nand expected Missed Notification response TestCaseStartedEventId:\n" + expectedTestCaseStartedEventId,
235262
expectedTestCaseStartedEventId, actualTestCaseStartedEventId);
236-
263+
237264
}
238265

239266
@And("^Check missed notification has been returned$")
240267
public void check_missed_notification_has_been_returned() throws Throwable {
241268
final String expectedResponse = "{\"responseEntity\":\"[]\"}";
242269
final String subscriptionName = "Subscription_1";
243270
final String entryPoint = "/queryMissedNotifications";
244-
271+
245272
LOGGER.debug("Trying to query /queryMissedNotifications RestApi one more time with subscriptionName: "
246273
+ subscriptionName);
247-
mvcResult = mockMvc.perform(get(entryPoint)
248-
.param("SubscriptionName", subscriptionName)
249-
.accept(MediaType.APPLICATION_JSON)
250-
.contentType(MediaType.APPLICATION_JSON))
251-
.andReturn();
252-
String responseAsString = mvcResult.getResponse().getContentAsString();
253-
int reponseStatusCode = mvcResult.getResponse().getStatus();
274+
275+
HttpRequest getRequest = new HttpRequest(HttpMethod.GET);
276+
response = getRequest.setPort(applicationPort)
277+
.setHost(hostName)
278+
.setHeaders("content-type", "application/json")
279+
.setHeaders("Accept", "application/json")
280+
.setEndpoint(entryPoint)
281+
.setParam("SubscriptionName", subscriptionName)
282+
.performRequest();
283+
284+
String responseAsString = response.getBody().toString();
285+
int reponseStatusCode = response.getStatusCodeValue();
254286
LOGGER.debug("Response of /queryMissedNotifications RestApi, Status Code: " + reponseStatusCode +
255287
"\nResponse: " + responseAsString);
256-
288+
257289
assertEquals(HttpStatus.OK.toString(), Integer.toString(reponseStatusCode));
258290
assertEquals("Diffences between actual Missed Notification response:\n" + responseAsString
259291
+ "\nand expected Missed Notification response:\n" + expectedResponse,
260292
expectedResponse, responseAsString);
261293
}
262-
294+
263295
/**
264296
* Method that creates a document in MongoDb database.
265-
*
297+
*
266298
* @param databaseName - Name of the database in MongoDb to use.
267299
* @param collectionName - Name of the collection in MongoDb to use.
268300
* @param objToBeInserted - Object in string format to be inserted to database.
269-
*
301+
*
270302
* @return boolean - Returns true or false depending if object/document was successfully created in database.
271-
*
303+
*
272304
*/
273305
private boolean createDocumentInMongoDb(String databaseName, String collectionName, String objToBeInserted) {
274306
LOGGER.debug("Inserting Object to MongoDb.\nDatabase: " + databaseName

0 commit comments

Comments
 (0)