2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modification Copyright (C) 2021-2022 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.rest.exceptions
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import groovy.json.JsonSlurper
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.TestUtils
28 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
29 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
30 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
31 import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper
32 import org.onap.cps.spi.exceptions.CpsException
33 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
34 import org.onap.cps.spi.exceptions.DataValidationException
35 import org.onap.cps.utils.JsonObjectMapper
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
40 import org.springframework.http.MediaType
41 import org.springframework.test.web.servlet.MockMvc
42 import spock.lang.Shared
43 import spock.lang.Specification
45 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMP
46 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMPINVENTORY
47 import static org.springframework.http.HttpStatus.BAD_REQUEST
48 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
49 import static org.springframework.http.HttpStatus.NOT_FOUND
50 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
51 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
54 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
60 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
63 JsonObjectMapper jsonObjectMapper = Stub()
66 NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
68 @Value('${rest.api.ncmp-base-path}')
71 @Value('${rest.api.ncmp-inventory-base-path}')
72 def basePathNcmpInventory
74 def dataNodeBaseEndpointNcmp
75 def dataNodeBaseEndpointNcmpInventory
78 def sampleErrorMessage = 'some error message'
80 def sampleErrorDetails = 'some error details'
83 dataNodeBaseEndpointNcmp = "$basePathNcmp/v1"
84 dataNodeBaseEndpointNcmpInventory = "$basePathNcmpInventory/v1"
87 def 'Get request with generic #scenario exception returns correct HTTP Status with #scenario'() {
88 when: 'an exception is thrown by the service'
89 setupTestException(exception, NCMP)
90 def response = performTestRequest(NCMP)
91 then: 'an HTTP response is returned with correct message and details'
92 assertTestResponse(response, expectedErrorCode, expectedErrorMessage, expectedErrorDetails)
94 scenario | exception || expectedErrorDetails | expectedErrorMessage | expectedErrorCode
95 'CPS' | new CpsException(sampleErrorMessage, sampleErrorDetails) || sampleErrorDetails | sampleErrorMessage | INTERNAL_SERVER_ERROR
96 'NCMP-server' | new ServerNcmpException(sampleErrorMessage, sampleErrorDetails) || null | sampleErrorMessage | INTERNAL_SERVER_ERROR
97 'NCMP-client' | new DmiRequestException(sampleErrorMessage, sampleErrorDetails) || null | sampleErrorMessage | BAD_REQUEST
98 'DataNode Validation' | new DataNodeNotFoundException('myDataspaceName', 'myAnchorName') || null | 'DataNode not found' | NOT_FOUND
99 'other' | new IllegalStateException(sampleErrorMessage) || null | sampleErrorMessage | INTERNAL_SERVER_ERROR
100 'Data Node Not Found' | new DataNodeNotFoundException('myDataspaceName', 'myAnchorName') || 'DataNode not found' | 'DataNode not found' | NOT_FOUND
103 def 'Post request with exception returns correct HTTP Status.'() {
104 given: 'the service throws data validation exception'
105 def exception = new DataValidationException(sampleErrorMessage, sampleErrorDetails)
106 setupTestException(exception, NCMPINVENTORY)
107 when: 'the HTTP request is made'
108 def response = performTestRequest(NCMPINVENTORY)
109 then: 'an HTTP response is returned with correct message and details'
110 assertTestResponse(response, BAD_REQUEST, sampleErrorMessage, sampleErrorDetails)
113 def setupTestException(exception, apiType) {
114 if (NCMP == apiType) {
115 mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception }
117 mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> { throw exception }
120 def performTestRequest(apiType) {
121 if (NCMP == apiType) {
122 return mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/modules")).andReturn().response
124 def jsonData = TestUtils.getResourceFileContent('dmi_registration_all_singing_and_dancing.json')
125 return mvc.perform(post("$dataNodeBaseEndpointNcmpInventory/ch").contentType(MediaType.APPLICATION_JSON).content(jsonData)).andReturn().response
128 static void assertTestResponse(response, expectedStatus , expectedErrorMessage , expectedErrorDetails) {
129 assert response.status == expectedStatus.value()
130 def content = new JsonSlurper().parseText(response.contentAsString)
131 assert content['status'].toString().contains(expectedStatus.toString())
132 assert content['message'].toString().contains(expectedErrorMessage)
133 assert expectedErrorDetails == null || content['details'].toString().contains(expectedErrorDetails)