2
2
3
3
import static org .junit .Assert .assertEquals ;
4
4
import static org .mockserver .integration .ClientAndServer .startClientAndServer ;
5
+ import static org .mockserver .stop .Stop .stopQuietly ;
5
6
import static org .mockserver .model .HttpRequest .request ;
6
7
import static org .mockserver .model .HttpResponse .response ;
7
8
15
16
import org .junit .Before ;
16
17
import org .junit .BeforeClass ;
17
18
import org .junit .Test ;
18
- import org .mockserver .client .MockServerClient ;
19
19
import org .mockserver .integration .ClientAndServer ;
20
20
import org .openqa .selenium .support .ui .WebDriverWait ;
21
21
import org .springframework .boot .test .mock .mockito .MockBean ;
25
25
26
26
public class TestSubscriptionHandling extends SeleniumBaseClass {
27
27
28
- private static final String DOWNLOADED_TEMPLATE_FILE_PATH = String .join (File .separator , SeleniumConfig .getTempDownloadDirectory ().getPath (),
28
+ private static final String DOWNLOADED_TEMPLATE_FILE_PATH = String .join (File .separator ,
29
+ SeleniumConfig .getTempDownloadDirectory ().getPath (),
29
30
"subscriptionsTemplate.json" );
30
- private static final String DOWNLOADED_BULK_SUBSCRIPTIONS_FILE_PATH = String .join (File .separator , SeleniumConfig .getTempDownloadDirectory ().getPath (),
31
+ private static final String DOWNLOADED_BULK_SUBSCRIPTIONS_FILE_PATH = String .join (
32
+ File .separator , SeleniumConfig .getTempDownloadDirectory ().getPath (),
31
33
"subscriptionsData.json" );
32
- private static final String SUBSCRIPTION_TEMPLATE_FILE_PATH = String .join (File .separator , "src" , "functionaltest" , "resources" , "responses" ,
34
+ private static final String SUBSCRIPTION_TEMPLATE_FILE_PATH = String .join (File .separator , "src" ,
35
+ "functionaltest" , "resources" , "responses" ,
33
36
"SubscriptionTemplate.json" );
34
- private static final String SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP = String .join (File .separator , "src" , "functionaltest" , "resources" ,
37
+ private static final String SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP = String .join (
38
+ File .separator , "src" , "functionaltest" , "resources" ,
35
39
"responses" , "SubscriptionForUploadLDAP.json" );
36
40
37
41
private static final String EXPAND_BUTTON_XPATH = "//tr[contains(.,'Subscription1')]/td[1]" ;
@@ -59,9 +63,7 @@ public class TestSubscriptionHandling extends SeleniumBaseClass {
59
63
private static final String TEMPLATE_JENKINS = "Jenkins Pipeline Parameterized Job Trigger" ;
60
64
private static final String EXPECTED_JENKINS_URL = "http://<JenkinsHost:port>/job/<JobName>/job/<branch>/build" ;
61
65
62
- private static MockServerClient mockClient ;
63
- private static ClientAndServer mockServer ;
64
- private static final String BASE_URL = "localhost" ;
66
+ private static ClientAndServer clientAndServer ;
65
67
66
68
@ MockBean
67
69
protected CloseableHttpClient mockedHttpClient ;
@@ -70,14 +72,13 @@ public class TestSubscriptionHandling extends SeleniumBaseClass {
70
72
71
73
@ BeforeClass
72
74
public static void setUpMocks () throws IOException {
73
- mockServer = startClientAndServer ();
74
- mockClient = new MockServerClient (BASE_URL , mockServer .getLocalPort ());
75
+ clientAndServer = startClientAndServer ();
75
76
}
76
77
77
78
@ Before
78
79
public void before () throws IOException {
79
- int serverPort = mockServer .getLocalPort ();
80
- setBackendInstance ("new_instance_default" , "localhost" , serverPort , "" , true );
80
+ int portServer = clientAndServer .getLocalPort ();
81
+ setBackendInstance ("new_instance_default" , "localhost" , portServer , "" , true );
81
82
subscriptionPage = new SubscriptionPage (mockedHttpClient , driver , baseUrl );
82
83
}
83
84
@@ -88,9 +89,10 @@ public void testSubscriptionButtons() throws Exception {
88
89
setupMockEndpoints (security , username );
89
90
removeAllSubscriptions ();
90
91
clickAndVerifyGetTemplateButton ();
91
- clickAndVerifyBulkDownloadButton ();
92
92
uploadSubscriptions ();
93
93
verifySubscriptionsRequestDeleteAndPost ();
94
+ setupMockEndpointMultiSubscription ();
95
+ clickAndVerifyBulkDownloadButton ();
94
96
}
95
97
96
98
@ Test
@@ -146,7 +148,7 @@ public void testSubscriptionHandlingWithLDAPEnabled() throws Exception {
146
148
147
149
@ AfterClass
148
150
public static void tearDownMocks () throws IOException {
149
- mockClient . stop ( );
151
+ stopQuietly ( clientAndServer );
150
152
}
151
153
152
154
private void loadAndRefreshSubscriptionPage () {
@@ -165,29 +167,41 @@ private void removeAllSubscriptions() throws IOException {
165
167
subscriptionPage .refreshPage ();
166
168
}
167
169
168
- private void verifySubscriptionsRequestDeleteAndPost () throws InterruptedException , IOException {
170
+ private void verifySubscriptionsRequestDeleteAndPost ()
171
+ throws InterruptedException , IOException {
169
172
Thread .sleep (1000 );
170
- String downloadedSubscriptionsTemplate = getJSONStringFromFile (DOWNLOADED_TEMPLATE_FILE_PATH );
171
- mockClient .verify (request ().withMethod ("DELETE" ).withPath ("/subscriptions/Subscription1,Subscription2,Subscription3" ));
172
- mockClient .verify (request ().withMethod ("POST" ).withPath ("/subscriptions" ).withBody (downloadedSubscriptionsTemplate ));
173
+ String downloadedSubscriptionsTemplate = getJSONStringFromFile (
174
+ DOWNLOADED_TEMPLATE_FILE_PATH );
175
+ clientAndServer .verify (request ().withMethod ("DELETE" )
176
+ .withPath ("/subscriptions" )
177
+ .withQueryStringParameter ("subscriptionNames" ,
178
+ "Subscription1,Subscription2,Subscription3" ));
179
+ clientAndServer .verify (request ().withMethod ("POST" )
180
+ .withPath ("/subscriptions" )
181
+ .withBody (downloadedSubscriptionsTemplate ));
173
182
}
174
183
175
184
private void clickAndVerifyBulkDownloadButton () throws IOException {
176
185
subscriptionPage .loadPage ();
177
186
subscriptionPage .clickCheckAll ();
178
187
subscriptionPage .clickBulkDownload ();
179
- new WebDriverWait (driver , 10 ).until ((webdriver ) -> Files .exists (Paths .get (DOWNLOADED_BULK_SUBSCRIPTIONS_FILE_PATH )));
180
- String downloadedSubscriptionsTemplate = getJSONStringFromFile (DOWNLOADED_BULK_SUBSCRIPTIONS_FILE_PATH );
188
+ new WebDriverWait (driver , 10 ).until (
189
+ (webdriver ) -> Files .exists (Paths .get (DOWNLOADED_BULK_SUBSCRIPTIONS_FILE_PATH )));
190
+ String downloadedSubscriptionsTemplate = getJSONStringFromFile (
191
+ DOWNLOADED_BULK_SUBSCRIPTIONS_FILE_PATH );
181
192
String subscriptions = getJSONStringFromFile (SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP );
182
193
assertEquals (subscriptions , downloadedSubscriptionsTemplate );
183
194
}
184
195
185
196
private void clickAndVerifyGetTemplateButton () throws IOException {
186
- new WebDriverWait (driver , 10 ).until ((webdriver ) -> subscriptionPage .presenceOfClickGetTemplateButton ());
197
+ new WebDriverWait (driver , 10 ).until (
198
+ (webdriver ) -> subscriptionPage .presenceOfClickGetTemplateButton ());
187
199
subscriptionPage .clickGetTemplate ();
188
- new WebDriverWait (driver , 10 ).until ((webdriver ) -> Files .exists (Paths .get (DOWNLOADED_TEMPLATE_FILE_PATH )));
200
+ new WebDriverWait (driver , 10 ).until (
201
+ (webdriver ) -> Files .exists (Paths .get (DOWNLOADED_TEMPLATE_FILE_PATH )));
189
202
String subscriptionTemplate = getJSONStringFromFile (SUBSCRIPTION_TEMPLATE_FILE_PATH );
190
- String downloadedSubscriptionsTemplate = getJSONStringFromFile (DOWNLOADED_TEMPLATE_FILE_PATH );
203
+ String downloadedSubscriptionsTemplate = getJSONStringFromFile (
204
+ DOWNLOADED_TEMPLATE_FILE_PATH );
191
205
assertEquals (subscriptionTemplate , downloadedSubscriptionsTemplate );
192
206
}
193
207
@@ -238,7 +252,8 @@ private void verifySelectTemplateJenkins() {
238
252
assert (subscriptionPage .isRadioCheckboxSelected (RADIO_BUTTON_REST ));
239
253
assert (!subscriptionPage .isRadioCheckboxSelected (RADIO_BUTTON_JSON ));
240
254
assert (subscriptionPage .isRadioCheckboxSelected (RADIO_BUTTON_KEY_VALUE ));
241
- assertEquals (EXPECTED_JENKINS_URL , subscriptionPage .getValueFromElement (NOTIFICATION_META_ID ));
255
+ assertEquals (EXPECTED_JENKINS_URL ,
256
+ subscriptionPage .getValueFromElement (NOTIFICATION_META_ID ));
242
257
}
243
258
244
259
private void verifySelectTemplateREST () {
@@ -274,7 +289,8 @@ private void clickCloneSubscriptionAndVerifyFormOpen() {
274
289
private void verifyViewButtonOnSubscription () {
275
290
subscriptionPage .clickExpandButtonByXPath (EXPAND_BUTTON_XPATH2 );
276
291
subscriptionPage .clickButtonByXPath (VIEW_BUTTON_XPATH2 );
277
- assert (new WebDriverWait (driver , 10 ).until ((webdriver ) -> driver .getPageSource ().contains ("View Subscription" )));
292
+ assert (new WebDriverWait (driver , 10 ).until (
293
+ (webdriver ) -> driver .getPageSource ().contains ("View Subscription" )));
278
294
subscriptionPage .clickFormCloseBtn ();
279
295
}
280
296
@@ -293,35 +309,62 @@ private void verifyUnauthorizedSubscriptionCRUD() {
293
309
}
294
310
295
311
private void setupMockEndpoints (boolean security , String user ) throws IOException {
296
- mockClient .clear (request ());
297
- String subscriptionResponse = getJSONStringFromFile (SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP );
298
- String downloadBulkSubscriptionResponse = "{\" foundSubscriptions\" : " + subscriptionResponse + "}" ;
299
- mockClient .when (request ().withMethod ("GET" ).withPath ("/subscriptions" ))
300
- .respond (response ().withStatusCode (200 ).withBody (subscriptionResponse ));
301
- mockClient .when (request ().withMethod ("GET" ).withPath ("/subscriptions/Subscription1,Subscription2,Subscription3" ))
302
- .respond (response ().withStatusCode (200 ).withBody (downloadBulkSubscriptionResponse ));
303
- mockClient .when (request ().withMethod ("DELETE" ).withPath ("/subscriptions" )).respond (response ().withStatusCode (200 ).withBody ("" ));
304
- mockClient .when (request ().withMethod ("POST" ).withPath ("/subscriptions" )).respond (response ().withStatusCode (200 ).withBody ("" ));
312
+ clientAndServer .clear (request ());
313
+ String subscriptionResponse = getJSONStringFromFile (
314
+ SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP );
315
+
316
+ clientAndServer .when (request ().withMethod ("GET" ).withPath ("/subscriptions" ))
317
+ .respond (response ().withStatusCode (200 ).withBody (subscriptionResponse ));
318
+ clientAndServer .when (
319
+ request ().withMethod ("DELETE" )
320
+ .withPath ("/subscriptions" )
321
+ .withQueryStringParameter ("subscriptionNames" ,
322
+ "Subscription1,Subscription2,Subscription3" ))
323
+ .respond (response ().withStatusCode (200 ).withBody ("" ));
324
+ clientAndServer .when (request ().withMethod ("POST" ).withPath ("/subscriptions" ))
325
+ .respond (response ().withStatusCode (200 ).withBody ("" ));
305
326
306
327
String subscriptionResponse2 = getJSONStringFromFile (SUBSCRIPTION_TEMPLATE_FILE_PATH );
307
- String downloadSubscriptionResponse = "{\" foundSubscriptions\" : " + subscriptionResponse2 + "}" ;
308
- mockClient .when (request ().withMethod ("GET" ).withPath ("/subscriptions/Subscription2" ))
309
- .respond (response ().withStatusCode (200 ).withBody (subscriptionResponse2 ));
310
- mockClient .when (request ().withMethod ("GET" ).withPath ("/subscriptions/Subscription1" ))
311
- .respond (response ().withStatusCode (200 ).withBody (downloadSubscriptionResponse ));
328
+ clientAndServer .when (request ().withMethod ("GET" ).withPath ("/subscriptions/Subscription2" ))
329
+ .respond (response ().withStatusCode (200 ).withBody (subscriptionResponse2 ));
330
+ clientAndServer .when (request ().withMethod ("GET" ).withPath ("/subscriptions/Subscription1" ))
331
+ .respond (response ().withStatusCode (200 ).withBody (subscriptionResponse2 ));
312
332
313
333
String responseStatus = "{\" status\" :\" OK\" }" ;
314
- mockClient .when (request ().withMethod ("GET" ).withPath ("/auth/checkStatus" )).respond (response ().withStatusCode (200 ).withBody (responseStatus ));
334
+ clientAndServer .when (request ().withMethod ("GET" ).withPath ("/auth/checkStatus" ))
335
+ .respond (response ().withStatusCode (200 ).withBody (responseStatus ));
315
336
316
337
String mockedTemplateResponse = getJSONStringFromFile (SUBSCRIPTION_TEMPLATE_FILE_PATH );
317
- mockClient .when (request ().withMethod ("DELETE" ).withPath ("/subscriptions/Subscription1,Subscription2,Subscription3" ))
318
- .respond (response ().withStatusCode (200 ).withBody ("" ));
319
- mockClient .when (request ().withMethod ("GET" ).withPath ("/download/subscriptionsTemplate" ))
320
- .respond (response ().withStatusCode (200 ).withBody (mockedTemplateResponse ));
338
+ clientAndServer .when (
339
+ request ().withMethod ("DELETE" )
340
+ .withPath ("/subscriptions" )
341
+ .withQueryStringParameter ("subscriptionNames" ,
342
+ "Subscription1,Subscription2,Subscription3" ))
343
+ .respond (response ().withStatusCode (200 ).withBody ("" ));
344
+ clientAndServer .when (
345
+ request ().withMethod ("GET" ).withPath ("/download/subscriptionsTemplate" ))
346
+ .respond (response ().withStatusCode (200 ).withBody (mockedTemplateResponse ));
321
347
322
348
String responseAuth = "{\" security\" :" + security + "}" ;
323
349
String responseUser = "{\" user\" :\" " + user + "\" }" ;
324
- mockClient .when (request ().withMethod ("GET" ).withPath ("/auth" )).respond (response ().withStatusCode (200 ).withBody (responseAuth ));
325
- mockClient .when (request ().withMethod ("GET" ).withPath ("/auth/login" )).respond (response ().withStatusCode (200 ).withBody (responseUser ));
350
+ clientAndServer .when (request ().withMethod ("GET" ).withPath ("/auth" ))
351
+ .respond (response ().withStatusCode (200 ).withBody (responseAuth ));
352
+ clientAndServer .when (request ().withMethod ("GET" ).withPath ("/auth/login" ))
353
+ .respond (response ().withStatusCode (200 ).withBody (responseUser ));
354
+ }
355
+
356
+ private void setupMockEndpointMultiSubscription () throws IOException {
357
+ clientAndServer .clear (request ());
358
+ String subscriptionResponse = getJSONStringFromFile (
359
+ SUBSCRIPTION_FOR_RELOAD_TEST_FILE_PATH_LDAP );
360
+ String downloadBulkSubscriptionResponse = "{\" foundSubscriptions\" : " + subscriptionResponse
361
+ + "}" ;
362
+ clientAndServer .when (
363
+ request ().withMethod ("GET" )
364
+ .withPath ("/subscriptions" )
365
+ .withQueryStringParameter ("subscriptionNames" ,
366
+ "Subscription1,Subscription2,Subscription3" ))
367
+ .respond (response ().withStatusCode (200 )
368
+ .withBody (downloadBulkSubscriptionResponse ));
326
369
}
327
370
}
0 commit comments