Skip to content

Commit

Permalink
EDGPATRON-150: 1) Implemented post staging user api with test cases. …
Browse files Browse the repository at this point in the history
…2) Removed previous LOC code and tests
  • Loading branch information
kapil-epam committed Oct 21, 2024
1 parent 7659857 commit 7401778
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 466 deletions.
29 changes: 11 additions & 18 deletions src/main/java/org/folio/edge/patron/MainVerticle.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package org.folio.edge.patron;

import static org.folio.edge.patron.Constants.DEFAULT_NULL_PATRON_ID_CACHE_TTL_MS;
import static org.folio.edge.patron.Constants.DEFAULT_PATRON_ID_CACHE_CAPACITY;
import static org.folio.edge.patron.Constants.DEFAULT_PATRON_ID_CACHE_TTL_MS;
import static org.folio.edge.patron.Constants.SYS_NULL_PATRON_ID_CACHE_TTL_MS;
import static org.folio.edge.patron.Constants.SYS_PATRON_ID_CACHE_CAPACITY;
import static org.folio.edge.patron.Constants.SYS_PATRON_ID_CACHE_TTL_MS;

import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.edge.core.EdgeVerticleHttp;
import org.folio.edge.core.utils.OkapiClientFactory;
import org.folio.edge.core.utils.OkapiClientFactoryInitializer;
import org.folio.edge.patron.cache.PatronIdCache;

import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import static org.folio.edge.patron.Constants.DEFAULT_NULL_PATRON_ID_CACHE_TTL_MS;
import static org.folio.edge.patron.Constants.DEFAULT_PATRON_ID_CACHE_CAPACITY;
import static org.folio.edge.patron.Constants.DEFAULT_PATRON_ID_CACHE_TTL_MS;
import static org.folio.edge.patron.Constants.SYS_NULL_PATRON_ID_CACHE_TTL_MS;
import static org.folio.edge.patron.Constants.SYS_PATRON_ID_CACHE_CAPACITY;
import static org.folio.edge.patron.Constants.SYS_PATRON_ID_CACHE_TTL_MS;

