Merge "Add null check for the InfraActiveRequests lookup"
diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/OrchestrationRequests.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/OrchestrationRequests.java
index b49f9b5..46bec98 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/OrchestrationRequests.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/OrchestrationRequests.java
@@ -81,7 +81,6 @@
 import io.swagger.v3.oas.annotations.media.Schema;
 import io.swagger.v3.oas.annotations.responses.ApiResponse;
 
-
 @Path("onap/so/infra/orchestrationRequests")
 @OpenAPIDefinition(info = @Info(title = "onap/so/infra/orchestrationRequests",
         description = "API Requests for Orchestration requests"))
@@ -128,33 +127,21 @@
             throw new ValidateException.Builder("Request Id " + requestId + " is not a valid UUID",
                     HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
         }
-        try {
-            infraActiveRequest = requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
-            requestProcessingData = requestsDbClient.getRequestProcessingDataBySoRequestId(requestId);
 
+        infraActiveRequest = infraActiveRequestLookup(requestId);
+
+        try {
+            requestProcessingData = requestsDbClient.getRequestProcessingDataBySoRequestId(requestId);
         } catch (Exception e) {
-            logger.error("Exception occurred", e);
+            logger.error("Exception occurred while communicating with RequestDb during requestProcessingData lookup ",
+                    e);
             ErrorLoggerInfo errorLoggerInfo =
                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.AvailabilityError).build();
 
-            ValidateException validateException =
-                    new ValidateException.Builder("Exception while communciate with Request DB - Infra Request Lookup",
-                            HttpStatus.SC_NOT_FOUND, ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e)
-                                    .errorInfo(errorLoggerInfo).build();
-
-            throw validateException;
-
-        }
-
-        if (infraActiveRequest == null) {
-
-            ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR,
-                    ErrorCode.BusinessProcesssError).build();
-
-            ValidateException validateException =
-                    new ValidateException.Builder("Orchestration RequestId " + requestId + " is not found in DB",
-                            HttpStatus.SC_NO_CONTENT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
-                                    .errorInfo(errorLoggerInfo).build();
+            ValidateException validateException = new ValidateException.Builder(
+                    "Exception occurred while communicating with RequestDb during requestProcessingData lookup",
+                    HttpStatus.SC_NOT_FOUND, ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e)
+                            .errorInfo(errorLoggerInfo).build();
 
             throw validateException;
         }
@@ -239,8 +226,6 @@
         logger.debug("requestId is: {}", requestId);
         ServiceInstancesRequest sir;
 
-        InfraActiveRequests infraActiveRequest;
-
         try {
             ObjectMapper mapper = new ObjectMapper();
             sir = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
@@ -270,41 +255,26 @@
             throw validateException;
         }
 
-        infraActiveRequest = requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
-        if (infraActiveRequest == null) {
-            ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND,
-                    ErrorCode.BusinessProcesssError).build();
+        InfraActiveRequests infraActiveRequest = infraActiveRequestLookup(requestId);
 
