Fix yang-export warnings
[yangtools.git] / model / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / AbstractYinExportTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o..  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;
9
10 import static org.junit.Assert.assertNotEquals;
11
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.nio.charset.StandardCharsets;
15 import java.util.Collection;
16 import javax.xml.stream.XMLStreamException;
17 import org.custommonkey.xmlunit.Diff;
18 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
19 import org.custommonkey.xmlunit.XMLAssert;
20 import org.custommonkey.xmlunit.XMLUnit;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.Submodule;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 import org.w3c.dom.Document;
26 import org.xml.sax.SAXException;
27
28 abstract class AbstractYinExportTest {
29     final void exportYinModules(final String yangDir, final String yinDir) throws IOException, SAXException,
30             XMLStreamException {
31         final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResourceDirectory(yangDir);
32         final Collection<? extends Module> modules = schemaContext.getModules();
33         assertNotEquals(0, modules.size());
34
35         for (Module module : modules) {
36             readAndValidateModule(schemaContext, module, yinDir);
37
38             for (Submodule submodule : module.getSubmodules()) {
39                 readAndValidateSubmodule(schemaContext, module, submodule, yinDir);
40             }
41         }
42     }
43
44     void validateOutput(final String yinDir, final String fileName, final String fileBody) throws IOException,
45             SAXException {
46         assertNotEquals(0, fileBody.length());
47         if (yinDir != null) {
48             final Document doc = YinExportTestUtils.loadDocument(yinDir + "/" + fileName);
49             assertXMLEquals(fileName, doc, fileBody);
50         }
51     }
52
53     private void readAndValidateModule(final EffectiveModelContext schemaContext, final Module module,
54             final String yinDir) throws XMLStreamException, IOException, SAXException {
55         final String fileName = YinExportUtils.wellFormedYinName(module.getName(), module.getRevision());
56         validateOutput(yinDir, fileName, export(module));
57     }
58
59     private void readAndValidateSubmodule(final EffectiveModelContext schemaContext, final Module module,
60             final Submodule submodule, final String yinDir) throws XMLStreamException, IOException, SAXException {
61         final String fileName = YinExportUtils.wellFormedYinName(submodule.getName(), submodule.getRevision());
62         validateOutput(yinDir, fileName, export(module, submodule));
63     }
64
65     private static String export(final Module module) throws XMLStreamException {
66         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
67         YinExportUtils.writeModuleAsYinText(module.asEffectiveStatement(), bos);
68         return bos.toString(StandardCharsets.UTF_8);
69     }
70
71     private static String export(final Module module, final Submodule submodule) throws XMLStreamException {
72         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
73         YinExportUtils.writeSubmoduleAsYinText(module.asEffectiveStatement(), submodule.asEffectiveStatement(), bos);
74         return bos.toString(StandardCharsets.UTF_8);
75     }
76
77     private static void assertXMLEquals(final String fileName, final Document expectedXMLDoc, final String output)
78             throws SAXException, IOException {
79         final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
80
81         XMLUnit.setIgnoreWhitespace(true);
82         XMLUnit.setNormalize(true);
83         XMLUnit.setNormalizeWhitespace(true);
84
85         final Diff diff = new Diff(expected, output);
86         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
87         XMLAssert.assertXMLEqual(fileName, diff, true);
88     }
89 }