14
14
import org .springframework .beans .factory .annotation .Autowired ;
15
15
import org .springframework .beans .factory .annotation .Value ;
16
16
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
17
+ import org .springframework .boot .web .server .LocalServerPort ;
17
18
import org .springframework .http .HttpStatus ;
18
19
import org .springframework .http .MediaType ;
20
+ import org .springframework .http .ResponseEntity ;
19
21
import org .springframework .test .web .servlet .MockMvc ;
20
22
import org .springframework .test .web .servlet .MvcResult ;
21
23
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
26
28
27
29
import com .ericsson .ei .mongodbhandler .MongoDBHandler ;
28
30
import com .ericsson .ei .utils .FunctionalTestBase ;
31
+ import com .ericsson .ei .utils .HttpRequest ;
32
+ import com .ericsson .ei .utils .HttpRequest .HttpMethod ;
29
33
30
34
31
35
@ Ignore
@@ -34,35 +38,36 @@ public class QueryAggregatedObjectsTestSteps extends FunctionalTestBase {
34
38
35
39
private static final Logger LOGGER = LoggerFactory .getLogger (QueryAggregatedObjectsTestSteps .class );
36
40
37
-
41
+
38
42
private static final String AGGREGATED_OBJ_JSON_PATH = "src/test/resources/AggregatedDocument.json" ;
39
43
private static final String MISSED_NOTIFICATION_JSON_PATH = "src/test/resources/MissedNotification.json" ;
40
44
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
+
46
51
@ Autowired
47
52
private MongoDBHandler mongoDBHandler ;
48
-
53
+
49
54
@ Value ("${spring.data.mongodb.database}" )
50
55
private String eiDatabaseName ;
51
-
56
+
52
57
@ Value ("${aggregated.collection.name}" )
53
58
private String aggrCollectionName ;
54
-
59
+
55
60
@ Value ("${missedNotificationCollectionName}" )
56
61
private String missedNotificationCollectionName ;
57
-
62
+
58
63
@ Value ("${missedNotificationDataBaseName}" )
59
64
private String missedNotificationDatabaseName ;
60
65
61
66
private String aggrObj ;
62
67
private String missedNotificationObj ;
63
-
68
+
64
69
private ObjectMapper objMapper ;
65
-
70
+
66
71
public QueryAggregatedObjectsTestSteps () {
67
72
objMapper = new ObjectMapper ();
68
73
@@ -75,7 +80,7 @@ public QueryAggregatedObjectsTestSteps() {
75
80
e .printStackTrace ();
76
81
}
77
82
}
78
-
83
+
79
84
@ Given ("^Aggregated object is created$" )
80
85
public void aggregated_object_is_created () throws Throwable {
81
86
LOGGER .debug ("Creating aggregated object in MongoDb" );
@@ -94,181 +99,208 @@ public void perform_valid_query_on_newly_created_aggregated_object() throws Thro
94
99
final String entryPoint = "/queryAggregatedObject" ;
95
100
final String documentId = "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43" ;
96
101
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
- "\n Response: " + 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
+ "\n Response: " + 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 );
107
116
String responseEntityFormattedString = responseEntityNode .toString ().
108
117
substring (1 , responseEntityNode .toString ().length ()-1 );
109
-
118
+
110
119
LOGGER .debug ("AggregatedObject from Response: " + responseEntityFormattedString );
111
120
JsonNode responseEntityFormattedJsonNode = objMapper .readValue (responseEntityFormattedString , JsonNode .class );
112
121
String actualTestCaseFinishedEventId = responseEntityFormattedJsonNode .get ("aggregatedObject" )
113
122
.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 ()));
116
125
assertEquals ("Failed to compare actual Aggregated Object TestCaseFinishedEventId:\n " + actualTestCaseFinishedEventId
117
126
+ "\n with expected Aggregated Object TestCaseFinishedEventId:\n " + expectedTestCaseFinishedEventId ,
118
127
expectedTestCaseFinishedEventId , actualTestCaseFinishedEventId );
119
128
}
120
-
129
+
121
130
@ And ("^Perform an invalid query on same Aggregated object$" )
122
131
public void perform_invalid_query_on_created_aggregated_object () throws Throwable {
123
132
final String invalidDocumentId = "6acc3c87-75e0-4aaa-88f5-b1a5d4e6cccc" ;
124
133
final String entryPoint = "/queryAggregatedObject" ;
125
134
final String expectedResponse = "{\" responseEntity\" :\" []\" }" ;
126
-
135
+
127
136
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 ();
135
148
LOGGER .debug ("Response of /queryAggregatedObject RestApi, Status Code: " + reponseStatusCode +
136
149
"\n Response: " + responseAsString );
137
-
150
+
138
151
assertEquals (HttpStatus .OK .toString (), Integer .toString (reponseStatusCode ));
139
152
assertEquals ("Diffences between actual Aggregated Object:\n " + responseAsString
140
153
+ "\n and expected Aggregated Object:\n " + expectedResponse ,
141
154
expectedResponse , responseAsString );
142
155
}
143
-
156
+
144
157
@ And ("^Perform valid freestyle query on created Aggregated object$" )
145
158
public void perform_valid_freestyle_query_on_created_aggregated_object () throws Throwable {
146
159
final String expectedAggrId = "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43" ;
147
160
final String entryPoint = "/query" ;
148
161
final String queryAggrObj = "{\" criteria\" :{\" aggregatedObject.id\" : \" " + expectedAggrId + "\" , \" aggregatedObject.gav.groupId\" : \" com.mycompany.myproduct\" }}" ;
149
162
150
163
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
- "\n Response: " + 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
+ "\n Response: " + 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 );
161
179
162
180
String actualAggrObjId = aggrObjResponse .get ("id" ).asText ();
163
181
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 ()));
167
185
assertEquals ("Failed to compare actual Aggregated Object Id:\n " + actualAggrObjId
168
186
+ "\n with expected Aggregated Object Id:\n " + expectedAggrId ,
169
187
expectedAggrId , actualAggrObjId );
170
188
}
171
-
189
+
172
190
@ And ("^Perform an invalid freesyle query on Aggregated object$" )
173
191
public void perform_invalid_freestyle_query_on_created_aggregated_object () throws Throwable {
174
192
final String invalidAggrId = "6acc3c87-75e0-4b6d-88f5-b1aee4e62b43" ;
175
193
final String entryPoint = "/query" ;
176
194
final String queryAggrObj = "{\" criteria\" :{\" aggregatedObject.id\" : \" " + invalidAggrId + "\" }}" ;
177
195
final String expectedResponse = "[]" ;
178
-
196
+
179
197
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 ();
187
210
LOGGER .debug ("Response of /query RestApi, Status Code: " + reponseStatusCode +
188
211
"\n Response: " + responseAsString );
189
-
212
+
190
213
assertEquals (HttpStatus .OK .toString (), Integer .toString (reponseStatusCode ));
191
214
assertEquals ("Diffences between actual Aggregated Object:\n " + responseAsString
192
215
+ "\n and expected Aggregated Object:\n " + expectedResponse ,
193
216
expectedResponse , responseAsString );
194
217
}
195
-
218
+
196
219
@ And ("^Perform a query for missed notification$" )
197
220
public void perform_a_query_for_missed_notification () throws Throwable {
198
-
221
+
199
222
final String subscriptionName = "Subscription_1" ;
200
223
final String entryPoint = "/queryMissedNotifications" ;
201
224
final String expectedTestCaseStartedEventId = "cb9d64b0-a6e9-4419-8b5d-a650c27c59ca" ;
202
-
225
+
203
226
LOGGER .debug ("Check if MissedNotification and " + subscriptionName + " exist in Database" );
204
227
final String queryRequest = "{\" subscriptionName\" :\" " + subscriptionName + "\" }" ;
205
228
String subscriptionNameCheck = objMapper .readValue (mongoDBHandler
206
229
.find (missedNotificationDatabaseName , missedNotificationCollectionName , queryRequest ).get (0 ),
207
230
JsonNode .class ).get ("subscriptionName" ).asText ();
208
231
assertEquals ("Expected subscriptionName in missed notification in Database is not as expected." ,
209
232
subscriptionName , subscriptionNameCheck );
210
-
233
+
211
234
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 ();
220
247
LOGGER .debug ("Response of /queryMissedNotifications RestApi, Status Code: " + reponseStatusCode +
221
248
"\n Response: " + responseAsString );
222
-
223
- JsonNode jsonNodeResult = objMapper .readValue (mvcResult . getResponse ().getContentAsString (), JsonNode .class );
249
+
250
+ JsonNode jsonNodeResult = objMapper .readValue (response . getBody ().toString (), JsonNode .class );
224
251
JsonNode responseEntityNode = jsonNodeResult .get ("responseEntity" );
225
252
String responseEntityNodeFormatted = responseEntityNode .asText ().replace ("[" , "" ).replace ("]" , "" );
226
253
JsonNode responseEntityFormattedJsonNode = objMapper .readValue (responseEntityNodeFormatted , JsonNode .class );
227
-
254
+
228
255
LOGGER .debug ("AggregatedObject from Response: " + responseEntityFormattedJsonNode .toString ());
229
-
256
+
230
257
String actualTestCaseStartedEventId = responseEntityFormattedJsonNode
231
258
.get ("testCaseExecutions" ).get ("testCaseStartedEventId" ).asText ();
232
- assertEquals (HttpStatus .OK .toString (), Integer .toString (mvcResult . getResponse (). getStatus ()));
259
+ assertEquals (HttpStatus .OK .toString (), Integer .toString (response . getStatusCodeValue ()));
233
260
assertEquals ("Diffences between actual Missed Notification response TestCaseStartedEventId:\n " + actualTestCaseStartedEventId
234
261
+ "\n and expected Missed Notification response TestCaseStartedEventId:\n " + expectedTestCaseStartedEventId ,
235
262
expectedTestCaseStartedEventId , actualTestCaseStartedEventId );
236
-
263
+
237
264
}
238
265
239
266
@ And ("^Check missed notification has been returned$" )
240
267
public void check_missed_notification_has_been_returned () throws Throwable {
241
268
final String expectedResponse = "{\" responseEntity\" :\" []\" }" ;
242
269
final String subscriptionName = "Subscription_1" ;
243
270
final String entryPoint = "/queryMissedNotifications" ;
244
-
271
+
245
272
LOGGER .debug ("Trying to query /queryMissedNotifications RestApi one more time with subscriptionName: "
246
273
+ 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 ();
254
286
LOGGER .debug ("Response of /queryMissedNotifications RestApi, Status Code: " + reponseStatusCode +
255
287
"\n Response: " + responseAsString );
256
-
288
+
257
289
assertEquals (HttpStatus .OK .toString (), Integer .toString (reponseStatusCode ));
258
290
assertEquals ("Diffences between actual Missed Notification response:\n " + responseAsString
259
291
+ "\n and expected Missed Notification response:\n " + expectedResponse ,
260
292
expectedResponse , responseAsString );
261
293
}
262
-
294
+
263
295
/**
264
296
* Method that creates a document in MongoDb database.
265
- *
297
+ *
266
298
* @param databaseName - Name of the database in MongoDb to use.
267
299
* @param collectionName - Name of the collection in MongoDb to use.
268
300
* @param objToBeInserted - Object in string format to be inserted to database.
269
- *
301
+ *
270
302
* @return boolean - Returns true or false depending if object/document was successfully created in database.
271
- *
303
+ *
272
304
*/
273
305
private boolean createDocumentInMongoDb (String databaseName , String collectionName , String objToBeInserted ) {
274
306
LOGGER .debug ("Inserting Object to MongoDb.\n Database: " + databaseName
0 commit comments