Composite State to handle dmi-reg YANG updates
[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.ncmp.api.inventory.CompositeState
29 import org.onap.cps.spi.CpsDataPersistenceService
30 import org.onap.cps.spi.FetchDescendantsOption
31 import org.onap.cps.spi.model.DataNode
32 import org.onap.cps.utils.JsonObjectMapper
33 import spock.lang.Shared
34 import spock.lang.Specification
35
36 import java.time.OffsetDateTime
37
38 class SyncUtilsSpec extends Specification{
39
40     def mockCpsDataService = Mock(CpsDataService)
41     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
42     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
43     def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
44
45     def objectUnderTest = new SyncUtils(mockCpsDataService, mockCpsDataPersistenceService, spiedJsonObjectMapper, mockYangModelCmHandleRetriever)
46
47     @Shared
48     def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
49
50
51
52     def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
53         given: 'the cps (persistence service) returns a collection of data nodes'
54             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin',
55                 'ncmp-dmi-registry', '//cm-handles[@state=\"ADVISED\"]',
56                 FetchDescendantsOption.OMIT_DESCENDANTS) >> dataNodeCollection
57         when: 'get advised cm handle is called'
58             objectUnderTest.getAnAdvisedCmHandle()
59         then: 'the returned data node collection is the correct size'
60             dataNodeCollection.size() == expectedDataNodeSize
61         and: 'get yang model cm handles is invoked the correct number of times'
62            expectedCallsToGetYangModelCmHandle * mockYangModelCmHandleRetriever.getYangModelCmHandle('cm-handle-123')
63         where: 'the following scenarios are used'
64             scenario         | dataNodeCollection || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
65             'exists'         | [ dataNode ]       || 1                                   | 1
66             'does not exist' | [ ]                || 0                                   | 0
67
68     }
69
70     def 'Update cm handle state from Advised to Ready'() {
71         given: 'a yang model cm handle and the expected json data'
72             def compositeState = new CompositeState()
73             compositeState.cmhandleState = CmHandleState.ADVISED
74             def yangModelCmHandle = new YangModelCmHandle(id: 'Some-Cm-Handle', compositeState: compositeState )
75             def expectedJsonData = '{"cm-handles":[{"id":"Some-Cm-Handle","state":{"cm-handle-state":"READY"}}]}'
76         when: 'update cm handle state is called'
77             objectUnderTest.updateCmHandleState(yangModelCmHandle, CmHandleState.READY)
78         then: 'update data note leaves is invoked with the correct params'
79             1 * mockCpsDataService.updateNodeLeaves('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, _ as OffsetDateTime)
80     }
81
82 }