Search in sources :

Example 1 with InvoiceDocument

use of org.folio.rest.jaxrs.model.InvoiceDocument in project mod-invoice by folio-org.

the class InvoicesImpl method processDocumentCreation.

private void processDocumentCreation(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
    DocumentHelper documentHelper = new DocumentHelper(okapiHeaders, vertxContext, lang);
    InvoiceDocument entity = new JsonObject(new String(requestBytesArray, StandardCharsets.UTF_8)).mapTo(InvoiceDocument.class);
    if (!entity.getDocumentMetadata().getInvoiceId().equals(id)) {
        asyncResultHandler.handle(succeededFuture(documentHelper.buildErrorResponse(new HttpException(422, MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY))));
    } else {
        documentHelper.createDocument(id, entity).thenAccept(document -> {
            logInfo("Successfully created document with id={}", document);
            asyncResultHandler.handle(succeededFuture(documentHelper.buildResponseWithLocation(String.format(DOCUMENTS_LOCATION_PREFIX, id, document.getDocumentMetadata().getId()), document)));
        }).exceptionally(t -> handleErrorResponse(asyncResultHandler, documentHelper, t));
    }
}
Also used : Future.succeededFuture(io.vertx.core.Future.succeededFuture) BufferedInputStream(java.io.BufferedInputStream) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Context(io.vertx.core.Context) StringUtils(org.apache.commons.lang3.StringUtils) HttpException(org.folio.invoices.rest.exceptions.HttpException) STREAM_ABORT(org.folio.rest.RestVerticle.STREAM_ABORT) RequestContext(org.folio.rest.core.models.RequestContext) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) STREAM_COMPLETE(org.folio.rest.RestVerticle.STREAM_COMPLETE) Errors(org.folio.rest.jaxrs.model.Errors) DOCUMENT_IS_TOO_LARGE(org.folio.invoices.utils.ErrorCodes.DOCUMENT_IS_TOO_LARGE) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument) IOException(java.io.IOException) Validate(org.folio.rest.annotations.Validate) StandardCharsets(java.nio.charset.StandardCharsets) ONE_MB(org.apache.commons.io.FileUtils.ONE_MB) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Stream(org.folio.rest.annotations.Stream) MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY(org.folio.invoices.utils.ErrorCodes.MISMATCH_BETWEEN_ID_IN_PATH_AND_BODY) Logger(org.apache.logging.log4j.Logger) Response(javax.ws.rs.core.Response) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) Invoice(org.folio.rest.jaxrs.model.Invoice) InputStream(java.io.InputStream) JsonObject(io.vertx.core.json.JsonObject) HttpException(org.folio.invoices.rest.exceptions.HttpException) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument)

Example 2 with InvoiceDocument

use of org.folio.rest.jaxrs.model.InvoiceDocument in project mod-invoice by folio-org.

the class DocumentsApiTest method testGetDocument.

@Test
public void testGetDocument() {
    logger.info("=== Test get document by id ===");
    String endpoint = String.format(DOCUMENT_ENDPOINT_WITH_ID, DOCUMENT_ID);
    final InvoiceDocument retrievedDocument = verifySuccessGet(endpoint, InvoiceDocument.class);
    assertThat(MockServer.serverRqRs.get(INVOICE_DOCUMENTS, HttpMethod.GET), hasSize(1));
    assertEquals(DOCUMENT_ID, retrievedDocument.getDocumentMetadata().getId());
}
Also used : InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument) Test(org.junit.jupiter.api.Test)

Example 3 with InvoiceDocument

use of org.folio.rest.jaxrs.model.InvoiceDocument in project mod-invoice by folio-org.

the class DocumentsApiTest method testPostAndDocumentOversize.

