Create Endpoint For Get Cm Handles By Name
[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.impl.operations.YangModelCmHandleRetriever
32 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
33 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
35 import org.onap.cps.spi.exceptions.DataValidationException
36 import org.onap.cps.utils.JsonObjectMapper
37 import spock.lang.Shared
38 import spock.lang.Specification
39
40 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
41
42 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
43
44     @Shared
45     def ncmpServiceCmHandle = new NcmpServiceCmHandle()
46
47     @Shared
48     def cmHandlesArray = ['cmHandle001']
49
50     def mockCpsDataService = Mock(CpsDataService)
51     def mockCpsModuleService = Mock(CpsModuleService)
52     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
53     def mockCpsAdminService = Mock(CpsAdminService)
54     def mockDmiModelOperations = Mock(DmiModelOperations)
55     def mockDmiDataOperations = Mock(DmiDataOperations)
56     def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
57     def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
58
59     def noTimestamp = null
60
61     def 'Register or re-register a DMI Plugin for the given cm-handle(s) with #scenario process.'() {
62         given: 'a registration'
63             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
64             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
65             ncmpServiceCmHandle.cmHandleID = '123'
66             ncmpServiceCmHandle.dmiProperties = [dmiProp1: 'dmiValue1', dmiProp2: 'dmiValue2']
67             ncmpServiceCmHandle.publicProperties = [publicProp1: 'publicValue1', publicProp2: 'publicValue2' ]
68             dmiPluginRegistration.createdCmHandles = createdCmHandles
69             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
70             dmiPluginRegistration.removedCmHandles = removedCmHandles
71             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,' +
72                 '"additional-properties":[{"name":"dmiProp1","value":"dmiValue1"},{"name":"dmiProp2","value":"dmiValue2"}],' +
73                 '"public-properties":[{"name":"publicProp1","value":"publicValue1"},{"name":"publicProp2","value":"publicValue2"}]' +
74                 '}]}'
75         when: 'registration is updated and modules are synced'
76             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
77         then: 'save list elements is invoked with the expected parameters'
78             expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
79                 '/dmi-registry', expectedJsonData, noTimestamp)
80         and: 'update data node leaves is called with correct parameters'
81             expectedCallsToUpdateCmHandleProperty * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(updatedCmHandles)
82         and: 'delete schema set is invoked with the correct parameters'
83             expectedCallsToDeleteSchemaSetAndListElement * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'cmHandle001', CASCADE_DELETE_ALLOWED)
84         and: 'delete list or list element is invoked with the correct parameters'
85             expectedCallsToDeleteSchemaSetAndListElement * mockCpsDataService.deleteListOrListElement('NCMP-Admin',
86                     'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
87         where:
88             scenario                    | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode | expectedCallsToDeleteSchemaSetAndListElement | expectedCallsToUpdateCmHandleProperty
89             'create'                    | [ncmpServiceCmHandle] | []                    | []               || 1                       | 0                                            | 0
90             'update'                    | []                    | [ncmpServiceCmHandle] | []               || 0                       | 0                                            | 1
91             'delete'                    | []                    | []                    | cmHandlesArray   || 0                       | 1                                            | 0
92             'create, update and delete' | [ncmpServiceCmHandle] | [ncmpServiceCmHandle] | cmHandlesArray   || 1                       | 1                                            | 1
93             'no valid data'             | []                    | []                    | []               || 0                       | 0                                            | 0
94     }
95
96     def 'Register a DMI Plugin for the given cm-handle(s) without DMI properties.'() {
97         given: 'a registration without cm-handle properties'
98             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
99             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
100             ncmpServiceCmHandle.cmHandleID = '123'
101             ncmpServiceCmHandle.dmiProperties = Collections.emptyMap()
102             ncmpServiceCmHandle.publicProperties = Collections.emptyMap()
103             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
104             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":[]}]}'
105         when: 'registration is updated'
106             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
107         then: 'save list elements is invoked with the expected parameters'
108             1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
109                     '/dmi-registry', expectedJsonData, noTimestamp)
110     }
111
112     def 'Register a DMI Plugin for a given cm-handle(s) with JSON processing errors during process.'() {
113         given: 'a registration without cm-handle properties '
114             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
115             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
116             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
117         and: 'an json processing exception occurs'
118             spiedJsonObjectMapper.asJsonString(_) >> { throw (new JsonProcessingException('')) }
119         when: 'registration is updated and modules are synced'
120             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
121         then: 'a data validation exception is thrown'
122             thrown(DataValidationException)
123     }
124
125     def 'Register a DMI Plugin for the given cm-handle(s) with no data found during delete process.'() {
126         given: 'a registration without cm-handle properties '
127             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
128             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
129             dmiPluginRegistration.removedCmHandles = ['some cm handle']
130         and: 'an json processing exception occurs during delete process'
131             mockCpsDataService.deleteListOrListElement(*_) >>  { throw (new DataNodeNotFoundException('','')) }
132         when: 'registration is updated and modules are synced'
133             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
134         then: 'no exception is thrown'
135             noExceptionThrown()
136     }
137
138     def 'Register a DMI Plugin for the given cm-handle(s) with no schema set found during delete process.'() {
139         given: 'a registration'
140             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
141             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
142             dmiPluginRegistration.removedCmHandles = cmHandlesArray
143         and: 'an exception occurs during delete schema set process'
144             mockCpsModuleService.deleteSchemaSet(_,_,_) >>  { throw (new Exception('')) }
145         when: 'registration is updated and modules are synced'
146             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
147         then: 'delete list or list element is still called'
148             1 * mockCpsDataService.deleteListOrListElement(_,_,_,_)
149     }
150
151     def 'Dmi plugin registration with #scenario'() {
152         given: 'a registration '
153             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
154             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
155                     dmiDataPlugin:dmiDataPlugin)
156             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
157         when: 'update registration and sync module is called with correct DMI plugin information'
158             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
159         then: 'create cm handles registration and sync modules is called with the correct plugin information'
160             1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
161         where:
162             scenario                          | dmiPlugin  | dmiModelPlugin | dmiDataPlugin
163             'combined DMI plugin'             | 'service1' | ''             | ''
164             'data & model DMI plugins'        | ''         | 'service1'     | 'service2'
165             'data & model using same service' | ''         | 'service1'     | 'service1'
166     }
167
168     def 'Invalid DMI plugin registration with #scenario'() {
169         given: 'a registration '
170             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
171             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
172                     dmiDataPlugin:dmiDataPlugin)
173             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
174         when: 'registration is called with incorrect DMI plugin information'
175             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
176         then: 'a DMI Request Exception is thrown with correct message details'
177             def exceptionThrown = thrown(DmiRequestException.class)
178             assert exceptionThrown.getMessage().contains(expectedMessageDetails)
179         and: 'registration is not called'
180             0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
181         where:
182             scenario                        | dmiPlugin  | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
183             'empty DMI plugins'             | ''         | ''             | ''            || 'No DMI plugin service names'
184             'blank DMI plugins'             | ' '        | ' '            | ' '           || 'No DMI plugin service names'
185             'null DMI plugins'              | null       | null           | null          || 'No DMI plugin service names'
186             'all DMI plugins'               | 'service1' | 'service2'     | 'service3'    || 'Cannot register combined plugin service name and other service names'
187             '(combined)DMI and Data Plugin' | 'service1' | ''             | 'service2'    || 'Cannot register combined plugin service name and other service names'
188             '(combined)DMI and model Plugin'| 'service1' | 'service2'     | ''            || 'Cannot register combined plugin service name and other service names'
189             'only model DMI plugin'         | ''         | 'service1'     | ''            || 'Cannot register just a Data or Model plugin service name'
190             'only data DMI plugin'          | ''         | ''             | 'service1'    || 'Cannot register just a Data or Model plugin service name'
191     }
192
193     def 'Exception thrown on CM-Handle registration update request'() {
194         given: 'a CM-handle registration'
195             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
196         and: 'dmi plugin registration input update request'
197             def dmiPluginReg = new DmiPluginRegistration();
198             dmiPluginReg.dmiPlugin = 'onap.dmap.plugin';
199             dmiPluginReg.updatedCmHandles = [new NcmpServiceCmHandle(cmHandleID: 'unknownHandle')]
200         and: 'update data node leaves is unable to find data node'
201             mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> { throw new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') }
202         when: 'update dmi registration is called'
203             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginReg)
204         then: 'data validation exception is thrown'
205             def exceptionThrown = thrown(DataValidationException.class)
206             assert exceptionThrown.getDetails().contains('DataNode not found')
207     }
208
209     def getObjectUnderTestWithModelSyncDisabled() {
210         def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
211                 mockCpsModuleService, mockCpsAdminService, mockNetworkCmProxyDataServicePropertyHandler,mockYangModelCmHandleRetriever))
212         objectUnderTest.syncModulesAndCreateAnchor(*_) >> null
213         return objectUnderTest
214     }
215 }