public class MainVerticle extends EdgeVerticleHttp {

Expand Down Expand Up @@ -57,20 +56,14 @@ public Router defineRoutes() {
router.route(HttpMethod.GET, "/patron/account/:patronId")
.handler(patronHandler::handleGetAccount);

router.route(HttpMethod.GET, "/patron/account/:patronId/external-patrons")
.handler(patronHandler::handleGetExtPatronsAccounts);

router.route(HttpMethod.PUT, "/patron/account/:patronId/by-email/:emailId")
.handler(patronHandler::handlePutExtPatronAccountByEmail);

router.route(HttpMethod.POST, "/patron/account/:patronId/item/:itemId/renew")
.handler(patronHandler::handleRenew);

router.route(HttpMethod.POST, "/patron/account/:patronId/item/:itemId/hold")
.handler(patronHandler::handlePlaceItemHold);

router.route(HttpMethod.POST, "/patron/account/:patronId")
.handler(patronHandler::handlePatronRequest);
router.route(HttpMethod.POST, "/patron")
.handler(patronHandler::handlePostPatronRequest);

router.route(HttpMethod.POST, "/patron/account/:patronId/instance/:instanceId/hold")
.handler(patronHandler::handlePlaceInstanceHold);
Expand Down
141 changes: 75 additions & 66 deletions src/main/java/org/folio/edge/patron/PatronHandler.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
package org.folio.edge.patron;

import static org.folio.edge.core.Constants.APPLICATION_JSON;
import static org.folio.edge.patron.Constants.FIELD_EXPIRATION_DATE;
import static org.folio.edge.patron.Constants.FIELD_REQUEST_DATE;
import static org.folio.edge.patron.Constants.MSG_ACCESS_DENIED;
import static org.folio.edge.patron.Constants.MSG_EXTERNAL_NOBODY;
import static org.folio.edge.patron.Constants.MSG_HOLD_NOBODY;
import static org.folio.edge.patron.Constants.MSG_INTERNAL_SERVER_ERROR;
import static org.folio.edge.patron.Constants.MSG_REQUEST_TIMEOUT;
import static org.folio.edge.patron.Constants.PARAM_EMAIL_ID;
import static org.folio.edge.patron.Constants.PARAM_EXPIRED;
import static org.folio.edge.patron.Constants.PARAM_HOLD_ID;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_CHARGES;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_HOLDS;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_LOANS;
import static org.folio.edge.patron.Constants.PARAM_INSTANCE_ID;
import static org.folio.edge.patron.Constants.PARAM_ITEM_ID;
import static org.folio.edge.patron.Constants.PARAM_LIMIT;
import static org.folio.edge.patron.Constants.PARAM_OFFSET;
import static org.folio.edge.patron.Constants.PARAM_PATRON_ID;
import static org.folio.edge.patron.Constants.PARAM_SORT_BY;
import static org.folio.edge.patron.model.HoldCancellationValidator.validateCancelHoldRequest;

import com.amazonaws.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.vertx.core.buffer.Buffer;
Expand All @@ -31,15 +9,6 @@
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.HttpResponse;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.edge.core.Handler;
Expand All @@ -53,6 +22,35 @@
import org.folio.edge.patron.utils.PatronIdHelper;
import org.folio.edge.patron.utils.PatronOkapiClient;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;

import static org.folio.edge.core.Constants.APPLICATION_JSON;
import static org.folio.edge.patron.Constants.FIELD_EXPIRATION_DATE;
import static org.folio.edge.patron.Constants.FIELD_REQUEST_DATE;
import static org.folio.edge.patron.Constants.MSG_ACCESS_DENIED;
import static org.folio.edge.patron.Constants.MSG_HOLD_NOBODY;
import static org.folio.edge.patron.Constants.MSG_INTERNAL_SERVER_ERROR;
import static org.folio.edge.patron.Constants.MSG_REQUEST_TIMEOUT;
import static org.folio.edge.patron.Constants.PARAM_EMAIL_ID;
import static org.folio.edge.patron.Constants.PARAM_HOLD_ID;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_CHARGES;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_HOLDS;
import static org.folio.edge.patron.Constants.PARAM_INCLUDE_LOANS;
import static org.folio.edge.patron.Constants.PARAM_INSTANCE_ID;
import static org.folio.edge.patron.Constants.PARAM_ITEM_ID;
import static org.folio.edge.patron.Constants.PARAM_LIMIT;
import static org.folio.edge.patron.Constants.PARAM_OFFSET;
import static org.folio.edge.patron.Constants.PARAM_PATRON_ID;
import static org.folio.edge.patron.Constants.PARAM_SORT_BY;
import static org.folio.edge.patron.model.HoldCancellationValidator.validateCancelHoldRequest;

public class PatronHandler extends Handler {

public static final String WRONG_INTEGER_PARAM_MESSAGE = "'%s' parameter is incorrect."
Expand Down Expand Up @@ -142,22 +140,6 @@ public void handleRenew(RoutingContext ctx) {

}

public void handlePutExtPatronAccountByEmail(RoutingContext ctx) {
if (ctx.body().asJsonObject() == null) {
badRequest(ctx, MSG_EXTERNAL_NOBODY);
return;
}
final String body = String.valueOf(ctx.body().asJsonObject());
handleCommon(ctx,
new String[] {PARAM_PATRON_ID, PARAM_EMAIL_ID},
new String[] {},
(client, params) -> ((PatronOkapiClient) client).putPatron(
params.get(PARAM_EMAIL_ID),
body,
resp -> handleProxyResponse(ctx, resp),
t -> handleProxyException(ctx, t)));
}

public void handlePlaceItemHold(RoutingContext ctx) {
if (ctx.body().asJsonObject() == null) {
badRequest(ctx, MSG_HOLD_NOBODY);
Expand All @@ -175,19 +157,56 @@ public void handlePlaceItemHold(RoutingContext ctx) {
t -> handleProxyException(ctx, t)));
}

public void handlePatronRequest(RoutingContext ctx) {
public void handlePostPatronRequest(RoutingContext ctx) {
if (ctx.body().asJsonObject() == null) {
badRequest(ctx, MSG_EXTERNAL_NOBODY);
logger.warn("handlePostPatronRequest:: missing body found");
ctx.response()
.setStatusCode(400)
.putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
.end(getErrorMsg("MISSING_BODY", "Request body must not null"));
return;
}

final String body = String.valueOf(ctx.body().asJsonObject());
handleCommon(ctx,
new String[] {},
new String[] {},
(client, params) -> ((PatronOkapiClient) client).postPatron(
body,
resp -> handleProxyResponse(ctx, resp),
t -> handleProxyException(ctx, t)));
super.handleCommon(ctx, new String[]{}, new String[]{}, (client, params) -> {
String alternateTenantId = ctx.request().getParam("alternateTenantId", client.tenant);
final PatronOkapiClient patronClient = new PatronOkapiClient(client, alternateTenantId);
patronClient.postPatron(body,
resp -> handlePostPatronResponse(ctx, resp),
t -> handleProxyException(ctx, t));
});
}

private void handlePostPatronResponse(RoutingContext ctx, HttpResponse<Buffer> resp) {
HttpServerResponse serverResponse = ctx.response();

int statusCode = resp.statusCode();
serverResponse.setStatusCode(statusCode);

String respBody = resp.bodyAsString();
if (logger.isDebugEnabled() ) {
logger.debug("handlePostPatronResponse:: response {} ", respBody);
}

String contentType = resp.getHeader(HttpHeaders.CONTENT_TYPE.toString());

if (resp.statusCode() < 400 && Objects.nonNull(respBody)){
setContentType(serverResponse, contentType);
serverResponse.end(respBody); //not an error case, pass on the response body as received
}
else {
String errorMsg = generateErrorMessage(statusCode, respBody);
setContentType(serverResponse, APPLICATION_JSON);
serverResponse.end(errorMsg);
}
}

private String generateErrorMessage(int statusCode, String respBody) {
return switch (statusCode) {
case 422 -> getFormattedErrorMsg(statusCode, respBody);
case 400 -> getErrorMsg("BAD_REQUEST", respBody);
default -> getStructuredErrorMessage(statusCode, respBody);
};
}

public void handleCancelHold(RoutingContext ctx) {
Expand Down Expand Up @@ -215,16 +234,6 @@ public void handleCancelHold(RoutingContext ctx) {
);
}

public void handleGetExtPatronsAccounts(RoutingContext ctx) {
handleCommon(ctx,
new String[] { PARAM_PATRON_ID, PARAM_EXPIRED },
new String[] {},
(client, params) -> ((PatronOkapiClient) client).getExtPatronAccounts(
Boolean.parseBoolean(params.get(PARAM_EXPIRED)),
resp -> handleProxyResponse(ctx, resp),
t -> handleProxyException(ctx, t)));
}

public void handlePlaceInstanceHold(RoutingContext ctx) {
if (ctx.body().asJsonObject() == null) {
badRequest(ctx, MSG_HOLD_NOBODY);
Expand Down
Loading

0 comments on commit 7401778

Please sign in to comment.