@Test
void testPostAndDocumentOversize(VertxTestContext context) throws Throwable {
    logger.info("=== Test create document - oversize ===");
    InvoiceDocument document = new JsonObject(getMockData(INVOICE_DOCUMENT_SAMPLE_PATH)).mapTo(InvoiceDocument.class);
    String stringData = StringUtils.repeat("*", size);
    document.getContents().setData(stringData);
    HttpRequest<Buffer> request = WebClient.wrap(Vertx.vertx().createHttpClient()).requestAbs(HttpMethod.POST, RestAssured.baseURI + DOCUMENT_ENDPOINT);
    prepareRequestHeaders(request);
    context.assertComplete(request.sendBuffer(Buffer.buffer(JsonObject.mapFrom(document).encode()))).onComplete(event -> {
        if (event.succeeded()) {
            verifyErrorResponse(context, event.result());
        } else {
            processException(context, event.cause());
        }
    });
    assertTrue(context.awaitCompletion(60, TimeUnit.SECONDS));
    if (context.failed()) {
        fail(context.causeOfFailure());
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) JsonObject(io.vertx.core.json.JsonObject) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument) Test(org.junit.jupiter.api.Test)

Example 4 with InvoiceDocument

use of org.folio.rest.jaxrs.model.InvoiceDocument in project mod-invoice by folio-org.

the class MockServer method handleGetInvoiceDocumentById.

private void handleGetInvoiceDocumentById(RoutingContext ctx) {
    logger.info("handleGetInvoiceDocumentById got: GET " + ctx.request().path());
    String documentId = ctx.request().getParam("documentId");
    logger.info("documentId: " + documentId);
    if (ID_FOR_INTERNAL_SERVER_ERROR.equals(documentId)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        JsonObject document = getMockEntry(INVOICE_DOCUMENTS, documentId).orElseGet(getJsonObjectFromFile(INVOICE_DOCUMENTS_SAMPLE_PATH, documentId));
        if (document == null) {
            ctx.response().setStatusCode(404).end(documentId);
        } else {
            // validate content against schema
            InvoiceDocument documentSchema = document.mapTo(InvoiceDocument.class);
            documentSchema.getDocumentMetadata().setId(documentId);
            document = JsonObject.mapFrom(documentSchema);
            addServerRqRsData(HttpMethod.GET, INVOICE_DOCUMENTS, document);
            serverResponse(ctx, 200, APPLICATION_JSON, document.encodePrettily());
        }
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument)

Example 5 with InvoiceDocument

use of org.folio.rest.jaxrs.model.InvoiceDocument in project mod-invoice by folio-org.

the class MockServer method handlePostInvoiceDocument.

private void handlePostInvoiceDocument(RoutingContext ctx) {
    InvoiceDocument invoiceDocument = ctx.getBodyAsJson().mapTo(InvoiceDocument.class);
    String id = UUID.randomUUID().toString();
    invoiceDocument.getDocumentMetadata().setId(id);
    JsonObject jsonDocument = JsonObject.mapFrom(invoiceDocument);
    addServerRqRsData(HttpMethod.POST, INVOICE_DOCUMENTS, jsonDocument);
    ctx.response().putHeader(HttpHeaders.LOCATION, ctx.request().path() + "/" + id);
    serverResponse(ctx, 201, APPLICATION_JSON, jsonDocument.encodePrettily());
}
Also used : JsonObject(io.vertx.core.json.JsonObject) InvoiceDocument(org.folio.rest.jaxrs.model.InvoiceDocument)

Aggregations

InvoiceDocument (org.folio.rest.jaxrs.model.InvoiceDocument)5 JsonObject (io.vertx.core.json.JsonObject)4 Test (org.junit.jupiter.api.Test)2 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future.succeededFuture (io.vertx.core.Future.succeededFuture)1 Handler (io.vertx.core.Handler)1 Buffer (io.vertx.core.buffer.Buffer)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Response (javax.ws.rs.core.Response)1 ONE_MB (org.apache.commons.io.FileUtils.ONE_MB)1 IOUtils (org.apache.commons.io.IOUtils)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1