b954b41ef2aee8853e0c1898c2c84081022d5f90
[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.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
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
56     def noTimestamp = null
57
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.cmHandleProperties = [name1: 'value1', name2: 'value2']
64             dmiPluginRegistration.createdCmHandles = createdCmHandles
65             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
66             dmiPluginRegistration.removedCmHandles = removedCmHandles
67             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,"additional-properties":[{"name":"name1","value":"value1"},{"name":"name2","value":"value2"}]}]}'
68         when: 'registration is updated and modules are synced'
69             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
70         then: 'save list elements is invoked with the expected parameters'
71             expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
72                     '/dmi-registry', expectedJsonData, noTimestamp)
73         and: 'update node and child data nodes is invoked with correct parameters'
74             expectedCallsToUpdateNode * mockCpsDataService.updateNodeLeavesAndExistingDescendantLeaves('NCMP-Admin',
75                     'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, noTimestamp)
76         and: 'delete schema set is invoked with the correct parameters'
77             expectedCallsToDeleteSchemaSetAndListElement * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'cmHandle001', CASCADE_DELETE_ALLOWED)
78         and: 'delete list or list element is invoked with the correct parameters'
79             expectedCallsToDeleteSchemaSetAndListElement * mockCpsDataService.deleteListOrListElement('NCMP-Admin',
80                     'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
81         where:
82             scenario                    | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode | expectedCallsToUpdateNode | expectedCallsToDeleteSchemaSetAndListElement
83             'create'                    | [persistenceCmHandle] | []                    | []               || 1                       | 0                         | 0
84             'update'                    | []                    | [persistenceCmHandle] | []               || 0                       | 1                         | 0
85             'delete'                    | []                    | []                    | cmHandlesArray   || 0                       | 0                         | 1
86             'create, update and delete' | [persistenceCmHandle] | [persistenceCmHandle] | cmHandlesArray   || 1                       | 1                         | 1
87             'no valid data'             | null                  | null                  | null             || 0                       | 0                         | 0
88     }
89
90     def 'Register a DMI Plugin for the given cm-handle(s) without additional properties.'() {
91         given: 'a registration without cm-handle properties'
92             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
93             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
94             persistenceCmHandle.cmHandleID = '123'
95             persistenceCmHandle.cmHandleProperties = null
96             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
97             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,"additional-properties":[]}]}'
98         when: 'registration is updated'
99             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
100         then: 'save list elements is invoked with the expected parameters'
101             1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
102                     '/dmi-registry', expectedJsonData, noTimestamp)
103     }
104
105     def 'Register a DMI Plugin for a given cm-handle(s) with JSON processing errors during #scenario process.'() {
106         given: 'a registration without cm-handle properties '
107             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
108             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
109             dmiPluginRegistration.createdCmHandles = createdCmHandles
110             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
111         and: 'an json processing exception occurs'
112             spiedJsonObjectMapper.asJsonString(_) >> { throw (new JsonProcessingException('')) }
113         when: 'registration is updated and modules are synced'
114             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
115         then: 'a data validation exception is thrown'
116             thrown(DataValidationException)
117         where:
118             scenario | createdCmHandles      | updatedCmHandles
119             'create' | [persistenceCmHandle] | []
120             'update' | []                    | [persistenceCmHandle]
121     }
122
123     def 'Register a DMI Plugin for the given cm-handle(s) with no data found during delete process.'() {
124         given: 'a registration without cm-handle properties '
125             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
126             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
127             dmiPluginRegistration.removedCmHandles = ['some cm handle']
128         and: 'an json processing exception occurs during delete process'
129             mockCpsDataService.deleteListOrListElement(*_) >>  { throw (new DataNodeNotFoundException('','')) }
130         when: 'registration is updated and modules are synced'
131             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
132         then: 'no exception is thrown'
133             noExceptionThrown()
134     }
135
136     def 'Register a DMI Plugin for the given cm-handle(s) with no schema set found during delete process.'() {
137         given: 'a registration'
138             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
139             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
140             dmiPluginRegistration.removedCmHandles = cmHandlesArray
141         and: 'an exception occurs during delete schema set process'
142             mockCpsModuleService.deleteSchemaSet(_,_,_) >>  { throw (new Exception('')) }
143         when: 'registration is updated and modules are synced'
144             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
145         then: 'delete list or list element is still called'
146             1 * mockCpsDataService.deleteListOrListElement(_,_,_,_)
147     }
148
149     def 'Dmi plugin registration with #scenario'() {
150         given: 'a registration '
151             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
152             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
153                     dmiDataPlugin:dmiDataPlugin)
154             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
155         when: 'update registration and sync module is called with correct DMI plugin information'
156             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
157         then: 'create cm handles registration and sync modules is called with the correct plugin information'
158             1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
159         where:
160             scenario                          | dmiPlugin  | dmiModelPlugin | dmiDataPlugin
161             'combined DMI plugin'             | 'service1' | ''             | ''
162             'data & model DMI plugins'        | ''         | 'service1'     | 'service2'
163             'data & model using same service' | ''         | 'service1'     | 'service1'
164     }
165
166     def 'Invalid dmi plugin registration with #scenario'() {
167         given: 'a registration '
168             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
169             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
170                     dmiDataPlugin:dmiDataPlugin)
171             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
172         when: 'registration is called with incorrect DMI plugin information'
173             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
174         then: 'an NcmpException is thrown with correct message details'
175             def exceptionThrown = thrown(NcmpException)
176             assert exceptionThrown.getMessage().contains(expectedMessageDetails)
177         and: 'registration is not called'
178             0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
179         where:
180             scenario                        | dmiPlugin  | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
181             'empty DMI plugins'             | ''         | ''             | ''            || 'No DMI plugin service names'
182             'blank DMI plugins'             | ' '        | ' '            | ' '           || 'No DMI plugin service names'
183             'null DMI plugins'              | null       | null           | null          || 'No DMI plugin service names'
184             'all DMI plugins'               | 'service1' | 'service2'     | 'service3'    || 'Cannot register combined plugin service name and other service names'
185             '(combined)DMI and Data Plugin' | 'service1' | ''             | 'service2'    || 'Cannot register combined plugin service name and other service names'
186             '(combined)DMI and model Plugin'| 'service1' | 'service2'     | ''            || 'Cannot register combined plugin service name and other service names'
187             'only model DMI plugin'         | ''         | 'service1'     | ''            || 'Cannot register just a Data or Model plugin service name'
188             'only data DMI plugin'          | ''         | ''             | 'service1'    || 'Cannot register just a Data or Model plugin service name'
189     }
190
191     def getObjectUnderTestWithModelSyncDisabled() {
192         def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
193                 mockCpsModuleService, mockCpsAdminService))
194         objectUnderTest.syncModulesAndCreateAnchor(*_) >> null
195         return objectUnderTest
196     }
197 }