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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.rest.controller
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.TestUtils
26 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
27 import org.onap.cps.ncmp.impl.NetworkCmProxyInventoryFacadeImpl
28 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters
29 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse
30 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
31 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistrationResponse
32 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters
33 import org.onap.cps.ncmp.rest.model.CmHandlerRegistrationErrorResponse
34 import org.onap.cps.ncmp.rest.model.DmiPluginRegistrationErrorResponse
35 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
36 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle
37 import org.onap.cps.ncmp.rest.util.DeprecationHelper
38 import org.onap.cps.ncmp.rest.util.NcmpRestInputMapper
39 import org.onap.cps.ncmp.rest.util.RestOutputCmHandleMapper
40 import org.onap.cps.utils.JsonObjectMapper
41 import org.spockframework.spring.SpringBean
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.beans.factory.annotation.Value
44 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
45 import org.springframework.context.annotation.Import
46 import org.springframework.http.HttpStatus
47 import org.springframework.http.MediaType
48 import org.springframework.test.web.servlet.MockMvc
49 import reactor.core.publisher.Flux
50 import spock.lang.Specification
52 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
53 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
55 @WebMvcTest(NetworkCmProxyInventoryController)
57 class NetworkCmProxyInventoryControllerSpec extends Specification {
63 NetworkCmProxyInventoryFacadeImpl mockNetworkCmProxyInventoryFacade = Mock()
66 NcmpRestInputMapper ncmpRestInputMapper = Mock()
69 RestOutputCmHandleMapper mockRestOutputCmHandleMapper = Mock()
72 DeprecationHelper deprecationHelper = Mock()
74 DmiPluginRegistration mockDmiPluginRegistration = Mock()
76 CmHandleQueryServiceParameters cmHandleQueryServiceParameters = Mock()
79 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
81 @Value('${rest.api.ncmp-inventory-base-path}/v1')
84 def 'Dmi plugin registration #scenario'() {
85 given: 'a dmi plugin registration with #scenario'
86 def jsonData = TestUtils.getResourceFileContent(dmiRegistrationJson)
87 and: 'the expected rest input as an object'
88 def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration)
89 and: 'the converter returns a dmi registration (only for the expected input object)'
90 ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration
91 when: 'post request is performed & registration is called with correct DMI plugin information'
92 def response = mvc.perform(
93 post("$ncmpBasePathV1/ch")
94 .contentType(MediaType.APPLICATION_JSON)
96 ).andReturn().response
97 then: 'the converted object is forwarded to the registration service'
98 1 * mockNetworkCmProxyInventoryFacade.updateDmiRegistration(mockDmiPluginRegistration) >> new DmiPluginRegistrationResponse()
99 and: 'response status is no content'
100 response.status == HttpStatus.OK.value()
101 where: 'the following registration json is used'
102 scenario | dmiRegistrationJson
103 'multiple services, added, updated and removed cm handles and many properties' | 'dmi_registration_all_singing_and_dancing.json'
104 'updated cm handle with updated/new and removed properties' | 'dmi_registration_updates_only.json'
105 'without any properties' | 'dmi_registration_without_properties.json'
108 def 'Dmi plugin registration with invalid json'() {
109 given: 'a dmi plugin registration with #scenario'
110 def jsonDataWithUndefinedDataLabel = '{"notAdmiPlugin":""}'
111 when: 'post request is performed & registration is called with correct DMI plugin information'
112 def response = mvc.perform(
113 post("$ncmpBasePathV1/ch")
114 .contentType(MediaType.APPLICATION_JSON)
115 .content(jsonDataWithUndefinedDataLabel)
116 ).andReturn().response
117 then: 'response status is bad request'
118 response.status == HttpStatus.BAD_REQUEST.value()
121 def 'CmHandle search endpoint test #scenario.'() {
122 given: 'a query object'
123 def cmHandleQueryParameters = jsonObjectMapper.asJsonString(new CmHandleQueryParameters())
124 and: 'the mapper service returns a converted object'
125 ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
126 and: 'the service returns the desired results'
127 mockNetworkCmProxyInventoryFacade.executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters, _) >> serviceMockResponse
128 when: 'post request is performed & search is called with the given request parameters'
129 def response = mvc.perform(
130 post("$ncmpBasePathV1/ch/searches")
131 .contentType(MediaType.APPLICATION_JSON)
132 .content(cmHandleQueryParameters)
133 ).andReturn().response
134 then: 'response status is OK'
135 assert response.status == HttpStatus.OK.value()
136 and: 'the response data matches the service response.'
137 jsonObjectMapper.convertJsonString(response.getContentAsString(), List) == serviceMockResponse
138 where: 'the service respond with'
139 scenario | serviceMockResponse
140 'empty response' | []
141 'populates response' | ['cmHandle1', 'cmHandle2']
144 def 'CmHandle search endpoint test #scenario with blank cmHandleQueryParameters.'() {
145 given: 'a query object'
146 def cmHandleQueryParameters = "{}"
147 and: 'the mapper service returns a converted object'
148 ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
149 and: 'the service returns the desired results'
150 mockNetworkCmProxyInventoryFacade.executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters, _) >> serviceMockResponse
151 when: 'post request is performed & search is called with the given request parameters'
152 def response = mvc.perform(
153 post("$ncmpBasePathV1/ch/searches")
154 .contentType(MediaType.APPLICATION_JSON)
155 .content(cmHandleQueryParameters)
156 ).andReturn().response
157 then: 'response status is OK'
158 assert response.status == HttpStatus.OK.value()
159 and: 'the response data matches the service response.'
160 jsonObjectMapper.convertJsonString(response.getContentAsString(), List) == serviceMockResponse
161 where: 'the service respond with'
162 scenario | serviceMockResponse
163 'empty response' | []
164 'populates response' | ['cmHandle1', 'cmHandle2']
167 def 'CmHandle search endpoint Error Handling.'() {
168 given: 'the mapper service returns a converted object'
169 ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
170 and: 'the service returns the desired results'
171 mockNetworkCmProxyInventoryFacade.executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters) >> []
172 when: 'post request is performed & search is called with the given request parameters'
173 def response = mvc.perform(
174 post("$ncmpBasePathV1/ch/searches")
175 .contentType(MediaType.APPLICATION_JSON)
176 .content(cmHandleQueryParameters)
177 ).andReturn().response
178 then: 'response status is BAD_REQUEST'
179 assert response.status == HttpStatus.BAD_REQUEST.value()
180 where: 'the cmHandleQueryParameters are'
181 scenario | cmHandleQueryParameters
183 'non-json string' | "this is a test"
186 def 'DMI Registration: All cm-handles operations processed successfully.'() {
187 given: 'a dmi plugin registration'
188 def dmiRegistrationRequest = '{}'
189 and: 'service can register cm-handles successfully'
190 def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
191 createdCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1')],
192 updatedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-2')],
193 removedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-3')]
195 mockNetworkCmProxyInventoryFacade.updateDmiRegistration(*_) >> dmiRegistrationResponse
196 when: 'registration endpoint is invoked'
197 def response = mvc.perform(
198 post("$ncmpBasePathV1/ch")
199 .contentType(MediaType.APPLICATION_JSON)
200 .content(dmiRegistrationRequest)
201 ).andReturn().response
202 then: 'response status is ok'
203 response.status == HttpStatus.OK.value()
204 and: 'the response body is empty'
205 response.getContentAsString() == ''
209 def 'DMI Registration Error Handling: #scenario.'() {
210 given: 'a dmi plugin registration'
211 def dmiRegistrationRequest = '{}'
212 and: '#scenario: service failed to register few cm-handle'
213 def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
214 createdCmHandles: [createCmHandleResponse],
215 updatedCmHandles: [updateCmHandleResponse],
216 removedCmHandles: [removeCmHandleResponse],
217 upgradedCmHandles: [upgradeCmHandleResponse]
219 mockNetworkCmProxyInventoryFacade.updateDmiRegistration(*_) >> dmiRegistrationResponse
220 when: 'registration endpoint is invoked'
221 def response = mvc.perform(
222 post("$ncmpBasePathV1/ch")
223 .contentType(MediaType.APPLICATION_JSON)
224 .content(dmiRegistrationRequest)
225 ).andReturn().response
226 then: 'request status is internal server error'
227 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
228 and: 'the response body is in the expected format'
229 def responseBody = jsonObjectMapper.convertJsonString(response.getContentAsString(), DmiPluginRegistrationErrorResponse)
230 and: 'contains only the failure responses'
231 responseBody.getFailedCreatedCmHandles() == expectedFailedCreatedCmHandle
232 responseBody.getFailedUpdatedCmHandles() == expectedFailedUpdateCmHandle
233 responseBody.getFailedRemovedCmHandles() == expectedFailedRemovedCmHandle
234 responseBody.getFailedUpgradeCmHandles() == expectedFailedUpgradedCmHandle
236 scenario | createCmHandleResponse | updateCmHandleResponse | removeCmHandleResponse | upgradeCmHandleResponse || expectedFailedCreatedCmHandle | expectedFailedUpdateCmHandle | expectedFailedRemovedCmHandle | expectedFailedUpgradedCmHandle
237 'only create failed' | expectedFailedResponse('cm-handle-1') | expectedSuccessResponse('cm-handle-2') | expectedSuccessResponse('cm-handle-3') | expectedSuccessResponse('cm-handle-4') || [expectedUnknownErrorResponse('cm-handle-1')] | [] | [] | []
238 'only update failed' | expectedSuccessResponse('cm-handle-1') | expectedFailedResponse('cm-handle-2') | expectedSuccessResponse('cm-handle-3') | expectedSuccessResponse('cm-handle-4') || [] | [expectedUnknownErrorResponse('cm-handle-2')] | [] | []
239 'only delete failed' | expectedSuccessResponse('cm-handle-1') | expectedSuccessResponse('cm-handle-2') | expectedFailedResponse('cm-handle-3') | expectedSuccessResponse('cm-handle-4') || [] | [] | [expectedUnknownErrorResponse('cm-handle-3')] | []
240 'only upgrade failed' | expectedSuccessResponse('cm-handle-1') | expectedSuccessResponse('cm-handle-2') | expectedSuccessResponse('cm-handle-3') | expectedFailedResponse('cm-handle-4') || [] | [] | [] | [expectedUnknownErrorResponse('cm-handle-4')]
241 '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')]
242 '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')] | [] | []
243 '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')] | []
244 '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')] | []
245 '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')]
248 def 'Get all cm handle references by DMI plugin identifier when #scenario.'() {
249 given: 'an endpoint for returning cm handle references for a registered dmi plugin'
250 def getUrl = "$ncmpBasePathV1/ch/cmHandles?dmi-plugin-identifier=some-dmi-plugin-identifier"+outputAlternateId
251 and: 'a collection of cm handle references are returned'
252 mockNetworkCmProxyInventoryFacade.getAllCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', false)
253 >> ['cm-handle-id-1','cm-handle-id-2']
254 mockNetworkCmProxyInventoryFacade.getAllCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', true)
255 >> ['alternate-id-1','alternate-id-2']
256 when: 'the endpoint is invoked'
257 def response = mvc.perform(
259 .contentType(MediaType.APPLICATION_JSON)
260 .accept(MediaType.APPLICATION_JSON_VALUE)
261 ).andReturn().response
262 then: 'the response matches the result returned by the service layer'
263 assert response.contentAsString.contains(firstReference)
264 assert response.contentAsString.contains(secondReference)
266 scenario | outputAlternateId || firstReference | secondReference
267 'output returns cm handle ids' | '' || 'cm-handle-id-1' | 'cm-handle-id-2'
268 'output returns alternate ids' | '&outputAlternateId=true' || 'alternate-id-1' | 'alternate-id-2'
271 def 'Get a cm handle by DMI service name.'() {
272 given: 'an endpoint for returning cm handles by dmi service name'
273 def postUrl = "$ncmpBasePathV1/ch/searchCmHandles?includePrivatePropertiesInQuery=true"
274 String jsonString = TestUtils.getResourceFileContent('cm-handle-search-by-dmi-service.json')
275 and: 'a cm handle is returned'
276 def ncmpServiceCmHandle = new NcmpServiceCmHandle(dmiProperties: ['someName': 'my dmi'])
277 mockNetworkCmProxyInventoryFacade.executeCmHandleInventorySearch(_) >> Flux.fromIterable([ncmpServiceCmHandle])
278 and: 'the mapper is requested to convert the object with private properties'
279 mockRestOutputCmHandleMapper.toRestOutputCmHandle(ncmpServiceCmHandle, true) >> new RestOutputCmHandle()
280 when: 'the endpoint is invoked'
281 def response = mvc.perform(post(postUrl).contentType(MediaType.APPLICATION_JSON).content(jsonString)).andReturn().response
282 then: 'a response status is OK'
283 assert response.status == 200
286 def expectedUnknownErrorResponse(cmHandle) {
287 return new CmHandlerRegistrationErrorResponse('cmHandle': cmHandle, 'errorCode': '108', 'errorText': 'Failed')
290 def expectedFailedResponse(cmHandle) {
291 return CmHandleRegistrationResponse.createFailureResponse(cmHandle, new RuntimeException('Failed'))
294 def expectedSuccessResponse(cmHandle) {
295 return CmHandleRegistrationResponse.createSuccessResponse(cmHandle)