2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.serializer;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import java.util.Collections;
22 import javax.xml.stream.XMLOutputFactory;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.util.xml.UntrustedXML;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
35 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
38 import org.w3c.dom.Document;
39 import org.xml.sax.SAXException;
41 public class Bug5396 {
42 private static final XMLOutputFactory XML_FACTORY;
45 XML_FACTORY = XMLOutputFactory.newFactory();
46 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
49 private QNameModule fooModuleQName;
51 private SchemaContext schemaContext;
54 public void Init() throws Exception {
55 fooModuleQName = QNameModule.create(new URI("foo"), SimpleDateFormatUtil.getRevisionFormat()
56 .parse("2016-03-22"));
57 root = QName.create(fooModuleQName, "root");
58 schemaContext = YangParserTestUtils
59 .parseYangFiles(new File(getClass().getResource("/bug5396/yang/foo.yang").toURI()));
63 public void test() throws Exception {
64 testInputXML("/bug5396/xml/foo.xml", "dp1o34");
65 testInputXML("/bug5396/xml/foo2.xml", "dp0s3f9");
66 testInputXML("/bug5396/xml/foo3.xml", "dp09P1p2s3");
67 testInputXML("/bug5396/xml/foo4.xml", "dp0p3p1");
68 testInputXML("/bug5396/xml/foo5.xml", "dp0s3");
71 testInputXML("/bug5396/xml/invalid-foo.xml", null);
72 fail("Test should fail due to invalid input string");
73 } catch (IllegalArgumentException e) {
74 assertTrue(e.getMessage().startsWith(
75 "Failed to parse element [my-leaf: null] as leaf AbsoluteSchemaPath{path=[(foo?revision=2016-03-22)root, (foo?revision=2016-03-22)my-leaf]"));
79 public void testInputXML(final String xmlPath, final String expectedValue) throws Exception {
80 final Document doc = loadDocument(xmlPath);
82 final ContainerNode output = DomToNormalizedNodeParserFactory
83 .getInstance(DomUtils.defaultValueCodecProvider(), schemaContext).getContainerNodeParser()
84 .parse(Collections.singletonList(doc.getDocumentElement()), schemaContext);
86 assertNotNull(output);
88 Optional<DataContainerChild<? extends PathArgument, ?>> child = output.getChild(new NodeIdentifier(root));
89 assertTrue(child.orNull() instanceof ContainerNode);
90 ContainerNode rootContainer = (ContainerNode) child.get();
92 Optional<DataContainerChild<? extends PathArgument, ?>> myLeaf = rootContainer.getChild(new NodeIdentifier(
93 QName.create(fooModuleQName, "my-leaf")));
94 assertTrue(myLeaf.orNull() instanceof LeafNode);
96 assertEquals(expectedValue, myLeaf.get().getValue());
99 private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
100 final InputStream resourceAsStream = Bug5396.class.getResourceAsStream(xmlPath);
102 final Document currentConfigElement = readXmlToDocument(resourceAsStream);
103 Preconditions.checkNotNull(currentConfigElement);
104 return currentConfigElement;
107 private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
108 final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
109 doc.getDocumentElement().normalize();