8
8
9
9
import java .io .File ;
10
10
import java .io .IOException ;
11
+ import java .net .BindException ;
11
12
import java .util .ArrayList ;
12
13
import java .util .Arrays ;
13
14
import java .util .List ;
@@ -94,11 +95,11 @@ public void afterScenario() throws IOException {
94
95
public void the_REST_API_is_up_and_running (String endPoint ) throws Exception {
95
96
HttpRequest getRequest = new HttpRequest (HttpRequest .HttpMethod .GET );
96
97
response = getRequest .setHost (getHostName ())
97
- .setPort (applicationPort )
98
- .addHeader ("content-type" , "application/json" )
99
- .addHeader ("Accept" , "application/json" )
100
- .setEndpoint (endPoint )
101
- .performRequest ();
98
+ .setPort (applicationPort )
99
+ .addHeader ("content-type" , "application/json" )
100
+ .addHeader ("Accept" , "application/json" )
101
+ .setEndpoint (endPoint )
102
+ .performRequest ();
102
103
assertEquals (HttpStatus .OK .value (), response .getStatusCodeValue ());
103
104
}
104
105
@@ -116,7 +117,8 @@ public void send_eiffel_events() throws Throwable {
116
117
LOGGER .debug ("About to send Eiffel events." );
117
118
List <String > eventNamesToSend = getEventNamesToSend ();
118
119
eventManager .sendEiffelEvents (EIFFEL_EVENTS_JSON_PATH , eventNamesToSend );
119
- List <String > missingEventIds = dbManager .verifyEventsInDB (eventManager .getEventsIdList (EIFFEL_EVENTS_JSON_PATH , eventNamesToSend ));
120
+ List <String > missingEventIds = dbManager .verifyEventsInDB (
121
+ eventManager .getEventsIdList (EIFFEL_EVENTS_JSON_PATH , eventNamesToSend ));
120
122
assertEquals ("The following events are missing in mongoDB: " + missingEventIds .toString (), 0 ,
121
123
missingEventIds .size ());
122
124
LOGGER .debug ("Eiffel events sent." );
@@ -126,7 +128,8 @@ public void send_eiffel_events() throws Throwable {
126
128
public void wait_for_ei_to_aggregate_objects_and_trigger_subscriptions () throws Throwable {
127
129
List <String > eventNamesToSend = getEventNamesToSend ();
128
130
LOGGER .debug ("Checking Aggregated Objects." );
129
- List <String > arguments = new ArrayList <>(eventManager .getEventsIdList (EIFFEL_EVENTS_JSON_PATH , eventNamesToSend ));
131
+ List <String > arguments = new ArrayList <>(
132
+ eventManager .getEventsIdList (EIFFEL_EVENTS_JSON_PATH , eventNamesToSend ));
130
133
arguments .add ("id=TC5" );
131
134
arguments .add ("conclusion=SUCCESSFUL" );
132
135
List <String > missingArguments = dbManager .verifyAggregatedObjectInDB (arguments );
@@ -151,8 +154,8 @@ public void check_mail_subscriptions_were_triggered() {
151
154
@ Then ("^Rest subscriptions were triggered$" )
152
155
public void check_rest_subscriptions_were_triggered () throws Throwable {
153
156
LOGGER .debug ("Verifying REST requests." );
154
- List <String > endpointsToCheck = new ArrayList <>(
155
- Arrays . asList ( REST_ENDPOINT , REST_ENDPOINT_AUTH , REST_ENDPOINT_PARAMS , REST_ENDPOINT_AUTH_PARAMS , REST_ENDPOINT_ROW_BODY ));
157
+ List <String > endpointsToCheck = new ArrayList <>(Arrays . asList ( REST_ENDPOINT , REST_ENDPOINT_AUTH ,
158
+ REST_ENDPOINT_PARAMS , REST_ENDPOINT_AUTH_PARAMS , REST_ENDPOINT_ROW_BODY ));
156
159
157
160
assert (allEndpointsGotAtLeastXCalls (endpointsToCheck , 1 ));
158
161
for (String endpoint : endpointsToCheck ) {
@@ -183,12 +186,12 @@ private void readSubscriptionNames(String jsonDataAsString) throws Throwable {
183
186
private void postSubscriptions (String jsonDataAsString , String endPoint ) throws Exception {
184
187
HttpRequest postRequest = new HttpRequest (HttpRequest .HttpMethod .POST );
185
188
response = postRequest .setHost (getHostName ())
186
- .setPort (applicationPort )
187
- .addHeader ("content-type" , "application/json" )
188
- .addHeader ("Accept" , "application/json" )
189
- .setEndpoint (endPoint )
190
- .setBody (jsonDataAsString )
191
- .performRequest ();
189
+ .setPort (applicationPort )
190
+ .addHeader ("content-type" , "application/json" )
191
+ .addHeader ("Accept" , "application/json" )
192
+ .setEndpoint (endPoint )
193
+ .setBody (jsonDataAsString )
194
+ .performRequest ();
192
195
assertEquals (HttpStatus .OK .value (), response .getStatusCodeValue ());
193
196
}
194
197
@@ -201,11 +204,11 @@ private void postSubscriptions(String jsonDataAsString, String endPoint) throws
201
204
private void validateSubscriptionsSuccessfullyAdded (String endPoint ) throws Exception {
202
205
HttpRequest getRequest = new HttpRequest (HttpRequest .HttpMethod .GET );
203
206
response = getRequest .setHost (getHostName ())
204
- .setPort (applicationPort )
205
- .addHeader ("content-type" , "application/json" )
206
- .addHeader ("Accept" , "application/json" )
207
- .setEndpoint (endPoint )
208
- .performRequest ();
207
+ .setPort (applicationPort )
208
+ .addHeader ("content-type" , "application/json" )
209
+ .addHeader ("Accept" , "application/json" )
210
+ .setEndpoint (endPoint )
211
+ .performRequest ();
209
212
assertEquals (HttpStatus .OK .value (), response .getStatusCodeValue ());
210
213
LOGGER .debug ("Checking that response contains all subscriptions" );
211
214
for (String subscriptionName : subscriptionNames ) {
@@ -277,15 +280,16 @@ private void setupRestEndpoints() {
277
280
LOGGER .debug ("Setting up endpoints on host '" + getHostName () + "' and port '" + port + "'." );
278
281
mockClient = new MockServerClient (getHostName (), port );
279
282
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT )).respond (response ().withStatusCode (201 ));
280
- mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_AUTH )
281
- .withHeader ("Authorization" , "Basic bXlVc2VyTmFtZTpteVBhc3N3b3Jk" ))
282
- .respond (response ().withStatusCode (201 ));
283
+ mockClient .when (request ().withMethod ("POST" )
284
+ .withPath (REST_ENDPOINT_AUTH )
285
+ .withHeader ("Authorization" , "Basic bXlVc2VyTmFtZTpteVBhc3N3b3Jk" ))
286
+ .respond (response ().withStatusCode (201 ));
283
287
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_PARAMS ))
284
- .respond (response ().withStatusCode (201 ));
288
+ .respond (response ().withStatusCode (201 ));
285
289
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_AUTH_PARAMS ))
286
- .respond (response ().withStatusCode (201 ));
290
+ .respond (response ().withStatusCode (201 ));
287
291
mockClient .when (request ().withMethod ("POST" ).withPath (REST_ENDPOINT_ROW_BODY ))
288
- .respond (response ().withStatusCode (201 ));
292
+ .respond (response ().withStatusCode (201 ));
289
293
}
290
294
291
295
/**
@@ -294,10 +298,20 @@ private void setupRestEndpoints() {
294
298
* @throws IOException
295
299
*/
296
300
private void setupSMTPServer () throws IOException {
297
- int port = SocketUtils .findAvailableTcpPort ();
298
- LOGGER .debug ("Setting SMTP port to " + port );
299
- mailSender .setPort (port );
300
- smtpServer = SimpleSmtpServer .start (port );
301
+ boolean connected = false ;
302
+ while (!connected ) {
303
+ try {
304
+ int port = SocketUtils .findAvailableTcpPort ();
305
+ LOGGER .debug ("Setting SMTP port to " + port );
306
+ mailSender .setPort (port );
307
+ smtpServer = SimpleSmtpServer .start (port );
308
+ // connected, go on
309
+ connected = true ;
310
+ } catch (BindException e ) {
311
+ String msg = "Failed to start SMTP server. address already in use. Try again!" ;
312
+ LOGGER .debug (msg , e );
313
+ }
314
+ }
301
315
}
302
316
303
317
/**
0 commit comments