Move yang-model-export unit tests
[yangtools.git] / yang / yang-model-export / src / test / java / org / opendaylight / yangtools / yang / model / export / EffectiveSchemaContextEmitterTest.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;
9
10 import static org.junit.Assert.assertNotEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import com.google.common.collect.BiMap;
14 import com.google.common.collect.HashBiMap;
15 import java.io.BufferedOutputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.File;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.net.URI;
22 import java.util.Map;
23 import javax.xml.stream.XMLOutputFactory;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26 import org.custommonkey.xmlunit.Diff;
27 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
28 import org.custommonkey.xmlunit.XMLAssert;
29 import org.custommonkey.xmlunit.XMLUnit;
30 import org.junit.Test;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
36 import org.opendaylight.yangtools.yang.model.export.SchemaContextEmitter.EffectiveSchemaContextEmitter;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
38 import org.w3c.dom.Document;
39 import org.xml.sax.SAXException;
40
41 public class EffectiveSchemaContextEmitterTest {
42     @Test
43     public void test() throws Exception {
44         final SchemaContext schema = YangParserTestUtils.parseYangResource("/bugs/bug2444/yang/notification.yang");
45         assertNotNull(schema);
46
47         final File outDir = new File("target/bug2444-export");
48         outDir.mkdirs();
49
50         for (final Module module : schema.getModules()) {
51             exportModule(schema, module, outDir);
52             final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
53             final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
54             try {
55                 writeModuleToOutputStream(schema, module, bufferedOutputStream, false);
56                 final String output = byteArrayOutputStream.toString();
57                 assertNotNull(output);
58                 assertNotEquals(0, output.length());
59
60                 final Document doc = YinExportTestUtils.loadDocument("/bugs/bug2444/yin-effective-emitter", module);
61                 assertXMLEquals(module.getName(), doc, output);
62             } finally {
63                 byteArrayOutputStream.close();
64                 bufferedOutputStream.close();
65             }
66         }
67     }
68
69     private static void writeModuleToOutputStream(final SchemaContext ctx, final Module module, final OutputStream str,
70             final boolean emitInstantiated) throws XMLStreamException {
71         final XMLOutputFactory factory = XMLOutputFactory.newFactory();
72         final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(str);
73         writeModuleToOutputStream(ctx, module, xmlStreamWriter, emitInstantiated);
74         xmlStreamWriter.flush();
75     }
76
77     private static void writeModuleToOutputStream(final SchemaContext ctx, final Module module,
78             final XMLStreamWriter xmlStreamWriter, final boolean emitInstantiated) {
79         final URI moduleNs = module.getNamespace();
80         final Map<String, URI> prefixToNs = prefixToNamespace(ctx, module);
81         final StatementTextWriter statementWriter = SingleModuleYinStatementWriter.create(xmlStreamWriter, moduleNs,
82                 prefixToNs);
83         final YangModuleWriter yangSchemaWriter = SchemaToStatementWriterAdaptor.from(statementWriter);
84         final Map<QName, StatementDefinition> extensions = ExtensionStatement.mapFrom(ctx.getExtensions());
85         new EffectiveSchemaContextEmitter(yangSchemaWriter, extensions, module.getYangVersion(), emitInstantiated)
86         .emitModule(module);
87     }
88
89     private static Map<String, URI> prefixToNamespace(final SchemaContext ctx, final Module module) {
90         final BiMap<String, URI> prefixMap = HashBiMap.create(module.getImports().size() + 1);
91         prefixMap.put(module.getPrefix(), module.getNamespace());
92         for (final ModuleImport imp : module.getImports()) {
93             final String prefix = imp.getPrefix();
94             final URI namespace = getModuleNamespace(ctx, imp.getModuleName());
95             prefixMap.put(prefix, namespace);
96         }
97         return prefixMap;
98     }
99
100     private static URI getModuleNamespace(final SchemaContext ctx, final String moduleName) {
101         for (final Module module : ctx.getModules()) {
102             if (moduleName.equals(module.getName())) {
103                 return module.getNamespace();
104             }
105         }
106         throw new IllegalArgumentException("Module " + moduleName + "does not exists in provided schema context");
107     }
108
109     private static File exportModule(final SchemaContext schemaContext, final Module module, final File outDir)
110             throws Exception {
111         final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
112         try (OutputStream output = new FileOutputStream(outFile)) {
113             writeModuleToOutputStream(schemaContext, module, output, false);
114         }
115         return outFile;
116     }
117
118     private static void assertXMLEquals(final String name, final Document expectedXMLDoc, final String output)
119             throws SAXException, IOException {
120         final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());
121
122         XMLUnit.setIgnoreWhitespace(true);
123         XMLUnit.setNormalize(true);
124         XMLUnit.setNormalizeWhitespace(true);
125
126         final Diff diff = new Diff(expected, output);
127         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
128         XMLAssert.assertXMLEqual(name, diff, true);
129     }
130 }