-            ValidateException validateException =
-                    new ValidateException.Builder("Null response from RequestDB when searching by RequestId",
-                            HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
-                                    .build();
+        String status = infraActiveRequest.getRequestStatus();
+        if (Status.IN_PROGRESS.toString().equalsIgnoreCase(status) || Status.PENDING.toString().equalsIgnoreCase(status)
+                || Status.PENDING_MANUAL_TASK.toString().equalsIgnoreCase(status)) {
+            infraActiveRequest.setRequestStatus(Status.UNLOCKED.toString());
+            infraActiveRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
+            infraActiveRequest.setRequestId(requestId);
+            requestsDbClient.save(infraActiveRequest);
+        } else {
+
+            ErrorLoggerInfo errorLoggerInfo =
+                    new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError).build();
+
+            ValidateException validateException = new ValidateException.Builder(
+                    "Orchestration RequestId " + requestId + " has a status of " + status + " and can not be unlocked",
+                    HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
+                            .build();
 
             throw validateException;
-
-        } else {
-            String status = infraActiveRequest.getRequestStatus();
-            if (Status.IN_PROGRESS.toString().equalsIgnoreCase(status)
-                    || Status.PENDING.toString().equalsIgnoreCase(status)
-                    || Status.PENDING_MANUAL_TASK.toString().equalsIgnoreCase(status)) {
-                infraActiveRequest.setRequestStatus(Status.UNLOCKED.toString());
-                infraActiveRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
-                infraActiveRequest.setRequestId(requestId);
-                requestsDbClient.save(infraActiveRequest);
-            } else {
-
-                ErrorLoggerInfo errorLoggerInfo =
-                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
-                                .build();
-
-                ValidateException validateException = new ValidateException.Builder(
-                        "Orchestration RequestId " + requestId + " has a status of " + status
-                                + " and can not be unlocked",
-                        HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
-                                .build();
-
-                throw validateException;
-            }
         }
         return Response.status(HttpStatus.SC_NO_CONTENT).entity("").build();
     }
@@ -553,4 +523,34 @@
         addedRequestProcessingData.add(finalProcessingData);
         return addedRequestProcessingData;
     }
+
+    protected InfraActiveRequests infraActiveRequestLookup(String requestId) throws ApiException {
+        InfraActiveRequests infraActiveRequest = null;
+        try {
+            infraActiveRequest = requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
+        } catch (Exception e) {
+            logger.error("Exception occurred while communicating with RequestDb during InfraActiveRequest lookup ", e);
+            ErrorLoggerInfo errorLoggerInfo =
+                    new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.AvailabilityError).build();
+
+            ValidateException validateException = new ValidateException.Builder(
+                    "Exception occurred while communicating with RequestDb during InfraActiveRequest lookup",
+                    HttpStatus.SC_NOT_FOUND, ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e)
+                            .errorInfo(errorLoggerInfo).build();
+
+            throw validateException;
+        }
+
+        if (infraActiveRequest == null) {
+            ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR,
+                    ErrorCode.BusinessProcesssError).build();
+
+            ValidateException validateException = new ValidateException.Builder(
+                    "Null response from RequestDB when searching by RequestId " + requestId, HttpStatus.SC_NOT_FOUND,
+                    ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
+
+            throw validateException;
+        }
+        return infraActiveRequest;
+    }
 }
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsTest.java
index 12f0ffc..151785d 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsTest.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsTest.java
@@ -335,7 +335,7 @@
         expectedRequestError = new RequestError();
         se = new ServiceException();
         se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
-        se.setText("Null response from RequestDB when searching by RequestId");
+        se.setText("Null response from RequestDB when searching by RequestId " + INVALID_REQUEST_ID);
         expectedRequestError.setServiceException(se);
 
         builder = UriComponentsBuilder.fromHttpUrl(
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsUnitTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsUnitTest.java
index 627bbc6..47aa3cc 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsUnitTest.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsUnitTest.java
@@ -21,6 +21,7 @@
 package org.onap.so.apihandlerinfra;
 
 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
+import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertEquals;
@@ -39,6 +40,7 @@
 import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.so.apihandler.common.ResponseBuilder;
 import org.onap.so.apihandlerinfra.exceptions.ApiException;
+import org.onap.so.apihandlerinfra.exceptions.ValidateException;
 import org.onap.so.constants.OrchestrationRequestFormat;
 import org.onap.so.constants.Status;
 import org.onap.so.db.request.beans.InfraActiveRequests;
@@ -327,4 +329,13 @@
 
         assertEquals(Status.FAILED.toString(), result);
     }
+
+    @Test
+    public void infraActiveRequestNullValidationExceptionTest() throws ApiException {
+        iar.setRequestId(REQUEST_ID);
+        thrown.expect(ValidateException.class);
+        thrown.expectMessage(containsString("Null response from RequestDB when searching by RequestId " + REQUEST_ID));
+        orchestrationRequests.infraActiveRequestLookup(iar.getRequestId());
+    }
+
 }