2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
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.api.impl
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.api.CpsModuleService
26 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
27 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
28 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
31 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
32 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
33 import org.onap.cps.spi.exceptions.AlreadyDefinedException
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
35 import org.onap.cps.spi.exceptions.DataValidationException
36 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
37 import org.onap.cps.utils.JsonObjectMapper
38 import spock.lang.Shared
39 import spock.lang.Specification
41 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST
42 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_ALREADY_EXIST
43 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_INVALID_ID
44 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.UNKNOWN_ERROR
45 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
46 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
48 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
51 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
53 def mockCpsModuleService = Mock(CpsModuleService)
54 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
55 def mockDmiDataOperations = Mock(DmiDataOperations)
56 def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
57 def mockInventoryPersistence = Mock(InventoryPersistence)
58 def stubbedNetworkCmProxyCmHandlerQueryService = Stub(NetworkCmProxyCmHandlerQueryService)
59 def objectUnderTest = getObjectUnderTest()
61 def 'DMI Registration: Create, Update & Delete operations are processed in the right order'() {
62 given: 'a registration with operations of all three types'
63 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
64 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
65 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
66 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
67 when: 'registration is processed'
68 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
69 // Spock validated invocation order between multiple then blocks
70 then: 'cm-handles are removed first'
71 1 * objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_)
72 then: 'cm-handles are created'
73 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_)
74 then: 'cm-handles are updated'
75 1 * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_)
78 def 'DMI Registration: Response from all operations types are in response'() {
79 given: 'a registration with operations of all three types'
80 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
81 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
82 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
83 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
84 and: 'update cm-handles can be processed successfully'
85 def updateResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-2')]
86 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> updateResponses
87 and: 'create cm-handles can be processed successfully'
88 def createdResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-1')]
89 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_) >> createdResponses
90 and: 'delete cm-handles can be processed successfully'
91 def removeResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-3')]
92 objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_) >> removeResponses
93 when: 'registration is processed'
94 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
95 then: 'response has values from all operations'
96 response.getRemovedCmHandles() == removeResponses
97 response.getCreatedCmHandles() == createdResponses
98 response.getUpdatedCmHandles() == updateResponses
101 def 'Create CM-handle Validation: Registration with valid Service names: #scenario'() {
102 given: 'a registration '
103 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
104 dmiDataPlugin: dmiDataPlugin)
105 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
106 when: 'update registration and sync module is called with correct DMI plugin information'
107 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
108 then: 'create cm handles registration and sync modules is called with the correct plugin information'
109 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
111 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin
112 'combined DMI plugin' | 'service1' | '' | ''
113 'data & model DMI plugins' | '' | 'service1' | 'service2'
114 'data & model using same service' | '' | 'service1' | 'service1'
117 def 'Create CM-handle Validation: Invalid DMI plugin service name with #scenario'() {
118 given: 'a registration '
119 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
120 dmiDataPlugin: dmiDataPlugin)
121 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
122 when: 'registration is called with incorrect DMI plugin information'
123 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
124 then: 'a DMI Request Exception is thrown with correct message details'
125 def exceptionThrown = thrown(DmiRequestException.class)
126 assert exceptionThrown.getMessage().contains(expectedMessageDetails)
127 and: 'registration is not called'
128 0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
130 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
131 'empty DMI plugins' | '' | '' | '' || 'No DMI plugin service names'
132 'blank DMI plugins' | ' ' | ' ' | ' ' || 'No DMI plugin service names'
133 'null DMI plugins' | null | null | null || 'No DMI plugin service names'
134 'all DMI plugins' | 'service1' | 'service2' | 'service3' || 'Cannot register combined plugin service name and other service names'
135 '(combined)DMI and Data Plugin' | 'service1' | '' | 'service2' || 'Cannot register combined plugin service name and other service names'
136 '(combined)DMI and model Plugin' | 'service1' | 'service2' | '' || 'Cannot register combined plugin service name and other service names'
137 'only model DMI plugin' | '' | 'service1' | '' || 'Cannot register just a Data or Model plugin service name'
138 'only data DMI plugin' | '' | '' | 'service1' || 'Cannot register just a Data or Model plugin service name'
141 def 'Create CM-Handle Successfully: #scenario.'() {
142 given: 'a registration without cm-handle properties'
143 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
144 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle', dmiProperties: dmiProperties, publicProperties: publicProperties)]
145 when: 'registration is updated'
146 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
147 then: 'a successful response is received'
148 response.getCreatedCmHandles().size() == 1
149 with(response.getCreatedCmHandles().get(0)) {
150 assert it.status == Status.SUCCESS
151 assert it.cmHandle == 'cmhandle'
153 and: 'save list elements is invoked once with the expected parameters'
154 1 * mockInventoryPersistence.saveListElements(_) >> {
156 assert args[0].startsWith('{"cm-handles":[{"id":"cmhandle","dmi-service-name":"my-server","state":{"cm-handle-state":"ADVISED","last-update-time":"20')
160 scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties
161 'with dmi & public properties' | ['dmi-key': 'dmi-value'] | ['public-key': 'public-value'] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[{"name":"public-key","value":"public-value"}]'
162 'with only public properties' | [:] | ['public-key': 'public-value'] || '[]' | '[{"name":"public-key","value":"public-value"}]'
163 'with only dmi properties' | ['dmi-key': 'dmi-value'] | [:] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[]'
164 'without dmi & public properties' | [:] | [:] || '[]' | '[]'
168 def 'Create CM-Handle Multiple Requests: All cm-handles creation requests are processed'() {
169 given: 'a registration with three cm-handles to be created'
170 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
171 createdCmHandles: [new NcmpServiceCmHandle(cmHandleId: 'cmhandle1'),
172 new NcmpServiceCmHandle(cmHandleId: 'cmhandle2'),
173 new NcmpServiceCmHandle(cmHandleId: 'cmhandle3')])
174 and: 'cm-handle creation is successful for 1st and 3rd; failed for 2nd'
175 mockInventoryPersistence.saveListElements(_) >> {} >> { throw new RuntimeException("Failed") } >> {}
176 when: 'registration is updated to create cm-handles'
177 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
178 then: 'a response is received for all cm-handles'
179 response.getCreatedCmHandles().size() == 3
180 and: '1st and 3rd cm-handle are created successfully'
181 with(response.getCreatedCmHandles().get(0)) {
182 assert it.status == Status.SUCCESS
183 assert it.cmHandle == 'cmhandle1'
185 with(response.getCreatedCmHandles().get(2)) {
186 assert it.status == Status.SUCCESS
187 assert it.cmHandle == 'cmhandle3'
189 and: '2nd cm-handle creation fails'
190 with(response.getCreatedCmHandles().get(1)) {
191 assert it.status == Status.FAILURE
192 assert it.registrationError == UNKNOWN_ERROR
193 assert it.errorText == 'Failed'
194 assert it.cmHandle == 'cmhandle2'
198 def 'Create CM-Handle Error Handling: Registration fails: #scenario'() {
199 given: 'a registration without cm-handle properties'
200 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
201 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: cmHandleId)]
202 and: 'cm-handler registration fails: #scenario'
203 mockInventoryPersistence.saveListElements(_) >> { throw exception }
204 when: 'registration is updated'
205 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
206 then: 'a failure response is received'
207 response.getCreatedCmHandles().size() == 1
208 with(response.getCreatedCmHandles().get(0)) {
209 assert it.status == Status.FAILURE
210 assert it.cmHandle == cmHandleId
211 assert it.registrationError == expectedError
212 assert it.errorText == expectedErrorText
215 scenario | cmHandleId | exception || expectedError | expectedErrorText
216 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedException('', new RuntimeException()) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
217 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
218 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
221 def 'Update CM-Handle: Update Operation Response is added to the response'() {
222 given: 'a registration to update CmHandles'
223 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
224 updatedCmHandles: [{}])
225 and: 'cm-handle updates can be processed successfully'
226 def updateOperationResponse = [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1'),
227 CmHandleRegistrationResponse.createFailureResponse('cm-handle-2', new Exception("Failed")),
228 CmHandleRegistrationResponse.createFailureResponse('cm-handle-3', CM_HANDLE_DOES_NOT_EXIST),
229 CmHandleRegistrationResponse.createFailureResponse('cm handle 4', CM_HANDLE_INVALID_ID)]
230 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(_) >> updateOperationResponse
231 when: 'registration is updated'
232 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
233 then: 'the response contains updateOperationResponse'
234 assert response.getUpdatedCmHandles().size() == 4
235 assert response.getUpdatedCmHandles().containsAll(updateOperationResponse)
238 def 'Remove CmHandle Successfully: #scenario'() {
239 given: 'a registration'
240 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
241 removedCmHandles: ['cmhandle'])
243 mockCpsModuleService.deleteSchemaSet(_, 'cmhandle', CASCADE_DELETE_ALLOWED) >>
244 { if (!schemaSetExist) { throw new SchemaSetNotFoundException("", "") } }
245 when: 'registration is updated to delete cmhandle'
246 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
247 then: 'delete list or list element is called'
248 1 * mockInventoryPersistence.deleteListOrListElement(_)
249 and: 'successful response is received'
250 assert response.getRemovedCmHandles().size() == 1
251 with(response.getRemovedCmHandles().get(0)) {
252 assert it.status == Status.SUCCESS
253 assert it.cmHandle == 'cmhandle'
256 scenario | schemaSetExist
257 'schema-set exists and can be deleted successfully' | true
258 'schema-set does not exist' | false
261 def 'Remove CmHandle: All cm-handles delete requests are processed'() {
262 given: 'a registration with three cm-handles to be deleted'
263 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
264 removedCmHandles: ['cmhandle1', 'cmhandle2', 'cmhandle3'])
265 and: 'cm-handle deletion is successful for 1st and 3rd; failed for 2nd'
266 mockInventoryPersistence.deleteListOrListElement(_) >> {} >> { throw new RuntimeException("Failed") } >> {}
267 when: 'registration is updated to delete cmhandles'
268 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
269 then: 'a response is received for all cm-handles'
270 response.getRemovedCmHandles().size() == 3
271 and: '1st and 3rd cm-handle deletes successfully'
272 with(response.getRemovedCmHandles().get(0)) {
273 assert it.status == Status.SUCCESS
274 assert it.cmHandle == 'cmhandle1'
276 with(response.getRemovedCmHandles().get(2)) {
277 assert it.status == Status.SUCCESS
278 assert it.cmHandle == 'cmhandle3'
280 and: '2nd cm-handle deletion fails'
281 with(response.getRemovedCmHandles().get(1)) {
282 assert it.status == Status.FAILURE
283 assert it.registrationError == UNKNOWN_ERROR
284 assert it.errorText == 'Failed'
285 assert it.cmHandle == 'cmhandle2'
289 def 'Remove CmHandle Error Handling: Schema Set Deletion failed'() {
290 given: 'a registration'
291 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
292 removedCmHandles: ['cmhandle'])
293 and: 'schema set deletion failed with unknown error'
294 mockInventoryPersistence.deleteSchemaSetWithCascade(_) >> { throw new RuntimeException('Failed') }
295 when: 'registration is updated to delete cmhandle'
296 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
297 then: 'no exception is thrown'
299 and: 'cm-handle is not deleted'
300 0 * mockInventoryPersistence.deleteListOrListElement(_)
301 and: 'a failure response is received'
302 assert response.getRemovedCmHandles().size() == 1
303 with(response.getRemovedCmHandles().get(0)) {
304 assert it.status == Status.FAILURE
305 assert it.cmHandle == 'cmhandle'
306 assert it.errorText == 'Failed'
307 assert it.registrationError == UNKNOWN_ERROR
311 def 'Remove CmHandle Error Handling: #scenario'() {
312 given: 'a registration'
313 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
314 removedCmHandles: ['cmhandle'])
315 and: 'cm-handle deletion throws exception'
316 mockInventoryPersistence.deleteListOrListElement(_) >> { throw deleteListElementException }
317 when: 'registration is updated to delete cmhandle'
318 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
319 then: 'no exception is thrown'
321 and: 'a failure response is received'
322 assert response.getRemovedCmHandles().size() == 1
323 with(response.getRemovedCmHandles().get(0)) {
324 assert it.status == Status.FAILURE
325 assert it.cmHandle == 'cmhandle'
326 assert it.registrationError == expectedError
327 assert it.errorText == expectedErrorText
330 scenario | cmHandleId | deleteListElementException || expectedError | expectedErrorText
331 'cm-handle does not exist' | 'cmhandle' | new DataNodeNotFoundException("", "", "") || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
332 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
333 'an unexpected exception' | 'cmhandle' | new RuntimeException("Failed") || UNKNOWN_ERROR | 'Failed'
336 def getObjectUnderTest() {
337 return Spy(new NetworkCmProxyDataServiceImpl(spiedJsonObjectMapper, mockDmiDataOperations,
338 mockNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, stubbedNetworkCmProxyCmHandlerQueryService))