Help NBI user to get information 03/61803/1
authorromaingimbert <romain.gimbert@orange.com>
Wed, 22 Aug 2018 08:00:22 +0000 (10:00 +0200)
committerromaingimbert <romain.gimbert@orange.com>
Wed, 22 Aug 2018 08:00:22 +0000 (10:00 +0200)
-add orderMessage 105 (service name already exists)
-add tests

Change-Id: I62ead9c22af49ee14d3a99eb97dddbf4fcd91e3c
Issue-ID: EXTAPI-116
Signed-off-by: romaingimbert <romain.gimbert@orange.com>
src/main/java/org/onap/nbi/apis/serviceinventory/ServiceInventoryService.java
src/main/java/org/onap/nbi/apis/serviceorder/SoClient.java
src/main/java/org/onap/nbi/apis/serviceorder/workflow/SOTaskProcessor.java
src/main/java/org/onap/nbi/exceptions/BackendErrorHandler.java
src/main/java/org/onap/nbi/exceptions/BackendFunctionalException.java
src/test/java/org/onap/nbi/apis/ApiTest.java

index 393236f..d38d012 100644 (file)
@@ -54,7 +54,7 @@ public class ServiceInventoryService {
 
         if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
             throw new BackendFunctionalException(HttpStatus.NOT_FOUND,
-                "serviceSpecName or serviceSpecId must be provided");
+                "serviceSpecName or serviceSpecId must be provided","serviceSpecName or serviceSpecId must be provided");
         }
 
         String customerId = getCustomerId(clientId);
@@ -68,7 +68,7 @@ public class ServiceInventoryService {
             addRelatedPartyId(customerId, serviceInventoryResponse);
             return serviceInventoryResponse;
         } else {
-            throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found");
+            throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found","no catalog service found");
         }
 
     }
