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.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.inventory.sync.DataSyncWatchdog
28 import org.onap.cps.ncmp.api.impl.inventory.sync.SyncUtils
29 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
30 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
31 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
32 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
33 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
34 import spock.lang.Specification
36 class DataSyncWatchdogSpec extends Specification {
38 def mockInventoryPersistence = Mock(InventoryPersistence)
40 def mockCpsDataService = Mock(CpsDataService)
42 def mockSyncUtils = Mock(SyncUtils)
44 def mockDataSyncSemaphores = Mock(IMap<String,Boolean>)
46 def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
48 def objectUnderTest = new DataSyncWatchdog(mockInventoryPersistence, mockCpsDataService, mockSyncUtils, mockDataSyncSemaphores)
50 def compositeState = getCompositeState()
52 def yangModelCmHandle1 = createSampleYangModelCmHandle('cm-handle-1')
54 def yangModelCmHandle2 = createSampleYangModelCmHandle('cm-handle-2')
56 def 'Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED.'() {
57 given: 'sample resource data'
58 def resourceData = jsonString
59 and: 'sync utilities returns a cm handle twice'
60 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1, yangModelCmHandle2]
61 when: 'data sync poll is executed'
62 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
63 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
64 1 * mockInventoryPersistence.getCmHandleState('cm-handle-1') >> compositeState
65 and: 'the sync util returns first resource data'
66 1 * mockSyncUtils.getResourceData('cm-handle-1') >> resourceData
67 and: 'the cm-handle data is saved'
68 1 * mockCpsDataService.saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cm-handle-1', jsonString, _)
69 and: 'the first cm handle operational sync state is updated'
70 1 * mockInventoryPersistence.saveCmHandleState('cm-handle-1', compositeState)
71 then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
72 1 * mockInventoryPersistence.getCmHandleState('cm-handle-2') >> compositeState
73 and: 'the sync util returns first resource data'
74 1 * mockSyncUtils.getResourceData('cm-handle-2') >> resourceData
75 and: 'the cm-handle data is saved'
76 1 * mockCpsDataService.saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cm-handle-2', jsonString, _)
77 and: 'the second cm handle operational sync state is updated from "UNSYNCHRONIZED" to "SYNCHRONIZED"'
78 1 * mockInventoryPersistence.saveCmHandleState('cm-handle-2', compositeState)
81 def 'Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED without resource data.'() {
82 given: 'sync utilities returns a cm handle'
83 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1]
84 when: 'data sync poll is executed'
85 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
86 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
87 1 * mockInventoryPersistence.getCmHandleState('cm-handle-1') >> compositeState
88 and: 'the sync util returns no resource data'
89 1 * mockSyncUtils.getResourceData('cm-handle-1') >> null
90 and: 'the cm-handle data is not saved'
91 0 * mockCpsDataService.saveData(*_)
94 def 'Data Sync for Cm Handle that is already being processed.'() {
95 given: 'sync utilities returns a cm handle'
96 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1]
97 and: 'the shared data sync semaphore indicate it is already being processed'
98 mockDataSyncSemaphores.putIfAbsent('cm-handle-1', _, _, _) >> 'something (not null)'
99 when: 'data sync poll is executed'
100 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
101 then: 'it is NOT processed e.g. state is not requested'
102 0 * mockInventoryPersistence.getCmHandleState(*_)
105 def createSampleYangModelCmHandle(cmHandleId) {
106 def compositeState = getCompositeState()
107 return new YangModelCmHandle(id: cmHandleId, compositeState: compositeState)
110 def getCompositeState() {
111 def cmHandleState = CmHandleState.READY
112 def compositeState = new CompositeState(cmHandleState: cmHandleState)
113 compositeState.setDataStores(CompositeState.DataStores.builder()
114 .operationalDataStore(CompositeState.Operational.builder().dataStoreSyncState(DataStoreSyncState.SYNCHRONIZED)
116 return compositeState