00fda149f2f4e3b89f4231c4691b325e4636ac61
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplRegistrationSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl
22
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.DmiRequestException
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
38
39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
40
41 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
42
43     @Shared
44     def persistenceCmHandle = new CmHandle()
45
46     @Shared
47     def cmHandlesArray = ['cmHandle001']
48
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)
55     def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
56
57     def noTimestamp = null
58
59     def 'Register or re-register a DMI Plugin for the given cm-handle(s) with #scenario process.'() {
60         given: 'a registration'
61             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
62             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
63             persistenceCmHandle.cmHandleID = '123'
64             persistenceCmHandle.dmiProperties = [dmiProp1: 'dmiValue1', dmiProp2: 'dmiValue2']
65             persistenceCmHandle.publicProperties = [publicProp1: 'publicValue1', publicProp2: 'publicValue2']
66             dmiPluginRegistration.createdCmHandles = createdCmHandles
67             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
68             dmiPluginRegistration.removedCmHandles = removedCmHandles
69             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,' +
70                 '"additional-properties":[{"name":"dmiProp1","value":"dmiValue1"},{"name":"dmiProp2","value":"dmiValue2"}],' +
71                 '"public-properties":[{"name":"publicProp1","value":"publicValue1"},{"name":"publicProp2","value":"publicValue2"}]' +
72                 '}]}'
73         when: 'registration is updated and modules are synced'
74             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
75         then: 'save list elements is invoked with the expected parameters'
76             expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
77                 '/dmi-registry', expectedJsonData, noTimestamp)
78         and: 'update data node leaves is called with correct parameters'
79             expectedCallsToPropertyHandler * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(updatedCmHandles)
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)
85         where:
86             scenario                    | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode | expectedCallsToDeleteSchemaSetAndListElement | expectedCallsToPropertyHandler
87             'create'                    | [persistenceCmHandle] | []                    | []               || 1                       | 0                                            | 1
88             'update'                    | []                    | [persistenceCmHandle] | []               || 0                       | 0                                            | 1
89             'delete'                    | []                    | []                    | cmHandlesArray   || 0                       | 1                                            | 1
90             'create, update and delete' | [persistenceCmHandle] | [persistenceCmHandle] | cmHandlesArray   || 1                       | 1                                            | 1
91             'no valid data'             | null                  | null                  | null             || 0                       | 0                                            | 0
92     }
93
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)
108     }
109
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)
122         where:
123             scenario | createdCmHandles      | updatedCmHandles
124             'create' | [persistenceCmHandle] | []
125             'update' | []                    | [persistenceCmHandle]
126     }
127
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'
138             noExceptionThrown()
139     }
140
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(_,_,_,_)
152     }
153
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)
164         where:
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'
169     }
170
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: 'a DMI Request Exception is thrown with correct message details'
180             def exceptionThrown = thrown(DmiRequestException.class)
181             assert exceptionThrown.getMessage().contains(expectedMessageDetails)
182         and: 'registration is not called'
183             0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
184         where:
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'
194     }
195
196     def 'Exception thrown on CM-Handle registration update request'() {
197         given: 'a CM-handle registration'
198             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
199         and: 'dmi plugin registration input update request'
200             def dmiPluginReg = new DmiPluginRegistration();
201             dmiPluginReg.dmiPlugin = 'onap.dmap.plugin';
202             dmiPluginReg.updatedCmHandles = [new CmHandle(cmHandleID: 'unknownHandle')]
203         and: 'update data node leaves is unable to find data node'
204             mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> { throw new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') }
205         when: 'update dmi registration is called'
206             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginReg)
207         then: 'data validation exception is thrown'
208             def exceptionThrown = thrown(DataValidationException.class)
209             assert exceptionThrown.getDetails().contains('DataNode not found')
210     }
211
212     def getObjectUnderTestWithModelSyncDisabled() {
213         def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
214             mockCpsModuleService, mockCpsAdminService, mockNetworkCmProxyDataServicePropertyHandler))
215         objectUnderTest.syncModulesAndCreateAnchor(*_) >> null
216         return objectUnderTest
217     }
218 }