use separated get methods for every cmHandle instead of one "get all"
[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.SyncState
29 import spock.lang.Specification
30
31 class DataSyncSpec extends Specification {
32
33     def mockInventoryPersistence = Mock(InventoryPersistence)
34
35     def mockCpsDataService = Mock(CpsDataService)
36
37     def mockSyncUtils = Mock(SyncUtils)
38
39     def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
40
41     def objectUnderTest = new DataSyncWatchdog(mockInventoryPersistence, mockCpsDataService, mockSyncUtils)
42
43     def compositeState = getCompositeState()
44
45     def yangModelCmHandle1 = createSampleYangModelCmHandle('some-cm-handle-1')
46
47     def yangModelCmHandle2 = createSampleYangModelCmHandle('some-cm-handle-2')
48
49     def 'Schedule Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED'() {
50         given: 'sample resource data'
51             def resourceData = jsonString;
52         and: 'sync utilities return a cm handle twice'
53             mockSyncUtils.getAnUnSynchronizedReadyCmHandle() >>> [yangModelCmHandle1, yangModelCmHandle2, null]
54         when: 'data sync poll is executed'
55             objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
56         then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
57             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-1') >> compositeState
58         and: 'the sync util returns first resource data'
59             1 * mockSyncUtils.getResourceData('some-cm-handle-1') >> resourceData
60         and: 'the cm-handle data is saved'
61             1 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-1', jsonString, _)
62         and: 'the first cm handle operational sync state is updated'
63             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-1', compositeState)
64         then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
65             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState
66         and: 'the sync util returns first resource data'
67             1 * mockSyncUtils.getResourceData('some-cm-handle-2') >> resourceData
68         and: 'the cm-handle data is saved'
69             1 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-2', jsonString, _)
70         and: 'the second cm handle operational sync state is updated from "UNSYNCHRONIZED" to "SYNCHRONIZED"'
71             1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState)
72     }
73
74     def 'Schedule Data Sync for Cm Handle State in READY and Operational Sync State in UNSYNCHRONIZED which return empty data from Node'() {
75         given: 'cm handles in an ready state and operational sync state in unsynchronized'
76         and: 'sync utilities return a cm handle twice'
77             mockSyncUtils.getAnUnSynchronizedReadyCmHandle() >>> [yangModelCmHandle1, null]
78         when: 'data sync poll is executed'
79             objectUnderTest.executeUnSynchronizedReadyCmHandlePoll()
80         then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
81             1 * mockInventoryPersistence.getCmHandleState('some-cm-handle-1') >> compositeState
82         and: 'the sync util returns first resource data'
83             1 * mockSyncUtils.getResourceData('some-cm-handle-1') >> null
84         and: 'the cm-handle data is not saved'
85             0 * mockCpsDataService.saveData('NFP-Operational', 'some-cm-handle-1', jsonString, _)
86     }
87
88     def createSampleYangModelCmHandle(cmHandleId) {
89         def compositeState = getCompositeState()
90         return new YangModelCmHandle(id: cmHandleId, compositeState: compositeState)
91     }
92
93     def getCompositeState() {
94         def cmHandleState = CmHandleState.READY
95         def compositeState = new CompositeState(cmHandleState: cmHandleState)
96         compositeState.setDataStores(CompositeState.DataStores.builder()
97             .operationalDataStore(CompositeState.Operational.builder().syncState(SyncState.SYNCHRONIZED)
98                 .build()).build())
99         return compositeState
100     }
101 }