2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Deutsche Telekom AG
4 * Modifications Copyright (c) 2023 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils
23 import org.onap.cps.TestUtils
24 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
25 import org.xml.sax.SAXParseException
26 import spock.lang.Specification
28 class XmlFileUtilsSpec extends Specification {
30 def 'Parse a valid xml content #scenario'(){
31 given: 'YANG model schema context'
32 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
33 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
34 when: 'the xml data is parsed'
35 def parsedXmlContent = XmlFileUtils.prepareXmlContent(xmlData, schemaContext)
36 then: 'the result xml is wrapped by root node defined in YANG schema'
37 assert parsedXmlContent == expectedOutput
39 scenario | xmlData || expectedOutput
40 '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>'
41 '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>'
42 'no xml header' | '<stores><class> </class></stores>' || '<stores><class> </class></stores>'
45 def 'Parse a invalid xml content'(){
46 given: 'YANG model schema context'
47 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('bookstore.yang')
48 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
49 when: 'attempt to parse invalid xml'
50 XmlFileUtils.prepareXmlContent('invalid-xml', schemaContext)
51 then: 'a Sax Parser exception is thrown'
52 thrown(SAXParseException)
55 def 'Parse a xml content with XPath container #scenario'() {
56 given: 'YANG model schema context'
57 def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
58 def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
59 and: 'Parent schema node by xPath'
60 def parentSchemaNode = YangUtils.getDataSchemaNodeAndIdentifiersByXpath(xPath, schemaContext).get("dataSchemaNode")
61 when: 'the XML data is parsed'
62 def parsedXmlContent = XmlFileUtils.prepareXmlContent(xmlData, parentSchemaNode, xPath)
63 then: 'the result XML is wrapped by xPath defined parent root node'
64 assert parsedXmlContent == expectedOutput
66 scenario | xmlData | xPath || expectedOutput
67 '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>'
68 '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>'