Remove use of Guava Charsets
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / Bug2964Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
10
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;
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.URI;
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;
36
37 public class Bug2964Test {
38
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>";
41
42     private static final DocumentBuilderFactory BUILDERFACTORY;
43
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";
48
49     static {
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;
56     }
57
58     private SchemaContext schema;
59
60     @Before
61     public void setUp() throws Exception {
62         File leafRefTestYang = new File(getClass().getResource("/leafref-test.yang").toURI());
63         schema = TestUtils.parseYangSources(leafRefTestYang);
64     }
65
66     public static Document readXmlToDocument(final String xmlContent) throws SAXException, IOException {
67         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8)));
68     }
69
70     @Test
71     public void testLeafrefIdentityRefDeserialization() throws Exception {
72         final URI namespaceUri = new URI(NAMESPACE);
73
74         final Document document = readXmlToDocument(XML_CONTENT);
75         final Element identityLeafRefElement = (Element) document.getDocumentElement().getFirstChild().getNextSibling();
76
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);
82
83         assertThat(parsedValue, instanceOf(QName.class));
84         final QName parsedQName = (QName) parsedValue;
85         assertEquals(namespaceUri, parsedQName.getNamespace());
86         assertEquals(TEST_IDENTITY, parsedQName.getLocalName());
87     }
88
89     public static Document readXmlToDocument(final InputStream xmlContent) throws SAXException, IOException {
90         final DocumentBuilder dBuilder;
91         try {
92             dBuilder = BUILDERFACTORY.newDocumentBuilder();
93         } catch (final ParserConfigurationException e) {
94             throw new IllegalStateException("Failed to parse XML document", e);
95         }
96         final Document doc = dBuilder.parse(xmlContent);
97
98         doc.getDocumentElement().normalize();
99         return doc;
100     }
101 }