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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.operations
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
28 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
29 import org.onap.cps.spi.model.DataNode
30 import spock.lang.Specification
32 class YangModelCmHandleRetrieverSpec extends Specification {
34 def mockCpsDataService = Mock(CpsDataService)
36 def objectUnderTest = new YangModelCmHandleRetriever(mockCpsDataService)
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']"
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"])]
47 def childDataNodesForCmHandleWithDMIProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"])]
50 def childDataNodesForCmHandleWithPublicProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
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")]
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'