use of org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT in project mod-orders by folio-org.
the class MockServer method handleGetContributorNameTypes.
private void handleGetContributorNameTypes(RoutingContext ctx) {
String queryParam = StringUtils.trimToEmpty(ctx.request().getParam("query"));
String tenantId = ctx.request().getHeader(OKAPI_HEADER_TENANT);
if (NON_EXIST_CONTRIBUTOR_NAME_TYPE_TENANT.equals(tenantId)) {
String body = buildEmptyCollection(CONTRIBUTOR_NAME_TYPES);
serverResponse(ctx, HttpStatus.HTTP_OK.toInt(), APPLICATION_JSON, body);
addServerRqRsData(HttpMethod.GET, CONTRIBUTOR_NAME_TYPES, new JsonObject(body));
} else if (queryParam.startsWith("id==")) {
List<String> contributorNameTypeIds = extractIdsFromQuery(queryParam);
JsonObject contributorNameTypeCollection = getMockAsJson(CONTRIBUTOR_NAME_TYPES_PATH);
List<JsonObject> contributorNameTypes = contributorNameTypeCollection.getJsonArray(CONTRIBUTOR_NAME_TYPES).stream().map(o -> ((JsonObject) o)).filter(contributorNameType -> contributorNameTypeIds.contains(contributorNameType.getString(ID))).collect(Collectors.toList());
contributorNameTypeCollection.put(CONTRIBUTOR_NAME_TYPES, contributorNameTypes);
serverResponse(ctx, HttpStatus.HTTP_OK.toInt(), APPLICATION_JSON, contributorNameTypeCollection.encodePrettily());
addServerRqRsData(HttpMethod.GET, CONTRIBUTOR_NAME_TYPES, contributorNameTypeCollection);
} else {
serverResponse(ctx, HttpStatus.HTTP_INTERNAL_SERVER_ERROR.toInt(), TEXT_PLAIN, "Illegal query");
}
}
use of org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT 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());
}
}
}
use of org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT in project mod-invoice by folio-org.
the class MockServer method handlePost.
private <T> void handlePost(RoutingContext ctx, Class<T> tClass, String entryName, boolean generateId) {
logger.info("got: " + ctx.getBodyAsString());
String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
if (ERROR_TENANT.equals(tenant) || CREATE_VOUCHER_ERROR_TENANT.equals(tenant) || CREATE_VOUCHER_LINES_ERROR_TENANT.equals(tenant)) {
serverResponse(ctx, 500, TEXT_PLAIN, INTERNAL_SERVER_ERROR.getReasonPhrase());
} else {
JsonObject body = ctx.getBodyAsJson();
if (generateId) {
String id = UUID.randomUUID().toString();
body.put(AbstractHelper.ID, id);
}
T entry = body.mapTo(tClass);
addServerRqRsData(HttpMethod.POST, entryName, body);
serverResponse(ctx, 201, APPLICATION_JSON, JsonObject.mapFrom(entry).encodePrettily());
}
}
use of org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT in project mod-invoice by folio-org.
the class MockServer method handleGetBudgetExpenseClass.
private void handleGetBudgetExpenseClass(RoutingContext ctx) {
logger.info("handle BudgetExpenseClasses got: {}?{}", ctx.request().path(), ctx.request().query());
String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
String queryParam = StringUtils.trimToEmpty(ctx.request().getParam(QUERY));
addServerRqQuery(BUDGET_EXPENSE_CLASSES, 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)) {
serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
} else {
// By default return 0
BudgetExpenseClassCollection budgetExpenseClasses = new BudgetExpenseClassCollection().withTotalRecords(0);
if (queryParam.contains(EXPENSE_CLASS_ID)) {
String expenseClassId = queryParam.split(EXPENSE_CLASS_ID + "==")[1];
Supplier<BudgetExpenseClassCollection> getFromFile = () -> {
try {
return new JsonObject(getMockData(BUDGET_EXPENSE_CLASS_COLLECTION)).mapTo(BudgetExpenseClassCollection.class);
} catch (IOException e) {
return new BudgetExpenseClassCollection().withTotalRecords(0);
}
};
budgetExpenseClasses.withBudgetExpenseClasses(getFromFile.get().getBudgetExpenseClasses().stream().filter(bec -> bec.getExpenseClassId().equals(expenseClassId)).collect(toList()));
budgetExpenseClasses.withTotalRecords(budgetExpenseClasses.getBudgetExpenseClasses().size());
}
JsonObject budgetExpenseClassesJson = JsonObject.mapFrom(budgetExpenseClasses);
logger.info(budgetExpenseClassesJson.encodePrettily());
addServerRqRsData(HttpMethod.GET, BUDGET_EXPENSE_CLASSES, budgetExpenseClassesJson);
serverResponse(ctx, 200, APPLICATION_JSON, budgetExpenseClassesJson.encodePrettily());
}
}
Aggregations