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