34ceedb09f491b71efe969ab3ae540c6a40d3edf
[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 com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSet.Builder;
15 import java.io.BufferedOutputStream;
16 import java.io.ByteArrayOutputStream;
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.parseYangResourceDirectory("/bugs/bug2444/yang");
36         assertNotNull(schema);
37
38         final ImmutableSet<Module> modulesAndSubmodules = getAllModulesAndSubmodules(schema);
39         for (final Module module : modulesAndSubmodules) {
40             final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
41             final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
42             try {
43                 YinExportUtils.writeModuleToOutputStream(schema, module, bufferedOutputStream);
44                 final String output = byteArrayOutputStream.toString();
45                 assertNotNull(output);
46                 assertNotEquals(0, output.length());
47
48                 final Document doc = YinExportTestUtils.loadDocument(String.format("/bugs/bug2444/yin/%s@%s.yin",
49                         module.getName(), SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision())));
50                 assertXMLEquals(doc, output);
51             } finally {
52                 byteArrayOutputStream.close();
53                 bufferedOutputStream.close();
54             }
55         }
56     }
57
58     private ImmutableSet<Module> getAllModulesAndSubmodules(final SchemaContext schema) {
59         final Builder<Module> builder = ImmutableSet.builder();
60         builder.addAll(schema.getModules());
61         for (final Module module : schema.getModules()) {
62             builder.addAll(module.getSubmodules());
63         }
64         return builder.build();
65     }
66
67     private static void assertXMLEquals(final Document expectedXMLDoc, final String output)
68             throws SAXException, IOException {
69         final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
70
71         XMLUnit.setIgnoreWhitespace(true);
72         XMLUnit.setNormalize(true);
73         XMLUnit.setNormalizeWhitespace(true);
74
75         final Diff diff = new Diff(expected, output);
76         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
77         XMLAssert.assertXMLEqual(diff, true);
78     }
79 }