Search in sources :

Example 1 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class FakeStorageModule method batchUpdate.

private void batchUpdate(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject body = routingContext.getBodyAsJson();
    if (batchUpdatePreProcessor != null) {
        body = batchUpdatePreProcessor.apply(body);
    }
    JsonArray entities = body.getJsonArray(collectionPropertyName);
    Result<Void> lastResult = Result.succeeded(null);
    for (int entityIndex = 0; entityIndex < entities.size(); entityIndex++) {
        JsonObject entity = entities.getJsonObject(entityIndex);
        String id = entity.getString("id");
        lastResult = lastResult.next(notUsed -> replaceSingleItem(context, id, entity));
    }
    if (lastResult.failed()) {
        lastResult.cause().writeTo(routingContext.response());
    } else {
        routingContext.response().setStatusCode(201).end();
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Router(io.vertx.ext.web.Router) DateFormatUtil.formatDateTime(org.folio.circulation.support.utils.DateFormatUtil.formatDateTime) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) JsonObject(io.vertx.core.json.JsonObject) MATCH_ALL_RECORDS(api.support.fakes.CqlPredicate.MATCH_ALL_RECORDS) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) EqualsAndHashCode(lombok.EqualsAndHashCode) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) NoContentResponse.noContent(org.folio.circulation.support.http.server.NoContentResponse.noContent) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Buffer(io.vertx.core.buffer.Buffer) ClockUtil(org.folio.circulation.support.utils.ClockUtil) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) Json(io.vertx.core.json.Json) Getter(lombok.Getter) WebContext(org.folio.circulation.support.http.server.WebContext) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) APITestContext(api.support.APITestContext) ClientErrorResponse(org.folio.circulation.support.http.server.ClientErrorResponse) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) JsonSchemaValidator(org.folio.circulation.infrastructure.serialization.JsonSchemaValidator) JsonArray(io.vertx.core.json.JsonArray) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) JsonHttpResponse.created(org.folio.circulation.support.http.server.JsonHttpResponse.created) HttpMethod(io.vertx.core.http.HttpMethod) HttpStatus(org.folio.HttpStatus) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) WebContext(org.folio.circulation.support.http.server.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class LogCheckInServiceTest method logCheckInOperationPropagatesException.

@Test
void logCheckInOperationPropagatesException() {
    final CheckInContext context = checkInProcessRecords();
    final ServerErrorFailure postError = new ServerErrorFailure("ServerError");
    when(checkInStorageClient.post(any(JsonObject.class))).thenReturn(CompletableFuture.completedFuture(Result.failed(postError)));
    final CompletableFuture<Result<CheckInContext>> logCheckInOperation = logCheckInService.logCheckInOperation(context);
    final Result<CheckInContext> logResult = logCheckInOperation.getNow(Result.failed(new ServerErrorFailure("Uncompleted")));
    assertThat(logResult.failed(), is(true));
    assertThat(logResult.cause(), is(postError));
}
Also used : ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) CheckInContext(org.folio.circulation.domain.CheckInContext) JsonObject(io.vertx.core.json.JsonObject) Result(org.folio.circulation.support.results.Result) Test(org.junit.jupiter.api.Test)

Example 3 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class ResponseInterpretationTests method shouldCaptureErrorWhenMappingFailsAtRuntime.

