Refactoring persistence classes
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdogSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory.sync
23
24 import com.hazelcast.map.IMap
25 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncTasks
26 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncWatchdog
27 import org.onap.cps.ncmp.api.impl.inventory.sync.SyncUtils
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.impl.inventory.sync.executor.AsyncTaskExecutor
30 import java.util.concurrent.ArrayBlockingQueue
31 import org.onap.cps.spi.model.DataNode
32 import spock.lang.Specification
33
34 class ModuleSyncWatchdogSpec extends Specification {
35
36     def mockSyncUtils = Mock(SyncUtils)
37
38     def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
39
40     def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
41
42     def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
43
44     def mockModuleSyncTasks = Mock(ModuleSyncTasks)
45
46     def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
47
48     def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor)
49
50     void setup() {
51         spiedAsyncTaskExecutor.setupThreadPool()
52     }
53
54     def 'Module sync advised cm handles with #scenario.'() {
55         given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
56             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
57         and: 'the executor has enough available threads'
58             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
59         when: ' module sync is started'
60             objectUnderTest.moduleSyncAdvisedCmHandles()
61         then: 'it performs #expectedNumberOfTaskExecutions tasks'
62             expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
63         where: 'the following parameter are used'
64             scenario              | numberOfAdvisedCmHandles                                          || expectedNumberOfTaskExecutions
65             'less then 1 batch'   | 1                                                                 || 1
66             'exactly 1 batch'     | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                         || 1
67             '2 batches'           | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                     || 2
68             'queue capacity'      | testQueueCapacity                                                 || 3
69             'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
70     }
71
72     def 'Module sync advised cm handles starts with no available threads.'() {
73         given: 'sync utilities returns a advise cm handles'
74             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
75         and: 'the executor first has no threads but has one thread on the second attempt'
76             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >>> [ 0, 1 ]
77         when: ' module sync is started'
78             objectUnderTest.moduleSyncAdvisedCmHandles()
79         then: 'it performs one task'
80             1 * spiedAsyncTaskExecutor.executeTask(*_)
81     }
82
83     def 'Module sync advised cm handles already handled.'() {
84         given: 'sync utilities returns a advise cm handles'
85             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
86         and: 'the executor has a thread available'
87             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 1
88         and: 'the semaphore cache indicates the cm handle is already being processed'
89             mockModuleSyncStartedOnCmHandles.putIfAbsent(*_) >> 'Started'
90         when: ' module sync is started'
91             objectUnderTest.moduleSyncAdvisedCmHandles()
92         then: 'it does NOT execute a task to process the (empty) batch'
93             0 * spiedAsyncTaskExecutor.executeTask(*_)
94     }
95
96     def 'Module sync with previous cm handle(s) left in work queue.'() {
97         given: 'there is still a cm handle in the queue'
98             moduleSyncWorkQueue.offer(new DataNode())
99         and: 'sync utilities returns many advise cm handles'
100             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(500)
101         and: 'the executor has plenty threads available'
102             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 10
103         when: ' module sync is started'
104             objectUnderTest.moduleSyncAdvisedCmHandles()
105         then: 'it does executes only one task to process the remaining handle in the queue'
106             1 * spiedAsyncTaskExecutor.executeTask(*_)
107     }
108
109     def 'Reset failed cm handles.'() {
110         given: 'sync utilities returns failed cm handles'
111             def failedCmHandles = [new YangModelCmHandle()]
112             mockSyncUtils.getModuleSyncFailedCmHandles() >> failedCmHandles
113         when: 'reset failed cm handles is started'
114             objectUnderTest.resetPreviouslyFailedCmHandles()
115         then: 'it is delegated to the module sync task (service)'
116             1 * mockModuleSyncTasks.resetFailedCmHandles(failedCmHandles)
117     }
118
119     def createDataNodes(numberOfDataNodes) {
120         def dataNodes = []
121         (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}
122         return dataNodes
123     }
124 }