URI is not absolute when service name is not set
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / operations / YangModelCmHandleRetrieverSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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.impl.operations
22
23 import org.onap.cps.api.CpsDataService
24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25 import org.onap.cps.spi.exceptions.DataValidationException
26 import spock.lang.Shared
27
28 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
29 import org.onap.cps.spi.model.DataNode
30 import spock.lang.Specification
31
32 class YangModelCmHandleRetrieverSpec extends Specification {
33
34     def mockCpsDataService = Mock(CpsDataService)
35
36     def objectUnderTest = new YangModelCmHandleRetriever(mockCpsDataService)
37
38     def cmHandleId = 'some-cm-handle'
39     def leaves = ["dmi-service-name":"common service name","dmi-data-service-name":"data service name","dmi-model-service-name":"model service name"]
40     def xpath = "/dmi-registry/cm-handles[@id='some-cm-handle']"
41
42     @Shared
43     def childDataNodesForCmHandleWithAllProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"]),
44                                                       new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
45
46     @Shared
47     def childDataNodesForCmHandleWithDMIProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"])]
48
49     @Shared
50     def childDataNodesForCmHandleWithPublicProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
51
52     def "Retrieve CmHandle using datanode with #scenario."() {
53         given: 'the cps data service returns a data node from the DMI registry'
54             def dataNode = new DataNode(childDataNodes:childDataNodes, leaves: leaves)
55             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
56         when: 'retrieving the yang modelled cm handle'
57             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
58         then: 'the result has the correct id and service names'
59             result.id == cmHandleId
60             result.dmiServiceName == 'common service name'
61             result.dmiDataServiceName == 'data service name'
62             result.dmiModelServiceName == 'model service name'
63         and: 'the expected DMI properties'
64             result.dmiProperties == expectedDmiProperties
65             result.publicProperties == expectedPublicProperties
66         where: 'the following parameters are used'
67             scenario                    | childDataNodes                                || expectedDmiProperties                               || expectedPublicProperties
68             'no properties'             | []                                            || []                                                  || []
69             'DMI and public properties' | childDataNodesForCmHandleWithAllProperties    || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")]
70             'just DMI properties'       | childDataNodesForCmHandleWithDMIProperties    || [new YangModelCmHandle.Property("name1", "value1")] || []
71             'just public properties'    | childDataNodesForCmHandleWithPublicProperties || []                                                  || [new YangModelCmHandle.Property("name2", "value2")]
72     }
73
74     def "Retrieve CmHandle using datanode with invalid CmHandle id."() {
75         when: 'retrieving the yang modelled cm handle with an invalid id'
76             def result = objectUnderTest.getYangModelCmHandle('cm handle id with spaces')
77         then: 'a data validation exception is thrown'
78             thrown(DataValidationException)
79         and: 'the result is not returned'
80             result == null
81     }
82
83     def "Handling missing service names as null CPS-1043."() {
84         given: 'the cps data service returns a data node from the DMI registry with empty child and leaf attributes'
85             def dataNode = new DataNode(childDataNodes:[], leaves: [:])
86             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
87         when: 'retrieving the yang modelled cm handle'
88             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
89         then: 'the service names ae returned as null'
90             result.dmiServiceName == null
91             result.dmiDataServiceName == null
92             result.dmiModelServiceName == null
93     }
94 }