2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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
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 com.hazelcast.map.IMap
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.inventory.CmHandleState
27 import org.onap.cps.ncmp.api.inventory.CompositeState
28 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
29 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
30 import spock.lang.Specification
32 class DataSyncWatchdogSpec extends Specification {
34 def mockInventoryPersistence = Mock(InventoryPersistence)
36 def mockCpsDataService = Mock(CpsDataService)
38 def mockSyncUtils = Mock(SyncUtils)
40 def mockDataSyncSemaphoreMap = Mock(IMap<String,Boolean>)
42 def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
44 def objectUnderTest = new DataSyncWatchdog(mockInventoryPersistence, mockCpsDataService, mockSyncUtils, mockDataSyncSemaphoreMap)
46 def compositeState = getCompositeState()
48 def yangModelCmHandle1 = createSampleYangModelCmHandle('some-cm-handle-1')
50 def yangModelCmHandle2 = createSampleYangModelCmHandle('some-cm-handle-2')
52 def 'Schedule Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED'() {
53 given: 'sample resource data'
54 def resourceData = jsonString
55 and: 'sync utilities return a cm handle twice'
56 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1, yangModelCmHandle2]
57 when: 'data sync poll is executed'
58 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
59 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
60 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-1') >> compositeState
61 and: 'the sync util returns first resource data'
62 1 * mockSyncUtils.getResourceData('some-cm-handle-1') >> resourceData
63 and: 'the cm-handle data is saved'
64 1 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-1', jsonString, _)
65 and: 'the first cm handle operational sync state is updated'
66 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-1', compositeState)
67 then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
68 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState
69 and: 'the sync util returns first resource data'
70 1 * mockSyncUtils.getResourceData('some-cm-handle-2') >> resourceData
71 and: 'the cm-handle data is saved'
72 1 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-2', jsonString, _)
73 and: 'the second cm handle operational sync state is updated from "UNSYNCHRONIZED" to "SYNCHRONIZED"'
74 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState)
77 def 'Schedule Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED which return empty data from Node'() {
78 given: 'cm handles in an ready state and operational sync state in unsynchronized'
79 and: 'sync utilities return a cm handle twice'
80 mockSyncUtils.getUnsynchronizedReadyCmHandles() >> [yangModelCmHandle1]
81 when: 'data sync poll is executed'
82 objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
83 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
84 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-1') >> compositeState
85 and: 'the sync util returns first resource data'
86 1 * mockSyncUtils.getResourceData('some-cm-handle-1') >> null
87 and: 'the cm-handle data is not saved'
88 0 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-1', jsonString, _)
91 def createSampleYangModelCmHandle(cmHandleId) {
92 def compositeState = getCompositeState()
93 return new YangModelCmHandle(id: cmHandleId, compositeState: compositeState)
96 def getCompositeState() {
97 def cmHandleState = CmHandleState.READY
98 def compositeState = new CompositeState(cmHandleState: cmHandleState)
99 compositeState.setDataStores(CompositeState.DataStores.builder()
100 .operationalDataStore(CompositeState.Operational.builder().dataStoreSyncState(DataStoreSyncState.SYNCHRONIZED)
102 return compositeState