2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modifications Copyright (C) 2021-2023 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 static org.springframework.http.HttpStatus.BAD_GATEWAY
25 import static org.springframework.http.HttpStatus.BAD_REQUEST
26 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
27 import static org.springframework.http.HttpStatus.NOT_FOUND
28 import static org.springframework.http.HttpStatus.CONFLICT
29 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMP
30 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMPINVENTORY
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
34 import groovy.json.JsonSlurper
35 import org.mapstruct.factory.Mappers
36 import org.onap.cps.TestUtils
37 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
38 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
39 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
40 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
41 import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper
42 import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler
43 import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler
44 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor
45 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper
46 import org.onap.cps.ncmp.rest.mapper.DataOperationRequestMapper
47 import org.onap.cps.ncmp.rest.util.DeprecationHelper
48 import org.onap.cps.spi.exceptions.AlreadyDefinedException
49 import org.onap.cps.spi.exceptions.CpsException
50 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
51 import org.onap.cps.spi.exceptions.DataValidationException
52 import org.onap.cps.utils.JsonObjectMapper
53 import org.spockframework.spring.SpringBean
54 import org.springframework.beans.factory.annotation.Autowired
55 import org.springframework.beans.factory.annotation.Value
56 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
57 import org.springframework.http.MediaType
58 import org.springframework.test.web.servlet.MockMvc
59 import spock.lang.Shared
60 import spock.lang.Specification
63 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
69 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
72 JsonObjectMapper stubbedJsonObjectMapper = Stub()
75 NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
78 CmHandleStateMapper cmHandleStateMapper = Mappers.getMapper(CmHandleStateMapper)
81 DataOperationRequestMapper dataOperationRequestMapper = Mappers.getMapper(DataOperationRequestMapper)
84 CpsNcmpTaskExecutor stubbedCpsTaskExecutor = Stub()
87 DeprecationHelper stubbedDeprecationHelper = Stub()
90 NcmpCachedResourceRequestHandler stubbedNcmpCachedResourceRequestHandler = Stub()
93 NcmpPassthroughResourceRequestHandler StubbedNcmpPassthroughResourceRequestHandler = Stub()
95 @Value('${rest.api.ncmp-base-path}')
98 @Value('${rest.api.ncmp-inventory-base-path}')
99 def basePathNcmpInventory
101 def dataNodeBaseEndpointNcmp
102 def dataNodeBaseEndpointNcmpInventory
105 def sampleErrorMessage = 'some error message'
107 def sampleErrorDetails = 'some error details'
110 dataNodeBaseEndpointNcmp = "$basePathNcmp/v1"
111 dataNodeBaseEndpointNcmpInventory = "$basePathNcmpInventory/v1"
114 def 'Get request with generic #scenario exception returns correct HTTP Status with #scenario'() {
115 when: 'an exception is thrown by the service'
116 setupTestException(exception, NCMP)
117 def response = performTestRequest(NCMP)
118 then: 'an HTTP response is returned with correct message and details'
119 assertTestResponse(response, expectedErrorCode, expectedErrorMessage, expectedErrorDetails)
121 scenario | exception || expectedErrorDetails | expectedErrorMessage | expectedErrorCode
122 'CPS' | new CpsException(sampleErrorMessage, sampleErrorDetails) || sampleErrorDetails | sampleErrorMessage | INTERNAL_SERVER_ERROR
123 'NCMP-server' | new ServerNcmpException(sampleErrorMessage, sampleErrorDetails) || null | sampleErrorMessage | INTERNAL_SERVER_ERROR
124 'NCMP-client' | new DmiRequestException(sampleErrorMessage, sampleErrorDetails) || null | sampleErrorMessage | BAD_REQUEST
125 'DataNode Validation' | new DataNodeNotFoundException('myDataspaceName', 'myAnchorName') || null | 'DataNode not found' | NOT_FOUND
126 'other' | new IllegalStateException(sampleErrorMessage) || null | sampleErrorMessage | INTERNAL_SERVER_ERROR
127 'Data Node Not Found' | new DataNodeNotFoundException('myDataspaceName', 'myAnchorName') || 'DataNode not found' | 'DataNode not found' | NOT_FOUND
128 'Existing entry' | new AlreadyDefinedException('name',null) || 'name already exists' | 'Already defined exception' | CONFLICT
129 'Existing entries' | AlreadyDefinedException.forDataNodes(['A', 'B'], 'myAnchorName') || '2 data node(s) already exist' | 'Already defined exception' | CONFLICT
132 def 'Post request with exception returns correct HTTP Status.'() {
133 given: 'the service throws data validation exception'
134 def exception = new DataValidationException(sampleErrorMessage, sampleErrorDetails)
135 setupTestException(exception, NCMPINVENTORY)
136 when: 'the HTTP request is made'
137 def response = performTestRequest(NCMPINVENTORY)
138 then: 'an HTTP response is returned with correct message and details'
139 assertTestResponse(response, BAD_REQUEST, sampleErrorMessage, sampleErrorDetails)
142 def 'Failing DMI Request - passthrough scenario'() {
143 given: 'failing DMI request'
144 setupTestException(new HttpClientRequestException('Error Message Details NCMP', 'Bad Request from DMI', 400), NCMP)
145 when: 'the DMI request is executed'
146 def response = performTestRequest(NCMP)
147 then: 'NCMP service responds with 502 Bad Gateway status'
148 response.status == BAD_GATEWAY.value()
149 and: 'the NCMP response also contains the original DMI response details'
150 response.contentAsString.contains('400')
151 response.contentAsString.contains('Bad Request from DMI')
154 def setupTestException(exception, apiType) {
155 if (NCMP == apiType) {
156 mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception }
158 mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> { throw exception }
161 def performTestRequest(apiType) {
162 if (NCMP == apiType) {
163 return mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/modules")).andReturn().response
165 def jsonData = TestUtils.getResourceFileContent('dmi_registration_all_singing_and_dancing.json')
166 return mvc.perform(post("$dataNodeBaseEndpointNcmpInventory/ch").contentType(MediaType.APPLICATION_JSON).content(jsonData)).andReturn().response
169 static void assertTestResponse(response, expectedStatus, expectedErrorMessage, expectedErrorDetails) {
170 assert response.status == expectedStatus.value()
171 def content = new JsonSlurper().parseText(response.contentAsString)
172 assert content['status'].toString().contains(expectedStatus.toString())
173 assert expectedErrorMessage == null || content['message'].toString().contains(expectedErrorMessage)
174 assert expectedErrorDetails == null || content['details'].toString().contains(expectedErrorDetails)