Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-storage into CIRCSTORE-377
  • Loading branch information
OleksandrVidinieiev committed Jun 23, 2023
2 parents 7af8848 + 65c26b6 commit d3d46c1
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 22 deletions.
2 changes: 1 addition & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
},
{
"id" : "cancellation-reason-storage",
"version": "1.1",
"version": "1.2",
"handlers": [
{
"methods": ["GET"],
Expand Down
4 changes: 4 additions & 0 deletions ramls/cancellation-reason.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"requiresAdditionalInformation": {
"type": "boolean"
},
"source": {
"description": "Origin of the cancellation reason record, e.g. 'System', 'User', 'Consortium' etc.",
"type": "string"
},
"metadata": {
"$ref": "raml-util/schemas/metadata.schema",
"readonly": true
Expand Down
2 changes: 1 addition & 1 deletion ramls/cancellation-reason.raml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#%RAML 1.0
title: Cancellation Reasons
version: v1.1
version: v1.2
protocols: [ HTTP, HTTPS ]
baseUri: http://localhost:9130

Expand Down
3 changes: 2 additions & 1 deletion ramls/examples/cancellation-reason.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"name": "Not available",
"description": "The item is no longer available",
"publicDescription": "The item status is unknown",
"requiresAdditionalInformation": false
"requiresAdditionalInformation": false,
"source": "System"
}

7 changes: 4 additions & 3 deletions ramls/examples/cancellation-reasons.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"cancellationReasons" : [
"cancellationReasons": [
{
"id": "5aace7aa-a741-4d69-9741-46737da78f7f",
"name": "Not available",
"description": "The item is no longer available",
"publicDescription": "The item status is unknown",
"requiresAdditionalInformation": false
}
"requiresAdditionalInformation": false,
"source": "System"
}
],
"totalRecords": 1
}
25 changes: 19 additions & 6 deletions src/main/java/org/folio/rest/client/ConfigurationClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.folio.rest.client;

import static io.vertx.core.Future.failedFuture;
import static io.vertx.core.Future.succeededFuture;
import static io.vertx.core.http.HttpMethod.GET;
import static java.lang.String.format;

import java.lang.invoke.MethodHandles;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -25,22 +27,32 @@ public class ConfigurationClient extends OkapiClient {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private static final String CONFIGURATIONS_URL = "/configurations/entries?query=%s";
private static final String TLR_SETTINGS_QUERY = "module==\"SETTINGS\" and configName==\"TLR\"";
private static final String URL = format(CONFIGURATIONS_URL, StringUtil.urlEncode(TLR_SETTINGS_QUERY));

public ConfigurationClient(Vertx vertx, Map<String, String> okapiHeaders) {
super(vertx, okapiHeaders);
}

public Future<TlrSettingsConfiguration> getTlrSettingsOrDefault() {
return getTlrSettings(
() -> succeededFuture(new TlrSettingsConfiguration(false, false, null, null, null)));
}

public Future<TlrSettingsConfiguration> getTlrSettings() {
String url = format(CONFIGURATIONS_URL, StringUtil.urlEncode(TLR_SETTINGS_QUERY));
return getTlrSettings(() -> failedFuture("Failed to find TLR configuration"));
}

private Future<TlrSettingsConfiguration> getTlrSettings(
Supplier<Future<TlrSettingsConfiguration>> otherwise) {

return okapiGet(url)
return okapiGet(URL)
.compose(response -> {
int responseStatus = response.statusCode();
if (responseStatus != 200) {
String errorMessage = format("Failed to find TLR configuration. Response: %d %s",
responseStatus, response.bodyAsString());
log.error(errorMessage);
return failedFuture(new HttpException(GET, url, response));
return failedFuture(new HttpException(GET, URL, response));
} else {
try {
return objectMapper.readValue(response.bodyAsString(), KvConfigurations.class)
Expand All @@ -50,12 +62,13 @@ public Future<TlrSettingsConfiguration> getTlrSettings() {
.map(JsonObject::new)
.map(TlrSettingsConfiguration::from)
.map(Future::succeededFuture)
.orElseGet(() -> failedFuture("Failed to find TLR configuration"));
.orElseGet(otherwise);
} catch (JsonProcessingException e) {
log.error("Failed to parse response: {}", response.bodyAsString());
return failedFuture(e);
}
}
});
}
);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/folio/rest/impl/RequestExpiryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void expireRequests(Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> handler, Context context) {

Vertx vertx = context.owner();
new ConfigurationClient(vertx, okapiHeaders).getTlrSettings()
new ConfigurationClient(vertx, okapiHeaders).getTlrSettingsOrDefault()
.compose(tlrSettings -> createRequestExpirationService(okapiHeaders, vertx, tlrSettings)
.doRequestExpiration())
.onSuccess(x -> handler.handle(succeededFuture(respond204())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ public boolean shouldMigrate(String moduleVersion) {
}

public Future<Integer> getBatchCount() {
return postgresClient.select(format("SELECT COUNT(*) FROM %s.%s", schemaName, tableName))
return selectRead(format("SELECT COUNT(*) FROM %s.%s", schemaName, tableName))
.compose(this::getBatchCount);
}

private Future<RowSet<Row>> selectRead(String sql) {
return Future.future(promise -> postgresClient.selectRead(sql, 0, promise));
}

public Future<Void> migrateRequests(int batchCount) {
return postgresClient.withTrans(conn ->
chainFutures(buildBatches(batchCount, conn), this::processBatch)
Expand Down Expand Up @@ -161,7 +165,7 @@ public void buildNewRequests(Batch<T> batch) {
public Future<Batch<T>> fetchRequests(Batch<T> batch) {
log.debug("fetchRequests:: {}, batch: {}", migrationName, batch);

return postgresClient.select(format("SELECT jsonb FROM %s.%s ORDER BY id LIMIT %d OFFSET %d",
return selectRead(format("SELECT jsonb FROM %s.%s ORDER BY id LIMIT %d OFFSET %d",
schemaName, tableName, BATCH_SIZE, batch.getBatchNumber() * BATCH_SIZE))
.onSuccess(r -> log.info("fetchRequests:: {}, {} {} requests fetched", migrationName, batch,
r.size()))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/folio/support/PgClientFutureAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PgClientFutureAdapter(PostgresClient client) {

public Future<RowSet<Row>> select(String sql) {
final Promise<RowSet<Row>> promise = Promise.promise();
client.select(sql, promise);
client.selectRead(sql, 0, promise);
return promise.future();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public void canCreateCancellationReason()
JsonObject request = new JsonObject()
.put("name", "cosmicrays")
.put("description", "Excess solar radiation has destroyed the item")
.put("publicDescription", "The item has been destroyed");
.put("publicDescription", "The item has been destroyed")
.put("source", "System");
assertCreateCancellationReason(request);
}

Expand All @@ -198,10 +199,12 @@ public void canCreateAndRetrieveCancellationRequest()
.put("name", "slime")
.put("id", id)
.put("description", "Item slimed")
.put("requiresAdditionalInformation", true);
.put("requiresAdditionalInformation", true)
.put("source", "System");
assertCreateCancellationReason(request);
IndividualResource reason = assertGetCancellationReason(id);
assertEquals("slime", reason.getJson().getString("name"));
assertEquals("System", reason.getJson().getString("source"));
}

@Test
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/folio/rest/api/RequestExpirationApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,38 @@ public void canExpireASingleOpenUnfilledRequest() throws InterruptedException,
assertThat(response.containsKey("position"), is(false));
}

@Test
public void canExpireRequestWhenTlrSettingsNotSet() throws InterruptedException,
MalformedURLException, TimeoutException, ExecutionException {
stubWithEmptyTlrSettings();
UUID id = UUID.randomUUID();
UUID itemId = UUID.randomUUID();

createEntity(
new RequestRequestBuilder()
.hold()
.withId(id)
.withRequestExpirationDate(new DateTime(2017, 7, 30, 10, 22, 54, DateTimeZone.UTC))
.withItemId(itemId)
.withPosition(1)
.withStatus(OPEN_NOT_YET_FILLED)
.create(),
requestStorageUrl());

expireRequests();

List<JsonObject> events = Awaitility.await()
.atMost(10, TimeUnit.SECONDS)
.until(MockServer::getPublishedEvents, hasSize(1));

assertPublishedEvents(events);

JsonObject response = getById(requestStorageUrl(String.format("/%s", id)));

assertThat(response.getString("status"), is(CLOSED_UNFILLED));
assertThat(response.containsKey("position"), is(false));
}

@Test
public void canExpireASingleOpenAwaitingPickupRequest() throws InterruptedException,
MalformedURLException, TimeoutException, ExecutionException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/folio/rest/api/StorageTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private static RowSet<Row> getRecordsWithUnmatchedIds(String tenantId, String ta
"SELECT null FROM %s_%s.%s" + " WHERE CAST(id AS VARCHAR(50)) != jsonb->>'id'",
tenantId, "mod_circulation_storage", tableName);

postgresClient.select(sql, result -> {
postgresClient.selectRead(sql, 0, result -> {
if (result.succeeded()) {
selectCompleted.complete(result.result());
} else {
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/org/folio/rest/api/TenantRefApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,28 +427,32 @@ private static Future<TenantJob> postTenant(TestContext context, String fromVers
}

private static void assertThatNoRequestsWereUpdatedByTlrMigration(TestContext context) {
postgresClient.select("SELECT COUNT(*) " +
selectRead("SELECT COUNT(*) " +
"FROM " + REQUEST_TABLE + " " +
"WHERE jsonb->>'requestLevel' IS NOT null")
.onFailure(context::fail)
.onSuccess(rowSet -> context.assertEquals(0, getCount(rowSet)));
}

private static void assertThatNoRequestsWereUpdatedByRequestSearchMigration(TestContext context) {
postgresClient.select("SELECT COUNT(*) " +
selectRead("SELECT COUNT(*) " +
"FROM " + REQUEST_TABLE + " " +
"WHERE jsonb->>'searchIndex' IS NOT null")
.onFailure(context::fail)
.onSuccess(rowSet -> context.assertEquals(0, getCount(rowSet)));
}

private static Future<List<JsonObject>> getAllRequestsAsJson() {
return postgresClient.select("SELECT * FROM " + REQUEST_TABLE)
return selectRead("SELECT * FROM " + REQUEST_TABLE)
.map(rowSet -> StreamSupport.stream(rowSet.spliterator(), false)
.map(row -> row.getJsonObject("jsonb"))
.collect(toList()));
}

private static Future<RowSet<Row>> selectRead(String sql) {
return Future.future(promise -> postgresClient.selectRead(sql, 0, promise));
}

private static Future<JsonObject> getRequestAsJson(String requestId) {
return postgresClient.getById(REQUEST_TABLE_NAME, requestId);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/folio/rest/support/ApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -165,6 +166,15 @@ protected void stubWithInvalidTlrSettings() {
.willReturn(ok().withBody("Invalid configurations response")));
}

protected void stubWithEmptyTlrSettings() {
StorageTestSuite.getWireMockServer().stubFor(WireMock.get(urlPathMatching(
CONFIGURATIONS_ENTRIES_URL_PATTERN))
.willReturn(ok().withBody(mapFrom(
new KvConfigurations()
.withConfigs(Collections.<Config>emptyList()))
.encodePrettily())));
}

public static <T> T waitFor(Future<T> future) {
return waitFor(future, 10);
}
Expand Down

0 comments on commit d3d46c1

Please sign in to comment.