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
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
15 import java.io.ByteArrayInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
20 import java.nio.charset.StandardCharsets;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.impl.TestUtils;
28 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.xml.sax.SAXException;
38 public class Bug2964Test {
40 public static final String XML_CONTENT = "<cont2 xmlns=\"urn:opendaylight:yangtools:leafref:test\">\n"
41 + "<point-to-identityrefleaf>test-identity</point-to-identityrefleaf>\n" + "</cont2>";
43 private static final DocumentBuilderFactory BUILDERFACTORY;
45 private static final String NAMESPACE = "urn:opendaylight:yangtools:leafref:test";
46 private static final String TEST_IDENTITY = "test-identity";
47 private static final String CONT_2 = "cont2";
48 private static final String IDENTITY_LEAFREF = "point-to-identityrefleaf";
51 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
52 factory.setNamespaceAware(true);
53 factory.setCoalescing(true);
54 factory.setIgnoringElementContentWhitespace(true);
55 factory.setIgnoringComments(true);
56 BUILDERFACTORY = factory;
59 private SchemaContext schema;
62 public void setUp() throws Exception {
63 final File leafRefTestYang = new File(getClass().getResource("/leafref-test.yang").toURI());
64 schema = TestUtils.parseYangSources(leafRefTestYang);
67 public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
68 return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8)));
72 public void testLeafrefIdentityRefDeserialization() throws Exception {
73 final URI namespaceUri = new URI(NAMESPACE);
75 final Document document = readXmlToDocument(XML_CONTENT);
76 final Element identityLeafRefElement = (Element) document.getDocumentElement().getFirstChild().getNextSibling();
78 final Module leafrefModule = schema.findModuleByNamespaceAndRevision(namespaceUri, null);
79 final ContainerSchemaNode cont2 = (ContainerSchemaNode) leafrefModule.getDataChildByName(QName.create(
80 leafrefModule.getQNameModule(), CONT_2));
81 final DataSchemaNode identityLeafRefSchema = cont2.getDataChildByName(QName.create(
82 leafrefModule.getQNameModule(), IDENTITY_LEAFREF));
83 final Object parsedValue = DomUtils.parseXmlValue(identityLeafRefElement, DomUtils.defaultValueCodecProvider(),
84 identityLeafRefSchema, ((LeafSchemaNode) identityLeafRefSchema).getType(), schema);
86 assertThat(parsedValue, instanceOf(QName.class));
87 final QName parsedQName = (QName) parsedValue;
88 assertEquals(namespaceUri, parsedQName.getNamespace());
89 assertEquals(TEST_IDENTITY, parsedQName.getLocalName());
92 public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
93 final DocumentBuilder dBuilder;
95 dBuilder = BUILDERFACTORY.newDocumentBuilder();
96 } catch (final ParserConfigurationException e) {
97 throw new IllegalStateException("Failed to parse XML document", e);
99 final Document doc = dBuilder.parse(xmlContent);
101 doc.getDocumentElement().normalize();