35544d94117e1e310319b20ce18af41fb68dd686
[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  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.rest.exceptions
23
24 import groovy.json.JsonSlurper
25 import org.modelmapper.ModelMapper
26 import org.onap.cps.TestUtils
27 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
28 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
29 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
30 import org.onap.cps.spi.exceptions.CpsException
31 import org.onap.cps.spi.exceptions.DataValidationException
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.beans.factory.annotation.Value
36 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
37 import org.springframework.http.MediaType
38 import org.springframework.test.web.servlet.MockMvc
39 import spock.lang.Shared
40 import spock.lang.Specification
41
42 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMP
43 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMPINVENTORY
44 import static org.springframework.http.HttpStatus.BAD_REQUEST
45 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
48
49 @WebMvcTest
50 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
51
52     @Autowired
53     MockMvc mvc
54
55     @SpringBean
56     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
57
58     @SpringBean
59     ModelMapper modelMapper = Stub()
60
61     @SpringBean
62     JsonObjectMapper jsonObjectMapper = Stub()
63
64     @Value('${rest.api.ncmp-base-path}')
65     def basePathNcmp
66
67     @Value('${rest.api.ncmp-inventory-base-path}')
68     def basePathNcmpInventory
69
70     def dataNodeBaseEndpointNcmp
71     def dataNodeBaseEndpointNcmpInventory
72
73     @Shared
74     def errorMessage = 'some error message'
75     @Shared
76     def errorDetails = 'some error details'
77
78     def setup() {
79         dataNodeBaseEndpointNcmp = "$basePathNcmp/v1"
80         dataNodeBaseEndpointNcmpInventory = "$basePathNcmpInventory/v1"
81     }
82
83     def 'Get request with generic #scenario exception returns correct HTTP Status.'() {
84         when: 'an exception is thrown by the service'
85             setupTestException(exception, NCMP)
86             def response = performTestRequest(NCMP)
87         then: 'an HTTP response is returned with correct message and details'
88             assertTestResponse(response, expectedErrorCode, errorMessage, expectedErrorDetails)
89         where:
90             scenario      | exception                                           || expectedErrorDetails | expectedErrorCode
91             'CPS'         | new CpsException(errorMessage, errorDetails)        || errorDetails         | INTERNAL_SERVER_ERROR
92             'NCMP-server' | new ServerNcmpException(errorMessage, errorDetails) || null                 | INTERNAL_SERVER_ERROR
93             'NCMP-client' | new DmiRequestException(errorMessage, errorDetails) || null                 | BAD_REQUEST
94             'other'       | new IllegalStateException(errorMessage)             || null                 | INTERNAL_SERVER_ERROR
95     }
96
97     def 'Post request with exception returns correct HTTP Status.'() {
98         given: 'the service throws data validation exception'
99             def exception = new DataValidationException(errorMessage, errorDetails)
100             setupTestException(exception, NCMPINVENTORY)
101         when: 'the HTTP request is made'
102             def response = performTestRequest(NCMPINVENTORY)
103         then: 'an HTTP response is returned with correct message and details'
104             assertTestResponse(response, BAD_REQUEST, errorMessage, errorDetails)
105     }
106
107     def setupTestException(exception, apiType) {
108         if (NCMP.equals(apiType)) {
109             mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception }
110         }
111         mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> { throw exception }
112     }
113
114     def performTestRequest(apiType) {
115         if (NCMP.equals(apiType)) {
116             return mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/modules")).andReturn().response
117         }
118         def jsonData = TestUtils.getResourceFileContent('dmi-registration.json')
119         return mvc.perform(post("$dataNodeBaseEndpointNcmpInventory/ch").contentType(MediaType.APPLICATION_JSON).content(jsonData)).andReturn().response
120     }
121
122     static void assertTestResponse(response, expectedStatus , expectedErrorMessage , expectedErrorDetails) {
123         assert response.status == expectedStatus.value()
124         def content = new JsonSlurper().parseText(response.contentAsString)
125         assert content['status'] == expectedStatus.toString()
126         assert content['message'] == expectedErrorMessage
127         assert expectedErrorDetails == null || content['details'] == expectedErrorDetails
128     }
129
130     enum ApiType {
131         NCMP,
132         NCMPINVENTORY;
133     }
134 }