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.CmHandleQueries
29 import org.onap.cps.ncmp.api.inventory.CmHandleState
30 import org.onap.cps.ncmp.api.inventory.CompositeState
31 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
32 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
33 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
34 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
35 import org.onap.cps.spi.FetchDescendantsOption
36 import org.onap.cps.spi.model.DataNode
37 import org.onap.cps.utils.JsonObjectMapper
38 import org.springframework.http.HttpStatus
39 import org.springframework.http.ResponseEntity
40 import spock.lang.Shared
41 import spock.lang.Specification
43 import java.time.OffsetDateTime
44 import java.time.format.DateTimeFormatter
45 import java.util.stream.Collectors
47 class SyncUtilsSpec extends Specification{
49 def mockInventoryPersistence = Mock(InventoryPersistence)
51 def mockCmHandleQueries = Mock(CmHandleQueries)
53 def mockDmiDataOperations = Mock(DmiDataOperations)
55 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
57 def objectUnderTest = new SyncUtils(mockInventoryPersistence, mockCmHandleQueries, mockDmiDataOperations, jsonObjectMapper)
60 def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(OffsetDateTime.now())
63 def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
65 def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
66 given: 'the inventory persistence service returns a collection of data nodes'
67 mockCmHandleQueries.getCmHandlesByState(CmHandleState.ADVISED) >> dataNodeCollection
68 when: 'get advised cm handles are fetched'
69 def yangModelCmHandles = objectUnderTest.getAdvisedCmHandles()
70 then: 'the returned data node collection is the correct size'
71 yangModelCmHandles.size() == expectedDataNodeSize
72 and: 'yang model collection contains the correct data'
73 yangModelCmHandles.stream().map(yangModel -> yangModel.id).collect(Collectors.toSet()) ==
74 dataNodeCollection.stream().map(dataNode -> dataNode.leaves.get("id")).collect(Collectors.toSet())
75 where: 'the following scenarios are used'
76 scenario | dataNodeCollection || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
77 'exists' | [dataNode] || 1 | 1
78 'does not exist' | [] || 0 | 0
81 def 'Update Lock Reason, Details and Attempts where lock reason #scenario'() {
82 given: 'A locked state'
83 def compositeState = new CompositeState(lockReason: lockReason)
84 when: 'update cm handle details and attempts is called'
85 objectUnderTest.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, 'new error message')
86 then: 'the composite state lock reason and details are updated'
87 assert compositeState.lockReason.lockReasonCategory == LockReasonCategory.LOCKED_MODULE_SYNC_FAILED
88 assert compositeState.lockReason.details == expectedDetails
90 scenario | lockReason || expectedDetails
91 'does not exist' | null || 'Attempt #1 failed: new error message'
92 'exists' | CompositeState.LockReason.builder().details("Attempt #2 failed: some error message").build() || 'Attempt #3 failed: new error message'
95 def 'Get all locked Cm-Handle where Lock Reason is LOCKED_MODULE_SYNC_FAILED cm handle #scenario'() {
96 given: 'the cps (persistence service) returns a collection of data nodes'
97 mockCmHandleQueries.getCmHandleDataNodesByCpsPath(
98 '//lock-reason[@reason="LOCKED_MODULE_SYNC_FAILED"]',
99 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> [dataNode]
100 when: 'get locked Misbehaving cm handle is called'
101 def result = objectUnderTest.getModuleSyncFailedCmHandles()
102 then: 'the returned cm handle collection is the correct size'
104 and: 'the correct cm handle is returned'
105 result[0].id == 'cm-handle-123'
108 def 'Retry Locked Cm-Handle where the last update time is #scenario'() {
109 when: 'retry locked cm handle is invoked'
110 def result = objectUnderTest.isReadyForRetry(new CompositeStateBuilder()
111 .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, details)
112 .withLastUpdatedTime(lastUpdateTime).build())
113 then: 'result returns #expectedResult'
114 result == expectedResult
116 scenario | lastUpdateTime | details || expectedResult
117 'the first attempt' | '1900-01-01T00:00:00.000+0100' | 'First Attempt' || true
118 'greater than one minute' | '1900-01-01T00:00:00.000+0100' | 'Attempt #1 failed:' || true
119 'less than eight minutes' | formattedDateAndTime | 'Attempt #3 failed:' || false
123 def 'Get a Cm-Handle where Operational Sync state is UnSynchronized and Cm-handle state is READY and #scenario'() {
124 given: 'the inventory persistence service returns a collection of data nodes'
125 mockCmHandleQueries.getCmHandlesByOperationalSyncState(DataStoreSyncState.UNSYNCHRONIZED) >> unSynchronizedDataNodes
126 mockCmHandleQueries.getCmHandlesByIdAndState("cm-handle-123", CmHandleState.READY) >> readyDataNodes
127 when: 'get advised cm handles are fetched'
128 objectUnderTest.getAnUnSynchronizedReadyCmHandle()
129 then: 'the returned data node collection is the correct size'
130 readyDataNodes.size() == expectedDataNodeSize
131 and: 'get yang model cm handles is invoked the correct number of times'
132 expectedCallsToGetYangModelCmHandle * mockInventoryPersistence.getYangModelCmHandle('cm-handle-123')
133 where: 'the following scenarios are used'
134 scenario | unSynchronizedDataNodes | readyDataNodes || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
135 'exists' | [dataNode] | [dataNode] || 1 | 1
136 'unsynchronized exist but not ready' | [dataNode] | [] || 0 | 0
137 'does not exist' | [] | [] || 0 | 0
140 def 'Get resource data through DMI Operations #scenario'() {
141 given: 'the inventory persistence service returns a collection of data nodes'
142 def jsonString = '{"stores:bookstore":{"categories":[{"code":"01"}]}}'
143 JsonNode jsonNode = jsonObjectMapper.convertToJsonNode(jsonString);
144 def responseEntity = new ResponseEntity<>(jsonNode, HttpStatus.OK)
145 mockDmiDataOperations.getResourceDataFromDmi('cm-handle-123', DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, _) >> responseEntity
146 when: 'get resource data is called'
147 def result = objectUnderTest.getResourceData('cm-handle-123')
148 then: 'the returned data is correct'