Skip to content

Commit

Permalink
[EDGEPATRON-133]-Added PUT endpoint. (#115)
Browse files Browse the repository at this point in the history
* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.

* [EDGEPATRON-133]-Added PUT endpoint.
  • Loading branch information
SinghAdes authored Jun 24, 2024
1 parent fcc44f3 commit 60b562c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
55 changes: 54 additions & 1 deletion ramls/edge-patron.raml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,60 @@ types:
body:
text/plain:
example: internal server error, contact administrator

put:
description: Updates external patron request
queryParameters:
apikey:
description: "API Key"
type: string
body:
application/json:
type: external_patron
example: !include examples/external_patron.json
responses:
204:
description: |
Returns data for a updated external patron request
body:
application/json:
type: external_patron
example: !include examples/external_patron.json
400:
description: Bad request
body:
text/plain:
example: unable to process request
401:
description: Not authorized to perform requested action
body:
text/plain:
example: unable to create request
404:
description: Item with a given ID not found
body:
text/plain:
example: item not found
403:
description: Access Denied
body:
text/plain:
example: Access Denied
409:
description: Conflict
body:
text/plain:
example: Optimistic Locking Conflict
422:
description: Validation error
body:
text/plain:
example: Validation error
500:
description: |
Internal server error, e.g. due to misconfiguration
body:
text/plain:
example: internal server error, contact administrator
/{id}:
displayName: Manage Accounts By Id
description: Service endpoints that manage accounts by an existing Id
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/folio/edge/patron/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public Router defineRoutes() {
router.route(HttpMethod.GET, "/patron/account/:patronId/by-email/:emailId")
.handler(patronHandler::handleGetExtPatronAccountByEmail);

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);

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/folio/edge/patron/PatronHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ public void handleGetExtPatronAccountByEmail(RoutingContext ctx) {
t -> handleProxyException(ctx, t)));
}

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) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/folio/edge/patron/utils/PatronOkapiClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.folio.edge.patron.utils;

import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -13,6 +15,7 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import static org.folio.edge.core.Constants.X_OKAPI_TOKEN;
import static org.folio.edge.patron.Constants.FIELD_CANCELED_DATE;
import static org.folio.edge.patron.Constants.FIELD_CANCELLATION_ADDITIONAL_INFO;
import static org.folio.edge.patron.Constants.FIELD_CANCELLATION_REASON_ID;
Expand Down Expand Up @@ -136,6 +139,17 @@ public void postPatron(String requestBody,
exceptionHandler);
}

public void putPatron(String emailId, String requestBody,
Handler<HttpResponse<Buffer>> responseHandler, Handler<Throwable> exceptionHandler) {
put(
String.format("%s/patron/account/by-email/%s", okapiURL, emailId),
tenant,
requestBody,
defaultHeaders,
responseHandler,
exceptionHandler);
}

public void cancelHold(String patronId, String holdId, JsonObject holdCancellationRequest,
Handler<HttpResponse<Buffer>> responseHandler, Handler<Throwable> exceptionHandler) {
getRequest(holdId,
Expand Down Expand Up @@ -219,4 +233,21 @@ public PatronLookupException(String msg) {
}
}

public void put(String url, String tenant, String payload, MultiMap headers, Handler<HttpResponse<Buffer>> responseHandler,
Handler<Throwable> exceptionHandler) {
logger.debug("put:: Trying to send request to Okapi with url: {}, payload: {}, tenant: {}", url, payload, tenant);
HttpRequest<Buffer> request = client.putAbs(url);
if (headers != null) {
request.headers().setAll(combineHeadersWithDefaults(headers));
} else {
request.headers().setAll(defaultHeaders);
}
logger.info("PUT '{}' tenant: {} token: {}", () -> url, () -> tenant, () -> request.headers()
.get(X_OKAPI_TOKEN));
request.timeout(reqTimeout);
request.sendBuffer(Buffer.buffer(payload))
.onSuccess(responseHandler)
.onFailure(exceptionHandler);
}
}

34 changes: 34 additions & 0 deletions src/test/java/org/folio/edge/patron/MainVerticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,40 @@ public void testPostExternalLCPatron(TestContext context) throws Exception {
.response();
}

@Test
public void testPutExternalLCPatron(TestContext context) throws Exception {
logger.info("=== Test put external patron ===");

Patron patron = PatronMockOkapi.getPatron();
int expectedStatusCode = 204;
RestAssured
.with()
.body(patron.toJson())
.contentType(APPLICATION_JSON)
.put(
String.format("/patron/account/%s/by-email/%s?apikey=%s", UUID.randomUUID(), "TestMail", apiKey))
.then()
.statusCode(expectedStatusCode)
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
}

@Test
public void testPutExternalLCPatronWithEmptyBody(TestContext context) {
logger.info("=== Test put external patron ===");

int expectedStatusCode = 400;
RestAssured
.with()
.contentType(APPLICATION_JSON)
.put(
String.format("/patron/account/%s/by-email/%s?apikey=%s", UUID.randomUUID(), "TestMail", apiKey))
.then()
.statusCode(expectedStatusCode)
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
.extract()
.response();
}

@Test
public void testPlaceInstanceHoldInstanceNotFound(TestContext context) throws Exception {
logger.info("=== Test place instance hold w/ instance not found ===");
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/folio/edge/patron/utils/PatronMockOkapi.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public Router defineRoutes() {
router.route(HttpMethod.GET, "/patron/account/by-email/:emailId")
.handler(this::getExtPatronAccountHandler);

router.route(HttpMethod.PUT, "/patron/account/by-email/:emailId")
.handler(this::putExtPatronAccountHandler);

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

Expand Down Expand Up @@ -249,6 +252,25 @@ public void getExtPatronAccountHandler(RoutingContext ctx) {
}
}

public void putExtPatronAccountHandler(RoutingContext ctx) {
String token = ctx.request().getHeader(X_OKAPI_TOKEN);
if (token == null || !token.equals(MOCK_TOKEN)) {
ctx.response()
.setStatusCode(403)
.putHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN)
.end("Access requires permission: patron.account.put");
} else if (ctx.body().isEmpty()) {
ctx.response()
.putHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN)
.end("No Body");
} else {
ctx.response()
.setStatusCode(204)
.putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON)
.end(getPatron().toString());
}
}

public void renewItemHandler(RoutingContext ctx) {
String patronId = ctx.request().getParam(PARAM_PATRON_ID);
String itemId = ctx.request().getParam(PARAM_ITEM_ID);
Expand Down

0 comments on commit 60b562c

Please sign in to comment.