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