f629b96b970d12e1703374e0e625eeddcf1da442
[yangtools.git] / yang / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / test / Bug2444Test.java
1 /*
2  * Copyright (c) 2017 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 package org.opendaylight.yangtools.yang.model.export.test;
9
10 import static org.junit.Assert.assertNotEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.io.BufferedOutputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.File;
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;
31
32 public class Bug2444Test {
33     @Test
34     public void test() throws Exception {
35         final SchemaContext schema = YangParserTestUtils.parseYangSources("/bugs/bug2444/yang");
36         assertNotNull(schema);
37
38         final File outDir = new File("target/bug2444-export");
39         outDir.mkdirs();
40
41         for (final Module module : schema.getModules()) {
42             exportModule(schema, module, outDir);
43
44             final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
45             final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
46             try {
47                 YinExportUtils.writeModuleToOutputStream(schema, module, bufferedOutputStream);
48                 final String output = byteArrayOutputStream.toString();
49                 assertNotNull(output);
50                 assertNotEquals(0, output.length());
51
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);
55             } finally {
56                 byteArrayOutputStream.close();
57                 bufferedOutputStream.close();
58             }
59         }
60     }
61
62     private static void assertXMLEquals(final Document expectedXMLDoc, final String output)
63             throws SAXException, IOException {
64         final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
65
66         XMLUnit.setIgnoreWhitespace(true);
67         XMLUnit.setNormalize(true);
68         XMLUnit.setNormalizeWhitespace(true);
69
70         final Diff diff = new Diff(expected, output);
71         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
72         XMLAssert.assertXMLEqual(diff, true);
73     }
74
75     private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
76             throws Exception {
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);
80         }
81         return outFile;
82     }
83 }