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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory.sync
23 import org.onap.cps.ncmp.api.inventory.CmHandleState
24 import org.onap.cps.ncmp.api.inventory.CompositeState
25 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
26 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
27 import org.onap.cps.spi.CpsDataPersistenceService
28 import org.onap.cps.spi.FetchDescendantsOption
29 import org.onap.cps.spi.model.DataNode
30 import spock.lang.Shared
31 import spock.lang.Specification
33 class SyncUtilsSpec extends Specification{
35 def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
36 def mockInventoryPersistence = Mock(InventoryPersistence)
38 def objectUnderTest = new SyncUtils(mockInventoryPersistence)
41 def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
45 def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
46 given: 'the inventory persistence service returns a collection of data nodes'
47 mockInventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED) >> dataNodeCollection
48 when: 'get advised cm handle is called'
49 objectUnderTest.getAnAdvisedCmHandle()
50 then: 'the returned data node collection is the correct size'
51 dataNodeCollection.size() == expectedDataNodeSize
52 and: 'get yang model cm handles is invoked the correct number of times'
53 expectedCallsToGetYangModelCmHandle * mockInventoryPersistence.getYangModelCmHandle('cm-handle-123')
54 where: 'the following scenarios are used'
55 scenario | dataNodeCollection || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
56 'exists' | [ dataNode ] || 1 | 1
57 'does not exist' | [ ] || 0 | 0
61 def 'Update Lock Reason, Details and Attempts where lock reason #scenario'() {
62 given: 'A locked state'
63 def compositeState = new CompositeState(lockReason: lockReason)
64 when: 'update cm handle details and attempts is called'
65 objectUnderTest.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING, 'new error message')
66 then: 'the composite state lock reason and details are updated'
67 assert compositeState.lockReason.lockReasonCategory == LockReasonCategory.LOCKED_MISBEHAVING
68 assert compositeState.lockReason.details == expectedDetails
70 scenario | lockReason || expectedDetails
71 'does not exist' | null || 'Attempt #1 failed: new error message'
72 'exists' | CompositeState.LockReason.builder().details("Attempt #2 failed: some error message").build() || 'Attempt #3 failed: new error message'