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
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.rest.exceptions
23 import groovy.json.JsonSlurper
24 import org.modelmapper.ModelMapper
25 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
26 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
27 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
28 import org.onap.cps.spi.exceptions.CpsException
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.beans.factory.annotation.Value
33 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
34 import org.springframework.test.web.servlet.MockMvc
35 import spock.lang.Shared
36 import spock.lang.Specification
38 import static org.springframework.http.HttpStatus.BAD_REQUEST
39 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
43 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
49 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
52 ModelMapper modelMapper = Stub()
55 JsonObjectMapper jsonObjectMapper = Stub()
57 @Value('${rest.api.ncmp-base-path}')
60 def dataNodeBaseEndpoint
63 def errorMessage = 'some error message'
65 def errorDetails = 'some error details'
68 dataNodeBaseEndpoint = "$basePath/v1"
71 def 'Get request with generic #scenario exception returns correct HTTP Status.'() {
72 when: 'an exception is thrown by the service'
73 setupTestException(exception)
74 def response = performTestRequest()
75 then: 'an HTTP response is returned with correct message and details'
76 assertTestResponse(response, expectedErrorCode, errorMessage, expectedErrorDetails)
78 scenario | exception || expectedErrorDetails | expectedErrorCode
79 'CPS' | new CpsException(errorMessage, errorDetails) || errorDetails | INTERNAL_SERVER_ERROR
80 'NCMP-server' | new ServerNcmpException(errorMessage, errorDetails) || null | INTERNAL_SERVER_ERROR
81 'NCMP-client' | new DmiRequestException(errorMessage, errorDetails) || null | BAD_REQUEST
82 'other' | new IllegalStateException(errorMessage) || null | INTERNAL_SERVER_ERROR
85 def setupTestException(exception){
86 mockNetworkCmProxyDataService.getYangResourcesModuleReferences('testCmHandle')>>
90 def performTestRequest(){
91 return mvc.perform(get("$dataNodeBaseEndpoint/ch/testCmHandle/modules")).andReturn().response
94 static void assertTestResponse(response, expectedStatus , expectedErrorMessage , expectedErrorDetails) {
95 assert response.status == expectedStatus.value()
96 def content = new JsonSlurper().parseText(response.contentAsString)
97 assert content['status'] == expectedStatus.toString()
98 assert content['message'] == expectedErrorMessage
99 assert expectedErrorDetails == null || content['details'] == expectedErrorDetails