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