forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CancellationReasonsApiTest.java
337 lines (292 loc) · 11.8 KB
/
CancellationReasonsApiTest.java
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
package org.folio.rest.api;
import io.vertx.core.json.JsonObject;
import org.folio.rest.support.*;
import org.folio.rest.support.builders.RequestRequestBuilder;
import org.hamcrest.junit.MatcherAssert;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
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 static org.folio.rest.support.matchers.OkapiResponseStatusCodeMatchers.matchesBadRequest;
import static org.folio.rest.support.matchers.OkapiResponseStatusCodeMatchers.matchesCreated;
import static org.folio.rest.support.matchers.OkapiResponseStatusCodeMatchers.matchesNoContent;
import static org.folio.rest.support.matchers.OkapiResponseStatusCodeMatchers.matchesNotFound;
import static org.folio.rest.support.matchers.OkapiResponseStatusCodeMatchers.matchesOk;
import static org.folio.rest.support.matchers.OkapiResponseStatusCodeMatchers.matchesUnprocessableEntity;
import static org.folio.rest.api.RequestsApiTest.requestStorageUrl;
import static org.folio.rest.support.builders.RequestRequestBuilder.CLOSED_CANCELLED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
*
* @author kurt
*/
public class CancellationReasonsApiTest extends ApiTests {
private static URL cancelReasonURL() throws MalformedURLException {
return cancelReasonURL("");
}
private static URL cancelReasonURL(String subPath)
throws MalformedURLException {
return StorageTestSuite.storageUrl(
"/cancellation-reason-storage/cancellation-reasons" + subPath);
}
private void assertCreateCancellationReason(JsonObject request)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
JsonResponse response = createCancellationReason(request);
MatcherAssert.assertThat(String.format("Failed to create cancellation reason: %s",
response.getBody()), response, matchesCreated());
new IndividualResource(response);
}
private JsonResponse createCancellationReason(JsonObject request)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
client.post(cancelReasonURL(), request, StorageTestSuite.TENANT_ID,
ResponseHandler.json(createCompleted));
return createCompleted.get(5, TimeUnit.SECONDS);
}
private IndividualResource assertGetCancellationReason(String id)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
JsonResponse response = getCancellationReason(id);
MatcherAssert.assertThat(String.format("Failed to retrieve cancellation reason: %s (%s)",
response.getBody(), response.getStatusCode()),
response, matchesOk());
return new IndividualResource(response);
}
private JsonResponse getCancellationReason(String id)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
CompletableFuture<JsonResponse> getReasonFuture = new CompletableFuture<>();
client.get(cancelReasonURL("/"+id), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getReasonFuture));
return getReasonFuture.get(5, TimeUnit.SECONDS);
}
private JsonResponse getCancellationReasonCollection(String query)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
CompletableFuture<JsonResponse> getReasonsFuture = new CompletableFuture<>();
String queryParam;
if(query != null) {
queryParam = "?query=" + query;
} else {
queryParam = "";
}
client.get(cancelReasonURL(queryParam), StorageTestSuite.TENANT_ID,
ResponseHandler.json(getReasonsFuture));
return getReasonsFuture.get(5, TimeUnit.SECONDS);
}
private TextResponse updateCancellationReason(String id, JsonObject request)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
CompletableFuture<TextResponse> updateReasonFuture = new CompletableFuture<>();
client.put(cancelReasonURL("/"+id), request, StorageTestSuite.TENANT_ID,
ResponseHandler.text(updateReasonFuture));
return updateReasonFuture.get(5, TimeUnit.SECONDS);
}
private void assertUpdateCancellationReason(String id, JsonObject request)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
TextResponse response = updateCancellationReason(id, request);
MatcherAssert.assertThat(String.format("Failed to update cancellation reason: %s (%s)",
response.getBody(), response.getStatusCode()),
response, matchesNoContent());
}
private TextResponse deleteCancellationReason(String id)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
CompletableFuture<TextResponse> deleteReasonFuture = new CompletableFuture<>();
client.delete(cancelReasonURL("/"+id), StorageTestSuite.TENANT_ID,
ResponseHandler.text(deleteReasonFuture));
return deleteReasonFuture.get(5, TimeUnit.SECONDS);
}
private void assertDeleteCancellationReason(String id)
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
TextResponse response = deleteCancellationReason(id);
MatcherAssert.assertThat(String.format("Failed to delete cancellation reason: %s (%s)",
response.getBody(), response.getStatusCode()),
response, matchesNoContent());
}
//Test Init
@Before
public void beforeEach()
throws MalformedURLException {
StorageTestSuite.deleteAll(requestStorageUrl());
StorageTestSuite.deleteAll(cancelReasonURL());
}
//Tests
@Test
public void canCreateCancellationReason()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
JsonObject request = new JsonObject()
.put("name", "cosmicrays")
.put("description", "Excess solar radiation has destroyed the item")
.put("publicDescription", "The item has been destroyed");
assertCreateCancellationReason(request);
}
@Test
public void canCreateAndRetrieveCancellationRequest()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
String id = UUID.randomUUID().toString();
JsonObject request = new JsonObject()
.put("name", "slime")
.put("id", id)
.put("description", "Item slimed")
.put("requiresAdditionalInformation", true);
assertCreateCancellationReason(request);
IndividualResource reason = assertGetCancellationReason(id);
assertEquals("slime", reason.getJson().getString("name"));
}
@Test
public void canUpdateCancellationRequest()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
String id = UUID.randomUUID().toString();
JsonObject request = new JsonObject()
.put("name", "slime")
.put("id", id)
.put("description", "Item slimed");
assertCreateCancellationReason(request);
request
.put("name", "oobleck")
.put("requiresAdditionalInformation", false);
assertUpdateCancellationReason(id, request);
IndividualResource reason = assertGetCancellationReason(id);
assertEquals("oobleck", reason.getJson().getString("name"));
}
@Test
public void canRetrieveByCQL()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
JsonObject request = new JsonObject()
.put("name", "ooze")
.put("description", "Item oozed");
JsonObject request2 = new JsonObject()
.put("name", "fire")
.put("description", "Item burnt");
assertCreateCancellationReason(request);
assertCreateCancellationReason(request2);
JsonResponse response = getCancellationReasonCollection("description=burnt");
assertTrue(response.getJson().containsKey("totalRecords"));
assertTrue(response.getJson().getInteger("totalRecords").equals(1));
assertEquals("fire", response.getJson().getJsonArray("cancellationReasons")
.getJsonObject(0).getString("name"));
}
@Test
public void canDeleteCancellationRequest()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
String id = UUID.randomUUID().toString();
JsonObject request = new JsonObject()
.put("name", "meteor")
.put("id", id)
.put("description", "SMOD incoming, all requests cancelled");
assertCreateCancellationReason(request);
assertDeleteCancellationReason(id);
JsonResponse getResponse = getCancellationReason(id);
assertThat(getResponse, matchesNotFound());
}
@Test
public void cannotCreateDuplicateCancellationRequestNames()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
JsonObject request = new JsonObject()
.put("name", "chicken")
.put("description", "Giant chicken has eaten the item");
JsonObject request2 = new JsonObject()
.put("name", "chicken")
.put("description", "Chicken grease stains on item");
assertCreateCancellationReason(request);
JsonResponse response = createCancellationReason(request2);
assertThat(response, matchesUnprocessableEntity());
}
@Test
public void cannotDeleteCancellationReasonInUse()
throws MalformedURLException,
InterruptedException,
ExecutionException,
TimeoutException {
UUID requestId = UUID.randomUUID();
UUID cancellationReasonId = UUID.randomUUID();
UUID itemId = UUID.randomUUID();
UUID requesterId = UUID.randomUUID();
UUID proxyId = UUID.randomUUID();
DateTime requestDate = new DateTime(2017, 7, 22, 10, 22, 54, DateTimeZone.UTC);
DateTime requestExpirationDate = new DateTime(2017, 7, 30, 0, 0, DateTimeZone.UTC);
DateTime holdShelfExpirationDate = new DateTime(2017, 8, 31, 0, 0, DateTimeZone.UTC);
JsonObject reasonRequest = new JsonObject()
.put("name", "worms").put("description", "Item Eaten by space worms.")
.put("id", cancellationReasonId.toString());
JsonObject requestRequest = new RequestRequestBuilder()
.recall()
.toHoldShelf()
.withId(requestId)
.withRequestDate(requestDate)
.withItemId(itemId)
.withRequesterId(requesterId)
.withProxyId(proxyId)
.withRequestExpiration(requestExpirationDate)
.withHoldShelfExpiration(holdShelfExpirationDate)
.withItem("Nod", "565578437802")
.withRequester("Jones", "Stuart", "Anthony", "6837502674015")
.withProxy("Stuart", "Rebecca", "6059539205")
.withStatus(CLOSED_CANCELLED)
.withCancellationReasonId(cancellationReasonId)
.create();
assertCreateCancellationReason(reasonRequest);
CompletableFuture<JsonResponse> createRequestFuture = new CompletableFuture<>();
client.post(requestStorageUrl(), requestRequest, StorageTestSuite.TENANT_ID,
ResponseHandler.json(createRequestFuture));
JsonResponse createRequestResponse = createRequestFuture.get(5, TimeUnit.SECONDS);
assertThat(createRequestResponse, matchesCreated());
TextResponse deleteReasonResponse = deleteCancellationReason(cancellationReasonId.toString());
assertThat(deleteReasonResponse, matchesBadRequest());
}
@Test
//My canary in the coalmine :)
public void dummyTest() {
assertTrue(true);
}
}