Retry CM-Handles that are LOCKED, Failed-to-Sync
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / SyncUtilsSpec.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 org.onap.cps.ncmp.api.inventory.CmHandleState
25 import org.onap.cps.ncmp.api.inventory.CompositeState
26 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
27 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
28 import org.onap.cps.spi.model.DataNode
29 import spock.lang.Shared
30 import spock.lang.Specification
31
32 class SyncUtilsSpec extends Specification{
33
34     def mockInventoryPersistence = Mock(InventoryPersistence)
35
36     def objectUnderTest = new SyncUtils(mockInventoryPersistence)
37
38     @Shared
39     def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
40
41     def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
42         given: 'the inventory persistence service returns a collection of data nodes'
43             mockInventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED) >> dataNodeCollection
44         when: 'get advised cm handle is called'
45             objectUnderTest.getAnAdvisedCmHandle()
46         then: 'the returned data node collection is the correct size'
47             dataNodeCollection.size() == expectedDataNodeSize
48         and: 'get yang model cm handles is invoked the correct number of times'
49            expectedCallsToGetYangModelCmHandle * mockInventoryPersistence.getYangModelCmHandle('cm-handle-123')
50         where: 'the following scenarios are used'
51             scenario         | dataNodeCollection || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
52             'exists'         | [ dataNode ]       || 1                                   | 1
53             'does not exist' | [ ]                || 0                                   | 0
54
55     }
56
57     def 'Update Lock Reason, Details and Attempts where lock reason #scenario'() {
58         given: 'A locked state'
59            def compositeState = new CompositeState(lockReason: lockReason)
60         when: 'update cm handle details and attempts is called'
61             objectUnderTest.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING, 'new error message')
62         then: 'the composite state lock reason and details are updated'
63             assert compositeState.lockReason.lockReasonCategory == LockReasonCategory.LOCKED_MISBEHAVING
64             assert compositeState.lockReason.details == expectedDetails
65         where:
66             scenario         | lockReason                                                                                   || expectedDetails
67             'does not exist' | null                                                                                         || 'Attempt #1 failed: new error message'
68             'exists'         | CompositeState.LockReason.builder().details("Attempt #2 failed: some error message").build() || 'Attempt #3 failed: new error message'
69     }
70     def 'Get all locked Cm-Handle where Lock Reason is LOCKED_MISBEHAVING cm handle #scenario'() {
71         given: 'the cps (persistence service) returns a collection of data nodes'
72             mockInventoryPersistence.getCmHandlesByCpsPath(
73                     '//lock-reason[@reason="LOCKED_MISBEHAVING"]/ancestor::cm-handles') >> [dataNode ]
74         when: 'get locked Misbehaving cm handle is called'
75             def result = objectUnderTest.getLockedMisbehavingCmHandles()
76         then: 'the returned cm handle collection is the correct size'
77             result.size() == 1
78         and: 'the correct cm handle is returned'
79             result[0].id == 'cm-handle-123'
80     }
81 }