From e86aaa3f57b1ac711f05ca0f470e889beaec5f6a Mon Sep 17 00:00:00 2001
From: gurleenkaurbp <gurleen.kaur2505@gmail.com>
Date: Fri, 27 Dec 2024 19:07:05 +0530
Subject: [PATCH] [EDGPATRON-161] - Extend get API for
 /patron/registration-status with {externalSystemId}

---
 ramls/schemas/user_error_400.schema           |  7 +++--
 .../org/folio/edge/patron/PatronHandler.java  | 20 +++++++++++---
 .../folio/edge/patron/MainVerticleTest.java   | 26 ++++++++++++++++---
 3 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/ramls/schemas/user_error_400.schema b/ramls/schemas/user_error_400.schema
index 55f2f61..51646aa 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..aa338a6 100644
--- a/src/main/java/org/folio/edge/patron/PatronHandler.java
+++ b/src/main/java/org/folio/edge/patron/PatronHandler.java
@@ -312,12 +312,13 @@ 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) {
+      logger.warn("handleGetPatronRegistrationStatus:: " + validationError);
       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 +334,19 @@ 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) {