forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckOutLockAPITest.java
131 lines (101 loc) · 5.09 KB
/
CheckOutLockAPITest.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
package org.folio.rest.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.json.JsonObject;
import lombok.SneakyThrows;
import org.apache.http.HttpStatus;
import org.folio.rest.jaxrs.model.CheckoutLockRequest;
import org.folio.rest.support.ApiTests;
import org.folio.rest.support.JsonResponse;
import org.folio.rest.support.MultipleRecords;
import org.folio.rest.support.TextResponse;
import org.folio.rest.support.http.AssertingRecordClient;
import org.folio.rest.support.http.InterfaceUrls;
import org.junit.Before;
import org.junit.Test;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class CheckOutLockAPITest extends ApiTests {
private static final String CHECK_OUT_LOCK_TABLE = "check_out_lock";
private final AssertingRecordClient checkOutLockClient =
new AssertingRecordClient(client, StorageTestSuite.TENANT_ID,
InterfaceUrls::checkOutStorageUrl, "checkoutLock");
private final ObjectMapper objectMapper = new ObjectMapper();
@Before
public void beforeEach() throws Exception {
StorageTestSuite.cleanUpTable(CHECK_OUT_LOCK_TABLE);
}
@SneakyThrows
@Test
public void canCreateCheckOutLock() {
String userId1 = UUID.randomUUID().toString();
JsonObject checkOutLock1 = toJsonObject(createCheckoutLockRequest(userId1, 1000));
checkOutLockClient.create(checkOutLock1);
JsonObject checkOutLock2 = toJsonObject(createCheckoutLockRequest(userId1, 100000));
JsonResponse response1 = checkOutLockClient.attemptCreate(checkOutLock2);
assertThat(response1.getStatusCode(), is(HttpStatus.SC_SERVICE_UNAVAILABLE));
assertThat(response1.getBody(), is("Unable to acquire lock"));
JsonObject checkOutLock3 = toJsonObject(createCheckoutLockRequest("", 1000));
JsonResponse response2 = checkOutLockClient.attemptCreate(checkOutLock3);
assertThat(response2.getStatusCode(), is(HttpStatus.SC_UNPROCESSABLE_ENTITY));
String userId2 = UUID.randomUUID().toString();
JsonObject checkOutLock4 = toJsonObject(createCheckoutLockRequest(userId2, 1000));
checkOutLockClient.create(checkOutLock4);
JsonObject checkOutLock5 = toJsonObject(createCheckoutLockRequest(userId2, 0));
checkOutLockClient.create(checkOutLock5);
}
@SneakyThrows
@Test
public void canGetCheckOutLock() {
String userId1 = UUID.randomUUID().toString();
JsonObject checkOutLock1 = toJsonObject(createCheckoutLockRequest(userId1, 1000));
JsonResponse response = checkOutLockClient.attemptCreate(checkOutLock1);
assertThat(response.getStatusCode(),is(HttpStatus.SC_CREATED));
String id = response.getJson().getString("id");
JsonResponse response1 = checkOutLockClient.attemptGetById(UUID.fromString(id));
assertThat(response1.getStatusCode(),is(HttpStatus.SC_OK));
assertThat(response1.getJson(),is(response.getJson()));
JsonResponse response2 = checkOutLockClient.attemptGetById("abcd");
assertThat(response2.getStatusCode(),is(HttpStatus.SC_BAD_REQUEST));
assertThat(response2.getBody(), is("Invalid lock id"));
}
@SneakyThrows
@Test
public void canGetCheckoutLocksByQueryParams() {
String userId1 = UUID.randomUUID().toString();
JsonObject checkOutLock1 = toJsonObject(createCheckoutLockRequest(userId1, 1000));
JsonResponse response1 = checkOutLockClient.attemptCreate(checkOutLock1);
assertThat(response1.getStatusCode(),is(HttpStatus.SC_CREATED));
String userId2 = UUID.randomUUID().toString();
JsonObject checkOutLock2 = toJsonObject(createCheckoutLockRequest(userId2, 1000));
JsonResponse response2 = checkOutLockClient.attemptCreate(checkOutLock2);
assertThat(response2.getStatusCode(),is(HttpStatus.SC_CREATED));
MultipleRecords<JsonObject> response = checkOutLockClient.getManyWithQueryParams(userId1,0,10);
assertThat(response.getTotalRecords(), is(1));
}
@SneakyThrows
@Test
public void canDeleteCheckOutLock() {
String userId1 = UUID.randomUUID().toString();
JsonObject checkOutLock1 = toJsonObject(createCheckoutLockRequest(userId1, 1000));
JsonResponse response1 = checkOutLockClient.attemptCreate(checkOutLock1);
String id = response1.getJson().getString("id");
checkOutLockClient.deleteById(UUID.fromString(id));
JsonObject checkOutLock2 = toJsonObject(createCheckoutLockRequest(userId1, 100000));
JsonResponse response2 = checkOutLockClient.attemptCreate(checkOutLock2);
assertThat(response2.getStatusCode(), is(HttpStatus.SC_CREATED));
TextResponse response3 = checkOutLockClient.attemptDeleteById("1234");
assertThat(response3.getStatusCode(), is(HttpStatus.SC_BAD_REQUEST));
assertThat(response3.getBody(), is("Invalid lock id"));
}
private CheckoutLockRequest createCheckoutLockRequest(String userId, int ttlMs) {
return new CheckoutLockRequest()
.withTtlMs(ttlMs)
.withUserId(userId);
}
private JsonObject toJsonObject(CheckoutLockRequest checkoutLockRequest)
throws JsonProcessingException {
return new JsonObject(objectMapper.writeValueAsString(checkoutLockRequest));
}
}