Replacing ModelMapper with MapStruct
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / exceptions / NetworkCmProxyRestExceptionHandlerSpec.groovy
index 35544d9..3c39a33 100644 (file)
 
 package org.onap.cps.ncmp.rest.exceptions
 
+import com.fasterxml.jackson.databind.ObjectMapper
 import groovy.json.JsonSlurper
-import org.modelmapper.ModelMapper
+import org.mapstruct.factory.Mappers
 import org.onap.cps.TestUtils
 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
+import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
+import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper
 import org.onap.cps.spi.exceptions.CpsException
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException
 import org.onap.cps.spi.exceptions.DataValidationException
 import org.onap.cps.utils.JsonObjectMapper
 import org.spockframework.spring.SpringBean
@@ -56,10 +60,10 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
 
     @SpringBean
-    ModelMapper modelMapper = Stub()
+    JsonObjectMapper jsonObjectMapper = Stub()
 
     @SpringBean
-    JsonObjectMapper jsonObjectMapper = Stub()
+    NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
 
     @Value('${rest.api.ncmp-base-path}')
     def basePathNcmp
@@ -74,6 +78,8 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
     def errorMessage = 'some error message'
     @Shared
     def errorDetails = 'some error details'
+    @Shared
+    def dataNodeNotFoundErrorMessage = 'DataNode not found'
 
     def setup() {
         dataNodeBaseEndpointNcmp = "$basePathNcmp/v1"
@@ -85,13 +91,14 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
             setupTestException(exception, NCMP)
             def response = performTestRequest(NCMP)
         then: 'an HTTP response is returned with correct message and details'
-            assertTestResponse(response, expectedErrorCode, errorMessage, expectedErrorDetails)
+            assertTestResponse(response, expectedErrorCode, expectedErrorMessage, expectedErrorDetails)
         where:
-            scenario      | exception                                           || expectedErrorDetails | expectedErrorCode
-            'CPS'         | new CpsException(errorMessage, errorDetails)        || errorDetails         | INTERNAL_SERVER_ERROR
-            'NCMP-server' | new ServerNcmpException(errorMessage, errorDetails) || null                 | INTERNAL_SERVER_ERROR
-            'NCMP-client' | new DmiRequestException(errorMessage, errorDetails) || null                 | BAD_REQUEST
-            'other'       | new IllegalStateException(errorMessage)             || null                 | INTERNAL_SERVER_ERROR
+            scenario              | exception                                                                 || expectedErrorDetails | expectedErrorMessage          | expectedErrorCode
+            'CPS'                 | new CpsException(errorMessage, errorDetails)                              || errorDetails         |  errorMessage                 | INTERNAL_SERVER_ERROR
+            'NCMP-server'         | new ServerNcmpException(errorMessage, errorDetails)                       || null                 |  errorMessage                 | INTERNAL_SERVER_ERROR
+            'NCMP-client'         | new DmiRequestException(errorMessage, errorDetails)                       || null                 |  errorMessage                 | BAD_REQUEST
+            'DataNode Validation' | new DataNodeNotFoundException(dataNodeNotFoundErrorMessage, errorDetails) || null                 |  dataNodeNotFoundErrorMessage | BAD_REQUEST
+            'other'               | new IllegalStateException(errorMessage)                                   || null                 |  errorMessage                 | INTERNAL_SERVER_ERROR
     }
 
     def 'Post request with exception returns correct HTTP Status.'() {
@@ -105,17 +112,17 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
     }
 
     def setupTestException(exception, apiType) {
-        if (NCMP.equals(apiType)) {
+        if (NCMP == apiType) {
             mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception }
         }
         mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> { throw exception }
     }
 
     def performTestRequest(apiType) {
-        if (NCMP.equals(apiType)) {
+        if (NCMP == apiType) {
             return mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/modules")).andReturn().response
         }
-        def jsonData = TestUtils.getResourceFileContent('dmi-registration.json')
+        def jsonData = TestUtils.getResourceFileContent('dmi_registration_all_singing_and_dancing.json')
         return mvc.perform(post("$dataNodeBaseEndpointNcmpInventory/ch").contentType(MediaType.APPLICATION_JSON).content(jsonData)).andReturn().response
     }