2
2
3
3
import static org .junit .Assert .assertEquals ;
4
4
import static org .junit .Assert .assertFalse ;
5
- import static org .junit .Assert .assertNull ;
6
5
import static org .junit .Assert .assertTrue ;
7
6
7
+ import java .util .Arrays ;
8
+ import java .util .List ;
9
+
10
+ import org .hamcrest .Description ;
11
+ import org .hamcrest .TypeSafeMatcher ;
8
12
import org .jmock .Expectations ;
9
13
import org .jmock .integration .junit4 .JUnitRuleMockery ;
10
14
import org .joda .time .DateTime ;
22
26
*/
23
27
24
28
public class ServiceApiDriverTest {
29
+
25
30
@ Rule
26
31
public JUnitRuleMockery context = new JUnitRuleMockery ();
27
32
@@ -57,7 +62,7 @@ public void test_custom_host() {
57
62
58
63
@ Test
59
64
public void test_authrep_usage_is_encoded () throws ServerError {
60
- assertAuthrepUrlWithParams ("& %5Busage%5D%5Bmethod%5D=666" );
65
+ assertAuthrepUrlWithParams ("%5Busage%5D%5Bmethod%5D=666" );
61
66
62
67
ParameterMap params = new ParameterMap ();
63
68
ParameterMap usage = new ParameterMap ();
@@ -70,7 +75,7 @@ public void test_authrep_usage_is_encoded() throws ServerError {
70
75
@ Test
71
76
public void test_authrep_usage_values_are_encoded () throws ServerError {
72
77
73
- assertAuthrepUrlWithParams ("& %5Busage%5D%5Bhits%5D=%230" );
78
+ assertAuthrepUrlWithParams ("%5Busage%5D%5Bhits%5D=%230" );
74
79
75
80
ParameterMap params = new ParameterMap ();
76
81
ParameterMap usage = new ParameterMap ();
@@ -83,7 +88,7 @@ public void test_authrep_usage_values_are_encoded() throws ServerError {
83
88
@ Test
84
89
public void test_authrep_usage_defaults_to_hits_1 () throws ServerError {
85
90
86
- assertAuthrepUrlWithParams ("& %5Busage%5D%5Bhits%5D=1&app_id=appid" );
91
+ assertAuthrepUrlWithParams ("%5Busage%5D%5Bhits%5D=1&app_id=appid" );
87
92
88
93
ParameterMap params = new ParameterMap ();
89
94
params .add ("app_id" , "appid" );
@@ -93,7 +98,7 @@ public void test_authrep_usage_defaults_to_hits_1() throws ServerError {
93
98
94
99
@ Test
95
100
public void test_authrep_supports_app_id_app_key_auth_mode () throws ServerError {
96
- assertAuthrepUrlWithParams ("& %5Busage%5D%5Bhits%5D=1&app_key=appkey&app_id=appid" );
101
+ assertAuthrepUrlWithParams ("%5Busage%5D%5Bhits%5D=1&app_key=appkey&app_id=appid" );
97
102
98
103
ParameterMap params = new ParameterMap ();
99
104
params .add ("app_id" , "appid" );
@@ -108,8 +113,8 @@ public void test_successful_authrep_with_app_keys() throws ServerError {
108
113
"<plan>Ultimate</plan>" +
109
114
"</status>" ;
110
115
111
- context .checking (new Expectations () {{
112
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authrep.xml?provider_key=1234abcd&%5Busage%5D%5Bhits%5D=1&app_key=toosecret&app_id=foo" );
116
+ context .checking (new UrlWithParamsExpectations () {{
117
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authrep.xml?provider_key=1234abcd&%5Busage%5D%5Bhits%5D=1&app_key=toosecret&app_id=foo" ) );
113
118
will (returnValue (new HttpResponse (200 , body )));
114
119
}});
115
120
@@ -144,8 +149,8 @@ public void test_successful_authorize() throws ServerError {
144
149
" </usage_reports>" +
145
150
"</status>" ;
146
151
147
- context .checking (new Expectations () {{
148
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" );
152
+ context .checking (new UrlWithParamsExpectations () {{
153
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" ) );
149
154
will (returnValue (new HttpResponse (200 , body )));
150
155
}});
151
156
@@ -177,8 +182,8 @@ public void test_successful_authorize_with_app_keys() throws ServerError {
177
182
"<plan>Ultimate</plan>" +
178
183
"</status>" ;
179
184
180
- context .checking (new Expectations () {{
181
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_key=toosecret&app_id=foo" );
185
+ context .checking (new UrlWithParamsExpectations () {{
186
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_key=toosecret&app_id=foo" ) );
182
187
will (returnValue (new HttpResponse (200 , body )));
183
188
}});
184
189
@@ -216,8 +221,8 @@ public void test_authorize_with_exceeded_usage_limits() throws ServerError {
216
221
"</status>" ;
217
222
218
223
219
- context .checking (new Expectations () {{
220
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" );
224
+ context .checking (new UrlWithParamsExpectations () {{
225
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" ) );
221
226
will (returnValue (new HttpResponse (409 , body )));
222
227
}});
223
228
@@ -234,8 +239,8 @@ public void test_authorize_with_exceeded_usage_limits() throws ServerError {
234
239
public void test_authorize_with_invalid_app_id () throws ServerError {
235
240
final String body = "<error code=\" application_not_found\" >application with id=\" foo\" was not found</error>" ;
236
241
237
- context .checking (new Expectations () {{
238
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" );
242
+ context .checking (new UrlWithParamsExpectations () {{
243
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" ) );
239
244
will (returnValue (new HttpResponse (403 , body )));
240
245
}});
241
246
@@ -263,8 +268,8 @@ public void test_authorize_metric_period_eternity() throws ServerError {
263
268
"</status>" ;
264
269
265
270
266
- context .checking (new Expectations () {{
267
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" );
271
+ context .checking (new UrlWithParamsExpectations () {{
272
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" ) );
268
273
will (returnValue (new HttpResponse (200 , body )));
269
274
}});
270
275
@@ -287,8 +292,8 @@ public void test_authorize_metric_period_eternity() throws ServerError {
287
292
288
293
@ Test (expected = ServerError .class )
289
294
public void test_authorize_with_server_error () throws ServerError {
290
- context .checking (new Expectations () {{
291
- oneOf (htmlServer ).get ("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" );
295
+ context .checking (new UrlWithParamsExpectations () {{
296
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo" ) );
292
297
will (returnValue (new HttpResponse (500 , "OMG! WTF!" )));
293
298
}});
294
299
@@ -325,8 +330,8 @@ public void test_successful_oauth_authorize() throws ServerError {
325
330
"</usage_reports>" +
326
331
"</status>" ;
327
332
328
- context .checking (new Expectations () {{
329
- oneOf (htmlServer ).get ("http://" + host + "/transactions/oauth_authorize.xml?redirect_url=http%3A//localhost%3A8080/oauth/oauth_redirect&provider_key=1234abcd&app_id=foo" );
333
+ context .checking (new UrlWithParamsExpectations () {{
334
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/oauth_authorize.xml?redirect_url=http%3A//localhost%3A8080/oauth/oauth_redirect&provider_key=1234abcd&app_id=foo" ) );
330
335
will (returnValue (new HttpResponse (200 , body )));
331
336
}});
332
337
@@ -384,8 +389,8 @@ public void test_oauth_authorize_with_exceeded_usage_limits() throws ServerError
384
389
"</usage_reports>" +
385
390
"</status>" ;
386
391
387
- context .checking (new Expectations () {{
388
- oneOf (htmlServer ).get ("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo" );
392
+ context .checking (new UrlWithParamsExpectations () {{
393
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo" ) );
389
394
will (returnValue (new HttpResponse (409 , body )));
390
395
}});
391
396
@@ -403,8 +408,8 @@ public void test_oauth_authorize_with_exceeded_usage_limits() throws ServerError
403
408
public void test_oauth_authorize_with_invalid_app_id () throws ServerError {
404
409
final String body = "<error code=\" application_not_found\" >application with id=\" foo\" was not found</error>" ;
405
410
406
- context .checking (new Expectations () {{
407
- oneOf (htmlServer ).get ("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo" );
411
+ context .checking (new UrlWithParamsExpectations () {{
412
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo" ) );
408
413
will (returnValue (new HttpResponse (403 , body )));
409
414
}});
410
415
@@ -421,8 +426,8 @@ public void test_oauth_authorize_with_invalid_app_id() throws ServerError {
421
426
@ Test (expected = ServerError .class )
422
427
public void test_oath_authorize_with_server_error () throws ServerError {
423
428
424
- context .checking (new Expectations () {{
425
- oneOf (htmlServer ).get ("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo" );
429
+ context .checking (new UrlWithParamsExpectations () {{
430
+ oneOf (htmlServer ).get (withUrl ( "http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo" ) );
426
431
will (returnValue (new HttpResponse (500 , "OMG! WTF!" )));
427
432
}});
428
433
@@ -434,13 +439,13 @@ public void test_oath_authorize_with_server_error() throws ServerError {
434
439
435
440
@ Test (expected = IllegalArgumentException .class )
436
441
public void test_report_raises_an_exception_if_no_transactions_given () throws ServerError {
437
- serviceApi .report (null , null );
442
+ serviceApi .report (null );
438
443
}
439
444
440
445
@ Test
441
446
public void test_successful_report () throws ServerError {
442
447
443
- context .checking (new Expectations () {{
448
+ context .checking (new UrlWithParamsExpectations () {{
444
449
oneOf (htmlServer ).post (with ("http://" + host + "/transactions.xml" ), with (any (String .class )));
445
450
will (returnValue (new HttpResponse (202 , "" )));
446
451
}});
@@ -471,8 +476,8 @@ public void test_report_encodes_transactions() throws ServerError {
471
476
"&transactions%5B1%5D%5Bapp_id%5D=bar" +
472
477
"&provider_key=1234abcd" ;
473
478
474
- context .checking (new Expectations () {{
475
- oneOf (htmlServer ).post ("http://" + host + "/transactions.xml" , urlParams );
479
+ context .checking (new UrlWithParamsExpectations () {{
480
+ oneOf (htmlServer ).post (with ( "http://" + host + "/transactions.xml" ), withParams ( urlParams ) );
476
481
will (returnValue (new HttpResponse (200 , "" )));
477
482
}});
478
483
@@ -501,7 +506,7 @@ public void test_report_encodes_transactions() throws ServerError {
501
506
public void test_failed_report () throws ServerError {
502
507
final String error_body = "<error code=\" provider_key_invalid\" >provider key \" foo\" is invalid</error>" ;
503
508
504
- context .checking (new Expectations () {{
509
+ context .checking (new UrlWithParamsExpectations () {{
505
510
oneOf (htmlServer ).post (with ("http://" + host + "/transactions.xml" ), with (any (String .class )));
506
511
will (returnValue (new HttpResponse (403 , error_body )));
507
512
}});
@@ -522,7 +527,7 @@ public void test_failed_report() throws ServerError {
522
527
@ Test (expected = ServerError .class )
523
528
public void test_report_with_server_error () throws ServerError {
524
529
525
- context .checking (new Expectations () {{
530
+ context .checking (new UrlWithParamsExpectations () {{
526
531
oneOf (htmlServer ).post (with ("http://" + host + "/transactions.xml" ), with (any (String .class )));
527
532
will (returnValue (new HttpResponse (500 , "OMG! WTF!" )));
528
533
}});
@@ -537,16 +542,94 @@ public void test_report_with_server_error() throws ServerError {
537
542
538
543
539
544
private void assertAuthrepUrlWithParams (final String params ) throws ServerError {
540
- final String authrep_url = "http://" + host + "/transactions/authrep.xml?provider_key= " + provider_key + params ;
545
+ final String authrep_url = "http://" + host + "/transactions/authrep.xml?" + params + "& provider_key=" + provider_key ;
541
546
542
547
final String body = "<status>" +
543
548
"<authorized>true</authorized>" +
544
549
"<plan>Ultimate</plan>" +
545
550
"</status>" ;
546
551
//System.out.println("Expect: "+ authrep_url);
547
- context .checking (new Expectations () {{
548
- oneOf (htmlServer ).get (authrep_url );
549
- will (returnValue (new HttpResponse (200 , body )));
550
- }});
552
+ context .checking (new UrlWithParamsExpectations () {
553
+ {
554
+ oneOf (htmlServer ).get (withUrl (authrep_url ));
555
+ will (returnValue (new HttpResponse (200 , body )));
556
+ }
557
+ });
558
+ }
559
+
560
+ private static class UrlWithParamsExpectations extends Expectations {
561
+
562
+ protected String withUrl (String url ) {
563
+ currentBuilder ().addParameterMatcher (new UrlWithParamsMatcher (url ));
564
+ return url ;
565
+ }
566
+
567
+ protected String withParams (String params ) {
568
+ currentBuilder ().addParameterMatcher (new ParamsMatcher (params ));
569
+ return params ;
570
+ }
571
+
551
572
}
573
+
574
+ private static class UrlWithParamsMatcher extends TypeSafeMatcher <String > {
575
+
576
+ private final String expectedUrl ;
577
+
578
+ public UrlWithParamsMatcher (String expectedUrl ) {
579
+ this .expectedUrl = expectedUrl ;
580
+ }
581
+
582
+ @ Override
583
+ public boolean matchesSafely (String actualUrl ) {
584
+ // Match the first part of the URL (scheme, host, port, etc)
585
+ String expectedUrlHead = expectedUrl .substring (0 , expectedUrl .indexOf ("?" ));
586
+ String actualUrlHead = actualUrl .substring (0 , actualUrl .indexOf ("?" ));
587
+
588
+ // Match the params (order then compare)
589
+ String expectedParams = expectedUrl .substring (expectedUrl .indexOf ("?" ) + 1 );
590
+ String [] expectedParamsSplit = expectedParams .split ("&" );
591
+ Arrays .sort (expectedParamsSplit );
592
+ String actualParams = actualUrl .substring (actualUrl .indexOf ("?" ) + 1 );
593
+ String [] actualParamsSplit = actualParams .split ("&" );
594
+ Arrays .sort (actualParamsSplit );
595
+
596
+ List <String > expected = Arrays .asList (expectedParamsSplit );
597
+ List <String > actual = Arrays .asList (actualParamsSplit );
598
+
599
+ return expected .equals (actual ) && expectedUrlHead .equals (actualUrlHead );
600
+ }
601
+
602
+ @ Override
603
+ public void describeTo (Description description ) {
604
+ description .appendText ("a url matching '" + this .expectedUrl + "'" );
605
+ }
606
+ }
607
+
608
+ private static class ParamsMatcher extends TypeSafeMatcher <String > {
609
+
610
+ private final String expectedParams ;
611
+
612
+ public ParamsMatcher (String expectedParams ) {
613
+ this .expectedParams = expectedParams ;
614
+ }
615
+
616
+ @ Override
617
+ public boolean matchesSafely (String actualParams ) {
618
+ String [] expectedParamsSplit = expectedParams .split ("&" );
619
+ Arrays .sort (expectedParamsSplit );
620
+ String [] actualParamsSplit = actualParams .split ("&" );
621
+ Arrays .sort (actualParamsSplit );
622
+
623
+ List <String > expected = Arrays .asList (expectedParamsSplit );
624
+ List <String > actual = Arrays .asList (actualParamsSplit );
625
+
626
+ return expected .equals (actual );
627
+ }
628
+
629
+ @ Override
630
+ public void describeTo (Description description ) {
631
+ description .appendText ("params matching '" + this .expectedParams + "'" );
632
+ }
633
+ }
634
+
552
635
}
0 commit comments