index f9724f9..57cb6b5 100644 (file)
@@ -77,7 +77,7 @@ public class SoClient {
 
         } catch (BackendFunctionalException e) {
             LOGGER.error("error on calling " + url + " ," + e);
-            return new ResponseEntity<>(e.getHttpStatus());
+            return new ResponseEntity(e.getBodyResponse(),e.getHttpStatus());
         } catch (ResourceAccessException e) {
             LOGGER.error("error on calling " + url + " ," + e);
             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
@@ -101,7 +101,7 @@ public class SoClient {
 
         } catch (BackendFunctionalException e) {
             LOGGER.error("error on calling " + url + " ," + e);
-            return new ResponseEntity<>(e.getHttpStatus());
+            return new ResponseEntity(e.getBodyResponse(),e.getHttpStatus());
         } catch (ResourceAccessException e) {
             LOGGER.error("error on calling " + url + " ," + e);
             return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
index 3f9b556..7c954ff 100644 (file)
@@ -83,7 +83,7 @@ public class SOTaskProcessor {
         ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderInfo.getServiceOrderId());
         ServiceOrderItem serviceOrderItem = getServiceOrderItem(executionTask, serviceOrder);
         boolean e2eService = E2EServiceUtils.isE2EService(serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId()));
-        
+
         if (StateType.ACKNOWLEDGED == serviceOrderItem.getState()) {
                if (e2eService) {
                 ResponseEntity<CreateE2EServiceInstanceResponse> response = postE2EServiceOrderItem(serviceOrderInfo,
@@ -104,7 +104,7 @@ public class SOTaskProcessor {
                 pollE2ESoRequestStatus(serviceOrder, serviceOrderItem);
             else
                 pollSoRequestStatus(serviceOrder, serviceOrderItem);
-            
+
             if (serviceOrderItem.getState().equals(StateType.COMPLETED)) {
                 updateSuccessTask(executionTask);
             } else {
@@ -143,7 +143,7 @@ public class SOTaskProcessor {
         }
         return response;
     }
-    
+
     private ServiceOrderItem getServiceOrderItem(ExecutionTask executionTask, ServiceOrder serviceOrder) {
         for (ServiceOrderItem item : serviceOrder.getOrderItem()) {
             if (item.getId().equals(executionTask.getOrderItemId())) {
@@ -187,12 +187,26 @@ public class SOTaskProcessor {
             default:
                 break;
         }
-        if(response!=null && response.getStatusCode()== HttpStatus.INTERNAL_SERVER_ERROR) {
-            serviceOrderService.addOrderMessage(serviceOrder, "502");
-        }
+        buildOrderMessageIfNeeded(serviceOrderItem, serviceOrder, response);
         return response;
     }
 
+    private void buildOrderMessageIfNeeded(ServiceOrderItem serviceOrderItem, ServiceOrder serviceOrder,
+        ResponseEntity<?> response) {
+        if(response!=null)
+        {
+            if(response.getStatusCode()== HttpStatus.INTERNAL_SERVER_ERROR) {
+                serviceOrderService.addOrderMessage(serviceOrder, "502");
+            }
+            if(response.getStatusCode()== HttpStatus.BAD_REQUEST) {
+                ResponseEntity<?> messageError=response;
+                if(messageError.getBody().toString().toLowerCase().contains("serviceinstance already exists")){
+                    serviceOrderService.addOrderItemMessage(serviceOrder, serviceOrderItem, "105");
+                }
+            }
+        }
+    }
+
     private ResponseEntity<CreateE2EServiceInstanceResponse> postE2ESORequest(ServiceOrderItem serviceOrderItem,
         ServiceOrderInfo serviceOrderInfo, ServiceOrder serviceOrder) {
         ServiceModel service = buildE2ESoRequest(serviceOrderItem, serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId()).getCatalogResponse(), serviceOrderInfo.getSubscriberInfo(), serviceOrder);
@@ -208,12 +222,10 @@ public class SOTaskProcessor {
             default:
                 break;
         }
-        if(response!=null && response.getStatusCode()== HttpStatus.INTERNAL_SERVER_ERROR) {
-            serviceOrderService.addOrderMessage(serviceOrder, "502");
-        }
+        buildOrderMessageIfNeeded(serviceOrderItem, serviceOrder, response);
         return response;
     }
-    
+
     private void updateServiceOrder(ServiceOrder serviceOrder) {
         boolean atLeastOneCompleted = false;
         boolean atLeastOneNotFinished = false;
@@ -335,7 +347,7 @@ public class SOTaskProcessor {
             }
         }
     }
-    
+
     /**
      * Build SO CREATE request from the ServiceOrder and catalog informations from SDC
      *
@@ -388,7 +400,7 @@ public class SOTaskProcessor {
 
         return requestDetails;
     }
-    
+
     /**
      * Build E2E SO CREATE request from the ServiceOrder and catalog informations from SDC
      *
@@ -517,7 +529,7 @@ public class SOTaskProcessor {
             }
         }
     }
-    
+
     /**
      * Update an executionTask in database when it's process with a success
      *
index 56496b8..637cbdd 100644 (file)
  */
 package org.onap.nbi.exceptions;
 
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.IOUtils;
 import org.springframework.http.client.ClientHttpResponse;
 import org.springframework.web.client.DefaultResponseErrorHandler;
 import org.springframework.web.client.ResponseErrorHandler;
-import java.io.IOException;
 
 public class BackendErrorHandler implements ResponseErrorHandler {
 
@@ -32,7 +34,12 @@ public class BackendErrorHandler implements ResponseErrorHandler {
     @Override
     public void handleError(ClientHttpResponse response) throws IOException {
         if (response.getStatusCode() != null) {
-            throw new BackendFunctionalException(response.getStatusCode(), response.getStatusText());
+            String body=null;
+            if(response.getBody()!=null) {
+                body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8.name());
+            }
+
+            throw new BackendFunctionalException(response.getStatusCode(), response.getStatusText(),body);
         }
     }
 }
index e037987..43cb502 100644 (file)
@@ -21,13 +21,20 @@ public class BackendFunctionalException extends ApiException {
 
     private final HttpStatus httpStatus;
 
-    public BackendFunctionalException(HttpStatus httpStatus, String message) {
+    private final String bodyResponse;
+
+    public BackendFunctionalException(HttpStatus httpStatus, String message, String bodyResponse) {
         super(message);
         this.httpStatus = httpStatus;
+        this.bodyResponse = bodyResponse;
     }
 
     public HttpStatus getHttpStatus() {
         return httpStatus;
     }
 
+    public String getBodyResponse() {
+        return bodyResponse;
+    }
+
 }
index f18c5f8..3268bab 100644 (file)
@@ -17,6 +17,8 @@ package org.onap.nbi.apis;
 
 
 import static org.assertj.core.api.Assertions.assertThat;
+
+import com.github.tomakehurst.wiremock.http.ResponseDefinition;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -128,10 +130,27 @@ public class ApiTest {
             if (mapping.getRequest().getUrl().equals(s)) {
                 mappingToDelete = mapping;
             }
+
+
         }
+
         wireMockServer.removeStubMapping(mappingToDelete);
     }
 
+    private void changeWireMockResponse(String s,int statusCode, String bodyContent) {
+        ListStubMappingsResult listStubMappingsResult = wireMockServer.listAllStubMappings();
+        ResponseDefinition responseDefinition = new ResponseDefinition(statusCode,bodyContent);
+        List<StubMapping> mappings = listStubMappingsResult.getMappings();
+        for (StubMapping mapping : mappings) {
+            if (mapping.getRequest().getUrl().equals(s)) {
+                mapping.setResponse(responseDefinition);
+            }
+        }
+    }
+
+
+
+
     // serviceCatalog
 
 
@@ -1033,4 +1052,39 @@ public class ApiTest {
         assertThat(executionTaskRepository.count()).isEqualTo(0);
 
     }
+
+
+    @Test
+    public void testExecutionTaskFailedBadRequestSo() throws Exception {
+
+        ExecutionTask executionTaskA = ServiceOrderAssertions.setUpBddForExecutionTaskSucess(serviceOrderRepository,
+            executionTaskRepository, ActionType.ADD);
+
+
+        changeWireMockResponse("/ecomp/mso/infra/serviceInstances/v6",400,"\"serviceException\": {\n"
+            + "        \"messageId\": \"SVC0002\",\n"
+            + "        \"text\": \"Error parsing request.  org.openecomp.mso.apihandler.common.ValidationException: serviceInstance already existsd\"\n"
+            + "    }");
+
+
+        SoTaskProcessor.processOrderItem(executionTaskA);
+        ServiceOrder serviceOrderChecked = serviceOrderRepository.findOne("test");
+        assertThat(serviceOrderChecked.getState()).isEqualTo(StateType.FAILED);
+        for (ServiceOrderItem serviceOrderItem : serviceOrderChecked.getOrderItem()) {
+            assertThat(serviceOrderItem.getState()).isEqualTo(StateType.FAILED);
+        }
+
+        assertThat(executionTaskRepository.count()).isEqualTo(0);
+
+        for (ServiceOrderItem serviceOrderItem : serviceOrderChecked.getOrderItem()) {
+            if(serviceOrderItem.getId().equals("A")) {
+                assertThat(serviceOrderItem.getOrderItemMessage().size()).isEqualTo(1);
+                assertThat(serviceOrderItem.getOrderItemMessage().get(0).getCode()).isEqualTo("105");
+                assertThat(serviceOrderItem.getOrderItemMessage().get(0).getField()).isEqualTo("service.name");
+            }
+        }
+
+    }
+
+
 }