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