41084541c2a1a7d5cf5d14feff53daa1b6942e4d
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplRegistrationSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.CpsDataService
26 import org.onap.cps.api.CpsModuleService
27 import org.onap.cps.ncmp.api.impl.exception.NcmpException
28 import org.onap.cps.ncmp.api.models.CmHandle
29 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
30 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
31 import org.onap.cps.spi.exceptions.DataValidationException
32 import spock.lang.Shared
33 import spock.lang.Specification
34
35 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
36
37 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
38
39     @Shared
40     def persistenceCmHandle = new CmHandle()
41
42     @Shared
43     def cmHandlesArray = ['cmHandle001']
44
45     def mockCpsDataService = Mock(CpsDataService)
46     def mockCpsModuleService = Mock(CpsModuleService)
47     def spyObjectMapper = Spy(ObjectMapper)
48
49     def noTimestamp = null
50
51     def 'Register or re-register a DMI Plugin for the given cm-handle(s) with #scenario process.'() {
52         given: 'a registration'
53             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
54             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
55             persistenceCmHandle.cmHandleID = '123'
56             persistenceCmHandle.cmHandleProperties = [name1: 'value1', name2: 'value2']
57             dmiPluginRegistration.createdCmHandles = createdCmHandles
58             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
59             dmiPluginRegistration.removedCmHandles = removedCmHandles
60             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"}]}]}'
61         when: 'registration is updated and modules are synced'
62             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
63         then: 'save list elements is invoked with the expected parameters'
64             expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
65                     '/dmi-registry', expectedJsonData, noTimestamp)
66         and: 'update node and child data nodes is invoked with correct parameters'
67             expectedCallsToUpdateNode * mockCpsDataService.updateNodeLeavesAndExistingDescendantLeaves('NCMP-Admin',
68                     'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, noTimestamp)
69         and: 'delete schema set is invoked with the correct parameters'
70             expectedCallsToDeleteSchemaSetAndListElement * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'cmHandle001', CASCADE_DELETE_ALLOWED)
71         and: 'delete list or list element is invoked with the correct parameters'
72             expectedCallsToDeleteSchemaSetAndListElement * mockCpsDataService.deleteListOrListElement('NCMP-Admin',
73                     'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
74         where:
75             scenario                    | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode | expectedCallsToUpdateNode | expectedCallsToDeleteSchemaSetAndListElement
76             'create'                    | [persistenceCmHandle] | []                    | []               || 1                       | 0                         | 0
77             'update'                    | []                    | [persistenceCmHandle] | []               || 0                       | 1                         | 0
78             'delete'                    | []                    | []                    | cmHandlesArray   || 0                       | 0                         | 1
79             'create, update and delete' | [persistenceCmHandle] | [persistenceCmHandle] | cmHandlesArray   || 1                       | 1                         | 1
80             'no valid data'             | null                  | null                  | null             || 0                       | 0                         | 0
81     }
82
83     def 'Register a DMI Plugin for the given cm-handle(s) without additional properties.'() {
84         given: 'a registration without cm-handle properties'
85             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
86             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
87             persistenceCmHandle.cmHandleID = '123'
88             persistenceCmHandle.cmHandleProperties = null
89             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
90             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,"additional-properties":[]}]}'
91         when: 'registration is updated'
92             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
93         then: 'save list elements is invoked with the expected parameters'
94             1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
95                     '/dmi-registry', expectedJsonData, noTimestamp)
96     }
97
98     def 'Register a DMI Plugin for a given cm-handle(s) with JSON processing errors during #scenario process.'() {
99         given: 'a registration without cm-handle properties '
100             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
101             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
102             dmiPluginRegistration.createdCmHandles = createdCmHandles
103             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
104         and: 'an json processing exception occurs'
105             spyObjectMapper.writeValueAsString(_) >> { throw (new JsonProcessingException('')) }
106         when: 'registration is updated and modules are synced'
107             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
108         then: 'a data validation exception is thrown'
109             thrown(DataValidationException)
110         where:
111             scenario | createdCmHandles      | updatedCmHandles
112             'create' | [persistenceCmHandle] | []
113             'update' | []                    | [persistenceCmHandle]
114     }
115
116     def 'Register a DMI Plugin for the given cm-handle(s) with no data found during delete process.'() {
117         given: 'a registration without cm-handle properties '
118             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
119             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
120             dmiPluginRegistration.removedCmHandles = ['some cm handle']
121         and: 'an json processing exception occurs during delete process'
122             mockCpsDataService.deleteListOrListElement(*_) >>  { throw (new DataNodeNotFoundException('','')) }
123         when: 'registration is updated and modules are synced'
124             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
125         then: 'no exception is thrown'
126             noExceptionThrown()
127     }
128
129     def 'Dmi plugin registration with #scenario'() {
130         given: 'a registration '
131             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
132             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
133                     dmiDataPlugin:dmiDataPlugin)
134             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
135         when: 'update registration and sync module is called with correct DMI plugin information'
136             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
137         then: 'create cm handles registration and sync modules is called with the correct plugin information'
138             1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
139         where:
140             scenario                          | dmiPlugin  | dmiModelPlugin | dmiDataPlugin
141             'combined DMI plugin'             | 'service1' | ''             | ''
142             'data & model DMI plugins'        | ''         | 'service1'     | 'service2'
143             'data & model using same service' | ''         | 'service1'     | 'service1'
144     }
145
146     def 'Invalid dmi plugin registration with #scenario'() {
147         given: 'a registration '
148             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
149             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
150                     dmiDataPlugin:dmiDataPlugin)
151             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
152         when: 'registration is called with incorrect DMI plugin information'
153             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
154         then: 'an NcmpException is thrown with correct message details'
155             def exceptionThrown = thrown(NcmpException)
156             assert exceptionThrown.getMessage().contains(expectedMessageDetails)
157         and: 'registration is not called'
158             0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
159         where:
160             scenario              | dmiPlugin  | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
161             'no DMI plugin'       | ''         | ''             | ''            || 'No DMI plugin service names'
162             'all DMI plugins'     | 'service1' | 'service2'     | 'service3'    || 'Invalid combination of plugin service names'
163             'no model DMI plugin' | 'service1' | ''             | 'service2'    || 'Invalid combination of plugin service names'
164             'no data DMI plugin'  | 'service1' | 'service2'     | ''            || 'Invalid combination of plugin service names'
165     }
166
167     def getObjectUnderTestWithModelSyncDisabled() {
168         def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(null, null, mockCpsModuleService,
169                 mockCpsDataService, null, null, spyObjectMapper))
170         objectUnderTest.syncModulesAndCreateAnchor(*_) >> null
171         return objectUnderTest
172     }
173 }