73b57c5f9be89c5189508bab6e272132016da05d
[cps.git] /
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.nfproxy.rest.exceptions
21
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
33
34 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
35 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
36
37 @WebMvcTest
38 class NfProxyRestExceptionHandlerSpec extends Specification {
39
40     @Autowired
41     MockMvc mvc
42
43     @SpringBean
44     NfProxyDataService mockNfProxyDataService = Mock()
45
46     @Value('${rest.api.xnf-base-path}')
47     def basePath
48
49     def dataNodeBaseEndpoint
50
51     @Shared
52     def errorMessage = 'some error message'
53     @Shared
54     def errorDetails = 'some error details'
55
56     def cmHandle = 'some handle'
57     def xpath = 'some xpath'
58
59     def setup() {
60         dataNodeBaseEndpoint = "$basePath/v1"
61     }
62
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)
69     }
70
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)
77     }
78
79     def setupTestException(exception) {
80         mockNfProxyDataService.getDataNode(cmHandle, xpath, FetchDescendantsOption.OMIT_DESCENDANTS) >>
81                 { throw exception}
82     }
83
84     def performTestRequest() {
85         return mvc.perform(get("$dataNodeBaseEndpoint/cm-handles/$cmHandle/node").param('xpath', xpath))
86                 .andReturn().response
87     }
88
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
96     }
97 }