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