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 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;
31 public class Bug2444Test {
33 public void test() throws Exception {
34 final SchemaContext schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/bug2444/yang");
35 assertNotNull(schema);
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);
42 YinExportUtils.writeModuleToOutputStream(schema, module, bufferedOutputStream);
43 final String output = byteArrayOutputStream.toString();
44 assertNotNull(output);
45 assertNotEquals(0, output.length());
47 final Document doc = YinExportTestUtils.loadDocument("/bugs/bug2444/yin", module);
48 assertXMLEquals(module.getName(), doc, output);
50 byteArrayOutputStream.close();
51 bufferedOutputStream.close();
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());
62 return builder.build();
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());
69 XMLUnit.setIgnoreWhitespace(true);
70 XMLUnit.setNormalize(true);
71 XMLUnit.setNormalizeWhitespace(true);
73 final Diff diff = new Diff(expected, output);
74 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
75 XMLAssert.assertXMLEqual(fileName, diff, true);