Merge branch 'master' of ../controller
[yangtools.git] / yang / 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.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24 import org.w3c.dom.Document;
25 import org.xml.sax.SAXException;
26
27 abstract class AbstractYinExportTest {
28     final void exportYinModules(final String yangDir, final String yinDir) throws IOException, SAXException,
29             XMLStreamException {
30         final SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory(yangDir);
31         final Collection<Module> modules = schemaContext.getModules();
32         assertNotEquals(0, modules.size());
33
34         for (Module module : modules) {
35             readAndValidateModule(schemaContext, module, yinDir);
36
37             for (Module submodule : module.getSubmodules()) {
38                 readAndValidateSubmodule(schemaContext, module, submodule, yinDir);
39             }
40         }
41     }
42
43     void validateOutput(final String yinDir, final String fileName, final String fileBody) throws IOException,
44             SAXException {
45         assertNotEquals(0, fileBody.length());
46         if (yinDir != null) {
47             final Document doc = YinExportTestUtils.loadDocument(yinDir + "/" + fileName);
48             assertXMLEquals(fileName, doc, fileBody);
49         }
50     }
51
52     private void readAndValidateModule(final SchemaContext schemaContext, final Module module, final String yinDir)
53             throws XMLStreamException, IOException, SAXException {
54         final String fileName = YinExportUtils.wellFormedYinName(module.getName(), module.getRevision());
55         validateOutput(yinDir, fileName, export(module));
56     }
57
58     private void readAndValidateSubmodule(final SchemaContext schemaContext, final Module module,
59             final Module submodule, final String yinDir) throws XMLStreamException, IOException, SAXException {
60         final String fileName = YinExportUtils.wellFormedYinName(submodule.getName(), submodule.getRevision());
61         validateOutput(yinDir, fileName, export(module, submodule));
62     }
63
64     private static String export(final Module module) throws XMLStreamException {
65         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
66         YinExportUtils.writeModuleAsYinText(module, bos);
67         return new String(bos.toByteArray(), StandardCharsets.UTF_8);
68     }
69
70     private static String export(final Module module, final Module submodule) throws XMLStreamException {
71         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
72         YinExportUtils.writeSubmoduleAsYinText(module, submodule, bos);
73         return new String(bos.toByteArray(), StandardCharsets.UTF_8);
74     }
75
76     private static void assertXMLEquals(final String fileName, final Document expectedXMLDoc, final String output)
77             throws SAXException, IOException {
78         final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
79
80         XMLUnit.setIgnoreWhitespace(true);
81         XMLUnit.setNormalize(true);
82         XMLUnit.setNormalizeWhitespace(true);
83
84         final Diff diff = new Diff(expected, output);
85         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
86         XMLAssert.assertXMLEqual(fileName, diff, true);
87     }
88 }