e5d3652d2177a8525ef1e35a334fce5d0640f0a2
[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  *  ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.inventory.sync
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.spi.CpsDataPersistenceService
29 import org.onap.cps.spi.FetchDescendantsOption
30 import org.onap.cps.spi.model.DataNode
31 import org.onap.cps.utils.JsonObjectMapper
32 import spock.lang.Shared
33 import spock.lang.Specification
34
35 import java.time.OffsetDateTime
36
37 class SyncUtilsSpec extends Specification{
38
39     def mockCpsDataService = Mock(CpsDataService)
40     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
41     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
42     def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
43
44     def objectUnderTest = new SyncUtils(mockCpsDataService, mockCpsDataPersistenceService, spiedJsonObjectMapper, mockYangModelCmHandleRetriever)
45
46     @Shared
47     def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
48
49
50
51     def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
52         given: 'the cps (persistence service) returns a collection of data nodes'
53             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin',
54                 'ncmp-dmi-registry', '//cm-handles[@state=\"ADVISED\"]',
55                 FetchDescendantsOption.OMIT_DESCENDANTS) >> 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 * mockYangModelCmHandleRetriever.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
66
67     }
68
69     def 'Update cm handle state from Advised to Ready'() {
70         given: 'a yang model cm handle and the expected json data'
71             def yangModelCmHandle = new YangModelCmHandle(id: 'Some-Cm-Handle', cmHandleState: CmHandleState.ADVISED)
72             def expectedJsonData = '{"cm-handles":[{"id":"Some-Cm-Handle","state":"READY"}]}'
73         when: 'update cm handle state is called'
74             objectUnderTest.updateCmHandleState(yangModelCmHandle, CmHandleState.READY)
75         then: 'update data note leaves is invoked with the correct params'
76             1 * mockCpsDataService.updateNodeLeaves('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, _ as OffsetDateTime)
77     }
78
79 }