1b72b8c084f4ca6787bd0f55e199634c1cdcbe56
[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-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
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.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
37
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
41
42 @WebMvcTest
43 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
44
45     @Autowired
46     MockMvc mvc
47
48     @SpringBean
49     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
50
51     @SpringBean
52     ModelMapper modelMapper = Stub()
53
54     @SpringBean
55     JsonObjectMapper jsonObjectMapper = Stub()
56
57     @Value('${rest.api.ncmp-base-path}')
58     def basePath
59
60     def dataNodeBaseEndpoint
61
62     @Shared
63     def errorMessage = 'some error message'
64     @Shared
65     def errorDetails = 'some error details'
66
67     def setup() {
68         dataNodeBaseEndpoint = "$basePath/v1"
69     }
70
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)
77         where:
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
83     }
84
85     def setupTestException(exception){
86         mockNetworkCmProxyDataService.getYangResourcesModuleReferences('testCmHandle')>>
87                 { throw exception}
88     }
89
90     def performTestRequest(){
91         return mvc.perform(get("$dataNodeBaseEndpoint/ch/testCmHandle/modules")).andReturn().response
92     }
93
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
100     }
101 }