Search in sources :

Example 1 with LISTED_PRINT_MONOGRAPH_PATH

use of org.folio.rest.impl.PurchaseOrdersApiTest.LISTED_PRINT_MONOGRAPH_PATH in project mod-orders by folio-org.

the class MockServer method handleGetPoLines.

private void handleGetPoLines(RoutingContext ctx, String type) {
    logger.info("handleGetPoLines got: {}?{}", ctx.request().path(), ctx.request().query());
    String queryParam = StringUtils.trimToEmpty(ctx.request().getParam("query"));
    addServerRqQuery(type, queryParam);
    if (queryParam.contains(BAD_QUERY)) {
        serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
    } else if (queryParam.contains(ID_FOR_INTERNAL_SERVER_ERROR) || queryParam.contains(PO_ID_GET_LINES_INTERNAL_SERVER_ERROR)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        String poId = EMPTY;
        String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
        List<String> polIds = Collections.emptyList();
        if (queryParam.contains(PURCHASE_ORDER_ID)) {
            Matcher matcher = Pattern.compile(".*" + PURCHASE_ORDER_ID + "==(\\S[^)]+).*").matcher(queryParam);
            poId = matcher.find() ? matcher.group(1) : EMPTY;
        } else if (queryParam.startsWith("id==")) {
            polIds = extractIdsFromQuery(queryParam);
        }
        List<JsonObject> postedPoLines = getRqRsEntries(HttpMethod.SEARCH, type);
        try {
            PoLineCollection poLineCollection = new PoLineCollection();
            if (postedPoLines.isEmpty()) {
                if (poId.equals(ORDER_ID_WITH_PO_LINES) || !polIds.isEmpty()) {
                    poLineCollection = new JsonObject(getMockData(POLINES_COLLECTION)).mapTo(PoLineCollection.class);
                    // Filter PO Lines either by PO id or by expected line ids
                    Iterator<PoLine> iterator = poLineCollection.getPoLines().iterator();
                    while (iterator.hasNext()) {
                        PoLine poLine = iterator.next();
                        if (polIds.isEmpty() ? !poId.equals(poLine.getPurchaseOrderId()) : !polIds.contains(poLine.getId())) {
                            iterator.remove();
                        }
                    }
                    poLineCollection.setTotalRecords(poLineCollection.getPoLines().size());
                } else {
                    String filePath;
                    if (ID_FOR_PRINT_MONOGRAPH_ORDER.equals(poId)) {
                        filePath = LISTED_PRINT_MONOGRAPH_PATH;
                    } else {
                        filePath = String.format("%s%s.json", COMP_ORDER_MOCK_DATA_PATH, poId);
                    }
                    JsonObject compPO = new JsonObject(getMockData(filePath));
                    // Build PoLineCollection to make sure content is valid
                    poLineCollection = buildPoLineCollection(tenant, compPO.getJsonArray(COMPOSITE_PO_LINES), poId);
                }
            } else {
                // Attempt to find POLine in mock server memory
                poLineCollection.getPoLines().addAll(postedPoLines.stream().map(jsonObj -> jsonObj.mapTo(PoLine.class)).collect(Collectors.toList()));
            }
            poLineCollection.setTotalRecords(poLineCollection.getPoLines().size());
            // Update calculated data
            updatePoLineCalculatedData(poLineCollection);
            JsonObject po_lines = JsonObject.mapFrom(poLineCollection);
            logger.info(po_lines.encodePrettily());
            addServerRqRsData(HttpMethod.GET, type, po_lines);
            serverResponse(ctx, 200, APPLICATION_JSON, po_lines.encode());
        } catch (NoSuchFileException e) {
            PoLineCollection poLineCollection = new PoLineCollection();
            // Attempt to find POLine in mock server memory
            if (postedPoLines != null) {
                String finalPoId = poId;
                poLineCollection.getPoLines().addAll(postedPoLines.stream().map(json -> json.mapTo(PoLine.class)).filter(line -> finalPoId.equals(line.getPurchaseOrderId())).collect(Collectors.toList()));
            }
            poLineCollection.setTotalRecords(poLineCollection.getPoLines().size());
            JsonObject entries = JsonObject.mapFrom(poLineCollection);
            addServerRqRsData(HttpMethod.GET, type, entries);
            serverResponse(ctx, 200, APPLICATION_JSON, entries.encodePrettily());
        } catch (IOException e) {
            PoLineCollection poLineCollection = new PoLineCollection();
            poLineCollection.setTotalRecords(0);
            JsonObject entries = JsonObject.mapFrom(poLineCollection);
            addServerRqRsData(HttpMethod.GET, type, entries);
            serverResponse(ctx, 200, APPLICATION_JSON, JsonObject.mapFrom(poLineCollection).encodePrettily());
        }
    }
}
Also used : Arrays(java.util.Arrays) TestConstants(org.folio.TestConstants) LISTED_PRINT_MONOGRAPH_PATH(org.folio.rest.impl.PurchaseOrdersApiTest.LISTED_PRINT_MONOGRAPH_PATH) HttpServer(io.vertx.core.http.HttpServer) Header(io.restassured.http.Header) HashBasedTable(com.google.common.collect.HashBasedTable) FUND_ID(org.folio.orders.utils.HelperUtils.FUND_ID) StringUtils(org.apache.commons.lang3.StringUtils) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) REQUESTS(org.folio.service.inventory.InventoryManager.REQUESTS) Matcher(java.util.regex.Matcher) HelperUtils.convertIdsToCqlQuery(org.folio.orders.utils.HelperUtils.convertIdsToCqlQuery) JsonObject(io.vertx.core.json.JsonObject) PURCHASE_ORDER_ID(org.folio.rest.impl.PurchaseOrdersApiTest.PURCHASE_ORDER_ID) Budget(org.folio.rest.acq.model.finance.Budget) REASON_FOR_CLOSURE(org.folio.rest.impl.crud.CrudTestEntities.REASON_FOR_CLOSURE) Ledger(org.folio.rest.acq.model.finance.Ledger) Fund(org.folio.rest.acq.model.finance.Fund) Title(org.folio.rest.acq.model.Title) ORGANIZATION_NOT_VENDOR(org.folio.rest.impl.PurchaseOrdersApiTest.ORGANIZATION_NOT_VENDOR) TestUtils.encodePrettily(org.folio.TestUtils.encodePrettily) INACTIVE_VENDOR_ID(org.folio.rest.impl.PurchaseOrdersApiTest.INACTIVE_VENDOR_ID) Logger(org.apache.logging.log4j.Logger) Assertions.fail(org.assertj.core.api.Assertions.fail) BUDGET_IS_INACTIVE(org.folio.rest.core.exceptions.ErrorCodes.BUDGET_IS_INACTIVE) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) VENDOR_WITH_BAD_CONTENT(org.folio.rest.impl.PurchaseOrdersApiTest.VENDOR_WITH_BAD_CONTENT) ACQUISITIONS_UNIT_ID(org.folio.service.ProtectionService.ACQUISITIONS_UNIT_ID) Transaction(org.folio.rest.acq.model.finance.Transaction) Supplier(java.util.function.Supplier) BudgetCollection(org.folio.rest.acq.model.finance.BudgetCollection) ArrayList(java.util.ArrayList) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) NONEXISTING_PO_NUMBER(org.folio.rest.impl.PoNumberApiTest.NONEXISTING_PO_NUMBER) Lists(com.google.common.collect.Lists) ExchangeRate(org.folio.rest.acq.model.finance.ExchangeRate) ITEMS_NOT_FOUND(org.folio.rest.impl.PurchaseOrdersApiTest.ITEMS_NOT_FOUND) SequenceNumbers(org.folio.rest.acq.model.SequenceNumbers) ExpenseClassCollection(org.folio.rest.acq.model.finance.ExpenseClassCollection) ONLY_NEW_HOLDING_EXIST_ID(org.folio.service.inventory.InventoryManagerTest.ONLY_NEW_HOLDING_EXIST_ID) INTERNAL_SERVER_ERROR(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR) EXISTING_PO_NUMBER(org.folio.rest.impl.PoNumberApiTest.EXISTING_PO_NUMBER) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) BUDGET_NOT_FOUND_FOR_TRANSACTION(org.folio.rest.core.exceptions.ErrorCodes.BUDGET_NOT_FOUND_FOR_TRANSACTION) COMPOSITE_PO_LINES(org.folio.orders.utils.HelperUtils.COMPOSITE_PO_LINES) ExecutionException(java.util.concurrent.ExecutionException) JsonArray(io.vertx.core.json.JsonArray) DAYS(java.time.temporal.ChronoUnit.DAYS) IsbnUtil(org.folio.isbn.IsbnUtil) Piece(org.folio.rest.acq.model.Piece) BudgetExpenseClassCollection(org.folio.rest.acq.model.finance.BudgetExpenseClassCollection) TestUtils.getMinimalContentCompositePoLine(org.folio.TestUtils.getMinimalContentCompositePoLine) TestUtils.getMockAsJson(org.folio.TestUtils.getMockAsJson) AbstractHelper(org.folio.helper.AbstractHelper) NoSuchFileException(java.nio.file.NoSuchFileException) FUND_CANNOT_BE_PAID(org.folio.rest.core.exceptions.ErrorCodes.FUND_CANNOT_BE_PAID) Date(java.util.Date) DEFAULT_POLINE_LIMIT(org.folio.orders.utils.HelperUtils.DEFAULT_POLINE_LIMIT) Router(io.vertx.ext.web.Router) TimeoutException(java.util.concurrent.TimeoutException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) SequenceNumber(org.folio.rest.acq.model.SequenceNumber) OrderInvoiceRelationshipCollection(org.folio.rest.acq.model.OrderInvoiceRelationshipCollection) NEW_LOCATION_ID(org.folio.service.inventory.InventoryManagerTest.NEW_LOCATION_ID) InvoiceLineCollection(org.folio.rest.acq.model.invoice.InvoiceLineCollection) TestUtils.getMinimalContentCompositePurchaseOrder(org.folio.TestUtils.getMinimalContentCompositePurchaseOrder) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) HOLDING_INSTANCE_ID_2_HOLDING(org.folio.service.inventory.InventoryManagerTest.HOLDING_INSTANCE_ID_2_HOLDING) OLD_LOCATION_ID(org.folio.service.inventory.InventoryManagerTest.OLD_LOCATION_ID) LEDGER_NOT_FOUND_FOR_TRANSACTION(org.folio.rest.core.exceptions.ErrorCodes.LEDGER_NOT_FOUND_FOR_TRANSACTION) HttpHeaders(io.vertx.core.http.HttpHeaders) UUID(java.util.UUID) Instant(java.time.Instant) Tag(org.folio.rest.acq.model.tag.Tag) Collectors(java.util.stream.Collectors) FUND_ENCUMBRANCE_ERROR(org.folio.rest.impl.PurchaseOrdersApiTest.FUND_ENCUMBRANCE_ERROR) Objects(java.util.Objects) List(java.util.List) Response(javax.ws.rs.core.Response) ACTIVE_VENDOR_ID(org.folio.rest.impl.PurchaseOrdersApiTest.ACTIVE_VENDOR_ID) StreamEx(one.util.streamex.StreamEx) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) NON_EXIST_VENDOR_ID(org.folio.rest.impl.PurchaseOrdersApiTest.NON_EXIST_VENDOR_ID) PieceCollection(org.folio.rest.acq.model.PieceCollection) MOD_VENDOR_INTERNAL_ERROR_ID(org.folio.rest.impl.PurchaseOrdersApiTest.MOD_VENDOR_INTERNAL_ERROR_ID) HttpException(org.folio.rest.core.exceptions.HttpException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) FiscalYear(org.folio.rest.acq.model.finance.FiscalYear) TestUtils.getMockData(org.folio.TestUtils.getMockData) LedgerCollection(org.folio.rest.acq.model.finance.LedgerCollection) ITEM_PURCHASE_ORDER_LINE_IDENTIFIER(org.folio.service.inventory.InventoryManager.ITEM_PURCHASE_ORDER_LINE_IDENTIFIER) OrderTransactionSummary(org.folio.rest.acq.model.finance.OrderTransactionSummary) TestUtils.getTitle(org.folio.TestUtils.getTitle) Encumbrance(org.folio.rest.acq.model.finance.Encumbrance) EMPTY(org.apache.commons.lang3.StringUtils.EMPTY) ITEMS(org.folio.service.inventory.InventoryManager.ITEMS) Iterator(java.util.Iterator) PREFIX(org.folio.rest.impl.crud.CrudTestEntities.PREFIX) org.folio.rest.jaxrs.model(org.folio.rest.jaxrs.model) TimeUnit(java.util.concurrent.TimeUnit) Error(org.folio.rest.jaxrs.model.Error) SUFFIX(org.folio.rest.impl.crud.CrudTestEntities.SUFFIX) Collectors.toList(java.util.stream.Collectors.toList) ID_FOR_PRINT_MONOGRAPH_ORDER(org.folio.rest.impl.PurchaseOrdersApiTest.ID_FOR_PRINT_MONOGRAPH_ORDER) HttpMethod(io.vertx.core.http.HttpMethod) HelperUtils.calculateEstimatedPrice(org.folio.orders.utils.HelperUtils.calculateEstimatedPrice) HttpStatus(org.folio.HttpStatus) RECEIVING_HISTORY_PURCHASE_ORDER_ID(org.folio.rest.impl.ReceivingHistoryApiTest.RECEIVING_HISTORY_PURCHASE_ORDER_ID) FundCollection(org.folio.rest.acq.model.finance.FundCollection) InvoiceLine(org.folio.rest.acq.model.invoice.InvoiceLine) Table(com.google.common.collect.Table) LogManager(org.apache.logging.log4j.LogManager) TitleCollection(org.folio.rest.acq.model.TitleCollection) Collections(java.util.Collections) ResourcePathResolver(org.folio.orders.utils.ResourcePathResolver) ORDER_DELETE_ERROR_TENANT(org.folio.rest.impl.PurchaseOrdersApiTest.ORDER_DELETE_ERROR_TENANT) NON_EXISTED_NEW_HOLDING_ID(org.folio.service.inventory.InventoryManagerTest.NON_EXISTED_NEW_HOLDING_ID) Matcher(java.util.regex.Matcher) TestUtils.getMinimalContentCompositePoLine(org.folio.TestUtils.getMinimalContentCompositePoLine) Iterator(java.util.Iterator) NoSuchFileException(java.nio.file.NoSuchFileException) JsonObject(io.vertx.core.json.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) IOException(java.io.IOException)

Aggregations

HashBasedTable (com.google.common.collect.HashBasedTable)1 Lists (com.google.common.collect.Lists)1 Table (com.google.common.collect.Table)1 Header (io.restassured.http.Header)1 Vertx (io.vertx.core.Vertx)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpMethod (io.vertx.core.http.HttpMethod)1 HttpServer (io.vertx.core.http.HttpServer)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 Router (io.vertx.ext.web.Router)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1 IOException (java.io.IOException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Instant (java.time.Instant)1 DAYS (java.time.temporal.ChronoUnit.DAYS)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1