forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestUpdateTriggerTest.java
144 lines (108 loc) · 4.94 KB
/
RequestUpdateTriggerTest.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
package org.folio.rest.api;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.folio.rest.api.RequestsApiTest.requestStorageUrl;
import static org.folio.rest.jaxrs.model.Request.Status.fromValue;
import static org.folio.rest.support.matchers.TextDateTimeMatcher.withinSecondsAfter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.folio.rest.jaxrs.model.Request;
import org.folio.rest.persist.PostgresClient;
class RequestUpdateTriggerTest {
private static final String REQUEST_TABLE = "request";
private static PostgresClient pgClient;
@BeforeAll
static void beforeAll() throws InterruptedException, ExecutionException, TimeoutException, IOException {
StorageTestSuite.before();
pgClient = PostgresClient.getInstance(StorageTestSuite.getVertx(), StorageTestSuite.TENANT_ID);
}
@AfterAll
static void afterAll() throws InterruptedException, ExecutionException, TimeoutException {
StorageTestSuite.after();
}
@BeforeEach
void beforeEach() throws MalformedURLException {
StorageTestSuite.deleteAll(requestStorageUrl());
}
@ParameterizedTest
@CsvSource(value = {
"Open - Awaiting pickup | Closed - Pickup expired",
"Open - Awaiting pickup | Closed - Cancelled "
}, delimiter = '|')
void awaitingPickupRequestClosedDateShouldBePresentAfterStatusTransition(String oldStatus, String newStatus)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonObject> future = new CompletableFuture<>();
String id = "3a57dc83-e70d-404b-b1f1-442b88760331";
Request request = new Request()
.withStatus(fromValue(oldStatus));
DateTime requestUpdatedDate = DateTime.now();
saveRequest(id, request)
.compose(v -> updateRequest(id, request.withStatus(fromValue(newStatus))))
.compose(v -> getRequest(id))
.onComplete(updatedRequest -> future.complete(updatedRequest.result()));
JsonObject updatedRequest = future.get(5, TimeUnit.SECONDS);
assertThat(updatedRequest.getString("awaitingPickupRequestClosedDate"),
is(notNullValue()));
assertThat(updatedRequest.getString("awaitingPickupRequestClosedDate"),
is(withinSecondsAfter(Seconds.seconds(2), requestUpdatedDate)));
}
@ParameterizedTest
@CsvSource(value = {
"Open - Not yet filled | Closed - Filled ",
"Open - Not yet filled | Closed - Cancelled ",
"Open - Not yet filled | Closed - Unfilled ",
"Open - Not yet filled | Open - In transit ",
"Open - Not yet filled | Open - Awaiting pickup",
"Open - Awaiting pickup | Open - In transit ",
"Open - In transit | Closed - Cancelled ",
"Open - In transit | Open - Awaiting pickup",
"Open - In transit | Open - Not yet filled "
}, delimiter = '|')
void awaitingPickupRequestClosedDateShouldNotBePresentAfterStatusTransition(String oldStatus, String newStatus)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<JsonObject> future = new CompletableFuture<>();
String id = "3a57dc83-e70d-404b-b1f1-442b88760331";
Request request = new Request()
.withStatus(fromValue(oldStatus));
saveRequest(id, request)
.compose(v -> updateRequest(id, request.withStatus(fromValue(newStatus))))
.compose(v -> getRequest(id))
.onComplete(updatedRequest -> future.complete(updatedRequest.result()));
JsonObject updatedRequest = future.get(5, TimeUnit.SECONDS);
assertThat(updatedRequest.getString("awaitingPickupRequestClosedDate"),
is(nullValue()));
}
private Future<Void> saveRequest(String id, Request request) {
Promise<String> promise = Promise.promise();
pgClient.save(REQUEST_TABLE, id, request, promise.future());
return promise.future().map(s -> null);
}
private Future<Void> updateRequest(String id, Request request) {
Promise<RowSet<Row>> promise = Promise.promise();
pgClient.update(REQUEST_TABLE, request, id, promise);
return promise.future().map(ur -> null);
}
private Future<JsonObject> getRequest(String id) {
Promise<JsonObject> promise = Promise.promise();
pgClient.getById(REQUEST_TABLE, id, promise.future());
return promise.future();
}
}