@Test
void shouldCaptureErrorWhenMappingFailsAtRuntime() {
    final JsonObject body = new JsonObject().put("foo", "hello").put("bar", "world");
    Result<JsonObject> result = new ResponseInterpreter<JsonObject>().flatMapOn(200, response -> {
        throw new RuntimeException("Not good");
    }).apply(new Response(200, body.toString(), ""));
    assertThat(result.succeeded(), is(false));
    assertThat(result.cause(), instanceOf(ServerErrorFailure.class));
    final ServerErrorFailure cause = (ServerErrorFailure) result.cause();
    assertThat(cause.getReason(), containsString("Not good"));
    assertThat(cause.getReason(), containsString("RuntimeException"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MultiMap.caseInsensitiveMultiMap(io.vertx.core.MultiMap.caseInsensitiveMultiMap) Result.of(org.folio.circulation.support.results.Result.of) TEXT_PLAIN(org.folio.circulation.support.http.ContentType.TEXT_PLAIN) Result(org.folio.circulation.support.results.Result) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Test(org.junit.jupiter.api.Test) APPLICATION_JSON(org.folio.circulation.support.http.ContentType.APPLICATION_JSON) Function.identity(java.util.function.Function.identity) JsonObject(io.vertx.core.json.JsonObject) ResponseMapping.mapUsingJson(org.folio.circulation.support.http.ResponseMapping.mapUsingJson) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 4 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class VertxWebClientOkapiHttpClientTests method canPutWithJson.

@Test
public void canPutWithJson() throws InterruptedException, ExecutionException, TimeoutException {
    fakeWebServer.stubFor(matchingFolioHeaders(put(urlPathEqualTo("/record/12345"))).withHeader("Content-Type", equalTo("application/json")).withRequestBody(equalToJson(dummyJsonRequestBody().encodePrettily())).willReturn(noContent()));
    OkapiHttpClient client = createClient();
    CompletableFuture<Result<Response>> postCompleted = client.put(fakeWebServer.url("/record/12345"), dummyJsonRequestBody());
    final Response response = postCompleted.get(2, SECONDS).value();
    assertThat(response, hasStatus(HTTP_NO_CONTENT));
    assertThat(response.getBody(), is(emptyOrNullString()));
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Result(org.folio.circulation.support.results.Result) Test(org.junit.Test)

Example 5 with Result

use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.

the class VertxWebClientOkapiHttpClientTests method canPostWithJson.

@Test
public void canPostWithJson() throws InterruptedException, ExecutionException, TimeoutException {
    final String locationResponseHeader = "/a-different-location";
    fakeWebServer.stubFor(matchingFolioHeaders(post(urlPathEqualTo("/record"))).withHeader("Content-Type", equalTo("application/json")).withRequestBody(equalToJson(dummyJsonRequestBody().encodePrettily())).willReturn(created().withBody(dummyJsonResponseBody()).withHeader("Content-Type", "application/json").withHeader("Location", locationResponseHeader)));
    OkapiHttpClient client = createClient();
    CompletableFuture<Result<Response>> postCompleted = client.post(fakeWebServer.url("/record"), dummyJsonRequestBody());
    final Response response = postCompleted.get(2, SECONDS).value();
    assertThat(response, hasStatus(HTTP_CREATED));
    assertThat(response.getJson().getString("message"), is("hello"));
    assertThat(response.getContentType(), is("application/json"));
    assertThat(response.getHeader("location"), is(locationResponseHeader));
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Result(org.folio.circulation.support.results.Result) Test(org.junit.Test)

Aggregations

Result (org.folio.circulation.support.results.Result)65 CompletableFuture (java.util.concurrent.CompletableFuture)42 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)35 Clients (org.folio.circulation.support.Clients)31 Loan (org.folio.circulation.domain.Loan)30 JsonObject (io.vertx.core.json.JsonObject)29 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)29 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)28 RoutingContext (io.vertx.ext.web.RoutingContext)26 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)24 WebContext (org.folio.circulation.support.http.server.WebContext)24 LogManager (org.apache.logging.log4j.LogManager)23 Logger (org.apache.logging.log4j.Logger)23 HttpClient (io.vertx.core.http.HttpClient)22 Router (io.vertx.ext.web.Router)22 List (java.util.List)21 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)21 EventPublisher (org.folio.circulation.services.EventPublisher)21 MethodHandles (java.lang.invoke.MethodHandles)20 Result.ofAsync (org.folio.circulation.support.results.Result.ofAsync)19