Improve error mapping in OrchestrationRequests class 95/141395/1
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Sun, 29 Jun 2025 12:13:59 +0000 (14:13 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Sun, 29 Jun 2025 12:39:14 +0000 (14:39 +0200)
- use status code of request to request-db-adapter in
  ValidationException instead of mapping any kind of
  exception to 404, which can be wildly misleading
- map other exceptions that may be thrown to
  Internal Server Error

Issue-ID: SO-4190
Change-Id: I3f56cfbdab204702fbfdfc95024d1f598e25de9f
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/OrchestrationRequests.java
mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/OrchestrationRequestsTest.java

index 5bf33b4..8c919f0 100644 (file)
@@ -75,6 +75,7 @@ import org.slf4j.MDC;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpClientErrorException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
 import io.swagger.v3.oas.annotations.Operation;
@@ -578,33 +579,57 @@ public class OrchestrationRequests {
         InfraActiveRequests infraActiveRequest = null;
         try {
             infraActiveRequest = requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
+        } catch (HttpClientErrorException e) {
+            ValidateException validateException = getValidateExceptionFromHttpClientErrorException(e);
+            throw validateException;
         } 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();
-
+            ValidateException validateException = getValidateExceptionForInternalServerError(e);
             throw validateException;
         }
 
         if (infraActiveRequest == null) {
-            ErrorLoggerInfo errorLoggerInfo =
-                    new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.BusinessProcessError)
-                            .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();
-
+            ValidateException validateException = getValidateExceptionForNotFound(requestId);
             throw validateException;
         }
         return infraActiveRequest;
     }
 
+    private ValidateException getValidateExceptionForNotFound(String requestId) {
+        ErrorLoggerInfo errorLoggerInfo =
+                new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.BusinessProcessError)
+                        .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();
+        return validateException;
+    }
+
+    private ValidateException getValidateExceptionForInternalServerError(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_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).cause(e)
+                        .errorInfo(errorLoggerInfo).build();
+        return validateException;
+    }
+
+    private ValidateException getValidateExceptionFromHttpClientErrorException(HttpClientErrorException 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",
+                e.getRawStatusCode(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e).errorInfo(errorLoggerInfo)
+                        .build();
+        return validateException;
+    }
+
     protected long daysSinceRequest(InfraActiveRequests request) {
         long startTime = request.getStartTime().getTime();
         long now = System.currentTimeMillis();
index 259ce41..54e09ef 100644 (file)
@@ -41,6 +41,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.apache.http.HttpStatus;
@@ -150,7 +151,7 @@ public class OrchestrationRequestsTest extends BaseTest {
                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
 
         ResponseEntity<GetOrchestrationResponse> response =
-                restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
+                restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
 
         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("request.startTime")
@@ -185,7 +186,7 @@ public class OrchestrationRequestsTest extends BaseTest {
                 .queryParam("format", "simple");
 
         ResponseEntity<GetOrchestrationResponse> response =
-                restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
+                restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
 
         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("request.startTime")
@@ -479,6 +480,27 @@ public class OrchestrationRequestsTest extends BaseTest {
         assertThat(actualProcessingData, sameBeanAs(expectedDataList));
     }
 
+    @Test
+    public void testThatActiveRequestsExceptionsAreMapped() throws Exception {
+        String testRequestId = UUID.randomUUID().toString();
+        wireMockServer.stubFor(any(urlPathEqualTo("/infraActiveRequests/" + testRequestId))
+                .willReturn(aResponse().withStatus(HttpStatus.SC_UNAUTHORIZED)));
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.set("Accept", MediaType.APPLICATION_JSON);
+        headers.set("Content-Type", MediaType.APPLICATION_JSON);
+        headers.set(ONAPLogConstants.Headers.REQUEST_ID, "1e45215d-b7b3-4c5a-9316-65bdddaf649f");
+        HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
+
+        UriComponentsBuilder builder = UriComponentsBuilder
+                .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
+
+        ResponseEntity<GetOrchestrationResponse> response =
+                restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
+
+        assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.getStatusCode().value());
+    }
+
     public void setupTestGetOrchestrationRequest() throws Exception {
         // For testGetOrchestrationRequest
         wireMockServer.stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-1a18-42e5-965d-8ea592502018"))