2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.nfproxy.rest.exceptions
22 import groovy.json.JsonSlurper
23 import org.onap.cps.nfproxy.api.NfProxyDataService
24 import org.onap.cps.spi.FetchDescendantsOption
25 import org.onap.cps.spi.exceptions.CpsException
26 import org.spockframework.spring.SpringBean
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.beans.factory.annotation.Value
29 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
30 import org.springframework.test.web.servlet.MockMvc
31 import spock.lang.Shared
32 import spock.lang.Specification
34 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
35 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
38 class NfProxyRestExceptionHandlerSpec extends Specification {
44 NfProxyDataService mockNfProxyDataService = Mock()
46 @Value('${rest.api.xnf-base-path}')
49 def dataNodeBaseEndpoint
52 def errorMessage = 'some error message'
54 def errorDetails = 'some error details'
56 def cmHandle = 'some handle'
57 def xpath = 'some xpath'
60 dataNodeBaseEndpoint = "$basePath/v1"
63 def 'Get request with runtime exception returns HTTP Status Internal Server Error.'() {
64 when: 'runtime exception is thrown by the service'
65 setupTestException(new IllegalStateException(errorMessage))
66 def response = performTestRequest()
67 then: 'an HTTP Internal Server Error response is returned with correct message and details'
68 assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, null)
71 def 'Get request with generic CPS exception returns HTTP Status Internal Server Error.'() {
72 when: 'generic CPS exception is thrown by the service'
73 setupTestException(new CpsException(errorMessage, errorDetails))
74 def response = performTestRequest()
75 then: 'an HTTP Internal Server Error response is returned with correct message and details'
76 assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, errorDetails)
79 def setupTestException(exception) {
80 mockNfProxyDataService.getDataNode(cmHandle, xpath, FetchDescendantsOption.OMIT_DESCENDANTS) >>
84 def performTestRequest() {
85 return mvc.perform(get("$dataNodeBaseEndpoint/cm-handles/$cmHandle/node").param('xpath', xpath))
89 static void assertTestResponse(response, expectedStatus,expectedErrorMessage,
90 expectedErrorDetails) {
91 assert response.status == expectedStatus.value()
92 def content = new JsonSlurper().parseText(response.contentAsString)
93 assert content['status'] == expectedStatus.toString()
94 assert content['message'] == expectedErrorMessage
95 assert expectedErrorDetails == null || content['details'] == expectedErrorDetails