1b421f793f4d754065d361cffcf2608eca843e64
[yangtools.git] / yang / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / test / SchemaContextEmitterTest.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.model.export.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import java.io.BufferedOutputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.net.URISyntaxException;
20 import javax.xml.stream.XMLStreamException;
21 import org.custommonkey.xmlunit.Diff;
22 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
23 import org.custommonkey.xmlunit.XMLAssert;
24 import org.custommonkey.xmlunit.XMLUnit;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.w3c.dom.Document;
32 import org.xml.sax.SAXException;
33
34 public class SchemaContextEmitterTest {
35
36     @Test
37     public void testSchemaContextEmitter() throws ReactorException, IOException, URISyntaxException,
38             XMLStreamException, SAXException {
39         final SchemaContext schemaContext = YangParserTestUtils.parseYangSources("/schema-context-emitter-test");
40         assertNotNull(schemaContext);
41         assertEquals(1, schemaContext.getModules().size());
42
43         final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
44         final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
45
46         for (final Module module : schemaContext.getModules()) {
47             YinExportUtils.writeModuleToOutputStream(schemaContext, module, bufferedOutputStream);
48         }
49
50         final String output = byteArrayOutputStream.toString();
51         assertNotNull(output);
52         assertNotEquals(0, output.length());
53
54         final Document doc = YinExportTestUtils.loadDocument("/schema-context-emitter-test/foo.yin");
55         final String expected = YinExportTestUtils.toString(doc.getDocumentElement());
56
57         XMLUnit.setIgnoreWhitespace(true);
58         XMLUnit.setNormalize(true);
59
60         final Diff diff = new Diff(expected, output);
61         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
62         XMLAssert.assertXMLEqual(diff, true);
63     }
64 }