2 * Copyright (c) 2017 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
8 package org.opendaylight.yangtools.yang.model.export.test;
10 import static org.junit.Assert.assertNotEquals;
11 import static org.junit.Assert.assertNotNull;
13 import java.io.BufferedOutputStream;
14 import java.io.ByteArrayOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import org.custommonkey.xmlunit.Diff;
20 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
21 import org.custommonkey.xmlunit.XMLAssert;
22 import org.custommonkey.xmlunit.XMLUnit;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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;
32 public class Bug2444Test {
34 public void test() throws Exception {
35 final SchemaContext schema = YangParserTestUtils.parseYangSources("/bugs/bug2444/yang");
36 assertNotNull(schema);
38 final File outDir = new File("target/bug2444-export");
41 for (final Module module : schema.getModules()) {
42 exportModule(schema, module, outDir);
44 final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
45 final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
47 YinExportUtils.writeModuleToOutputStream(schema, module, bufferedOutputStream);
48 final String output = byteArrayOutputStream.toString();
49 assertNotNull(output);
50 assertNotEquals(0, output.length());
52 final Document doc = YinExportTestUtils.loadDocument(String.format("/bugs/bug2444/yin/%s@%s.yin",
53 module.getName(), SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision())));
54 assertXMLEquals(doc, output);
56 byteArrayOutputStream.close();
57 bufferedOutputStream.close();
62 private static void assertXMLEquals(final Document expectedXMLDoc, final String output)
63 throws SAXException, IOException {
64 final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
66 XMLUnit.setIgnoreWhitespace(true);
67 XMLUnit.setNormalize(true);
68 XMLUnit.setNormalizeWhitespace(true);
70 final Diff diff = new Diff(expected, output);
71 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
72 XMLAssert.assertXMLEqual(diff, true);
75 private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
77 final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
78 try (OutputStream output = new FileOutputStream(outFile)) {
79 YinExportUtils.writeModuleToOutputStream(schemaContext, module, output);