diff --git a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java index bcb3297..7dc533c 100644 --- a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java +++ b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java @@ -191,12 +191,10 @@ public String callApiPost(String uriString, JsonObject body) } - public String callApiPut(String uriString, JsonObject body) throws Exception{ + public void callApiPut(String uriString, JsonObject body) throws Exception{ final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); int timeout = Integer.parseInt(timeoutString); logger.info("Using timeout: " + timeout); - String stringBody = body.toString(); - logger.info("With body: " + stringBody); RequestConfig config = RequestConfig.custom() .setConnectTimeout(timeout) .setSocketTimeout(timeout) @@ -205,7 +203,7 @@ public String callApiPut(String uriString, JsonObject body) throws Exception{ HttpUriRequest request = RequestBuilder.put() .setConfig(config) .setUri(uriString) - .setEntity(new StringEntity(stringBody, ContentType.APPLICATION_JSON)) + .setEntity(new StringEntity(body.toString(), ContentType.APPLICATION_JSON)) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN).setVersion(HttpVersion.HTTP_1_1) .setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON) @@ -215,12 +213,10 @@ public String callApiPut(String uriString, JsonObject body) throws Exception{ HttpResponse response; HttpEntity entity; - String responseString; int responseCode; try { response = client.execute(request); entity = response.getEntity(); - responseString = EntityUtils.toString(entity, "UTF-8"); responseCode = response.getStatusLine().getStatusCode(); } catch(Exception e) { @@ -238,13 +234,15 @@ public String callApiPut(String uriString, JsonObject body) throws Exception{ logger.info(body); logger.info(uriString); logger.info(responseCode); - logger.info(responseString); if (responseCode > 399) { - String responseBody = processErrorResponse(responseString); - throw new Exception(responseBody); + if (entity != null) { + String responseBody = processErrorResponse(EntityUtils.toString(entity, "UTF-8")); + throw new Exception(responseBody); + } else { + throw new Exception("Failed to update record"); + } } - return responseString; } public HttpResponse callApiDelete(String uriString) throws Exception, IOException, InterruptedException { @@ -960,8 +958,9 @@ public JsonObject cancelRequestItem(String requestId, UserId userId, String agen requestResponse.put("cancellationReasonId", reasonId); requestResponse.put("cancellationAdditionalInformation", Constants.REQUEST_CANCEL_ADDITIONAL_INFO); requestResponse.put("cancelledDate", date); + callApiPut(url, requestResponse); - return new JsonObject(callApiPut(url, requestResponse)); + return requestResponse; } catch(Exception e) { logger.error("Exception occurred during cancel request item"); diff --git a/src/main/java/org/folio/ncip/services/FolioCancelRequestItemService.java b/src/main/java/org/folio/ncip/services/FolioCancelRequestItemService.java index 7767ec9..95c95ff 100644 --- a/src/main/java/org/folio/ncip/services/FolioCancelRequestItemService.java +++ b/src/main/java/org/folio/ncip/services/FolioCancelRequestItemService.java @@ -61,6 +61,8 @@ public CancelRequestItemResponseData performService(CancelRequestItemInitiationD return addProblem(responseData, problem); } + responseData.setRequestId(requestId); + responseData.setUserId(userId); return responseData; } diff --git a/src/test/java/org/folio/ncip/CancelRequestItemTest.java b/src/test/java/org/folio/ncip/CancelRequestItemTest.java new file mode 100644 index 0000000..11a812e --- /dev/null +++ b/src/test/java/org/folio/ncip/CancelRequestItemTest.java @@ -0,0 +1,22 @@ +package org.folio.ncip; + +import io.restassured.response.Response; +import org.junit.Test; + +import java.net.MalformedURLException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class CancelRequestItemTest extends TestBase { + + private static final String REQUEST_ID = "5fc504cb-9042-4bfe-a54f-287c56cd7a11"; + @Test + public void callCancelRequestItem() throws MalformedURLException { + Response response = postData("src/test/resources/mockdata/ncip-cancelRequestItem.xml"); + String body = response.getBody().prettyPrint(); + System.out.println(body); + assertEquals(200, response.getStatusCode()); + assertTrue(body.contains(REQUEST_ID)); + } +} diff --git a/src/test/java/org/folio/ncip/MockServer.java b/src/test/java/org/folio/ncip/MockServer.java index 7806b2e..9a9a9ea 100644 --- a/src/test/java/org/folio/ncip/MockServer.java +++ b/src/test/java/org/folio/ncip/MockServer.java @@ -5,6 +5,7 @@ import io.vertx.core.Vertx; import io.vertx.core.logging.Logger; import io.vertx.core.logging.LoggerFactory; + import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -16,261 +17,328 @@ import java.util.Scanner; import java.util.UUID; import java.util.concurrent.CompletableFuture; + import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpServer; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.BodyHandler; + import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Stream; + import org.extensiblecatalog.ncip.v2.common.Translator; import org.extensiblecatalog.ncip.v2.service.ServiceContext; + import static org.junit.Assert.fail; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; + import org.folio.okapi.common.XOkapiHeaders; import org.folio.okapi.common.logging.FolioLoggingContext; public class MockServer { - - - private Translator translator; //for toolkit - private Properties toolkitProperties; - private Properties ncipProperties; - private ServiceContext serviceContext; - private static FolioNcipHelper folioNcipHelper; - - private static final Logger logger = LoggerFactory.getLogger(MockServer.class); - - - - - - static HashMap params = new HashMap<>(); - - private final int port; - private final Vertx vertx; - - MockServer(int port) { - this.port = port; - this.vertx = Vertx.vertx(); - } - - - void start() throws InterruptedException, ExecutionException, TimeoutException { - // Setup Mock Server... - final Promise promise = Promise.promise(); - folioNcipHelper = new FolioNcipHelper(promise); //TODO???? - //WebContext context = new WebContext(routingContext); - HttpServer server = vertx.createHttpServer(); - params.put("port", this.port); - CompletableFuture deploymentComplete = new CompletableFuture<>(); - server.requestHandler(defineRoutes()).listen(port, result -> { - if(result.succeeded()) { - deploymentComplete.complete(result.result()); - } - else { - deploymentComplete.completeExceptionally(result.cause()); - } - }); - deploymentComplete.get(60, TimeUnit.SECONDS); - } - - void close() { - vertx.close(res -> { - if (res.failed()) { - logger.error("Failed to shut down mock server", res.cause()); - fail(res.cause().getMessage()); - } else { - logger.info("Successfully shut down mock server"); - } - }); - } - - private Router defineRoutes() { - Router router = Router.router(vertx); - router.route().handler(BodyHandler.create()); - router.post("/test").handler(this::test); - router.post("/ncip").handler(this::ncip); - router.get("/groups/:id").handler(this::groupLookup); - router.get("/users").handler(this::users); - router.get("/service-points-users").handler(this::servicePointUsers); - router.get("/service-points").handler(this::servicePointUsers); - router.get("/manualblocks").handler(this::manualBlocks); - router.get("/automated-patron-blocks/:id").handler(this::automatedBlocks); - //router.get("/configurations/entries/maxloancount").handler(this::getMaxLoanCount); - //router.get("/configurations/entries/maxfineamount").handler(this::getMaxFineAmount); - router.get("/configurations/entries/toolkit").handler(this::getToolkitCofigs); - router.get("/configurations/entries").handler(this::getNcipConfigs); - router.get("/inventory/items").handler(this::items); - router.get("/inventory/instances").handler(this::instances); - router.get("/holdings-storage/holdings/:id").handler(this::holdingsById); - router.post("/circulation/requests").handler(this::requestsPost); - - return router; - } - - private void getNcipConfigs(RoutingContext ctx) { - - MultiMap params = ctx.request().params(); - List param = params.getAll("query"); - String toolkit = "configName=toolkit"; - - - param.contains(toolkit); - if (param.contains(toolkit)) { - ctx.reroute("/configurations/entries/toolkit"); - } - - - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "ncip-configs.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void getToolkitCofigs(RoutingContext ctx) { - - - - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "toolkit-configs.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void groupLookup(RoutingContext ctx) { - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "groups-get.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void manualBlocks(RoutingContext ctx) { - String query = ctx.request().getParam("query"); - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "manualBlocks-get-noblocks.json"; - if (query.contains(TestConstants.BLOCKED_PATRON_ID)) mockFileName = TestConstants.PATH_TO_MOCK_FILES + "manualBlocks-get-blocked.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void automatedBlocks(RoutingContext ctx) { - String query = ctx.request().getParam("id"); - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "automatedBlocks-get-noblocks.json"; - if (query.contains(TestConstants.BLOCKED_PATRON_ID_BY_AUTOMATED )) mockFileName = TestConstants.PATH_TO_MOCK_FILES + "automatedBlocks-get-blocked.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void users(RoutingContext ctx) { - String query = ctx.request().getParam("query"); - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get.json"; - if (query.contains(TestConstants.BLOCKED_PATRON_BARCODE)) mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get-blocked.json"; - if (query.contains(TestConstants.PATRON_DOESNT_EXIST_BARCODE)) mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get-notFound.json"; - if (query.contains(TestConstants.BLOCKED_PATRON_BARCODE_BY_AUTOMATED)) mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get-blocked-fine.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void items(RoutingContext ctx) { - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "itemsByHrid-get.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void instances(RoutingContext ctx) { - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "instancesByHrid-get.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void holdingsById(RoutingContext ctx) { - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "holdingsById-get.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void requestsPost(RoutingContext ctx) { - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "requests-post.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx, 200, APPLICATION_JSON, body); - } - - private void servicePointUsers(RoutingContext ctx) { - String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "servicePoints-get.json"; - String body = readLineByLine(mockFileName); - serverResponse(ctx,200,APPLICATION_JSON,body); - } - - private void serverResponse(RoutingContext ctx, int statusCode, String contentType, String body) { - ctx.response() - .setStatusCode(statusCode) - .putHeader(HttpHeaders.CONTENT_TYPE, contentType) - .end(body); - } - - - private void test(RoutingContext ctx) { - - - logger.info("gotx: " + ctx.getBodyAsString()); - String id = UUID.randomUUID().toString(); - JsonObject body = ctx.getBodyAsJson(); - - } - - private static String readLineByLine(String filePath) { - StringBuilder contentBuilder = new StringBuilder(); - try (Stream stream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8)) - { - stream.forEach(s -> contentBuilder.append(s).append("\n")); - } - catch (IOException e) - { - e.printStackTrace(); - } - return contentBuilder.toString(); - } - - - - protected void ncip(RoutingContext ctx) { - - String requestId = ctx.request().headers().get(XOkapiHeaders.REQUEST_ID); - String userId = ctx.request().headers().get(XOkapiHeaders.USER_ID); - String tenant = ctx.request().headers().get(XOkapiHeaders.TENANT); - FolioLoggingContext.put(FolioLoggingContext.REQUEST_ID_LOGGING_VAR_NAME, requestId); - FolioLoggingContext.put(FolioLoggingContext.MODULE_ID_LOGGING_VAR_NAME, "mod-ncip"); - FolioLoggingContext.put(FolioLoggingContext.TENANT_ID_LOGGING_VAR_NAME, tenant); - FolioLoggingContext.put(FolioLoggingContext.USER_ID_LOGGING_VAR_NAME, userId); - - vertx.executeBlocking(promise -> { - InputStream responseMsgInputStream = null; - try { - //FolioNcipHelper folioNcipHelper = new FolioNcipHelper(ctx); - responseMsgInputStream = folioNcipHelper.ncipProcess(ctx); - } - catch(Exception e) { - logger.error("error occured processing this request. Unable to construct a proper NCIP response with problem element"); - logger.error(e.toString()); - ctx.response() - .setStatusCode(500) - .putHeader(HttpHeaders.CONTENT_TYPE, "application/xml") //TODO CONSTANT - //THIS REALLY SHOULD BE AN NCIP RESONSE THAT MIRRORS THE NCIP REQUEST TYPE (WITH PROBLEM ELEMENT) HOWEVER... - //THAT IS NOT POSSIBLE IF WE'VE REACHED HERE BECAUSE ONLY THE MESSAGE HANDLER CAN CONSTRUCT A RESPONSE OBJECT - //WE SHOULDN'T EVER GET HERE - FAMOUS LAST WORDS - .end("problem processing NCIP request" + e.getLocalizedMessage() + ""); - } - - String inputStreamString = new Scanner(responseMsgInputStream,"UTF-8").useDelimiter("\\A").next(); - promise.complete(inputStreamString); - }, res -> { - System.out.println("The result is: " + res.result()); - ctx.response() - .setStatusCode(200) - .putHeader(HttpHeaders.CONTENT_TYPE, "application/xml") //TODO CONSTANT - .end(res.result().toString()); - }); - - } + + + private Translator translator; //for toolkit + private Properties toolkitProperties; + private Properties ncipProperties; + private ServiceContext serviceContext; + private static FolioNcipHelper folioNcipHelper; + + private static final Logger logger = LoggerFactory.getLogger(MockServer.class); + + + static HashMap params = new HashMap<>(); + + private final int port; + private final Vertx vertx; + + MockServer(int port) { + this.port = port; + this.vertx = Vertx.vertx(); + } + + + void start() throws InterruptedException, ExecutionException, TimeoutException { + // Setup Mock Server... + final Promise promise = Promise.promise(); + folioNcipHelper = new FolioNcipHelper(promise); //TODO???? + //WebContext context = new WebContext(routingContext); + HttpServer server = vertx.createHttpServer(); + params.put("port", this.port); + CompletableFuture deploymentComplete = new CompletableFuture<>(); + server.requestHandler(defineRoutes()).listen(port, result -> { + if (result.succeeded()) { + deploymentComplete.complete(result.result()); + } else { + deploymentComplete.completeExceptionally(result.cause()); + } + }); + deploymentComplete.get(60, TimeUnit.SECONDS); + } + + void close() { + vertx.close(res -> { + if (res.failed()) { + logger.error("Failed to shut down mock server", res.cause()); + fail(res.cause().getMessage()); + } else { + logger.info("Successfully shut down mock server"); + } + }); + } + + private Router defineRoutes() { + Router router = Router.router(vertx); + router.route().handler(BodyHandler.create()); + router.post("/test").handler(this::test); + router.post("/ncip").handler(this::ncip); + router.get("/groups/:id").handler(this::groupLookup); + router.get("/users").handler(this::users); + router.get("/service-points-users").handler(this::servicePointUsers); + router.get("/service-points").handler(this::servicePointUsers); + router.get("/manualblocks").handler(this::manualBlocks); + router.get("/automated-patron-blocks/:id").handler(this::automatedBlocks); + //router.get("/configurations/entries/maxloancount").handler(this::getMaxLoanCount); + //router.get("/configurations/entries/maxfineamount").handler(this::getMaxFineAmount); + router.get("/configurations/entries/toolkit").handler(this::getToolkitCofigs); + router.get("/configurations/entries").handler(this::getNcipConfigs); + router.get("/inventory/items").handler(this::items); + router.get("/inventory/instances").handler(this::instances); + router.get("/holdings-storage/holdings/:id").handler(this::holdingsById); + router.post("/circulation/requests").handler(this::requestsPost); + router.get("/addresstypes").handler(this::addressTypes); + router.get("/instance-types").handler(this::instanceTypes); + router.get("/identifier-types").handler(this::identifierTypes); + router.get("/material-types").handler(this::materialTypes); + router.get("/loan-types").handler(this::loanTypes); + router.get("/locations").handler(this::locations); + router.get("/holdings-sources").handler(this::holdingsSources); + router.get("/cancellation-reason-storage/cancellation-reasons").handler(this::cancellationReason); + router.get("/circulation/requests/:id").handler(this::getCirculationRequestById); + router.put("/circulation/requests/:id").handler(this::putCirculationRequestById); + + + return router; + } + + private void getNcipConfigs(RoutingContext ctx) { + + MultiMap params = ctx.request().params(); + List param = params.getAll("query"); + String toolkit = "configName=toolkit"; + + + param.contains(toolkit); + if (param.contains(toolkit)) { + ctx.reroute("/configurations/entries/toolkit"); + } + + + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "ncip-configs.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void getToolkitCofigs(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "toolkit-configs.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void groupLookup(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "groups-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void manualBlocks(RoutingContext ctx) { + String query = ctx.request().getParam("query"); + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "manualBlocks-get-noblocks.json"; + if (query.contains(TestConstants.BLOCKED_PATRON_ID)) + mockFileName = TestConstants.PATH_TO_MOCK_FILES + "manualBlocks-get-blocked.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void automatedBlocks(RoutingContext ctx) { + String query = ctx.request().getParam("id"); + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "automatedBlocks-get-noblocks.json"; + if (query.contains(TestConstants.BLOCKED_PATRON_ID_BY_AUTOMATED)) + mockFileName = TestConstants.PATH_TO_MOCK_FILES + "automatedBlocks-get-blocked.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void users(RoutingContext ctx) { + String query = ctx.request().getParam("query"); + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get.json"; + if (query.contains(TestConstants.BLOCKED_PATRON_BARCODE)) + mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get-blocked.json"; + if (query.contains(TestConstants.PATRON_DOESNT_EXIST_BARCODE)) + mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get-notFound.json"; + if (query.contains(TestConstants.BLOCKED_PATRON_BARCODE_BY_AUTOMATED)) + mockFileName = TestConstants.PATH_TO_MOCK_FILES + "usersByBarcode-get-blocked-fine.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void items(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "itemsByHrid-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void instances(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "instancesByHrid-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void holdingsById(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "holdingsById-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void addressTypes(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "addressTypes-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void requestsPost(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "requests-post.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void servicePointUsers(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "servicePoints-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void instanceTypes(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "instanceTypes-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void identifierTypes(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "identifierTypes-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void materialTypes(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "mTypes-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void loanTypes(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "loanTypes-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void locations(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "locations-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void holdingsSources(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "holdingsSources-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void cancellationReason(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "cancellationReason-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void getCirculationRequestById(RoutingContext ctx) { + String mockFileName = TestConstants.PATH_TO_MOCK_FILES + "circulationRequest-get.json"; + String body = readLineByLine(mockFileName); + serverResponse(ctx, 200, APPLICATION_JSON, body); + } + + private void putCirculationRequestById(RoutingContext ctx) { + ctx.response().setStatusCode(204).end(); + } + + private void serverResponse(RoutingContext ctx, int statusCode, String contentType, String body) { + ctx.response() + .setStatusCode(statusCode) + .putHeader(HttpHeaders.CONTENT_TYPE, contentType) + .end(body); + } + + + private void test(RoutingContext ctx) { + + + logger.info("gotx: " + ctx.getBodyAsString()); + String id = UUID.randomUUID().toString(); + JsonObject body = ctx.getBodyAsJson(); + + } + + private static String readLineByLine(String filePath) { + StringBuilder contentBuilder = new StringBuilder(); + try (Stream stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) { + stream.forEach(s -> contentBuilder.append(s).append("\n")); + } catch (IOException e) { + e.printStackTrace(); + } + return contentBuilder.toString(); + } + + + protected void ncip(RoutingContext ctx) { + + String requestId = ctx.request().headers().get(XOkapiHeaders.REQUEST_ID); + String userId = ctx.request().headers().get(XOkapiHeaders.USER_ID); + String tenant = ctx.request().headers().get(XOkapiHeaders.TENANT); + FolioLoggingContext.put(FolioLoggingContext.REQUEST_ID_LOGGING_VAR_NAME, requestId); + FolioLoggingContext.put(FolioLoggingContext.MODULE_ID_LOGGING_VAR_NAME, "mod-ncip"); + FolioLoggingContext.put(FolioLoggingContext.TENANT_ID_LOGGING_VAR_NAME, tenant); + FolioLoggingContext.put(FolioLoggingContext.USER_ID_LOGGING_VAR_NAME, userId); + + vertx.executeBlocking(promise -> { + InputStream responseMsgInputStream = null; + try { + //FolioNcipHelper folioNcipHelper = new FolioNcipHelper(ctx); + responseMsgInputStream = folioNcipHelper.ncipProcess(ctx); + } catch (Exception e) { + logger.error("error occured processing this request. Unable to construct a proper NCIP response with problem element"); + logger.error(e.toString()); + ctx.response() + .setStatusCode(500) + .putHeader(HttpHeaders.CONTENT_TYPE, "application/xml") //TODO CONSTANT + //THIS REALLY SHOULD BE AN NCIP RESONSE THAT MIRRORS THE NCIP REQUEST TYPE (WITH PROBLEM ELEMENT) HOWEVER... + //THAT IS NOT POSSIBLE IF WE'VE REACHED HERE BECAUSE ONLY THE MESSAGE HANDLER CAN CONSTRUCT A RESPONSE OBJECT + //WE SHOULDN'T EVER GET HERE - FAMOUS LAST WORDS + .end("problem processing NCIP request" + e.getLocalizedMessage() + ""); + } + + String inputStreamString = new Scanner(responseMsgInputStream, "UTF-8").useDelimiter("\\A").next(); + promise.complete(inputStreamString); + }, res -> { + System.out.println("The result is: " + res.result()); + ctx.response() + .setStatusCode(200) + .putHeader(HttpHeaders.CONTENT_TYPE, "application/xml") //TODO CONSTANT + .end(res.result().toString()); + }); + + } } diff --git a/src/test/java/org/folio/ncip/NcipTestSuite.java b/src/test/java/org/folio/ncip/NcipTestSuite.java index a46e1b0..308ba50 100644 --- a/src/test/java/org/folio/ncip/NcipTestSuite.java +++ b/src/test/java/org/folio/ncip/NcipTestSuite.java @@ -14,7 +14,7 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ - LookupUser.class,RequestItem.class + LookupUser.class, RequestItem.class, CancelRequestItemTest.class }) public class NcipTestSuite { diff --git a/src/test/resources/mockdata/addressTypes-get.json b/src/test/resources/mockdata/addressTypes-get.json new file mode 100644 index 0000000..16a3337 --- /dev/null +++ b/src/test/resources/mockdata/addressTypes-get.json @@ -0,0 +1,71 @@ +{ + "addressTypes": [ + { + "addressType": "Home", + "desc": "Home Address", + "id": "93d3d88d-499b-45d0-9bc7-ac73c3a19880", + "metadata": { + "createdDate": "2024-03-13T00:03:41.415+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:48.888+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + }, + { + "addressType": "Order", + "desc": "Order Address", + "id": "46ff3f08-8f41-485c-98d8-701ba8404f4f", + "metadata": { + "createdDate": "2024-03-13T00:03:41.416+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:48.875+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + }, + { + "addressType": "Claim", + "desc": "Claim Address", + "id": "b6f4d1c6-0dfa-463c-9534-f49c4f0ae090", + "metadata": { + "createdDate": "2024-03-13T00:03:41.419+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:48.873+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + }, + { + "addressType": "Returns", + "desc": "Returns Address", + "id": "c42be2ab-c3fd-486c-a1fe-9e5ea0f16198", + "metadata": { + "createdDate": "2024-03-13T00:03:41.417+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:48.878+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + }, + { + "addressType": "Work", + "desc": "Work Address", + "id": "1c4b225f-f669-4e9b-afcd-ebc0e273a34e", + "metadata": { + "createdDate": "2024-03-13T00:03:41.431+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:48.876+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + }, + { + "addressType": "Payment", + "desc": "Payment Address", + "id": "a3c3d60b-df9e-41d9-b7f5-983008bc1a45", + "metadata": { + "createdDate": "2024-03-13T00:03:41.416+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:48.877+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + } + ], + "totalRecords": 6 +} \ No newline at end of file diff --git a/src/test/resources/mockdata/cancellationReason-get.json b/src/test/resources/mockdata/cancellationReason-get.json new file mode 100644 index 0000000..852b7c8 --- /dev/null +++ b/src/test/resources/mockdata/cancellationReason-get.json @@ -0,0 +1,17 @@ +{ + "cancellationReasons": [ + { + "id": "ff474f60-d9ce-4bd8-8659-eb51af825a56", + "name": "Item Not Available", + "description": "Item is no longer available", + "requiresAdditionalInformation": false, + "metadata": { + "createdDate": "2024-03-13T00:03:47.185+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:07:02.468+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + } + ], + "totalRecords": 1 +} \ No newline at end of file diff --git a/src/test/resources/mockdata/circulationRequest-get.json b/src/test/resources/mockdata/circulationRequest-get.json new file mode 100644 index 0000000..6ce369e --- /dev/null +++ b/src/test/resources/mockdata/circulationRequest-get.json @@ -0,0 +1,50 @@ +{ + "id": "5fc504cb-9042-4bfe-a54f-287c56cd7a11", + "requestLevel": "Title", + "requestType": "Page", + "requestDate": "2024-06-18T14:47:28.668+00:00", + "requesterId": "a0774de4-d6fa-4ce4-83a2-e86ad07b15de", + "instanceId": "2f380c68-4903-4f89-8039-c8ab14e30a05", + "holdingsRecordId": "64fff52f-136e-48e5-b1c9-5c829574d65b", + "itemId": "37980327-208d-4b49-9541-2e115dcbd28e", + "status": "Open - Awaiting delivery", + "instance": { + "title": "Test Apple (Lord of the Flies)" + }, + "item": { + "barcode": "at-a18", + "location": { + "name": "SLNP TWO Location A", + "libraryName": "SLNP TWO Library A", + "code": "slnp_two_loc_a" + }, + "status": "Available", + "callNumberComponents": {} + }, + "requester": { + "lastName": "SLNP_ONE_inst_user", + "barcode": "slnp_one_inst_user", + "patronGroup": { + "id": "3684a786-6671-4268-8ed0-9db82ebca60b", + "group": "staff", + "desc": "Staff Member" + }, + "patronGroupId": "3684a786-6671-4268-8ed0-9db82ebca60b" + }, + "fulfillmentPreference": "Delivery", + "pickupServicePointId": "cc860dbd-d5e7-4149-b327-5426f0c4034b", + "metadata": { + "createdDate": "2024-06-18T14:47:28.961+00:00", + "createdByUserId": "9666d8ea-5d30-406c-949b-b983018a5832", + "updatedDate": "2024-06-18T14:48:02.311+00:00", + "updatedByUserId": "9666d8ea-5d30-406c-949b-b983018a5832" + }, + "pickupServicePoint": { + "name": "SLNP TWO service A", + "code": "slnp_two_service_a", + "discoveryDisplayName": "SLNP TWO service A", + "description": null, + "shelvingLagTime": null, + "pickupLocation": true + } +} \ No newline at end of file diff --git a/src/test/resources/mockdata/holdingsSources-get.json b/src/test/resources/mockdata/holdingsSources-get.json new file mode 100644 index 0000000..0e02028 --- /dev/null +++ b/src/test/resources/mockdata/holdingsSources-get.json @@ -0,0 +1,16 @@ +{ + "holdingsRecordsSources": [ + { + "id": "f32d531e-df79-46b3-8932-cdd35f7a2264", + "name": "FOLIO", + "source": "folio", + "metadata": { + "createdDate": "2024-03-13T00:03:16.852+00:00", + "createdByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89", + "updatedDate": "2024-05-16T14:06:53.312+00:00", + "updatedByUserId": "15a65190-1f3e-5dd7-bbd7-a203d6e85a89" + } + } + ], + "totalRecords": 2 +} \ No newline at end of file diff --git a/src/test/resources/mockdata/ncip-cancelRequestItem.xml b/src/test/resources/mockdata/ncip-cancelRequestItem.xml new file mode 100644 index 0000000..64e7bf3 --- /dev/null +++ b/src/test/resources/mockdata/ncip-cancelRequestItem.xml @@ -0,0 +1,25 @@ + + + + + + Relais + + + Lehigh University + + + + Relais + slnp_one_inst_user + + + Relais + 5fc504cb-9042-4bfe-a54f-287c56cd7a11 + + + at-013 + + + + \ No newline at end of file