forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaffSlipsApiTest.java
245 lines (165 loc) · 9.76 KB
/
StaffSlipsApiTest.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
package org.folio.rest.api;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.*;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.assertThat;
import java.net.HttpURLConnection;
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 java.util.concurrent.atomic.AtomicBoolean;
import org.folio.rest.support.ApiTests;
import org.folio.rest.support.JsonResponse;
import org.folio.rest.support.Response;
import org.folio.rest.support.ResponseHandler;
import org.folio.rest.support.builders.StaffSlipRequestBuilder;
import org.junit.Before;
import org.junit.Test;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
public class StaffSlipsApiTest extends ApiTests {
private static final String TEST_STAFF_SLIP_1_NAME = "Test Staff Slip 1";
private static final String TEST_STAFF_SLIP_1_DESCRIPTION = "Test Staff Slip 1 Description";
private static final String TEST_STAFF_SLIP_1_DESCRIPTION_ALTERNATE = "Test Staff Slip 1 Description Updated";
private static final String TEST_STAFF_SLIP_1_Template = "Test Staff Slip 1 Template";
private static final String ID_KEY = "id";
private static final String ACTIVE_KEY = "active";
private static final String NAME_KEY = "name";
private static final String DESCRIPTON_KEY = "description";
private static final String TEMPLATE_KEY = "template";
private static AtomicBoolean isRefTestDone = new AtomicBoolean(false);
@Before
public void beforeEach() throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
// check reference data once before deletion
synchronized (this) {
if (!isRefTestDone.get()) {
canGetStaffSlipReferenceData();
isRefTestDone.set(true);
}
}
StorageTestSuite.deleteAll(staffSlipsStorageUrl());
}
private void canGetStaffSlipReferenceData()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
client.get(staffSlipsStorageUrl(""), StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
JsonResponse getResponse = getCompleted.get(5, TimeUnit.SECONDS);
assertThat(getResponse.getStatusCode(), is(HttpURLConnection.HTTP_OK));
JsonArray slipsJsonArray = getResponse.getJson().getJsonArray("staffSlips");
Object [] names = slipsJsonArray.stream().map(o -> ((JsonObject) o).getString(NAME_KEY)).toArray();
assertThat(names, arrayContainingInAnyOrder("Hold", "Transit", "Request delivery", "Pick slip"));
}
/* Begin Tests */
@Test
public void canCreateAStaffSlip()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonResponse creationResponse = makeStaffSlip(new StaffSlipRequestBuilder().withName(TEST_STAFF_SLIP_1_NAME)
.withDescription(TEST_STAFF_SLIP_1_DESCRIPTION).withTemplate(TEST_STAFF_SLIP_1_Template).create());
assertThat(creationResponse.getStatusCode(), is(HttpURLConnection.HTTP_CREATED));
assertThat(creationResponse.getJson().getString(ID_KEY), notNullValue());
assertThat(creationResponse.getJson().getBoolean(ACTIVE_KEY), is(true));
assertThat(creationResponse.getJson().getString(NAME_KEY), is(TEST_STAFF_SLIP_1_NAME));
assertThat(creationResponse.getJson().getString(DESCRIPTON_KEY), is(TEST_STAFF_SLIP_1_DESCRIPTION));
assertThat(creationResponse.getJson().getString(TEMPLATE_KEY), is(TEST_STAFF_SLIP_1_Template));
}
@Test
public void cannotCreateAStaffSlipWithoutAName()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonResponse creationResponse = makeStaffSlip(new StaffSlipRequestBuilder().withName(null).create());
assertThat(String.format("Creating the loan should fail: %s", creationResponse.getBody()),
creationResponse, isUnprocessableEntity());
}
@Test
public void canCreateAnInactiveStaffSlip()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonResponse creationResponse = makeStaffSlip(new StaffSlipRequestBuilder().withActive(false).create());
assertThat(creationResponse.getJson().getBoolean(ACTIVE_KEY), is(false));
}
@Test
public void cannotCreateStaffSlipWithDuplicateName()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
makeStaffSlip(new StaffSlipRequestBuilder().withName(TEST_STAFF_SLIP_1_NAME).create());
JsonResponse creationAttemptResponse = makeStaffSlip(
new StaffSlipRequestBuilder().withName(TEST_STAFF_SLIP_1_NAME).create());
assertThat(creationAttemptResponse, isBadRequest());
}
@Test
public void canGetStaffSlipById()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonResponse creationResponse = makeStaffSlip(new StaffSlipRequestBuilder().create());
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
String slipId = creationResponse.getJson().getString(ID_KEY);
client.get(staffSlipsStorageUrl("/" + slipId), StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
JsonResponse getResponse = getCompleted.get(5, TimeUnit.SECONDS);
assertThat(getResponse.getStatusCode(), is(HttpURLConnection.HTTP_OK));
assertThat(getResponse.getJson().getString(ID_KEY), is(slipId));
}
@Test
public void canQueryStaffSlip()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonResponse creationResponse = makeStaffSlip(new StaffSlipRequestBuilder().create());
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
String slipId = creationResponse.getJson().getString(ID_KEY);
URL path = staffSlipsStorageUrl(String.format("?query=%s==\"%s\"", ID_KEY, slipId));
client.get(path, StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
JsonResponse getResponse = getCompleted.get(10, TimeUnit.SECONDS);
assertThat(getResponse.getStatusCode(), is(HttpURLConnection.HTTP_OK));
JsonArray slipsJsonArray = getResponse.getJson().getJsonArray("staffSlips");
assertThat(slipsJsonArray.getJsonObject(0).getString(ID_KEY), is(slipId));
}
@Test
public void canUpdateStaffSlipById()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
UUID slipId = UUID.randomUUID();
JsonResponse creationResponse = makeStaffSlip(
new StaffSlipRequestBuilder().withId(slipId).withName(TEST_STAFF_SLIP_1_NAME)
.withDescription(TEST_STAFF_SLIP_1_DESCRIPTION).withTemplate(TEST_STAFF_SLIP_1_Template).create());
assertThat(String.format("%s", creationResponse.getBody()), creationResponse.getStatusCode(),
is(HttpURLConnection.HTTP_CREATED));
CompletableFuture<Response> putCompleted = new CompletableFuture<>();
JsonObject updatedSlip = new StaffSlipRequestBuilder().withId(slipId).withName(TEST_STAFF_SLIP_1_NAME)
.withDescription(TEST_STAFF_SLIP_1_DESCRIPTION_ALTERNATE).create();
client.put(staffSlipsStorageUrl("/" + slipId), updatedSlip, StorageTestSuite.TENANT_ID,
ResponseHandler.empty(putCompleted));
Response putReponse = putCompleted.get(5, TimeUnit.SECONDS);
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
client.get(staffSlipsStorageUrl("/" + slipId), StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
JsonResponse getResponse = getCompleted.get(5, TimeUnit.SECONDS);
assertThat(putReponse.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
assertThat(getResponse.getJson().getString(DESCRIPTON_KEY), is(TEST_STAFF_SLIP_1_DESCRIPTION_ALTERNATE));
}
@Test
public void canDeleteSlipById()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
UUID slipId = UUID.randomUUID();
makeStaffSlip(new StaffSlipRequestBuilder().withId(slipId).withName(TEST_STAFF_SLIP_1_NAME)
.withDescription(TEST_STAFF_SLIP_1_DESCRIPTION).withTemplate(TEST_STAFF_SLIP_1_Template).create());
CompletableFuture<Response> deleteCompleted = new CompletableFuture<>();
client.delete(staffSlipsStorageUrl("/" + slipId), StorageTestSuite.TENANT_ID,
ResponseHandler.empty(deleteCompleted));
Response deleteReponse = deleteCompleted.get(5, TimeUnit.SECONDS);
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
client.get(staffSlipsStorageUrl("/" + slipId), StorageTestSuite.TENANT_ID, ResponseHandler.json(getCompleted));
JsonResponse getResponse = getCompleted.get(5, TimeUnit.SECONDS);
assertThat(deleteReponse.getStatusCode(), is(HttpURLConnection.HTTP_NO_CONTENT));
assertThat(getResponse.getStatusCode(), is(HttpURLConnection.HTTP_NOT_FOUND));
}
private JsonResponse makeStaffSlip(JsonObject staffSlipJson)
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonResponse> createCompleted = new CompletableFuture<>();
client.post(staffSlipsStorageUrl(), staffSlipJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(createCompleted));
return createCompleted.get(5, TimeUnit.SECONDS);
}
private URL staffSlipsStorageUrl() throws MalformedURLException {
return staffSlipsStorageUrl("");
}
private URL staffSlipsStorageUrl(String subPath) throws MalformedURLException {
return StorageTestSuite.storageUrl("/staff-slips-storage/staff-slips" + subPath);
}
}