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