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.model.export.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
15 import com.google.common.base.Preconditions;
16 import java.io.BufferedOutputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.io.StringWriter;
22 import java.net.URISyntaxException;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.transform.OutputKeys;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.TransformerFactoryConfigurationError;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import org.custommonkey.xmlunit.Diff;
35 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
36 import org.custommonkey.xmlunit.XMLAssert;
37 import org.custommonkey.xmlunit.XMLUnit;
38 import org.junit.Test;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Node;
45 import org.xml.sax.SAXException;
47 public class SchemaContextEmitterTest {
50 public void testSchemaContextEmitter() throws ReactorException, IOException, URISyntaxException,
51 XMLStreamException, SAXException {
52 final SchemaContext schemaContext = StmtTestUtils.parseYangSources("/schema-context-emitter-test");
53 assertNotNull(schemaContext);
54 assertEquals(1, schemaContext.getModules().size());
56 final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
57 final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
59 for (Module module : schemaContext.getModules()) {
60 YinExportUtils.writeModuleToOutputStream(schemaContext, module, bufferedOutputStream);
63 final String output = byteArrayOutputStream.toString();
64 assertNotNull(output);
65 assertNotEquals(0, output.length());
67 final Document doc = loadDocument("/schema-context-emitter-test/foo.xml");
68 final String expected = toString(doc.getDocumentElement());
70 XMLUnit.setIgnoreWhitespace(true);
71 XMLUnit.setNormalize(true);
73 final Diff diff = new Diff(expected, output);
74 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
75 XMLAssert.assertXMLEqual(diff, true);
78 private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
79 final InputStream resourceAsStream = SchemaContextEmitterTest.class.getResourceAsStream(xmlPath);
80 final Document currentConfigElement = readXmlToDocument(resourceAsStream);
81 Preconditions.checkNotNull(currentConfigElement);
82 return currentConfigElement;
85 private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
86 final DocumentBuilder dBuilder;
88 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
89 factory.setNamespaceAware(true);
90 factory.setCoalescing(true);
91 factory.setIgnoringElementContentWhitespace(true);
92 factory.setIgnoringComments(true);
93 dBuilder = factory.newDocumentBuilder();
94 } catch (final ParserConfigurationException e) {
95 throw new RuntimeException("Failed to parse XML document", e);
97 final Document doc = dBuilder.parse(xmlContent);
99 doc.getDocumentElement().normalize();
103 private static String toString(final Node xml) {
105 final Transformer transformer = TransformerFactory.newInstance().newTransformer();
106 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
107 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
109 final StreamResult result = new StreamResult(new StringWriter());
110 final DOMSource source = new DOMSource(xml);
111 transformer.transform(source, result);
113 return result.getWriter().toString();
114 } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
115 throw new RuntimeException("Unable to serialize xml element " + xml, e);