X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ncmp-rest%2Fsrc%2Ftest%2Fgroovy%2Forg%2Fonap%2Fcps%2Fncmp%2Frest%2Fexceptions%2FNetworkCmProxyRestExceptionHandlerSpec.groovy;h=8004328bc2b84cbca5a1d328b0dee67932efe7c3;hb=e557338803286d8aaa0f877aa25d52d18735f309;hp=7b3cd89145454f38041c807fd462993003f42ee0;hpb=a5ce86d02493f141848f9d609c65883d2198894a;p=cps.git diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy index 7b3cd8914..8004328bc 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy @@ -8,6 +8,7 @@ * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,20 +23,30 @@ package org.onap.cps.ncmp.rest.exceptions import groovy.json.JsonSlurper import org.modelmapper.ModelMapper +import org.onap.cps.TestUtils import org.onap.cps.ncmp.api.NetworkCmProxyDataService -import org.onap.cps.ncmp.api.impl.exception.NcmpException +import org.onap.cps.ncmp.api.impl.exception.DmiRequestException +import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException +import org.onap.cps.ncmp.rest.controller.RestInputMapper 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 import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc import spock.lang.Shared import spock.lang.Specification +import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMP +import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMPINVENTORY +import static org.springframework.http.HttpStatus.BAD_REQUEST import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post @WebMvcTest class NetworkCmProxyRestExceptionHandlerSpec extends Specification { @@ -52,40 +63,68 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification { @SpringBean JsonObjectMapper jsonObjectMapper = Stub() + @SpringBean + RestInputMapper restInputMapper = Mock() + @Value('${rest.api.ncmp-base-path}') - def basePath + def basePathNcmp - def dataNodeBaseEndpoint + @Value('${rest.api.ncmp-inventory-base-path}') + def basePathNcmpInventory + + def dataNodeBaseEndpointNcmp + def dataNodeBaseEndpointNcmpInventory @Shared def errorMessage = 'some error message' @Shared def errorDetails = 'some error details' + @Shared + def dataNodeNotFoundErrorMessage = 'DataNode not found' def setup() { - dataNodeBaseEndpoint = "$basePath/v1" + dataNodeBaseEndpointNcmp = "$basePathNcmp/v1" + dataNodeBaseEndpointNcmpInventory = "$basePathNcmpInventory/v1" } - def 'Get request with generic #scenario exception returns HTTP Status Internal Server Error.'() { - when: 'generic CPS exception is thrown by the service' - setupTestException(exception) - def response = performTestRequest() - then: 'an HTTP Internal Server Error response is returned with correct message and details' - assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, expectedErrorDetails) + def 'Get request with generic #scenario exception returns correct HTTP Status.'() { + when: 'an exception is thrown by the service' + setupTestException(exception, NCMP) + def response = performTestRequest(NCMP) + then: 'an HTTP response is returned with correct message and details' + assertTestResponse(response, expectedErrorCode, expectedErrorMessage, expectedErrorDetails) where: - scenario | exception || expectedErrorDetails - 'CPS' | new CpsException(errorMessage, errorDetails) || errorDetails - 'NCMP' | new NcmpException(errorMessage, errorDetails) || null - 'other' | new IllegalStateException(errorMessage) || null + 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 setupTestException(exception){ - mockNetworkCmProxyDataService.getYangResourcesModuleReferences('testCmHandle')>> - { throw exception} + def 'Post request with exception returns correct HTTP Status.'() { + given: 'the service throws data validation exception' + def exception = new DataValidationException(errorMessage, errorDetails) + setupTestException(exception, NCMPINVENTORY) + when: 'the HTTP request is made' + def response = performTestRequest(NCMPINVENTORY) + then: 'an HTTP response is returned with correct message and details' + assertTestResponse(response, BAD_REQUEST, errorMessage, errorDetails) } - def performTestRequest(){ - return mvc.perform(get("$dataNodeBaseEndpoint/ch/testCmHandle/modules")).andReturn().response + def setupTestException(exception, apiType) { + if (NCMP == apiType) { + mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception } + } + mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> { throw exception } + } + + def performTestRequest(apiType) { + if (NCMP == apiType) { + return mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/modules")).andReturn().response + } + 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 } static void assertTestResponse(response, expectedStatus , expectedErrorMessage , expectedErrorDetails) { @@ -95,4 +134,9 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification { assert content['message'] == expectedErrorMessage assert expectedErrorDetails == null || content['details'] == expectedErrorDetails } + + enum ApiType { + NCMP, + NCMPINVENTORY; + } }