2 * Copyright (c) 2018 Pantheon Technologies, s.r.o.. 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;
10 import static org.junit.Assert.assertNotEquals;
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;
27 abstract class AbstractYinExportTest {
28 final void exportYinModules(final String yangDir, final String yinDir) throws IOException, SAXException,
30 final SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory(yangDir);
31 final Collection<Module> modules = schemaContext.getModules();
32 assertNotEquals(0, modules.size());
34 for (Module module : modules) {
35 readAndValidateModule(schemaContext, module, yinDir);
37 for (Module submodule : module.getSubmodules()) {
38 readAndValidateSubmodule(schemaContext, module, submodule, yinDir);
43 void validateOutput(final String yinDir, final String fileName, final String fileBody) throws IOException,
45 assertNotEquals(0, fileBody.length());
47 final Document doc = YinExportTestUtils.loadDocument(yinDir + "/" + fileName);
48 assertXMLEquals(fileName, doc, fileBody);
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, exportLegacy(schemaContext, module));
56 validateOutput(yinDir, fileName, export(module));
59 private void readAndValidateSubmodule(final SchemaContext schemaContext, final Module module,
60 final Module submodule, final String yinDir) throws XMLStreamException, IOException, SAXException {
61 final String fileName = YinExportUtils.wellFormedYinName(submodule.getName(), submodule.getRevision());
62 validateOutput(yinDir, fileName, exportLegacy(schemaContext, submodule));
63 validateOutput(yinDir, fileName, export(module, submodule));
66 private static String export(final Module module) throws XMLStreamException {
67 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
68 YinExportUtils.writeModuleAsYinText(module, bos);
69 return new String(bos.toByteArray(), StandardCharsets.UTF_8);
72 private static String export(final Module module, final Module submodule) throws XMLStreamException {
73 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
74 YinExportUtils.writeSubmoduleAsYinText(module, submodule, bos);
75 return new String(bos.toByteArray(), StandardCharsets.UTF_8);
78 private static String exportLegacy(final SchemaContext schemaContext, final Module module)
79 throws XMLStreamException {
80 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
81 YinExportUtils.writeModuleToOutputStream(schemaContext, module, bos);
82 return new String(bos.toByteArray(), StandardCharsets.UTF_8);
85 private static void assertXMLEquals(final String fileName, final Document expectedXMLDoc, final String output)
86 throws SAXException, IOException {
87 final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
89 XMLUnit.setIgnoreWhitespace(true);
90 XMLUnit.setNormalize(true);
91 XMLUnit.setNormalizeWhitespace(true);
93 final Diff diff = new Diff(expected, output);
94 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
95 XMLAssert.assertXMLEqual(fileName, diff, true);