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