Replacing ModelMapper with MapStruct
[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 com.fasterxml.jackson.databind.ObjectMapper
25 import groovy.json.JsonSlurper
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.TestUtils
28 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
29 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
30 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
31 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
32 import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper
33 import org.onap.cps.spi.exceptions.CpsException
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
35 import org.onap.cps.spi.exceptions.DataValidationException
36 import org.onap.cps.utils.JsonObjectMapper
37 import org.spockframework.spring.SpringBean
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.beans.factory.annotation.Value
40 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
41 import org.springframework.http.MediaType
42 import org.springframework.test.web.servlet.MockMvc
43 import spock.lang.Shared
44 import spock.lang.Specification
45
46 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMP
47 import static org.onap.cps.ncmp.rest.exceptions.NetworkCmProxyRestExceptionHandlerSpec.ApiType.NCMPINVENTORY
48 import static org.springframework.http.HttpStatus.BAD_REQUEST
49 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
50 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
51 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
52
53 @WebMvcTest
54 class NetworkCmProxyRestExceptionHandlerSpec extends Specification {
55
56     @Autowired
57     MockMvc mvc
58
59     @SpringBean
60     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
61
62     @SpringBean
63     JsonObjectMapper jsonObjectMapper = Stub()
64
65     @SpringBean
66     NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
67
68     @Value('${rest.api.ncmp-base-path}')
69     def basePathNcmp
70
71     @Value('${rest.api.ncmp-inventory-base-path}')
72     def basePathNcmpInventory
73
74     def dataNodeBaseEndpointNcmp
75     def dataNodeBaseEndpointNcmpInventory
76
77     @Shared
78     def errorMessage = 'some error message'
79     @Shared
80     def errorDetails = 'some error details'
81     @Shared
82     def dataNodeNotFoundErrorMessage = 'DataNode not found'
83
84     def setup() {
85         dataNodeBaseEndpointNcmp = "$basePathNcmp/v1"
86         dataNodeBaseEndpointNcmpInventory = "$basePathNcmpInventory/v1"
87     }
88
89     def 'Get request with generic #scenario exception returns correct HTTP Status.'() {
90         when: 'an exception is thrown by the service'
91             setupTestException(exception, NCMP)
92             def response = performTestRequest(NCMP)
93         then: 'an HTTP response is returned with correct message and details'
94             assertTestResponse(response, expectedErrorCode, expectedErrorMessage, expectedErrorDetails)
95         where:
96             scenario              | exception                                                                 || expectedErrorDetails | expectedErrorMessage          | expectedErrorCode
97             'CPS'                 | new CpsException(errorMessage, errorDetails)                              || errorDetails         |  errorMessage                 | INTERNAL_SERVER_ERROR
98             'NCMP-server'         | new ServerNcmpException(errorMessage, errorDetails)                       || null                 |  errorMessage                 | INTERNAL_SERVER_ERROR
99             'NCMP-client'         | new DmiRequestException(errorMessage, errorDetails)                       || null                 |  errorMessage                 | BAD_REQUEST
100             'DataNode Validation' | new DataNodeNotFoundException(dataNodeNotFoundErrorMessage, errorDetails) || null                 |  dataNodeNotFoundErrorMessage | BAD_REQUEST
101             'other'               | new IllegalStateException(errorMessage)                                   || null                 |  errorMessage                 | INTERNAL_SERVER_ERROR
102     }
103
104     def 'Post request with exception returns correct HTTP Status.'() {
105         given: 'the service throws data validation exception'
106             def exception = new DataValidationException(errorMessage, errorDetails)
107             setupTestException(exception, NCMPINVENTORY)
108         when: 'the HTTP request is made'
109             def response = performTestRequest(NCMPINVENTORY)
110         then: 'an HTTP response is returned with correct message and details'
111             assertTestResponse(response, BAD_REQUEST, errorMessage, errorDetails)
112     }
113
114     def setupTestException(exception, apiType) {
115         if (NCMP == apiType) {
116             mockNetworkCmProxyDataService.getYangResourcesModuleReferences(*_) >> { throw exception }
117         }
118         mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> { throw exception }
119     }
120
121     def performTestRequest(apiType) {
122         if (NCMP == apiType) {
123             return mvc.perform(get("$dataNodeBaseEndpointNcmp/ch/testCmHandle/modules")).andReturn().response
124         }
125         def jsonData = TestUtils.getResourceFileContent('dmi_registration_all_singing_and_dancing.json')
126         return mvc.perform(post("$dataNodeBaseEndpointNcmpInventory/ch").contentType(MediaType.APPLICATION_JSON).content(jsonData)).andReturn().response
127     }
128
129     static void assertTestResponse(response, expectedStatus , expectedErrorMessage , expectedErrorDetails) {
130         assert response.status == expectedStatus.value()
131         def content = new JsonSlurper().parseText(response.contentAsString)
132         assert content['status'] == expectedStatus.toString()
133         assert content['message'] == expectedErrorMessage
134         assert expectedErrorDetails == null || content['details'] == expectedErrorDetails
135     }
136
137     enum ApiType {
138         NCMP,
139         NCMPINVENTORY;
140     }
141 }