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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory.sync
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
27 import org.onap.cps.ncmp.api.impl.operations.DmiOperations
28 import org.onap.cps.ncmp.api.inventory.CmHandleState
29 import org.onap.cps.ncmp.api.inventory.CompositeState
30 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
31 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
32 import org.onap.cps.ncmp.api.inventory.SyncState
33 import org.onap.cps.spi.model.DataNode
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.springframework.http.HttpStatus
36 import org.springframework.http.ResponseEntity
37 import spock.lang.Shared
38 import spock.lang.Specification
40 class SyncUtilsSpec extends Specification{
42 def mockInventoryPersistence = Mock(InventoryPersistence)
44 def mockDmiDataOperations = Mock(DmiDataOperations)
46 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
48 def objectUnderTest = new SyncUtils(mockInventoryPersistence, mockDmiDataOperations, jsonObjectMapper)
51 def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
53 def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
54 given: 'the inventory persistence service returns a collection of data nodes'
55 mockInventoryPersistence.getCmHandlesByState(CmHandleState.ADVISED) >> dataNodeCollection
56 when: 'get advised cm handle is called'
57 objectUnderTest.getAnAdvisedCmHandle()
58 then: 'the returned data node collection is the correct size'
59 dataNodeCollection.size() == expectedDataNodeSize
60 and: 'get yang model cm handles is invoked the correct number of times'
61 expectedCallsToGetYangModelCmHandle * mockInventoryPersistence.getYangModelCmHandle('cm-handle-123')
62 where: 'the following scenarios are used'
63 scenario | dataNodeCollection || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
64 'exists' | [ dataNode ] || 1 | 1
65 'does not exist' | [ ] || 0 | 0
69 def 'Update Lock Reason, Details and Attempts where lock reason #scenario'() {
70 given: 'A locked state'
71 def compositeState = new CompositeState(lockReason: lockReason)
72 when: 'update cm handle details and attempts is called'
73 objectUnderTest.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MISBEHAVING, 'new error message')
74 then: 'the composite state lock reason and details are updated'
75 assert compositeState.lockReason.lockReasonCategory == LockReasonCategory.LOCKED_MISBEHAVING
76 assert compositeState.lockReason.details == expectedDetails
78 scenario | lockReason || expectedDetails
79 'does not exist' | null || 'Attempt #1 failed: new error message'
80 'exists' | CompositeState.LockReason.builder().details("Attempt #2 failed: some error message").build() || 'Attempt #3 failed: new error message'
83 def 'Get all locked Cm-Handle where Lock Reason is LOCKED_MISBEHAVING cm handle #scenario'() {
84 given: 'the cps (persistence service) returns a collection of data nodes'
85 mockInventoryPersistence.getCmHandleDataNodesByCpsPath(
86 '//lock-reason[@reason="LOCKED_MISBEHAVING"]/ancestor::cm-handles') >> [dataNode ]
87 when: 'get locked Misbehaving cm handle is called'
88 def result = objectUnderTest.getLockedMisbehavingYangModelCmHandles()
89 then: 'the returned cm handle collection is the correct size'
91 and: 'the correct cm handle is returned'
92 result[0].id == 'cm-handle-123'
95 def 'Get a Cm-Handle where Operational Sync state is UnSynchronized and Cm-handle state is READY and #scenario'() {
96 given: 'the inventory persistence service returns a collection of data nodes'
97 mockInventoryPersistence.getCmHandlesByOperationalSyncState(SyncState.UNSYNCHRONIZED) >> unSynchronizedDataNodes
98 mockInventoryPersistence.getCmHandlesByIdAndState("cm-handle-123", CmHandleState.READY) >> readyDataNodes
99 when: 'get advised cm handle is called'
100 objectUnderTest.getAnUnSynchronizedReadyCmHandle()
101 then: 'the returned data node collection is the correct size'
102 readyDataNodes.size() == expectedDataNodeSize
103 and: 'get yang model cm handles is invoked the correct number of times'
104 expectedCallsToGetYangModelCmHandle * mockInventoryPersistence.getYangModelCmHandle('cm-handle-123')
105 where: 'the following scenarios are used'
106 scenario | unSynchronizedDataNodes | readyDataNodes || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
107 'exists' | [dataNode] | [dataNode] || 1 | 1
108 'unsynchronized exist but not ready' | [dataNode] | [] || 0 | 0
109 'does not exist' | [] | [] || 0 | 0
112 def 'Get resource data through DMI Operations #scenario'() {
113 given: 'the inventory persistence service returns a collection of data nodes'
114 def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
115 JsonNode jsonNode = jsonObjectMapper.convertToJsonNode(jsonString);
116 def responseEntity = new ResponseEntity<>(jsonNode, HttpStatus.OK)
117 mockDmiDataOperations.getResourceDataFromDmi('cm-handle-123', DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, _) >> responseEntity
118 when: 'get resource data is called'
119 def result = objectUnderTest.getResourceData('cm-handle-123')
120 then: 'the returned data is correct'