cd8776f26a142df7116e7a5fa971b7359a8394db
[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.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 import org.w3c.dom.Document;
29 import org.xml.sax.SAXException;
30
31 public class Bug2444Test {
32     @Test
33     public void test() throws Exception {
34         final SchemaContext schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/bug2444/yang");
35         assertNotNull(schema);
36
37         final ImmutableSet<Module> modulesAndSubmodules = getAllModulesAndSubmodules(schema);
38         for (final Module module : modulesAndSubmodules) {
39             final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
40             final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
41             try {
42                 YinExportUtils.writeModuleToOutputStream(schema, module, bufferedOutputStream);
43                 final String output = byteArrayOutputStream.toString();
44                 assertNotNull(output);
45                 assertNotEquals(0, output.length());
46
47                 final Document doc = YinExportTestUtils.loadDocument("/bugs/bug2444/yin", module);
48                 assertXMLEquals(module.getName(), doc, output);
49             } finally {
50                 byteArrayOutputStream.close();
51                 bufferedOutputStream.close();
52             }
53         }
54     }
55
56     private static ImmutableSet<Module> getAllModulesAndSubmodules(final SchemaContext schema) {
57         final Builder<Module> builder = ImmutableSet.builder();
58         builder.addAll(schema.getModules());
59         for (final Module module : schema.getModules()) {
60             builder.addAll(module.getSubmodules());
61         }
62         return builder.build();
63     }
64
65     private static void assertXMLEquals(final String fileName, final Document expectedXMLDoc, final String output)
66             throws SAXException, IOException {
67         final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
68
69         XMLUnit.setIgnoreWhitespace(true);
70         XMLUnit.setNormalize(true);
71         XMLUnit.setNormalizeWhitespace(true);
72
73         final Diff diff = new Diff(expected, output);
74         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
75         XMLAssert.assertXMLEqual(fileName, diff, true);
76     }
77 }