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;
14 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
19 import java.nio.charset.StandardCharsets;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.impl.TestUtils;
27 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.xml.sax.SAXException;
37 public class Bug2964Test {
39 public static final String XML_CONTENT = "<cont2 xmlns=\"urn:opendaylight:yangtools:leafref:test\">\n"
40 + "<point-to-identityrefleaf>test-identity</point-to-identityrefleaf>\n" + "</cont2>";
42 private static final DocumentBuilderFactory BUILDERFACTORY;
44 private static final String NAMESPACE = "urn:opendaylight:yangtools:leafref:test";
45 private static final String TEST_IDENTITY = "test-identity";
46 private static final String CONT_2 = "cont2";
47 private static final String IDENTITY_LEAFREF = "point-to-identityrefleaf";
50 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51 factory.setNamespaceAware(true);
52 factory.setCoalescing(true);
53 factory.setIgnoringElementContentWhitespace(true);
54 factory.setIgnoringComments(true);
55 BUILDERFACTORY = factory;
58 private SchemaContext schema;
61 public void setUp() throws Exception {
62 File leafRefTestYang = new File(getClass().getResource("/leafref-test.yang").toURI());
63 schema = TestUtils.parseYangSources(leafRefTestYang);
66 public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
67 return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8)));
71 public void testLeafrefIdentityRefDeserialization() throws Exception {
72 final URI namespaceUri = new URI(NAMESPACE);
74 final Document document = readXmlToDocument(XML_CONTENT);
75 final Element identityLeafRefElement = (Element) document.getDocumentElement().getFirstChild().getNextSibling();
77 final Module leafrefModule = schema.findModuleByNamespaceAndRevision(namespaceUri, null);
78 final ContainerSchemaNode cont2 = (ContainerSchemaNode) leafrefModule.getDataChildByName(CONT_2);
79 final DataSchemaNode identityLeafRefSchema = cont2.getDataChildByName(IDENTITY_LEAFREF);
80 final Object parsedValue = DomUtils.parseXmlValue(identityLeafRefElement, DomUtils.defaultValueCodecProvider(),
81 identityLeafRefSchema, ((LeafSchemaNode) identityLeafRefSchema).getType(), schema);
83 assertThat(parsedValue, instanceOf(QName.class));
84 final QName parsedQName = (QName) parsedValue;
85 assertEquals(namespaceUri, parsedQName.getNamespace());
86 assertEquals(TEST_IDENTITY, parsedQName.getLocalName());
89 public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
90 final DocumentBuilder dBuilder;
92 dBuilder = BUILDERFACTORY.newDocumentBuilder();
93 } catch (final ParserConfigurationException e) {
94 throw new IllegalStateException("Failed to parse XML document", e);
96 final Document doc = dBuilder.parse(xmlContent);
98 doc.getDocumentElement().normalize();