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