Handle error in case of upgrade operation
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / NetworkCmProxyInventoryControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Bell Canada
4  *  Modifications Copyright (C) 2021-2023 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.controller
23
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.TestUtils
26 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
27 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
28 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
29 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse
30 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters
31 import org.onap.cps.ncmp.rest.model.CmHandlerRegistrationErrorResponse
32 import org.onap.cps.ncmp.rest.model.DmiPluginRegistrationErrorResponse
33 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
34 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
35 import org.onap.cps.utils.JsonObjectMapper
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
40 import org.springframework.context.annotation.Import
41 import org.springframework.http.HttpStatus
42 import org.springframework.http.MediaType
43 import org.springframework.test.web.servlet.MockMvc
44 import spock.lang.Specification
45
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(NetworkCmProxyInventoryController)
50 @Import(ObjectMapper)
51 class NetworkCmProxyInventoryControllerSpec extends Specification {
52
53     @Autowired
54     MockMvc mvc
55
56     @SpringBean
57     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
58
59     @SpringBean
60     NcmpRestInputMapper ncmpRestInputMapper = Mock()
61
62     DmiPluginRegistration mockDmiPluginRegistration = Mock()
63
64     CmHandleQueryServiceParameters cmHandleQueryServiceParameters = Mock()
65
66     @SpringBean
67     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
68
69     @Value('${rest.api.ncmp-inventory-base-path}/v1')
70     def ncmpBasePathV1
71
72     def 'Dmi plugin registration #scenario'() {
73         given: 'a dmi plugin registration with #scenario'
74             def jsonData = TestUtils.getResourceFileContent(dmiRegistrationJson)
75         and: 'the expected rest input as an object'
76             def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration)
77         and: 'the converter returns a dmi registration (only for the expected input object)'
78             ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration
79         when: 'post request is performed & registration is called with correct DMI plugin information'
80             def response = mvc.perform(
81                 post("$ncmpBasePathV1/ch")
82                     .contentType(MediaType.APPLICATION_JSON)
83                     .content(jsonData)
84             ).andReturn().response
85         then: 'the converted object is forwarded to the registration service'
86             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(mockDmiPluginRegistration) >> new DmiPluginRegistrationResponse()
87         and: 'response status is no content'
88             response.status == HttpStatus.OK.value()
89         where: 'the following registration json is used'
90             scenario                                                                       | dmiRegistrationJson
91             'multiple services, added, updated and removed cm handles and many properties' | 'dmi_registration_all_singing_and_dancing.json'
92             'updated cm handle with updated/new and removed properties'                    | 'dmi_registration_updates_only.json'
93             'without any properties'                                                       | 'dmi_registration_without_properties.json'
94     }
95
96     def 'Dmi plugin registration with invalid json'() {
97         given: 'a dmi plugin registration with #scenario'
98             def jsonDataWithUndefinedDataLabel = '{"notAdmiPlugin":""}'
99         when: 'post request is performed & registration is called with correct DMI plugin information'
100             def response = mvc.perform(
101                 post("$ncmpBasePathV1/ch")
102                     .contentType(MediaType.APPLICATION_JSON)
103                     .content(jsonDataWithUndefinedDataLabel)
104             ).andReturn().response
105         then: 'response status is bad request'
106             response.status == HttpStatus.BAD_REQUEST.value()
107     }
108
109     def 'CmHandle search endpoint test #scenario.'() {
110         given: 'a query object'
111             def cmHandleQueryParameters = jsonObjectMapper.asJsonString(new CmHandleQueryParameters())
112         and: 'the mapper service returns a converted object'
113             ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
114         and: 'the service returns the desired results'
115             mockNetworkCmProxyDataService.executeCmHandleIdSearchForInventory(cmHandleQueryServiceParameters) >> serviceMockResponse
116         when: 'post request is performed & search is called with the given request parameters'
117             def response = mvc.perform(
118                     post("$ncmpBasePathV1/ch/searches")
119                             .contentType(MediaType.APPLICATION_JSON)
120                             .content(cmHandleQueryParameters)
121             ).andReturn().response
122         then: 'response status is OK'
123             assert response.status == HttpStatus.OK.value()
124         and: 'the response data matches the service response.'
125             jsonObjectMapper.convertJsonString(response.getContentAsString(), List) == serviceMockResponse
126         where: 'the service respond with'
127             scenario             | serviceMockResponse
128             'empty response'     | []
129             'populates response' | ['cmHandle1', 'cmHandle2']
130     }
131
132     def 'CmHandle search endpoint test #scenario with blank cmHandleQueryParameters.'() {
133         given: 'a query object'
134             def cmHandleQueryParameters = "{}"
135         and: 'the mapper service returns a converted object'
136             ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
137         and: 'the service returns the desired results'
138             mockNetworkCmProxyDataService.executeCmHandleIdSearchForInventory(cmHandleQueryServiceParameters) >> serviceMockResponse
139         when: 'post request is performed & search is called with the given request parameters'
140             def response = mvc.perform(
141                     post("$ncmpBasePathV1/ch/searches")
142                             .contentType(MediaType.APPLICATION_JSON)
143                             .content(cmHandleQueryParameters)
144             ).andReturn().response
145         then: 'response status is OK'
146             assert response.status == HttpStatus.OK.value()
147         and: 'the response data matches the service response.'
148             jsonObjectMapper.convertJsonString(response.getContentAsString(), List) == serviceMockResponse
149         where: 'the service respond with'
150             scenario             | serviceMockResponse
151             'empty response'     | []
152             'populates response' | ['cmHandle1', 'cmHandle2']
153     }
154
155     def 'CmHandle search endpoint Error Handling.'() {
156         given: 'the mapper service returns a converted object'
157             ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
158         and: 'the service returns the desired results'
159             mockNetworkCmProxyDataService.executeCmHandleIdSearchForInventory(cmHandleQueryServiceParameters) >> []
160         when: 'post request is performed & search is called with the given request parameters'
161             def response = mvc.perform(
162                     post("$ncmpBasePathV1/ch/searches")
163                             .contentType(MediaType.APPLICATION_JSON)
164                             .content(cmHandleQueryParameters)
165             ).andReturn().response
166         then: 'response status is BAD_REQUEST'
167             assert response.status == HttpStatus.BAD_REQUEST.value()
168         where: 'the cmHandleQueryParameters are'
169             scenario          | cmHandleQueryParameters
170             'empty string'    | ""
171             'non-json string' | "this is a test"
172     }
173
174     def 'DMI Registration: All cm-handles operations processed successfully.'() {
175         given: 'a dmi plugin registration'
176             def dmiRegistrationRequest = '{}'
177         and: 'service can register cm-handles successfully'
178             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
179                 createdCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1')],
180                 updatedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-2')],
181                 removedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-3')]
182             )
183             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
184         when: 'registration endpoint is invoked'
185             def response = mvc.perform(
186                 post("$ncmpBasePathV1/ch")
187                     .contentType(MediaType.APPLICATION_JSON)
188                     .content(dmiRegistrationRequest)
189             ).andReturn().response
190         then: 'response status is ok'
191             response.status == HttpStatus.OK.value()
192         and: 'the response body is empty'
193             response.getContentAsString() == ''
194
195     }
196
197     def 'DMI Registration Error Handling: #scenario.'() {
198         given: 'a dmi plugin registration'
199             def dmiRegistrationRequest = '{}'
200         and: '#scenario: service failed to register few cm-handle'
201             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
202                 createdCmHandles: [createCmHandleResponse],
203                 updatedCmHandles: [updateCmHandleResponse],
204                 removedCmHandles: [removeCmHandleResponse],
205                 upgradedCmHandles: [upgradeCmHandleResponse]
206             )
207             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
208         when: 'registration endpoint is invoked'
209             def response = mvc.perform(
210                 post("$ncmpBasePathV1/ch")
211                     .contentType(MediaType.APPLICATION_JSON)
212                     .content(dmiRegistrationRequest)
213             ).andReturn().response
214         then: 'request status is internal server error'
215             response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
216         and: 'the response body is in the expected format'
217             def responseBody = jsonObjectMapper.convertJsonString(response.getContentAsString(), DmiPluginRegistrationErrorResponse)
218         and: 'contains only the failure responses'
219             responseBody.getFailedCreatedCmHandles() == expectedFailedCreatedCmHandle
220             responseBody.getFailedUpdatedCmHandles() == expectedFailedUpdateCmHandle
221             responseBody.getFailedRemovedCmHandles() == expectedFailedRemovedCmHandle
222             responseBody.getFailedUpgradeCmHandles() == expectedFailedUpgradedCmHandle
223         where:
224             scenario                | createCmHandleResponse                 | updateCmHandleResponse                 | removeCmHandleResponse                 | upgradeCmHandleResponse                || expectedFailedCreatedCmHandle                 | expectedFailedUpdateCmHandle                  | expectedFailedRemovedCmHandle                 | expectedFailedUpgradedCmHandle
225             'only create failed'    | expectedFailedResponse('cm-handle-1')  | expectedSuccessResponse('cm-handle-2') | expectedSuccessResponse('cm-handle-3') | expectedSuccessResponse('cm-handle-4') || [expectedUnknownErrorResponse('cm-handle-1')] | []                                            | []                                            | []
226             'only update failed'    | expectedSuccessResponse('cm-handle-1') | expectedFailedResponse('cm-handle-2')  | expectedSuccessResponse('cm-handle-3') | expectedSuccessResponse('cm-handle-4') || []                                            | [expectedUnknownErrorResponse('cm-handle-2')] | []                                            | []
227             'only delete failed'    | expectedSuccessResponse('cm-handle-1') | expectedSuccessResponse('cm-handle-2') | expectedFailedResponse('cm-handle-3')  | expectedSuccessResponse('cm-handle-4') || []                                            | []                                            | [expectedUnknownErrorResponse('cm-handle-3')] | []
228             'only upgrade failed'   | expectedSuccessResponse('cm-handle-1') | expectedSuccessResponse('cm-handle-2') | expectedSuccessResponse('cm-handle-3') | expectedFailedResponse('cm-handle-4')  || []                                            | []                                            | []                                            | [expectedUnknownErrorResponse('cm-handle-4')]
229             'all four failed'       | expectedFailedResponse('cm-handle-1')  | expectedFailedResponse('cm-handle-2')  | expectedFailedResponse('cm-handle-3')  | expectedFailedResponse('cm-handle-4')  || [expectedUnknownErrorResponse('cm-handle-1')] | [expectedUnknownErrorResponse('cm-handle-2')] | [expectedUnknownErrorResponse('cm-handle-3')] | [expectedUnknownErrorResponse('cm-handle-4')]
230             'create update failed'  | expectedFailedResponse('cm-handle-1')  | expectedFailedResponse('cm-handle-2')  | expectedSuccessResponse('cm-handle-3') | expectedSuccessResponse('cm-handle-4') || [expectedUnknownErrorResponse('cm-handle-1')] | [expectedUnknownErrorResponse('cm-handle-2')] | []                                            | []
231             'create delete failed'  | expectedFailedResponse('cm-handle-1')  | expectedSuccessResponse('cm-handle-2') | expectedFailedResponse('cm-handle-3')  | expectedSuccessResponse('cm-handle-4') || [expectedUnknownErrorResponse('cm-handle-1')] | []                                            | [expectedUnknownErrorResponse('cm-handle-3')] | []
232             'update delete failed'  | expectedSuccessResponse('cm-handle-1') | expectedFailedResponse('cm-handle-2')  | expectedFailedResponse('cm-handle-3')  | expectedSuccessResponse('cm-handle-4') || []                                            | [expectedUnknownErrorResponse('cm-handle-2')] | [expectedUnknownErrorResponse('cm-handle-3')] | []
233             'delete upgrade failed' | expectedSuccessResponse('cm-handle-1') | expectedSuccessResponse('cm-handle-2') | expectedFailedResponse('cm-handle-3')  | expectedFailedResponse('cm-handle-4')  || []                                            | []                                            | [expectedUnknownErrorResponse('cm-handle-3')] | [expectedUnknownErrorResponse('cm-handle-4')]
234     }
235
236     def 'Get all cm handle IDs by DMI plugin identifier.'() {
237         given: 'an endpoint for returning cm handle IDs for a registered dmi plugin'
238             def getUrl = "$ncmpBasePathV1/ch/cmHandles?dmi-plugin-identifier=some-dmi-plugin-identifier"
239         and: 'a collection of cm handle IDs are returned'
240             1 * mockNetworkCmProxyDataService.getAllCmHandleIdsByDmiPluginIdentifier('some-dmi-plugin-identifier')
241                     >> ['cm-handle-id-1','cm-handle-id-2']
242         when: 'the endpoint is invoked'
243             def response = mvc.perform(
244                     get(getUrl)
245                             .contentType(MediaType.APPLICATION_JSON)
246                             .accept(MediaType.APPLICATION_JSON_VALUE)
247             ).andReturn().response
248         then: 'the response matches the result returned by the service layer'
249             assert response.contentAsString.contains('cm-handle-id-1')
250             assert response.contentAsString.contains('cm-handle-id-2')
251     }
252
253     def expectedUnknownErrorResponse(cmHandle) {
254         return new CmHandlerRegistrationErrorResponse('cmHandle': cmHandle, 'errorCode': '108', 'errorText': 'Failed')
255     }
256
257     def expectedFailedResponse(cmHandle) {
258         return CmHandleRegistrationResponse.createFailureResponse(cmHandle, new RuntimeException('Failed'))
259     }
260
261     def expectedSuccessResponse(cmHandle) {
262         return CmHandleRegistrationResponse.createSuccessResponse(cmHandle)
263     }
264
265 }