Refactor YangDataConverter.convertCmHandleToYangModel
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / YangDataConverterSpec.groovy
1 /*
2  * ============LICENSE_START========================================================
3  *  Copyright (C) 2022-2024 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.impl.utils
22
23 import org.onap.cps.spi.model.DataNode
24 import spock.lang.Specification
25
26 class YangDataConverterSpec extends Specification{
27
28     def 'Convert a cm handle data node with private and public properties.'() {
29         given: 'a datanode with some additional (dmi, private) and public properties'
30             def dataNodeAdditionalProperties = new DataNode(xpath:'/additional-properties[@name="dmiProp1"]',
31                     leaves: ['name': 'dmiProp1', 'value': 'dmiValue1'])
32             def dataNodePublicProperties = new DataNode(xpath:'/public-properties[@name="pubProp1"]',
33                     leaves: ['name': 'pubProp1', 'value': 'pubValue1'])
34             def dataNodeCmHandle = new DataNode(leaves:['id':'sample-id'], childDataNodes:[dataNodeAdditionalProperties, dataNodePublicProperties])
35         when: 'the dataNode is converted'
36             def yangModelCmHandle =
37                     YangDataConverter.convertCmHandleToYangModel(dataNodeCmHandle)
38         then: 'the converted object has the correct id'
39             assert yangModelCmHandle.id == 'sample-id'
40         and: 'the additional (dmi, private) properties are included'
41             assert yangModelCmHandle.dmiProperties[0].name == 'dmiProp1'
42             assert yangModelCmHandle.dmiProperties[0].value == 'dmiValue1'
43         and: 'the public properties are included'
44             assert yangModelCmHandle.publicProperties[0].name == 'pubProp1'
45             assert yangModelCmHandle.publicProperties[0].value == 'pubValue1'
46     }
47
48     def 'Convert multiple cm handle data nodes'(){
49         given: 'two data nodes in a collection'
50             def dataNodes = [new DataNode(xpath:'/dmi-registry/cm-handles[@id=\'some-cm-handle\']', leaves: ['id':'some-cm-handle']),
51                              new DataNode(xpath:'/dmi-registry/cm-handles[@id=\'another-cm-handle\']', leaves: ['id':'another-cm-handle'])]
52         when: 'the data nodes are converted'
53             def yangModelCmHandles = YangDataConverter.convertDataNodesToYangModelCmHandles(dataNodes)
54         then: 'verify both have returned and CmHandleIds are correct'
55             assert yangModelCmHandles.size() == 2
56             assert yangModelCmHandles.id.containsAll(['some-cm-handle', 'another-cm-handle'])
57     }
58 }