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