2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.inventory.sync
23 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
25 import com.hazelcast.map.IMap
26 import org.onap.cps.api.CpsDataService
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
28 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
29 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
30 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
31 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
32 import spock.lang.Specification
34 class DataSyncWatchdogSpec extends Specification {
36 def mockInventoryPersistence = Mock(InventoryPersistence)
38 def mockCpsDataService = Mock(CpsDataService)
40 def mockSyncUtils = Mock(ModuleOperationsUtils)
42 def mockDataSyncSemaphores = Mock(IMap<String,Boolean>)
44 def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
46 def objectUnderTest = new DataSyncWatchdog(mockInventoryPersistence, mockCpsDataService, mockSyncUtils, mockDataSyncSemaphores)
48 def compositeState = getCompositeState()
50 def yangModelCmHandle1 = createSampleYangModelCmHandle('cm-handle-1')
52 def yangModelCmHandle2 = createSampleYangModelCmHandle('cm-handle-2')
54 def 'Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED.'() {
55 given: 'sample resource data'
56 def resourceData = jsonString
57 and: 'sync utilities returns a cm handle twice'
58 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1, yangModelCmHandle2]
59 when: 'data sync poll is executed'
60 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
61 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
62 1 * mockInventoryPersistence.getCmHandleState('cm-handle-1') >> compositeState
63 and: 'the sync util returns first resource data'
64 1 * mockSyncUtils.getResourceData('cm-handle-1') >> resourceData
65 and: 'the cm-handle data is saved'
66 1 * mockCpsDataService.saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cm-handle-1', jsonString, _)
67 and: 'the first cm handle operational sync state is updated'
68 1 * mockInventoryPersistence.saveCmHandleState('cm-handle-1', compositeState)
69 then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
70 1 * mockInventoryPersistence.getCmHandleState('cm-handle-2') >> compositeState
71 and: 'the sync util returns first resource data'
72 1 * mockSyncUtils.getResourceData('cm-handle-2') >> resourceData
73 and: 'the cm-handle data is saved'
74 1 * mockCpsDataService.saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cm-handle-2', jsonString, _)
75 and: 'the second cm handle operational sync state is updated from "UNSYNCHRONIZED" to "SYNCHRONIZED"'
76 1 * mockInventoryPersistence.saveCmHandleState('cm-handle-2', compositeState)
79 def 'Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED without resource data.'() {
80 given: 'sync utilities returns a cm handle'
81 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1]
82 when: 'data sync poll is executed'
83 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
84 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
85 1 * mockInventoryPersistence.getCmHandleState('cm-handle-1') >> compositeState
86 and: 'the sync util returns no resource data'
87 1 * mockSyncUtils.getResourceData('cm-handle-1') >> null
88 and: 'the cm-handle data is not saved'
89 0 * mockCpsDataService.saveData(*_)
92 def 'Data Sync for Cm Handle that is already being processed.'() {
93 given: 'sync utilities returns a cm handle'
94 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1]
95 and: 'the shared data sync semaphore indicate it is already being processed'
96 mockDataSyncSemaphores.putIfAbsent('cm-handle-1', _, _, _) >> 'something (not null)'
97 when: 'data sync poll is executed'
98 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
99 then: 'it is NOT processed e.g. state is not requested'
100 0 * mockInventoryPersistence.getCmHandleState(*_)
103 def createSampleYangModelCmHandle(cmHandleId) {
104 def compositeState = getCompositeState()
105 return new YangModelCmHandle(id: cmHandleId, compositeState: compositeState)
108 def getCompositeState() {
109 def cmHandleState = CmHandleState.READY
110 def compositeState = new CompositeState(cmHandleState: cmHandleState)
111 compositeState.setDataStores(CompositeState.DataStores.builder()
112 .operationalDataStore(CompositeState.Operational.builder().dataStoreSyncState(DataStoreSyncState.SYNCHRONIZED)
114 return compositeState