forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestPoliciesApiTest.java
More file actions
1088 lines (844 loc) · 42.3 KB
/
RequestPoliciesApiTest.java
File metadata and controls
1088 lines (844 loc) · 42.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.folio.rest.api;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static io.vertx.core.json.JsonObject.mapFrom;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isBadRequest;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isCreated;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isNoContent;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isNotFound;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isUnprocessableEntity;
import static org.folio.rest.support.matchers.TextDateTimeMatcher.withinSecondsAfter;
import static org.folio.rest.support.matchers.ValidationErrorMatchers.hasCode;
import static org.folio.rest.support.matchers.ValidationErrorMatchers.hasMessage;
import static org.folio.rest.support.matchers.ValidationResponseMatchers.isValidationResponseWhich;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.StringContains.containsString;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.folio.rest.jaxrs.model.AllowedServicePoints;
import org.folio.rest.jaxrs.model.RequestPolicy;
import org.folio.rest.jaxrs.model.RequestType;
import org.folio.rest.jaxrs.model.Servicepoint;
import org.folio.rest.jaxrs.model.Servicepoints;
import org.folio.rest.support.ApiTests;
import org.folio.rest.support.JsonResponse;
import org.folio.rest.support.ResponseHandler;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
@RunWith(JUnitParamsRunner.class)
public class RequestPoliciesApiTest extends ApiTests {
private static final int CONNECTION_TIMEOUT = 5;
private static final String DEFAULT_REQUEST_POLICY_NAME = "default_request_policy";
private static final String SERVICE_POINTS_URL = "/service-points";
private static final String INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE = "INVALID_ALLOWED_SERVICE_POINT";
private static int REQ_POLICY_NAME_INCR = 0; //This number is appended to the name of the default request policy to ensure uniqueness
@Before
public void beforeEach() {
truncateTables("request_policy");
}
@Test
public void canCreateARequestPolicy()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
createDefaultRequestPolicy();
}
@Test
public void canCreateRequestPolicyWithoutUID()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
List<RequestType> requestTypes = Collections.singletonList(RequestType.HOLD);
createDefaultRequestPolicy(null,"successful_get","test policy",requestTypes);
}
@Test
public void cannotCreateRequestPolicyWithoutName()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> failedCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Collections.singletonList(RequestType.HOLD);
//create requestPolicy without name
UUID id = UUID.randomUUID();
RequestPolicy requestPolicy = new RequestPolicy();
requestPolicy.withDescription("test policy 2");
requestPolicy.withRequestTypes(requestTypes);
requestPolicy.withId(id.toString());
client.post(requestPolicyStorageUrl(""),
requestPolicy,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(failedCompleted));
JsonResponse response2 = failedCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(response2, isUnprocessableEntity());
JsonObject error = extractErrorObject(response2);
assertThat("unexpected error message", error,
anyOf(hasMessage("must not be null"), hasMessage("darf nicht null sein"))); // any server language
}
@Test
public void cannotCreateRequestPolicyWithExistingName()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException{
CompletableFuture<JsonResponse> failedCompleted = new CompletableFuture<>();
String reqPolicyName = "request_policy_name";
List<RequestType> requestTypes = Collections.singletonList(RequestType.HOLD);
//create a requestPolicy with reqPolicyName as name
createDefaultRequestPolicy(UUID.randomUUID(), reqPolicyName,"test policy", requestTypes );
//create another requestPolicy with the same name
UUID id2 = UUID.randomUUID();
RequestPolicy requestPolicy2 = new RequestPolicy();
requestPolicy2.withDescription("test policy 2");
requestPolicy2.withName(reqPolicyName);
requestPolicy2.withRequestTypes(requestTypes);
requestPolicy2.withId(id2.toString());
client.post(requestPolicyStorageUrl(""),
requestPolicy2,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(failedCompleted));
JsonResponse response2 = failedCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(format("Failed to create request policy: %s", response2.getBody()),
response2.getStatusCode(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
assertThat("unexpected error message" , response2.getBody().contains("duplicate key value"));
}
@Test
public void cannotCreateRequestPolicyWithBadUID()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException{
CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Collections.singletonList(RequestType.HOLD);
String badId = "bad_uuid";
RequestPolicy requestPolicy = new RequestPolicy();
requestPolicy.withDescription("test policy");
requestPolicy.withName("successful_get");
requestPolicy.withRequestTypes(requestTypes);
requestPolicy.withId(badId);
client.post(requestPolicyStorageUrl(""),
requestPolicy,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(createCompleted));
JsonResponse response = createCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(format("Failed to create request policy: %s", response.getBody()),
response.getStatusCode(), is(HttpURLConnection.HTTP_INTERNAL_ERROR));
assertThat(response.getBody(), containsString("Invalid UUID string"));
}
@Test
public void canGetRequestPolicies()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CleanupRequestPolicyRecords();
CompletableFuture<JsonResponse> createCompletedGet = new CompletableFuture<>();
RequestPolicy requestPolicy1 = createDefaultRequestPolicy();
RequestPolicy requestPolicy2 = createDefaultRequestPolicy();
//Get the newly created request policies
client.get(requestPolicyStorageUrl(""), StorageTestSuite.TENANT_ID,
ResponseHandler.json(createCompletedGet));
JsonResponse responseGet = createCompletedGet.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject getResultsJson = responseGet.getJson();
JsonArray requestPolicies = getResultsJson.getJsonArray("requestPolicies");
assertThat(getResultsJson.getInteger("totalRecords"), is(2));
validateRequestPolicy(requestPolicy1, requestPolicies.getJsonObject(0));
validateRequestPolicy(requestPolicy2, requestPolicies.getJsonObject(1));
}
@Test
public void canGetRequestPoliciesByIdUsingQuery()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
RequestPolicy requestPolicy1 = createDefaultRequestPolicy();
//Get the newly created request policies
client.get(requestPolicyStorageUrl("?query=id="+ requestPolicy1.getId()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject responseJson = responseGet.getJson();
JsonArray requestPolicies = responseJson.getJsonArray("requestPolicies");
assertThat(responseJson.getInteger("totalRecords"), is(1));
JsonObject aPolicy = requestPolicies.getJsonObject(0);
validateRequestPolicy(requestPolicy1, aPolicy);
}
@Test
public void cannotGetRequestPoliciesByUsingNegativeLimit()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
RequestPolicy requestPolicy = createDefaultRequestPolicy();
//Get the newly created request policies
client.get(requestPolicyStorageUrl("", "query", "(name=" + requestPolicy.getId() + ")", "limit", "-230"),
StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(responseGet, isBadRequest());
assertThat("expected error message not found", responseGet.getBody().toLowerCase(), containsString("limit"));
}
@Test
public void cannotGetRequestPoliciesByUsingNegativeOffset()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
RequestPolicy requestPolicy = createDefaultRequestPolicy();
//Get the newly created request policies
client.get(requestPolicyStorageUrl("", "query", "(name=" + requestPolicy.getId() + ")", "offset", "-230"),
StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(responseGet, isBadRequest());
assertThat("expected error message not found", responseGet.getBody().toLowerCase(), containsString("offset"));
}
@Test
public void cannotGetRequestPolicyByNonExistentName()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
RequestPolicy requestPolicy = createDefaultRequestPolicy();
//Get the newly created request policies
client.get(requestPolicyStorageUrl("?query=name=" + requestPolicy.getName() + "blabblabla"), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to not get request-policy", responseGet.getStatusCode(), is(HttpURLConnection.HTTP_OK));
JsonObject getResultsJson = responseGet.getJson();
assertThat(getResultsJson.getInteger("totalRecords"), is(0));
}
@Test
public void canGetRequestPolicyByName()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
//create a couple of RequestPolicies
createDefaultRequestPolicy();
RequestPolicy requestPolicy2 = createDefaultRequestPolicy();
//Get the latter created request policy by name
client.get(requestPolicyStorageUrl("?query=name=" + requestPolicy2.getName()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject getResultsJson = responseGet.getJson();
JsonArray requestPolicies = getResultsJson.getJsonArray("requestPolicies");
assertThat(getResultsJson.getInteger("totalRecords"), is(1));
JsonObject aPolicy = requestPolicies.getJsonObject(0);
validateRequestPolicy(requestPolicy2, aPolicy);
}
@Test
public void canGetRequestPolicyById()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.RECALL);
createDefaultRequestPolicy();
RequestPolicy req2 = createDefaultRequestPolicy(UUID.randomUUID(), "request2 name",
"request policy 2 descr", requestTypes );
//Get the newly created request policies
client.get(requestPolicyStorageUrl("/" + req2.getId()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject aPolicy = responseGet.getJson();
validateRequestPolicy(req2, aPolicy);
}
@Test
public void cannotGetRequestPoliciesByNonexistingId()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
createDefaultRequestPolicy();
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
UUID id = UUID.randomUUID();
//Get the newly created request policies
client.get(requestPolicyStorageUrl("/" +id.toString()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(responseGet, isNotFound());
assertThat(responseGet.getBody().toLowerCase(), containsString("not found"));
}
@Test
public void canDeleteRequestPolicies()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CleanupRequestPolicyRecords();
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
CompletableFuture<JsonResponse> delCompleted = new CompletableFuture<>();
CompletableFuture<JsonResponse> getCompletedVerify = new CompletableFuture<>();
//create a couple of request policies to delete.
createDefaultRequestPolicy();
createDefaultRequestPolicy(UUID.randomUUID(), "test policy 2", "descr of test policy 2", null );
//Get the newly created request policies
client.get(requestPolicyStorageUrl( ""), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject getResultsJson = responseGet.getJson();
assertThat(getResultsJson.getInteger("totalRecords"), is(2));
//Delete all policies
client.delete(requestPolicyStorageUrl( ""), StorageTestSuite.TENANT_ID,
ResponseHandler.json(delCompleted));
JsonResponse responseDel = delCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to delete all request policies", responseDel.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
//Get all policies again to verify that none comes back
client.get(requestPolicyStorageUrl( ""), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompletedVerify));
JsonResponse responseGetVerify = getCompletedVerify.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject getResultsVerfiyJson = responseGetVerify.getJson();
assertThat(getResultsVerfiyJson.getInteger("totalRecords"), is(0));
}
@Test
public void canDeleteRequestPoliciesById()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
CompletableFuture<JsonResponse> delCompleted = new CompletableFuture<>();
CompletableFuture<JsonResponse> getCompletedVerify = new CompletableFuture<>();
//create a couple of request policies to delete.
RequestPolicy rp = createDefaultRequestPolicy();
//Get the newly created request policies
client.get(requestPolicyStorageUrl( "/" + rp.getId()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse responseGet = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
JsonObject getResultsJson = responseGet.getJson();
assertThat(getResultsJson, is(notNullValue()));
assertThat(getResultsJson.getString("id"), is(rp.getId()));
//Delete existing policy
client.delete(requestPolicyStorageUrl( "/" + rp.getId()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(delCompleted));
JsonResponse responseDel = delCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to delete all request policies", responseDel.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
//Get all policies again to verify that none comes back
client.get(requestPolicyStorageUrl( "/" + rp.getId()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompletedVerify));
JsonResponse responseGetVerify = getCompletedVerify.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(responseGetVerify, isNotFound());
assertThat(responseGetVerify.getBody().toLowerCase(), containsString("not found"));
}
@Test
public void cannotDeleteRequestPoliciesByNonExistingId()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> delCompleted = new CompletableFuture<>();
//Delete existing policy
client.delete(requestPolicyStorageUrl( "/" + UUID.randomUUID().toString()), StorageTestSuite.TENANT_ID,
ResponseHandler.json(delCompleted));
JsonResponse responseDel = delCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to not delete all request policies", responseDel, isNotFound());
}
@Test
public void canUpdateRequestPolicy()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> updateCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.PAGE);
RequestPolicy requestPolicy = createDefaultRequestPolicy(UUID.randomUUID(), "sample requet policy",
"plain description", requestTypes);
//update requestPolicy to new values
requestPolicy.setDescription("new description");
requestPolicy.setName("sample request policies");
requestPolicy.setRequestTypes(Arrays.asList( RequestType.RECALL, RequestType.HOLD));
client.put(requestPolicyStorageUrl("/" + requestPolicy.getId()),
requestPolicy,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(updateCompleted));
JsonResponse response = updateCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to update request-policy", response.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
//Get it and examine the updated content
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
client.get(requestPolicyStorageUrl("/" + requestPolicy.getId()),
StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse getResponse = getCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to update request-policy", getResponse.getStatusCode(), is(HttpURLConnection.HTTP_OK));
JsonObject aPolicy = getResponse.getJson();
validateRequestPolicy(requestPolicy, aPolicy);
}
@Test
public void cannotUpdateRequestPolicyWithWrongId()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> updateCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.PAGE);
RequestPolicy requestPolicy = createDefaultRequestPolicy(UUID.randomUUID(), "sample request policy",
"plain description", requestTypes);
//update
String newUid = UUID.randomUUID().toString();
requestPolicy.setDescription("new description");
client.put(requestPolicyStorageUrl("/" + newUid),
requestPolicy,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(updateCompleted));
//Because of the unique name constraint, instead of getting an error message about a nonexistent object, the message
//is about duplicate name. This happens because PUT can also be used to add a new record to the database.
JsonResponse response = updateCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(response, isBadRequest());
assertThat(response.getBody(), containsString("already exists"));
}
@Test
public void cannotUpdateRequestPolicyToExistingPolicyName()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> updateCompleted = new CompletableFuture<>();
String policy1Name = "Policy 1";
String policy2Name = "Policy 2";
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.PAGE);
createDefaultRequestPolicy(UUID.randomUUID(), policy1Name,
"plain description", requestTypes);
RequestPolicy requestPolicy2 = createDefaultRequestPolicy(UUID.randomUUID(), policy2Name,
"plain description", requestTypes);
//update: set the name of requestPolicy2 to be of policy1's.
requestPolicy2.setDescription("new description for policy 2");
requestPolicy2.setName(policy1Name);
requestPolicy2.setRequestTypes(Arrays.asList( RequestType.RECALL, RequestType.HOLD));
client.put(requestPolicyStorageUrl("/" + requestPolicy2.getId()),
requestPolicy2,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(updateCompleted));
JsonResponse response = updateCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to update request-policy", response, isBadRequest());
assertThat("Error message does not contain keyword 'already existed'", response.getBody(), containsString("already exists"));
}
@Test
public void cannotUpdateRequestPolicyWithoutName()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> failedCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Collections.singletonList(RequestType.HOLD);
RequestPolicy requestPolicy = createDefaultRequestPolicy();
//update requestPolicy
requestPolicy.setDescription("new description!!!");
requestPolicy.setName(null);
requestPolicy.setRequestTypes(requestTypes);
client.put(requestPolicyStorageUrl("/" + requestPolicy.getId()),
requestPolicy,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(failedCompleted));
JsonResponse response2 = failedCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat(response2, isUnprocessableEntity());
JsonObject error = extractErrorObject(response2);
assertThat("unexpected error message", error,
anyOf(hasMessage("must not be null"), hasMessage("darf nicht null sein"))); // any server language
}
@Test
public void canUpdateRequestPolicyWithNewId()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
CompletableFuture<JsonResponse> updateCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.PAGE);
RequestPolicy requestPolicy = createDefaultRequestPolicy(UUID.randomUUID(), "old name",
"plain description", requestTypes);
//update requestPolicy
String newUid = UUID.randomUUID().toString();
requestPolicy.setDescription("new description");
requestPolicy.setName("new name");
requestPolicy.setId(newUid);
client.put(requestPolicyStorageUrl("/" + newUid),
requestPolicy,
StorageTestSuite.TENANT_ID,
ResponseHandler.json(updateCompleted));
JsonResponse response = updateCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to update request-policy", response.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
}
@Test
public void createdRequestHasCreationMetadata()
throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
final String METADATA_PROPERTY = "metadata";
CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.PAGE);
RequestPolicy requestPolicy = new RequestPolicy();
requestPolicy.withDescription("Sample request policy");
requestPolicy.withName("Request Policy 1");
requestPolicy.withRequestTypes(requestTypes);
requestPolicy.withId(UUID.randomUUID().toString());
String creatorId = UUID.randomUUID().toString();
DateTime requestMade = DateTime.now();
//Create requestPolicy
client.post(requestPolicyStorageUrl(""),
requestPolicy,
StorageTestSuite.TENANT_ID,
creatorId,
ResponseHandler.json(createCompleted));
JsonResponse response = createCompleted.get(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
assertThat("Failed to create new request-policy", response.getStatusCode(), is(HttpURLConnection.HTTP_CREATED));
JsonObject createdResponse = response.getJson();
assertThat("Request should have metadata property",
createdResponse.containsKey(METADATA_PROPERTY), is(true));
JsonObject metadata = createdResponse.getJsonObject(METADATA_PROPERTY);
assertThat("Request should have created user",
metadata.getString("createdByUserId"), is(creatorId));
//RAML-Module-Builder also populates updated information at creation time
assertThat("Request should have updated user",
metadata.getString("updatedByUserId"), is(creatorId));
assertThat("Request should have update date close to when request was made",
metadata.getString("updatedDate"),
is(withinSecondsAfter(Seconds.seconds(2), requestMade)));
}
@Test
public void canCreateRequestPolicyWithNoAllowedServicePoints()
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
RequestPolicy policy = new RequestPolicy()
.withName("Request policy with no allowed service points")
.withAllowedServicePoints(new AllowedServicePoints());
JsonResponse response = createRequestPolicy(policy);
assertThat(response, isCreated());
assertThat(response.getJson().getJsonObject("allowedServicePoints"), emptyIterable());
}
@Test
@Parameters(source = RequestType.class)
public void canCreateRequestPolicyWithNullListOfAllowedServicePoints(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
RequestPolicy policy = new RequestPolicy()
.withName("Request policy with no allowed service points")
.withAllowedServicePoints(new AllowedServicePoints());
JsonObject requestPolicyJson = JsonObject.mapFrom(policy);
requestPolicyJson.getJsonObject("allowedServicePoints").putNull(requestType.value());
JsonResponse response = createRequestPolicy(requestPolicyJson);
assertThat(response, isCreated());
assertThat(response.getJson().getJsonObject("allowedServicePoints"), emptyIterable());
}
@Test
@Parameters(source = RequestType.class)
public void canCreateRequestPolicyWithAllowedServicePoints(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
String servicePointId = randomId();
createStubForServicePoints(new Servicepoint()
.withId(servicePointId)
.withPickupLocation(true));
JsonResponse response = createRequestPolicy(buildRequestPolicy(requestType, List.of(servicePointId)));
assertThat(response, isCreated());
}
@Test
@Parameters(source = RequestType.class)
public void canNotCreateRequestPolicyWithEmptyListOfAllowedServicePoints(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
JsonResponse response = createRequestPolicy(buildRequestPolicy(requestType, emptyList()));
assertThat(response, isUnprocessableEntity());
assertThat(extractErrorObject(response).getString("message"),
containsString("size must be between 1 and 2147483647"));
}
@Test
@Parameters(source = RequestType.class)
public void duplicateAllowedServicePointIdsAreRemoved(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
String servicePointId = randomId();
createStubForServicePoints(new Servicepoint()
.withId(servicePointId)
.withPickupLocation(true));
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId, servicePointId));
JsonResponse response = createRequestPolicy(requestPolicy);
assertThat(response, isCreated());
JsonArray allowedServicePoints = response.getJson()
.getJsonObject("allowedServicePoints")
.getJsonArray(requestType.value());
assertThat(allowedServicePoints, iterableWithSize(1));
assertThat(allowedServicePoints.getString(0), is(servicePointId));
}
@Test
@Parameters(source = RequestType.class)
public void canNotCreateRequestPolicyWithNonExistentAllowedServicePoint(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
createStubForServicePoints(emptyList());
final String servicePointId = randomId();
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse response = createRequestPolicy(requestPolicy);
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
@Parameters(source = RequestType.class)
public void canNotUpdateRequestPolicyWithNonExistentAllowedServicePoint(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
createStubForServicePoints(emptyList());
final String servicePointId = randomId();
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse response = updateRequestPolicy(requestPolicy);
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
@Parameters(source = RequestType.class)
public void canNotCreateRequestPolicyWithNonPickupLocationServicePoint(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
final String servicePointId = randomId();
Servicepoint servicePoint = new Servicepoint()
.withId(servicePointId)
.withPickupLocation(false);
createStubForServicePoints(servicePoint);
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse response = createRequestPolicy(requestPolicy);
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
@Parameters(source = RequestType.class)
public void canNotUpdateRequestPolicyWithNonPickupLocationServicePoint(RequestType requestType)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
final String servicePointId = randomId();
Servicepoint servicePoint = new Servicepoint()
.withId(servicePointId)
.withPickupLocation(false);
createStubForServicePoints(servicePoint);
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse response = updateRequestPolicy(requestPolicy);
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
@Parameters(source = RequestType.class)
public void canNotCreateRequestPolicyWhenAllowedServicePointHasNullPickupLocation(
RequestType requestType) throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
final String servicePointId = randomId();
Servicepoint servicePoint = new Servicepoint()
.withId(servicePointId)
.withPickupLocation(null);
createStubForServicePoints(servicePoint);
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse response = createRequestPolicy(requestPolicy);
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
@Parameters(source = RequestType.class)
public void canNotUpdateRequestPolicyWhenAllowedServicePointHasNullPickupLocation(
RequestType requestType) throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
final String servicePointId = randomId();
Servicepoint servicePoint = new Servicepoint()
.withId(servicePointId)
.withPickupLocation(null);
createStubForServicePoints(servicePoint);
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse response = updateRequestPolicy(requestPolicy);
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
public void singleErrorIsReturnedWhenUsingMultipleInvalidServicePointIds()
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
String nonExistentServicePointId = randomId();
String nonPickupLocationServicePointId = randomId();
String nullPickupLocationServicePointId = randomId();
Set<String> servicePointIds = Set.of(nonExistentServicePointId, nonPickupLocationServicePointId,
nullPickupLocationServicePointId);
Servicepoint nonPickupLocationServicePoint = new Servicepoint()
.withId(nonPickupLocationServicePointId)
.withPickupLocation(false);
Servicepoint nullPickupLocationServicePoint = new Servicepoint()
.withId(nullPickupLocationServicePointId)
.withPickupLocation(null);
createStubForServicePoints(List.of(nonPickupLocationServicePoint, nullPickupLocationServicePoint));
JsonResponse response = createRequestPolicy(new RequestPolicy()
.withName("Test request policy")
.withRequestTypes(List.of(RequestType.PAGE, RequestType.HOLD, RequestType.RECALL))
.withAllowedServicePoints(new AllowedServicePoints()
.withPage(servicePointIds)
.withHold(servicePointIds)
.withRecall(servicePointIds)));
assertThat(response, isUnprocessableEntity());
assertThat(response.getJson().getJsonArray("errors"), iterableWithSize(1));
assertThat(response, isValidationResponseWhich(allOf(
hasMessage("One or more Pickup locations are no longer available"),
hasCode(INVALID_ALLOWED_SERVICE_POINT_ERROR_CODE)
)));
}
@Test
@Parameters(source = RequestType.class)
public void requestPolicyIsCreatedAndUpdatedWhenAllowedServicePointContainsStaffSlipsConfiguration(
RequestType requestType) throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
String servicePointId = randomId();
JsonObject servicePoint = new JsonObject()
.put("id", servicePointId)
.put("pickupLocation", true)
.put("staffSlips", new JsonArray()
.add(new JsonObject()
.put("id", randomId())
.put("printByDefault", true)
));
createStubForServicePoints(new JsonObject().put("servicepoints", new JsonArray().add(servicePoint)));
JsonObject requestPolicy = buildRequestPolicy(requestType, List.of(servicePointId));
JsonResponse postResponse = createRequestPolicy(requestPolicy);
assertThat(postResponse, isCreated());
JsonResponse putResponse = updateRequestPolicy(requestPolicy);
assertThat(putResponse, isNoContent());
}
private static JsonObject buildRequestPolicy(RequestType requestType,
List<String> allowedServicePointIds) {
return new JsonObject()
.put("id", randomId())
.put("name", "Request policy for " + requestType.value())
.put("requestTypes", new JsonArray().add(requestType.value()))
.put("allowedServicePoints", new JsonObject()
.put(requestType.value(), new JsonArray(allowedServicePointIds)));
}
private JsonResponse createRequestPolicy(RequestPolicy requestPolicy)
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
return createRequestPolicy(JsonObject.mapFrom(requestPolicy));
}
private JsonResponse createRequestPolicy(JsonObject requestPolicy)
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonResponse> postFuture = new CompletableFuture<>();
client.post(requestPolicyStorageUrl(""), requestPolicy, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postFuture));
return postFuture.get(5, TimeUnit.SECONDS);
}
private JsonResponse updateRequestPolicy(RequestPolicy requestPolicy)
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonResponse> putFuture = new CompletableFuture<>();
client.put(requestPolicyStorageUrl("/" + requestPolicy.getId()), requestPolicy,
StorageTestSuite.TENANT_ID, ResponseHandler.json(putFuture));
return updateRequestPolicy(mapFrom(requestPolicy));
}
private JsonResponse updateRequestPolicy(JsonObject requestPolicy)
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonResponse> putFuture = new CompletableFuture<>();
client.put(requestPolicyStorageUrl("/" + requestPolicy.getString("id")), requestPolicy,
StorageTestSuite.TENANT_ID, ResponseHandler.json(putFuture));
return putFuture.get(5, TimeUnit.SECONDS);
}
private URL requestPolicyStorageUrl(String path, String... parameterKeyValue) throws MalformedURLException {
return StorageTestSuite.storageUrl("/request-policy-storage/request-policies" + path, parameterKeyValue);
}
private RequestPolicy createDefaultRequestPolicy() throws InterruptedException,
MalformedURLException,
TimeoutException,
ExecutionException {
List<RequestType> requestTypes = Arrays.asList( RequestType.HOLD, RequestType.PAGE);
UUID id1 = UUID.randomUUID();
REQ_POLICY_NAME_INCR++;