3
3
import com .dumbster .smtp .SimpleSmtpServer ;
4
4
import com .dumbster .smtp .SmtpMessage ;
5
5
import com .ericsson .ei .utils .FunctionalTestBase ;
6
-
7
- import static org .junit .Assert .assertEquals ;
8
- import static org .junit .Assert .assertTrue ;
9
- import static org .mockserver .model .HttpRequest .request ;
10
- import static org .mockserver .model .HttpResponse .response ;
11
- import static org .mockserver .integration .ClientAndServer .startClientAndServer ;
12
-
13
- import java .io .File ;
14
- import java .io .IOException ;
15
- import java .util .ArrayList ;
16
- import java .util .Arrays ;
17
- import java .util .List ;
18
- import java .util .concurrent .TimeUnit ;
19
-
6
+ import com .ericsson .ei .utils .HttpRequest ;
7
+ import cucumber .api .java .After ;
8
+ import cucumber .api .java .Before ;
9
+ import cucumber .api .java .en .Given ;
10
+ import cucumber .api .java .en .Then ;
11
+ import cucumber .api .java .en .When ;
20
12
import org .apache .commons .io .FileUtils ;
21
13
import org .json .JSONArray ;
22
14
import org .json .JSONException ;
23
15
import org .junit .Ignore ;
24
- import org .mockserver .client .server . MockServerClient ;
16
+ import org .mockserver .client .MockServerClient ;
25
17
import org .mockserver .integration .ClientAndServer ;
26
18
import org .mockserver .model .Format ;
27
19
import org .slf4j .Logger ;
28
20
import org .slf4j .LoggerFactory ;
29
21
import org .springframework .beans .factory .annotation .Autowired ;
30
22
import org .springframework .beans .factory .annotation .Value ;
31
- import org .springframework .boot .test . autoconfigure . web .servlet . AutoConfigureMockMvc ;
23
+ import org .springframework .boot .web .server . LocalServerPort ;
32
24
import org .springframework .http .HttpStatus ;
33
- import org .springframework .http .MediaType ;
25
+ import org .springframework .http .ResponseEntity ;
34
26
import org .springframework .mail .javamail .JavaMailSenderImpl ;
35
- import org .springframework .test .web .servlet .MockMvc ;
36
- import org .springframework .test .web .servlet .MvcResult ;
37
- import org .springframework .test .web .servlet .RequestBuilder ;
38
- import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
39
27
import org .springframework .util .SocketUtils ;
40
28
41
- import cucumber .api .java .After ;
42
- import cucumber .api .java .Before ;
43
- import cucumber .api .java .en .Given ;
44
- import cucumber .api .java .en .Then ;
45
- import cucumber .api .java .en .When ;
29
+ import java .io .File ;
30
+ import java .io .IOException ;
31
+ import java .util .ArrayList ;
32
+ import java .util .Arrays ;
33
+ import java .util .List ;
34
+ import java .util .concurrent .TimeUnit ;
35
+
36
+ import static org .junit .Assert .assertEquals ;
37
+ import static org .junit .Assert .assertTrue ;
38
+ import static org .mockserver .integration .ClientAndServer .startClientAndServer ;
39
+ import static org .mockserver .model .HttpRequest .request ;
40
+ import static org .mockserver .model .HttpResponse .response ;
46
41
47
42
@ Ignore
48
- @ AutoConfigureMockMvc
49
43
public class SubscriptionTriggerSteps extends FunctionalTestBase {
50
44
45
+ private static final Logger LOGGER = LoggerFactory .getLogger (SubscriptionTriggerSteps .class );
46
+
51
47
private static final String SUBSCRIPTION_WITH_JSON_PATH = "src/functionaltests/resources/subscription_multiple.json" ;
52
48
private static final String EIFFEL_EVENTS_JSON_PATH = "src/functionaltests/resources/eiffel_events_for_test.json" ;
53
-
54
49
private static final String REST_ENDPOINT = "/rest" ;
55
50
private static final String REST_ENDPOINT_AUTH = "/rest/with/auth" ;
56
51
private static final String REST_ENDPOINT_PARAMS = "/rest/with/params" ;
57
52
private static final String REST_ENDPOINT_AUTH_PARAMS = "/rest/with/auth/params" ;
58
- private static final String BASE_URL = "localhost " ;
53
+ private static final String REST_ENDPOINT_ROW_BODY = "/rest/rowBody " ;
59
54
60
55
private List <String > subscriptionNames = new ArrayList <>();
61
56
57
+ @ LocalServerPort
58
+ private int applicationPort ;
59
+
62
60
@ Value ("${email.sender}" )
63
61
private String sender ;
64
62
@@ -68,20 +66,13 @@ public class SubscriptionTriggerSteps extends FunctionalTestBase {
68
66
@ Value ("${spring.data.mongodb.database}" )
69
67
private String database ;
70
68
71
- @ Autowired
72
- private MockMvc mockMvc ;
73
-
74
69
@ Autowired
75
70
private JavaMailSenderImpl mailSender ;
76
71
77
- private MvcResult result ;
78
- private MvcResult postResult ;
79
- private MvcResult getResult ;
80
72
private SimpleSmtpServer smtpServer ;
81
73
private ClientAndServer restServer ;
82
74
private MockServerClient mockClient ;
83
-
84
- private static final Logger LOGGER = LoggerFactory .getLogger (SubscriptionTriggerSteps .class );
75
+ private ResponseEntity response ;
85
76
86
77
@ Before ("@SubscriptionTriggerScenario" )
87
78
public void beforeScenario () throws IOException {
@@ -99,12 +90,14 @@ public void afterScenario() throws IOException {
99
90
100
91
@ Given ("^The REST API \" ([^\" ]*)\" is up and running$" )
101
92
public void the_REST_API_is_up_and_running (String endPoint ) throws Exception {
102
- RequestBuilder requestBuilder = MockMvcRequestBuilders .get (endPoint ).accept (MediaType .APPLICATION_JSON );
103
-
104
- result = mockMvc .perform (requestBuilder ).andReturn ();
105
- LOGGER .debug ("Response code from mocked REST API: " + String .valueOf (result .getResponse ().getStatus ()));
106
-
107
- assertEquals (HttpStatus .OK .value (), result .getResponse ().getStatus ());
93
+ HttpRequest getRequest = new HttpRequest (HttpRequest .HttpMethod .GET );
94
+ response = getRequest .setHost (getHostName ())
95
+ .setPort (applicationPort )
96
+ .addHeader ("content-type" , "application/json" )
97
+ .addHeader ("Accept" , "application/json" )
98
+ .setEndpoint (endPoint )
99
+ .performRequest ();
100
+ assertEquals (HttpStatus .OK .value (), response .getStatusCodeValue ());
108
101
}
109
102
110
103
@ Given ("^Subscriptions are setup using REST API \" ([^\" ]*)\" $" )
@@ -140,7 +133,7 @@ public void wait_for_ei_to_aggregate_objects_and_trigger_subscriptions() throws
140
133
}
141
134
142
135
@ Then ("^Mail subscriptions were triggered$" )
143
- public void check_mail_subscriptions_were_triggered () throws Throwable {
136
+ public void check_mail_subscriptions_were_triggered () {
144
137
LOGGER .debug ("Verifying received emails." );
145
138
List <SmtpMessage > emails = smtpServer .getReceivedEmails ();
146
139
assert (emails .size () > 0 );
@@ -149,28 +142,26 @@ public void check_mail_subscriptions_were_triggered() throws Throwable {
149
142
// assert correct sender.
150
143
assertEquals (email .getHeaderValue ("From" ), sender );
151
144
// assert given test case exist in body.
152
- assert (email .getBody ().toString (). contains ("TC5" ));
145
+ assert (email .getBody ().contains ("TC5" ));
153
146
}
154
147
}
155
148
156
149
@ Then ("^Rest subscriptions were triggered$" )
157
150
public void check_rest_subscriptions_were_triggered () throws Throwable {
158
151
LOGGER .debug ("Verifying REST requests." );
159
152
List <String > endpointsToCheck = new ArrayList <>(
160
- Arrays .asList (REST_ENDPOINT , REST_ENDPOINT_AUTH , REST_ENDPOINT_PARAMS , REST_ENDPOINT_AUTH_PARAMS ));
153
+ Arrays .asList (REST_ENDPOINT , REST_ENDPOINT_AUTH , REST_ENDPOINT_PARAMS , REST_ENDPOINT_AUTH_PARAMS , REST_ENDPOINT_ROW_BODY ));
161
154
162
155
assert (allEndpointsGotAtLeastXCalls (endpointsToCheck , 1 ));
163
156
for (String endpoint : endpointsToCheck ) {
164
157
assert (requestBodyContainsStatedValues (endpoint ));
165
-
166
158
}
167
159
}
168
160
169
161
/**
170
162
* Assemble subscription names in a list.
171
163
*
172
- * @param jsonDataAsString
173
- * JSON string containing subscriptions
164
+ * @param jsonDataAsString JSON string containing subscriptions
174
165
* @throws Throwable
175
166
*/
176
167
private void readSubscriptionNames (String jsonDataAsString ) throws Throwable {
@@ -183,58 +174,55 @@ private void readSubscriptionNames(String jsonDataAsString) throws Throwable {
183
174
/**
184
175
* POST subscriptions to endpoint.
185
176
*
186
- * @param jsonDataAsString
187
- * JSON string containing subscriptions
188
- * @param endPoint
189
- * endpoint to use in POST
177
+ * @param jsonDataAsString JSON string containing subscriptions
178
+ * @param endPoint endpoint to use in POST
190
179
* @throws Exception
191
180
*/
192
181
private void postSubscriptions (String jsonDataAsString , String endPoint ) throws Exception {
193
- RequestBuilder requestBuilder = MockMvcRequestBuilders .post (endPoint ).accept (MediaType .APPLICATION_JSON )
194
- .content (jsonDataAsString ).contentType (MediaType .APPLICATION_JSON );
195
-
196
- postResult = mockMvc .perform (requestBuilder ).andReturn ();
197
- LOGGER .debug ("Response code from REST when adding subscriptions: "
198
- + String .valueOf (postResult .getResponse ().getStatus ()));
199
-
200
- assertEquals (HttpStatus .OK .value (), postResult .getResponse ().getStatus ());
182
+ HttpRequest postRequest = new HttpRequest (HttpRequest .HttpMethod .POST );
183
+ response = postRequest .setHost (getHostName ())
184
+ .setPort (applicationPort )
185
+ .addHeader ("content-type" , "application/json" )
186
+ .addHeader ("Accept" , "application/json" )
187
+ .setEndpoint (endPoint )
188
+ .setBody (jsonDataAsString )
189
+ .performRequest ();
190
+ assertEquals (HttpStatus .OK .value (), response .getStatusCodeValue ());
201
191
}
202
192
203
193
/**
204
194
* Verify that subscriptions were successfully posted.
205
195
*
206
- * @param endPoint
207
- * endpoint to use in GET
196
+ * @param endPoint endpoint to use in GET
208
197
* @throws Exception
209
198
*/
210
199
private void validateSubscriptionsSuccessfullyAdded (String endPoint ) throws Exception {
211
- RequestBuilder getRequest = MockMvcRequestBuilders .get (endPoint );
212
- getResult = mockMvc .perform (getRequest ).andReturn ();
213
-
214
- LOGGER .debug ("Response code from REST when getting subscriptions: "
215
- + String .valueOf (getResult .getResponse ().getStatus ()));
216
- assertEquals (HttpStatus .OK .value (), result .getResponse ().getStatus ());
217
-
200
+ HttpRequest getRequest = new HttpRequest (HttpRequest .HttpMethod .GET );
201
+ response = getRequest .setHost (getHostName ())
202
+ .setPort (applicationPort )
203
+ .addHeader ("content-type" , "application/json" )
204
+ .addHeader ("Accept" , "application/json" )
205
+ .setEndpoint (endPoint )
206
+ .performRequest ();
207
+ assertEquals (HttpStatus .OK .value (), response .getStatusCodeValue ());
218
208
LOGGER .debug ("Checking that response contains all subscriptions" );
219
209
for (String subscriptionName : subscriptionNames ) {
220
- assertTrue (getResult . getResponse (). getContentAsString ().contains (subscriptionName ));
210
+ assertTrue (response . toString ().contains (subscriptionName ));
221
211
}
222
212
}
223
213
224
214
/**
225
215
* Checks that an enpoint got at least the number of calls as expected.
226
216
*
227
- * @param endpoints
228
- * List of endpoints to check.
229
- * @param expectedCalls
230
- * Integer with the least number of calls.
217
+ * @param endpoints List of endpoints to check.
218
+ * @param expectedCalls Integer with the least number of calls.
231
219
* @return true if all endpoints had atleast the number of calls as expected.
232
220
* @throws JSONException
233
221
* @throws InterruptedException
234
222
*/
235
223
private boolean allEndpointsGotAtLeastXCalls (final List <String > endpoints , int expectedCalls )
236
224
throws JSONException , InterruptedException {
237
- List <String > endpointsToCheck = new ArrayList <String >(endpoints );
225
+ List <String > endpointsToCheck = new ArrayList <>(endpoints );
238
226
239
227
long stopTime = System .currentTimeMillis () + 30000 ;
240
228
while (!endpointsToCheck .isEmpty () && stopTime > System .currentTimeMillis ()) {
@@ -252,8 +240,7 @@ private boolean allEndpointsGotAtLeastXCalls(final List<String> endpoints, int e
252
240
/**
253
241
* Verify that request made to endpoint contains the correct information.
254
242
*
255
- * @param endpoint
256
- * endpoint to check
243
+ * @param endpoint endpoint to check
257
244
* @return true if verification was successful, false otherwise
258
245
* @throws JSONException
259
246
*/
@@ -285,15 +272,18 @@ private void setupRestEndpoints() {
285
272
int port = SocketUtils .findAvailableTcpPort ();
286
273
restServer = startClientAndServer (port );
287
274
288
- LOGGER .debug ("Setting up endpoints on host '" + BASE_URL + "' and port '" + port + "'." );
289
- mockClient = new MockServerClient (BASE_URL , port );
275
+ LOGGER .debug ("Setting up endpoints on host '" + getHostName () + "' and port '" + port + "'." );
276
+ mockClient = new MockServerClient (getHostName () , port );
290
277
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT )).respond (response ().withStatusCode (201 ));
291
- mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_AUTH ))
278
+ mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_AUTH )
279
+ .withHeader ("Authorization" , "Basic TXkgbW90aGVyIGhhcyAyIGNhdHMgYW5kIHRoZXkgYXJlIGNyYXp5" ))
292
280
.respond (response ().withStatusCode (201 ));
293
281
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_PARAMS ))
294
282
.respond (response ().withStatusCode (201 ));
295
283
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_AUTH_PARAMS ))
296
284
.respond (response ().withStatusCode (201 ));
285
+ mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_ROW_BODY ))
286
+ .respond (response ().withStatusCode (201 ));
297
287
}
298
288
299
289
/**
@@ -323,13 +313,13 @@ protected List<String> getEventNamesToSend() {
323
313
/**
324
314
* Replaces tags in the subscription JSON string with valid information.
325
315
*
326
- * @param text
327
- * JSON string containing replaceable tags
316
+ * @param text JSON string containing replaceable tags
328
317
* @return Processed content
329
318
*/
330
319
private String stringReplaceText (String text ) {
331
320
text = text .replaceAll ("\\ $\\ {rest\\ .host\\ }" , "localhost" );
332
- text = text .replaceAll ("\\ $\\ {rest\\ .port\\ }" , String .valueOf (restServer .getPort ()));
321
+ text = text .replaceAll ("\\ $\\ {rest\\ .port\\ }" , String .valueOf (restServer .getLocalPort ()));
322
+ text = text .replaceAll ("\\ $\\ {rest\\ .row.body\\ }" , REST_ENDPOINT_ROW_BODY );
333
323
text = text .replaceAll ("\\ $\\ {rest\\ .endpoint\\ }" , REST_ENDPOINT );
334
324
text = text .replaceAll ("\\ $\\ {rest\\ .endpoint\\ .auth\\ }" , REST_ENDPOINT_AUTH );
335
325
text = text .replaceAll ("\\ $\\ {rest\\ .endpoint\\ .params\\ }" , REST_ENDPOINT_PARAMS );
0 commit comments