Enable/Disable Data Sync for Cm Handle
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / DataSyncSpec.groovy
1 /*
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
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 org.onap.cps.api.CpsDataService
24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25 import org.onap.cps.ncmp.api.inventory.CmHandleState
26 import org.onap.cps.ncmp.api.inventory.CompositeState
27 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
28 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
29 import spock.lang.Shared
30 import spock.lang.Specification
31
32 class DataSyncSpec extends Specification {
33
34     def mockInventoryPersistence = Mock(InventoryPersistence)
35
36     def mockCpsDataService = Mock(CpsDataService)
37
38     def mockSyncUtils = Mock(SyncUtils)
39
40     @Shared
41     def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
42
43     def objectUnderTest = new DataSyncWatchdog(mockInventoryPersistence, mockCpsDataService, mockSyncUtils)
44
45     def compositeState = getCompositeState()
46
47     def yangModelCmHandle1 = createSampleYangModelCmHandle('some-cm-handle-1')
48
49     def yangModelCmHandle2 = createSampleYangModelCmHandle('some-cm-handle-2')
50
51     def 'Schedule Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED'() {
52         given: 'sample resource data'
53             def resourceData = jsonString
54         and: 'sync utilities return a cm handle twice'
55             mockSyncUtils.getAnUnSynchronizedReadyCmHandle() >>> [yangModelCmHandle1, yangModelCmHandle2, null]
56         when: 'data sync poll is executed'
57             objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
58         then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
59             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-1') >> compositeState
60         and: 'the sync util returns first resource data'
61             1 * mockSyncUtils.getResourceData('some-cm-handle-1') >> resourceData
62         and: 'the cm-handle data is saved'
63             1 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-1', jsonString, _)
64         and: 'the first cm handle operational sync state is updated'
65             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-1', compositeState)
66         then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
67             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState
68         and: 'the sync util returns first resource data'
69             1 * mockSyncUtils.getResourceData('some-cm-handle-2') >> resourceData
70         and: 'the cm-handle data is saved'
71             1 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-2', jsonString, _)
72         and: 'the second cm handle operational sync state is updated from "UNSYNCHRONIZED" to "SYNCHRONIZED"'
73             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState)
74     }
75
76     def 'Schedule Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED which return empty data from Node because #scenario'() {
77         given: 'a yang model cm handle'
78             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: new CompositeState(dataSyncEnabled: dataSyncEnabled))
79         and: 'sync utilities returns a single cm handle'
80             mockSyncUtils.getAnUnSynchronizedReadyCmHandle() >>> [yangModelCmHandle, null]
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') >> compositeState
85         and: 'the sync util returns first resource data'
86             1 * mockSyncUtils.getResourceData('some-cm-handle') >> resourceData
87         and: 'the cm-handle data is not saved'
88             0 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-1', jsonString, _)
89         where:
90             scenario                                             | dataSyncEnabled | resourceData
91             'data sync is not enabled'                           | false           | jsonString
92             'resource data is null'                              | true            | null
93             'data sync is not enabled and resource data is null' | false           | null
94     }
95
96     def createSampleYangModelCmHandle(cmHandleId) {
97         def compositeState = getCompositeState()
98         return new YangModelCmHandle(id: cmHandleId, compositeState: compositeState)
99     }
100
101     def getCompositeState() {
102         def cmHandleState = CmHandleState.READY
103         def compositeState = new CompositeState(cmHandleState: cmHandleState, dataSyncEnabled: true)
104         compositeState.setDataStores(CompositeState.DataStores.builder()
105             .operationalDataStore(CompositeState.Operational.builder().dataStoreSyncState(DataStoreSyncState.SYNCHRONIZED)
106                 .build()).build())
107         return compositeState
108     }
109 }