forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintEventsAPITest.java
More file actions
209 lines (190 loc) · 10.5 KB
/
PrintEventsAPITest.java
File metadata and controls
209 lines (190 loc) · 10.5 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
package org.folio.rest.api;
import io.vertx.core.json.JsonObject;
import org.folio.rest.support.ApiTests;
import org.folio.rest.support.JsonResponse;
import org.folio.rest.support.ResponseHandler;
import org.junit.Test;
import java.net.MalformedURLException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.IntStream;
import static org.folio.rest.support.http.InterfaceUrls.printEventsUrl;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isCreated;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isInternalServerError;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isOk;
import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isUnprocessableEntity;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.junit.MatcherAssert.assertThat;
public class PrintEventsAPITest extends ApiTests {
@Test
public void canCreatePrintEventLog() throws MalformedURLException, ExecutionException, InterruptedException {
JsonObject printEventsJson = getPrintEvent();
final CompletableFuture<JsonResponse> postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postCompleted));
final JsonResponse postResponse = postCompleted.get();
assertThat(postResponse, isCreated());
}
@Test
public void createPrintEventLogWithMissingFields() throws MalformedURLException, ExecutionException, InterruptedException {
List<String> requestIds = List.of("5f5751b4-e352-4121-adca-204b0c2aec43", "5f5751b4-e352-4121-adca-204b0c2aec44");
JsonObject printEventsJson = new JsonObject()
.put("requestIds", requestIds)
.put("requesterName", "Sample Requester")
.put("printEventDate", "2024-06-25T14:30:00Z");
final CompletableFuture<JsonResponse> postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postCompleted));
final JsonResponse postResponse = postCompleted.get();
assertThat(postResponse, isUnprocessableEntity());
}
@Test
public void createPrintEventLogWithBlankFields() throws MalformedURLException, ExecutionException, InterruptedException {
JsonObject printEventsJson = getPrintEvent();
printEventsJson.put("requesterId", " ");
final CompletableFuture<JsonResponse> postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postCompleted));
final JsonResponse postResponse = postCompleted.get();
assertThat(postResponse, isUnprocessableEntity());
}
@Test
public void createPrintEventLogWhenRequestListIsEmpty() throws MalformedURLException, ExecutionException, InterruptedException {
List<String> requestIds = List.of();
JsonObject printEventsJson = getPrintEvent();
printEventsJson.put("requestIds", requestIds);
final CompletableFuture<JsonResponse> postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postCompleted));
final JsonResponse postResponse = postCompleted.get();
assertThat(postResponse, isUnprocessableEntity());
}
@Test
public void createAndGetPrintEventDetails() throws MalformedURLException, ExecutionException, InterruptedException {
List<UUID> requestIds = IntStream.range(0, 10)
.mapToObj(notUsed -> UUID.randomUUID())
.toList();
// Creating print event entry for batch of requestIds
JsonObject printEventsJson = getPrintEvent();
printEventsJson.put("requestIds", requestIds);
printEventsJson.put("requesterName", "requester1");
CompletableFuture<JsonResponse> postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postCompleted));
JsonResponse postResponse = postCompleted.get();
assertThat(postResponse, isCreated());
// Fetching the print event status details for the batch of requestIds
CompletableFuture<JsonResponse> printEventStatusResponse = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-status"), createPrintRequestIds(requestIds), StorageTestSuite.TENANT_ID,
ResponseHandler.json(printEventStatusResponse));
JsonResponse response = printEventStatusResponse.get();
assertThat(response, isOk());
var jsonObject = response.getJson();
assertThat(jsonObject.getInteger("totalRecords"), is(10));
var printEventsArray = jsonObject.getJsonArray("printEventsStatusResponses");
IntStream.range(0, printEventsArray.size())
.mapToObj(printEventsArray::getJsonObject)
.forEach(printEvent -> {
assertThat(printEvent.getInteger("count"), is(1));
assertThat(printEvent.getString("requesterName"), is("requester1"));
assertThat(printEvent.getString("requesterId"), is("5f5751b4-e352-4121-adca-204b0c2aec43"));
assertThat(printEvent.getString("printEventDate"), is("2024-07-15T14:30:00.000+00:00"));
});
// creating another print event entry for first 5 requestIds in batch
var requestId2 = UUID.randomUUID();
printEventsJson = getPrintEvent();
printEventsJson.put("requestIds", requestIds.subList(0, 5));
printEventsJson.put("requesterId", requestId2);
printEventsJson.put("requesterName", "requester2");
printEventsJson.put("printEventDate", "2024-07-15T14:32:00Z");
postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(postCompleted));
postResponse = postCompleted.get();
assertThat(postResponse, isCreated());
// Fetching the print event status details for the first 5 request Ids in batch.
// As the first 5 request ids are printed twice
// count will be 2 and the latest requester id, name and printDate will be returned
printEventStatusResponse = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-status"),
createPrintRequestIds(requestIds.subList(0, 5)), StorageTestSuite.TENANT_ID,
ResponseHandler.json(printEventStatusResponse));
response = printEventStatusResponse.get();
assertThat(response, isOk());
jsonObject = response.getJson();
assertThat(jsonObject.getInteger("totalRecords"), is(5));
printEventsArray = jsonObject.getJsonArray("printEventsStatusResponses");
IntStream.range(0, printEventsArray.size())
.mapToObj(printEventsArray::getJsonObject)
.forEach(printEvent -> {
assertThat(printEvent.getInteger("count"), is(2));
assertThat(printEvent.getString("requesterName"), is("requester2"));
assertThat(printEvent.getString("requesterId"), is(requestId2.toString()));
assertThat(printEvent.getString("printEventDate"), is("2024-07-15T14:32:00.000+00:00"));
});
// Fetching the print event status details for the last 5 request Ids from batch
printEventStatusResponse = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-status"),
createPrintRequestIds(requestIds.subList(5, requestIds.size())), StorageTestSuite.TENANT_ID,
ResponseHandler.json(printEventStatusResponse));
response = printEventStatusResponse.get();
assertThat(response, isOk());
jsonObject = response.getJson();
assertThat(jsonObject.getInteger("totalRecords"), is(5));
printEventsArray = jsonObject.getJsonArray("printEventsStatusResponses");
IntStream.range(0, printEventsArray.size())
.mapToObj(printEventsArray::getJsonObject)
.forEach(printEvent -> {
assertThat(printEvent.getInteger("count"), is(1));
assertThat(printEvent.getString("requesterName"), is("requester1"));
assertThat(printEvent.getString("requesterId"), is("5f5751b4-e352-4121-adca-204b0c2aec43"));
assertThat(printEvent.getString("printEventDate"), is("2024-07-15T14:30:00.000+00:00"));
});
}
@Test
public void getPrintEventStatusWithEmptyRequestIds() throws MalformedURLException, ExecutionException, InterruptedException {
JsonObject printEventsStatusRequestJson = createPrintRequestIds(List.of());
CompletableFuture<JsonResponse> getCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-status"), printEventsStatusRequestJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(getCompleted));
JsonResponse postResponse = getCompleted.get();
assertThat(postResponse, isUnprocessableEntity());
}
@Test
public void getPrintEventStatusWithInvalidRequestIds() throws MalformedURLException, ExecutionException, InterruptedException {
CompletableFuture<JsonResponse> printEventStatusResponse = new CompletableFuture<>();
JsonObject printEventsStatusRequestJson = createPrintRequestIds(List.of(UUID.randomUUID(), UUID.randomUUID()));
client.post(printEventsUrl("/print-events-status"), printEventsStatusRequestJson, StorageTestSuite.TENANT_ID,
ResponseHandler.json(printEventStatusResponse));
JsonResponse response = printEventStatusResponse.get();
assertThat(response, isOk());
var jsonObject = response.getJson();
assertThat(jsonObject.getInteger("totalRecords"), is(0));
assertThat(jsonObject.getJsonArray("printEventsStatusResponses").size(), is(0));
}
@Test
public void createPrintEventLogAndValidate5XX() throws MalformedURLException,
ExecutionException, InterruptedException {
JsonObject printEventsJson = getPrintEvent();
final CompletableFuture<JsonResponse> postCompleted = new CompletableFuture<>();
client.post(printEventsUrl("/print-events-entry"), printEventsJson,
"INVALID_TENANT_ID",
ResponseHandler.json(postCompleted));
final JsonResponse postResponse = postCompleted.get();
assertThat(postResponse, isInternalServerError());
}
private JsonObject getPrintEvent() {
List<String> requestIds = List.of("5f5751b4-e352-4121-adca-204b0c2aec43", "5f5751b4-e352-4121-adca-204b0c2aec44");
return new JsonObject()
.put("requestIds", requestIds)
.put("requesterId", "5f5751b4-e352-4121-adca-204b0c2aec43")
.put("requesterName", "requester")
.put("printEventDate", "2024-07-15T14:30:00Z");
}
private JsonObject createPrintRequestIds(List<UUID> requestIds) {
return new JsonObject()
.put("requestIds", requestIds);
}
}