2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Deutsche Telekom AG
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=========================================================
20 package org.onap.cps.utils
22 import org.onap.cps.TestUtils
23 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
24 import spock.lang.Specification
26 class XmlFileUtilsSpec extends Specification {
27 def 'Parse a valid xml content #scenario'(){
28 given: 'YANG model schema context'
29 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
30 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
31 when: 'the XML data is parsed'
32 def parsedXmlContent = XmlFileUtils.prepareXmlContent(xmlData, schemaContext)
33 then: 'the result XML is wrapped by root node defined in YANG schema'
34 assert parsedXmlContent == expectedOutput
36 scenario | xmlData || expectedOutput
37 'without root data node' | '<?xml version="1.0" encoding="UTF-8"?><class> </class>' || '<?xml version="1.0" encoding="UTF-8"?><stores xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><class> </class></stores>'
38 'with root data node' | '<?xml version="1.0" encoding="UTF-8"?><stores><class> </class></stores>' || '<?xml version="1.0" encoding="UTF-8"?><stores><class> </class></stores>'
39 'no xml header' | '<stores><class> </class></stores>' || '<stores><class> </class></stores>'
42 def 'Parse a xml content with XPath container #scenario'() {
43 given: 'YANG model schema context'
44 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
45 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
46 and: 'Parent schema node by xPath'
47 def parentSchemaNode = YangUtils.getDataSchemaNodeAndIdentifiersByXpath(xPath, schemaContext)
48 .get("dataSchemaNode")
49 when: 'the XML data is parsed'
50 def parsedXmlContent = XmlFileUtils.prepareXmlContent(xmlData, parentSchemaNode, xPath)
51 then: 'the result XML is wrapped by xPath defined parent root node'
52 assert parsedXmlContent == expectedOutput
54 scenario | xmlData | xPath || expectedOutput
55 'XML element test tree' | '<?xml version="1.0" encoding="UTF-8"?><test-tree xmlns="org:onap:cps:test:test-tree"><branch><name>Left</name><nest><name>Small</name><birds>Sparrow</birds></nest></branch></test-tree>' | '/test-tree' || '<?xml version="1.0" encoding="UTF-8"?><test-tree xmlns="org:onap:cps:test:test-tree"><branch><name>Left</name><nest><name>Small</name><birds>Sparrow</birds></nest></branch></test-tree>'
56 'without root data node' | '<?xml version="1.0" encoding="UTF-8"?><nest xmlns="org:onap:cps:test:test-tree"><name>Small</name><birds>Sparrow</birds></nest>' | '/test-tree/branch[@name=\'Branch\']' || '<?xml version="1.0" encoding="UTF-8"?><branch xmlns="org:onap:cps:test:test-tree"><name>Branch</name><nest><name>Small</name><birds>Sparrow</birds></nest></branch>'