8153eeb70b5c4a43af38c542a8265d67cb5b2473
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 highstreet technologies GmbH
4  *  Modification Copyright (C) 2021 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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.exceptions
22
23 import groovy.json.JsonSlurper
24 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.exceptions.CpsException
27 import org.spockframework.spring.SpringBean
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.beans.factory.annotation.Value
30 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
31 import org.springframework.test.web.servlet.MockMvc
32 import spock.lang.Shared
33 import spock.lang.Specification
34
35 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
36 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
37
38 @WebMvcTest
39 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
40
41     @Autowired
42     MockMvc mvc
43
44     @SpringBean
45     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
46
47     @Value('${rest.api.ncmp-base-path}')
48     def basePath
49
50     def dataNodeBaseEndpoint
51
52     @Shared
53     def errorMessage = 'some error message'
54     @Shared
55     def errorDetails = 'some error details'
56
57     def cmHandle = 'some handle'
58     def xpath = 'some xpath'
59
60     def setup() {
61         dataNodeBaseEndpoint = "$basePath/v1"
62     }
63
64     def 'Get request with runtime exception returns HTTP Status Internal Server Error.'() {
65         when: 'runtime exception is thrown by the service'
66             setupTestException(new IllegalStateException(errorMessage))
67             def response = performTestRequest()
68         then: 'an HTTP Internal Server Error response is returned with correct message and details'
69             assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, null)
70     }
71
72     def 'Get request with generic CPS exception returns HTTP Status Internal Server Error.'() {
73         when: 'generic CPS exception is thrown by the service'
74             setupTestException(new CpsException(errorMessage, errorDetails))
75             def response = performTestRequest()
76         then: 'an HTTP Internal Server Error response is returned with correct message and details'
77             assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, errorDetails)
78     }
79
80     def setupTestException(exception) {
81         mockNetworkCmProxyDataService.getDataNode(cmHandle, xpath, FetchDescendantsOption.OMIT_DESCENDANTS) >>
82                 { throw exception}
83     }
84
85     def performTestRequest() {
86         return mvc.perform(get("$dataNodeBaseEndpoint/cm-handles/$cmHandle/node").param('xpath', xpath))
87                 .andReturn().response
88     }
89
90     static void assertTestResponse(response, expectedStatus,expectedErrorMessage,
91                                    expectedErrorDetails) {
92         assert response.status == expectedStatus.value()
93         def content = new JsonSlurper().parseText(response.contentAsString)
94         assert content['status'] == expectedStatus.toString()
95         assert content['message'] == expectedErrorMessage
96         assert expectedErrorDetails == null || content['details'] == expectedErrorDetails
97     }
98 }