diff --git a/ramls/schemas/user_error_400.schema b/ramls/schemas/user_error_400.schema index 55f2f61..972195a 100644 --- a/ramls/schemas/user_error_400.schema +++ b/ramls/schemas/user_error_400.schema @@ -9,7 +9,8 @@ "description": "Error code", "examples": [ "MULTIPLE_USER_WITH_EMAIL", - "EMAIL_NOT_PROVIDED" + "EMAIL_NOT_PROVIDED", + "INVALID_IDENTIFIERS" ] }, "errorMessage": { @@ -17,7 +18,9 @@ "description": "Error code description", "examples": [ "Multiple users found with the same email", - "emailId is missing in the request" + "emailId is missing in the request", + "Either emailId or externalSystemId must be provided in the request.", + "Provide either emailId or externalSystemId, not both." ] } }, diff --git a/src/main/java/org/folio/edge/patron/PatronHandler.java b/src/main/java/org/folio/edge/patron/PatronHandler.java index d2494c6..2440512 100644 --- a/src/main/java/org/folio/edge/patron/PatronHandler.java +++ b/src/main/java/org/folio/edge/patron/PatronHandler.java @@ -312,12 +312,12 @@ public void handleGetPatronRegistrationStatus(RoutingContext ctx) { String emailId = ctx.request().getParam(PARAM_EMAIL_ID); String externalSystemId = ctx.request().getParam(PARAM_EXTERNAL_SYSTEM_ID); - if (StringUtils.isNullOrEmpty(emailId) && StringUtils.isNullOrEmpty(externalSystemId)) { - logger.warn("handleGetPatronRegistrationStatus:: Missing or empty emailId and externalSystemId"); + String validationError = validateIdentifiers(emailId, externalSystemId); + if (validationError != null) { ctx.response() .setStatusCode(400) .putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) - .end(getErrorMsg("IDENTIFIERS_NOT_PROVIDED", "Either emailId or externalSystemId must be provided in the request")); + .end(getErrorMsg("INVALID_IDENTIFIERS", validationError)); return; } @@ -333,6 +333,17 @@ public void handleGetPatronRegistrationStatus(RoutingContext ctx) { }); } + private String validateIdentifiers(String emailId, String externalSystemId) { + if (StringUtils.isNullOrEmpty(emailId) && StringUtils.isNullOrEmpty(externalSystemId)) { + return "Either emailId or externalSystemId must be provided in the request."; + } + + if (!StringUtils.isNullOrEmpty(emailId) && !StringUtils.isNullOrEmpty(externalSystemId)) { + return "Provide either emailId or externalSystemId, not both."; + } + + return null; + } @Override protected void invalidApiKey(RoutingContext ctx, String msg) { diff --git a/src/test/java/org/folio/edge/patron/MainVerticleTest.java b/src/test/java/org/folio/edge/patron/MainVerticleTest.java index c2d8334..0d39e9c 100644 --- a/src/test/java/org/folio/edge/patron/MainVerticleTest.java +++ b/src/test/java/org/folio/edge/patron/MainVerticleTest.java @@ -393,8 +393,8 @@ public void testGetPatronRegistrationStatusWithoutEmail(TestContext context) { .response(); var jsonResponse = new JsonObject(response.body().asString()); - assertEquals("IDENTIFIERS_NOT_PROVIDED", jsonResponse.getString("code")); - assertEquals("Either emailId or externalSystemId must be provided in the request", jsonResponse.getString("errorMessage")); + assertEquals("INVALID_IDENTIFIERS", jsonResponse.getString("code")); + assertEquals("Either emailId or externalSystemId must be provided in the request.", jsonResponse.getString("errorMessage")); response = RestAssured .get(String.format("/patron/registration-status?emailId=%s&apikey=%s", "", apiKey)) @@ -405,10 +405,28 @@ public void testGetPatronRegistrationStatusWithoutEmail(TestContext context) { .response(); jsonResponse = new JsonObject(response.body().asString()); - assertEquals("IDENTIFIERS_NOT_PROVIDED", jsonResponse.getString("code")); - assertEquals("Either emailId or externalSystemId must be provided in the request", jsonResponse.getString("errorMessage")); + assertEquals("INVALID_IDENTIFIERS", jsonResponse.getString("code")); + assertEquals("Either emailId or externalSystemId must be provided in the request.", jsonResponse.getString("errorMessage")); } + @Test + public void testGetPatronRegistrationStatusWithEmailAndESID(TestContext context) { + + var response = RestAssured + .get(String.format("/patron/registration-status?emailId=%s&externalSystemId=%s&apikey=%s", "abc@abc.com", "9eb67301-6f6e-468f-9b1a-6134dc39a670", apiKey)) + .then() + .statusCode(400) + .header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON) + .extract() + .response(); + + var jsonResponse = new JsonObject(response.body().asString()); + assertEquals("INVALID_IDENTIFIERS", jsonResponse.getString("code")); + assertEquals("Provide either emailId or externalSystemId, not both.", jsonResponse.getString("errorMessage")); + + } + + @Test public void testGetPatronRegistrationStatusWithActiveEmail(TestContext context) {