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());
}
}
}
Aggregations