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