2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl
23 import com.fasterxml.jackson.core.JsonProcessingException
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.api.CpsAdminService
26 import org.onap.cps.api.CpsDataService
27 import org.onap.cps.api.CpsModuleService
28 import org.onap.cps.ncmp.api.impl.exception.NcmpException
29 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
30 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
31 import org.onap.cps.ncmp.api.models.CmHandle
32 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
33 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
34 import org.onap.cps.spi.exceptions.DataValidationException
35 import org.onap.cps.utils.JsonObjectMapper
36 import spock.lang.Shared
37 import spock.lang.Specification
39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
41 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
44 def persistenceCmHandle = new CmHandle()
47 def cmHandlesArray = ['cmHandle001']
49 def mockCpsDataService = Mock(CpsDataService)
50 def mockCpsModuleService = Mock(CpsModuleService)
51 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
52 def mockCpsAdminService = Mock(CpsAdminService)
53 def mockDmiModelOperations = Mock(DmiModelOperations)
54 def mockDmiDataOperations = Mock(DmiDataOperations)
56 def noTimestamp = null
58 def 'Register or re-register a DMI Plugin for the given cm-handle(s) with #scenario process.'() {
59 given: 'a registration'
60 def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
61 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
62 persistenceCmHandle.cmHandleID = '123'
63 persistenceCmHandle.dmiProperties = [dmiProp1: 'dmiValue1', dmiProp2: 'dmiValue2']
64 persistenceCmHandle.publicProperties = [publicProp1: 'publicValue1', publicProp2: 'publicValue2' ]
65 dmiPluginRegistration.createdCmHandles = createdCmHandles
66 dmiPluginRegistration.updatedCmHandles = updatedCmHandles
67 dmiPluginRegistration.removedCmHandles = removedCmHandles
68 def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,' +
69 '"additional-properties":[{"name":"dmiProp1","value":"dmiValue1"},{"name":"dmiProp2","value":"dmiValue2"}],' +
70 '"public-properties":[{"name":"publicProp1","value":"publicValue1"},{"name":"publicProp2","value":"publicValue2"}]' +
72 when: 'registration is updated and modules are synced'
73 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
74 then: 'save list elements is invoked with the expected parameters'
75 expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
76 '/dmi-registry', expectedJsonData, noTimestamp)
77 and: 'update node and child data nodes is invoked with correct parameters'
78 expectedCallsToUpdateNode * mockCpsDataService.updateNodeLeavesAndExistingDescendantLeaves('NCMP-Admin',
79 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, noTimestamp)
80 and: 'delete schema set is invoked with the correct parameters'
81 expectedCallsToDeleteSchemaSetAndListElement * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'cmHandle001', CASCADE_DELETE_ALLOWED)
82 and: 'delete list or list element is invoked with the correct parameters'
83 expectedCallsToDeleteSchemaSetAndListElement * mockCpsDataService.deleteListOrListElement('NCMP-Admin',
84 'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
86 scenario | createdCmHandles | updatedCmHandles | removedCmHandles || expectedCallsToSaveNode | expectedCallsToUpdateNode | expectedCallsToDeleteSchemaSetAndListElement
87 'create' | [persistenceCmHandle] | [] | [] || 1 | 0 | 0
88 'update' | [] | [persistenceCmHandle] | [] || 0 | 1 | 0
89 'delete' | [] | [] | cmHandlesArray || 0 | 0 | 1
90 'create, update and delete' | [persistenceCmHandle] | [persistenceCmHandle] | cmHandlesArray || 1 | 1 | 1
91 'no valid data' | null | null | null || 0 | 0 | 0
94 def 'Register a DMI Plugin for the given cm-handle(s) without DMI properties.'() {
95 given: 'a registration without cm-handle properties'
96 NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
97 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
98 persistenceCmHandle.cmHandleID = '123'
99 persistenceCmHandle.dmiProperties = Collections.emptyMap()
100 persistenceCmHandle.publicProperties = Collections.emptyMap()
101 dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
102 def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,"additional-properties":[],"public-properties":[]}]}'
103 when: 'registration is updated'
104 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
105 then: 'save list elements is invoked with the expected parameters'
106 1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
107 '/dmi-registry', expectedJsonData, noTimestamp)
110 def 'Register a DMI Plugin for a given cm-handle(s) with JSON processing errors during #scenario process.'() {
111 given: 'a registration without cm-handle properties '
112 NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
113 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
114 dmiPluginRegistration.createdCmHandles = createdCmHandles
115 dmiPluginRegistration.updatedCmHandles = updatedCmHandles
116 and: 'an json processing exception occurs'
117 spiedJsonObjectMapper.asJsonString(_) >> { throw (new JsonProcessingException('')) }
118 when: 'registration is updated and modules are synced'
119 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
120 then: 'a data validation exception is thrown'
121 thrown(DataValidationException)
123 scenario | createdCmHandles | updatedCmHandles
124 'create' | [persistenceCmHandle] | []
125 'update' | [] | [persistenceCmHandle]
128 def 'Register a DMI Plugin for the given cm-handle(s) with no data found during delete process.'() {
129 given: 'a registration without cm-handle properties '
130 NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
131 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
132 dmiPluginRegistration.removedCmHandles = ['some cm handle']
133 and: 'an json processing exception occurs during delete process'
134 mockCpsDataService.deleteListOrListElement(*_) >> { throw (new DataNodeNotFoundException('','')) }
135 when: 'registration is updated and modules are synced'
136 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
137 then: 'no exception is thrown'
141 def 'Register a DMI Plugin for the given cm-handle(s) with no schema set found during delete process.'() {
142 given: 'a registration'
143 def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
144 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
145 dmiPluginRegistration.removedCmHandles = cmHandlesArray
146 and: 'an exception occurs during delete schema set process'
147 mockCpsModuleService.deleteSchemaSet(_,_,_) >> { throw (new Exception('')) }
148 when: 'registration is updated and modules are synced'
149 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
150 then: 'delete list or list element is still called'
151 1 * mockCpsDataService.deleteListOrListElement(_,_,_,_)
154 def 'Dmi plugin registration with #scenario'() {
155 given: 'a registration '
156 def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
157 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
158 dmiDataPlugin:dmiDataPlugin)
159 dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
160 when: 'update registration and sync module is called with correct DMI plugin information'
161 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
162 then: 'create cm handles registration and sync modules is called with the correct plugin information'
163 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
165 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin
166 'combined DMI plugin' | 'service1' | '' | ''
167 'data & model DMI plugins' | '' | 'service1' | 'service2'
168 'data & model using same service' | '' | 'service1' | 'service1'
171 def 'Invalid DMI plugin registration with #scenario'() {
172 given: 'a registration '
173 def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
174 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
175 dmiDataPlugin:dmiDataPlugin)
176 dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
177 when: 'registration is called with incorrect DMI plugin information'
178 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
179 then: 'an NcmpException is thrown with correct message details'
180 def exceptionThrown = thrown(NcmpException)
181 assert exceptionThrown.getMessage().contains(expectedMessageDetails)
182 and: 'registration is not called'
183 0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
185 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
186 'empty DMI plugins' | '' | '' | '' || 'No DMI plugin service names'
187 'blank DMI plugins' | ' ' | ' ' | ' ' || 'No DMI plugin service names'
188 'null DMI plugins' | null | null | null || 'No DMI plugin service names'
189 'all DMI plugins' | 'service1' | 'service2' | 'service3' || 'Cannot register combined plugin service name and other service names'
190 '(combined)DMI and Data Plugin' | 'service1' | '' | 'service2' || 'Cannot register combined plugin service name and other service names'
191 '(combined)DMI and model Plugin'| 'service1' | 'service2' | '' || 'Cannot register combined plugin service name and other service names'
192 'only model DMI plugin' | '' | 'service1' | '' || 'Cannot register just a Data or Model plugin service name'
193 'only data DMI plugin' | '' | '' | 'service1' || 'Cannot register just a Data or Model plugin service name'
196 def getObjectUnderTestWithModelSyncDisabled() {
197 def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
198 mockCpsModuleService, mockCpsAdminService))
199 objectUnderTest.syncModulesAndCreateAnchor(*_) >> null
200 return objectUnderTest