Robustness cleaning of in progress cache
[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 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.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.inventory.sync.executor.AsyncTaskExecutor
27 import java.util.concurrent.ArrayBlockingQueue
28 import org.onap.cps.spi.model.DataNode
29 import spock.lang.Specification
30
31 class ModuleSyncWatchdogSpec extends Specification {
32
33     def mockSyncUtils = Mock(SyncUtils)
34
35     def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
36
37     def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
38
39     def stubModuleSyncStartedOnCmHandles = Stub(IMap<String, Object>)
40
41     def mockModuleSyncTasks = Mock(ModuleSyncTasks)
42
43     def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
44
45     def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , stubModuleSyncStartedOnCmHandles,
46             mockModuleSyncTasks, spiedAsyncTaskExecutor)
47
48     void setup() {
49         spiedAsyncTaskExecutor.setupThreadPool();
50     }
51
52     def 'Module sync advised cm handles with #scenario.'() {
53         given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
54             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
55         and: 'the executor has enough available threads'
56             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
57         when: ' module sync is started'
58             objectUnderTest.moduleSyncAdvisedCmHandles()
59         then: 'it performs #expectedNumberOfTaskExecutions tasks'
60             expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
61         where: ' the following parameter are used'
62             scenario              | numberOfAdvisedCmHandles                                          || expectedNumberOfTaskExecutions
63             'less then 1 batch'   | 1                                                                 || 1
64             'exactly 1 batch'     | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                         || 1
65             '2 batches'           | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                     || 2
66             'queue capacity'      | testQueueCapacity                                                 || 3
67             'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
68     }
69
70     def 'Reset failed cm handles.'() {
71         given: 'sync utilities returns failed cm handles'
72             def failedCmHandles = [new YangModelCmHandle()]
73             mockSyncUtils.getModuleSyncFailedCmHandles() >> failedCmHandles
74         when: ' reset failed cm handles is started'
75             objectUnderTest.resetPreviouslyFailedCmHandles()
76         then: 'it is delegated to the module sync task (service)'
77             1 * mockModuleSyncTasks.resetFailedCmHandles(failedCmHandles)
78     }
79
80     def createDataNodes(numberOfDataNodes) {
81         def dataNodes = []
82         (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}
83         return dataNodes
84     }
85 }