49d4c39a119fd6b88e6f2533998fa102865e667f
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Bell Canada
6  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.ncmp.impl.inventory
25
26 import com.fasterxml.jackson.databind.ObjectMapper
27 import org.onap.cps.ncmp.api.inventory.NetworkCmProxyInventoryFacade
28 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryApiParameters
29 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters
30 import org.onap.cps.ncmp.api.inventory.models.CompositeState
31 import org.onap.cps.ncmp.api.inventory.models.ConditionApiProperties
32 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
33 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
34 import org.onap.cps.ncmp.impl.inventory.models.CmHandleState
35 import org.onap.cps.ncmp.impl.inventory.models.LockReasonCategory
36 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
37 import org.onap.cps.spi.model.ConditionProperties
38 import org.onap.cps.utils.JsonObjectMapper
39 import spock.lang.Specification
40
41 import java.util.stream.Collectors
42
43 class NetworkCmProxyInventoryFacadeSpec extends Specification {
44
45     def mockCmHandleRegistrationService = Mock(CmHandleRegistrationService)
46     def mockCmHandleQueryService = Mock(CmHandleQueryService)
47     def mockParameterizedCmHandleQueryService = Mock(ParameterizedCmHandleQueryService)
48     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
49     def mockInventoryPersistence = Mock(InventoryPersistence)
50
51     def objectUnderTest = new NetworkCmProxyInventoryFacade(mockCmHandleRegistrationService, mockCmHandleQueryService, mockParameterizedCmHandleQueryService, mockInventoryPersistence, spiedJsonObjectMapper)
52
53     def 'Update DMI Registration'() {
54         given: 'an (updated) dmi plugin registration'
55             def dmiPluginRegistration = Mock(DmiPluginRegistration)
56         when: 'the registration is submitted '
57            objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
58         then: 'the call is delegated to the cm handle registration service'
59             1 * mockCmHandleRegistrationService.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
60     }
61
62     def 'Execute cm handle id search for inventory'() {
63         given: 'a ConditionApiProperties object'
64             def conditionProperties = new ConditionProperties()
65             conditionProperties.conditionName = 'hasAllProperties'
66             conditionProperties.conditionParameters = [ [ 'some-key' : 'some-value' ] ]
67             def cmHandleQueryServiceParameters = new CmHandleQueryServiceParameters()
68             cmHandleQueryServiceParameters.cmHandleQueryParameters = [conditionProperties] as List<ConditionProperties>
69         and: 'the system returns an set of cmHandle ids'
70             mockParameterizedCmHandleQueryService.queryCmHandleIdsForInventory(*_) >> [ 'cmHandle1', 'cmHandle2' ]
71         when: 'executing the search'
72             def result = objectUnderTest.executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters)
73         then: 'the result returns the correct 2 elements'
74             assert result.size() == 2
75             assert result.contains('cmHandle1')
76             assert result.contains('cmHandle2')
77     }
78
79     def 'Get all cm handle IDs by DMI plugin identifier.' () {
80         given: 'cm handle queries service returns cm handles'
81             1 * mockCmHandleQueryService.getCmHandleIdsByDmiPluginIdentifier('some-dmi-plugin-identifier') >> ['cm-handle-1','cm-handle-2']
82         when: 'cm handle Ids are requested with dmi plugin identifier'
83             def result = objectUnderTest.getAllCmHandleIdsByDmiPluginIdentifier('some-dmi-plugin-identifier')
84         then: 'the result size is correct'
85             assert result.size() == 2
86         and: 'the result returns the correct details'
87             assert result.containsAll('cm-handle-1','cm-handle-2')
88     }
89
90     def 'Getting Yang Resources.'() {
91         when: 'yang resources is called'
92             objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
93         then: 'CPS module services is invoked for the correct dataspace and cm handle'
94             1 * mockInventoryPersistence.getYangResourcesModuleReferences('some-cm-handle')
95     }
96
97     def 'Get a cm handle.'() {
98         given: 'the system returns a yang modelled cm handle'
99             def dmiServiceName = 'some service name'
100             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
101                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details("lock details").build(),
102                 lastUpdateTime: 'some-timestamp',
103                 dataSyncEnabled: false,
104                 dataStores: dataStores())
105             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
106             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
107             def moduleSetTag = 'some-module-set-tag'
108             def alternateId = 'some-alternate-id'
109             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName,
110                 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState,
111                 moduleSetTag: moduleSetTag, alternateId: alternateId)
112             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
113         when: 'getting cm handle details for a given cm handle id from ncmp service'
114             def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
115         then: 'the result is a ncmpServiceCmHandle'
116             result.class == NcmpServiceCmHandle.class
117         and: 'the cm handle contains the cm handle id'
118             result.cmHandleId == 'some-cm-handle'
119         and: 'the cm handle contains the alternate id'
120             result.alternateId == 'some-alternate-id'
121         and: 'the cm handle contains the module-set-tag'
122             result.moduleSetTag == 'some-module-set-tag'
123         and: 'the cm handle contains the DMI Properties'
124             result.dmiProperties ==[ Book:'Romance Novel' ]
125         and: 'the cm handle contains the public Properties'
126             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
127         and: 'the cm handle contains the cm handle composite state'
128             result.compositeState == compositeState
129     }
130
131     def 'Get cm handle public properties'() {
132         given: 'a yang modelled cm handle'
133             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
134             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
135             def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
136         and: 'the system returns this yang modelled cm handle'
137             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
138         when: 'getting cm handle public properties for a given cm handle id from ncmp service'
139             def result = objectUnderTest.getCmHandlePublicProperties('some-cm-handle')
140         then: 'the result returns the correct data'
141             result == [ 'public prop' : 'some public prop' ]
142     }
143
144     def 'Get cm handle composite state'() {
145         given: 'a yang modelled cm handle'
146             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
147                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details("lock details").build(),
148                 lastUpdateTime: 'some-timestamp',
149                 dataSyncEnabled: false,
150                 dataStores: dataStores())
151             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
152             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
153             def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
154         and: 'the system returns this yang modelled cm handle'
155             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
156         when: 'getting cm handle composite state for a given cm handle id from ncmp service'
157             def result = objectUnderTest.getCmHandleCompositeState('some-cm-handle')
158         then: 'the result returns the correct data'
159             result == compositeState
160     }
161
162     def 'Execute cm handle id search'() {
163         given: 'valid CmHandleQueryApiParameters input'
164             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
165             def conditionApiProperties = new ConditionApiProperties()
166             conditionApiProperties.conditionName = 'hasAllModules'
167             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
168             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
169         and: 'query cm handle method return with a data node list'
170             mockParameterizedCmHandleQueryService.queryCmHandleIds(
171                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
172                 >> ['cm-handle-id-1']
173         when: 'execute cm handle search is called'
174             def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters)
175         then: 'result is the same collection as returned by the CPS Data Service'
176             assert result == ['cm-handle-id-1']
177     }
178
179     def 'Getting module definitions by module'() {
180         when: 'get module definitions is performed with module name'
181             objectUnderTest.getModuleDefinitionsByCmHandleAndModule('some-cm-handle', 'some-module', '2021-08-04')
182         then: 'ncmp inventory persistence service is invoked once with correct parameters'
183             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleAndModule('some-cm-handle', 'some-module', '2021-08-04')
184     }
185
186     def 'Getting module definitions by cm handle id'() {
187         when: 'get module definitions is performed with cm handle id'
188             objectUnderTest.getModuleDefinitionsByCmHandleId('some-cm-handle')
189         then: 'ncmp inventory persistence service is invoked once with correct parameter'
190             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleId('some-cm-handle')
191     }
192
193     def 'Execute cm handle search'() {
194         given: 'valid CmHandleQueryApiParameters input'
195             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
196             def conditionApiProperties = new ConditionApiProperties()
197             conditionApiProperties.conditionName = 'hasAllModules'
198             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
199             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
200         and: 'query cm handle method return with a data node list'
201             mockParameterizedCmHandleQueryService.queryCmHandles(
202                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
203                 >> [new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1')]
204         when: 'execute cm handle search is called'
205             def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters)
206         then: 'result is the same collection as returned by the CPS Data Service'
207             assert result.stream().map(cmHandle -> cmHandle.cmHandleId).collect(Collectors.toSet()) == ['cm-handle-id-1'] as Set
208     }
209
210     def 'Set Cm Handle Data Sync flag.'() {
211         when: 'setting data sync enabled flag'
212             objectUnderTest.setDataSyncEnabled('ch-1',true)
213         then: 'call is delegated to the cm handle registration service'
214             mockCmHandleRegistrationService.setDataSyncEnabled('ch-1', true)
215     }
216
217     def dataStores() {
218         CompositeState.DataStores.builder()
219             .operationalDataStore(CompositeState.Operational.builder()
220                 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
221                 .lastSyncTime('some-timestamp').build()).build()
222     }
223 }