Propagate @NonNull collection annotations
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.java
1 /*
2  * Copyright (c) 2016 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.stmt;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
36 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
41 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
43 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.xml.sax.SAXException;
48
49 public final class TestUtils {
50     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
51
52     private TestUtils() {
53     }
54
55     public static SchemaContext loadModules(final URI resourceDirectory)
56             throws ReactorException, IOException, YangSyntaxErrorException {
57         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
58         File[] files = new File(resourceDirectory).listFiles();
59
60         for (File file : files) {
61             if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
62                 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
63             } else {
64                 LOG.info("Ignoring non-yang file {}", file);
65             }
66         }
67
68         return reactor.buildEffective();
69     }
70
71     public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
72             throws IOException, ReactorException, YangSyntaxErrorException {
73         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
74
75         for (String resourceName : resourceNames) {
76             reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
77                 resourceName)));
78         }
79
80         return reactor.buildEffective();
81     }
82
83     public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
84             IOException {
85         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
86
87         for (File file : new File(resourceDirectory).listFiles()) {
88             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
89                 YinTextSchemaSource.forFile(file))));
90         }
91
92         return reactor.buildEffective();
93     }
94
95     public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
96             IOException {
97         final SchemaContext ctx = RFC7950Reactors.defaultReactor().newBuild()
98                 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
99                 .buildEffective();
100         return ctx.getModules().iterator().next();
101     }
102
103     public static Optional<? extends @NonNull Module> findModule(final SchemaContext context, final String moduleName) {
104         return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
105     }
106
107     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
108         for (ModuleImport moduleImport : imports) {
109             if (moduleImport.getPrefix().equals(prefix)) {
110                 return moduleImport;
111             }
112         }
113         return null;
114     }
115
116     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
117             final String name) {
118         for (TypeDefinition<?> td : typedefs) {
119             if (td.getQName().getLocalName().equals(name)) {
120                 return td;
121             }
122         }
123         return null;
124     }
125
126     public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
127         List<QName> path = new ArrayList<>(names.length);
128         for (String name : names) {
129             path.add(QName.create(module, name));
130         }
131         return SchemaPath.create(path, absolute);
132     }
133
134     /**
135      * Test if node has augmenting flag set to expected value. In case this is
136      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
137      *
138      * @param node
139      *            node to check
140      * @param expected
141      *            expected value
142      */
143     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
144         assertEquals(expected, node.isAugmenting());
145         if (node instanceof DataNodeContainer) {
146             for (DataSchemaNode child : ((DataNodeContainer) node)
147                     .getChildNodes()) {
148                 checkIsAugmenting(child, expected);
149             }
150         } else if (node instanceof ChoiceSchemaNode) {
151             for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
152                 checkIsAugmenting(caseNode, expected);
153             }
154         }
155     }
156
157     /**
158      * Check if node has addedByUses flag set to expected value. In case this is
159      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
160      *
161      * @param node
162      *            node to check
163      * @param expected
164      *            expected value
165      */
166     public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
167         assertEquals(expected, node.isAddedByUses());
168         if (node instanceof DataNodeContainer) {
169             for (DataSchemaNode child : ((DataNodeContainer) node)
170                     .getChildNodes()) {
171                 checkIsAddedByUses(child, expected);
172             }
173         } else if (node instanceof ChoiceSchemaNode) {
174             for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
175                 checkIsAddedByUses(caseNode, expected);
176             }
177         }
178     }
179
180     public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
181         assertEquals(expected, node.isAddedByUses());
182         for (DataSchemaNode child : node.getChildNodes()) {
183             checkIsAddedByUses(child, expected);
184         }
185     }
186
187     public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
188         List<Module> result = new ArrayList<>();
189         for (Module module : modules) {
190             if (module.getName().equals(moduleName)) {
191                 result.add(module);
192             }
193         }
194         return result;
195     }
196
197     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
198         return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
199     }
200
201     public static SchemaContext parseYangSources(final File... files)
202             throws ReactorException, IOException, YangSyntaxErrorException {
203
204         StatementStreamSource[] sources = new StatementStreamSource[files.length];
205
206         for (int i = 0; i < files.length; i++) {
207             sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
208         }
209
210         return parseYangSources(sources);
211     }
212
213     public static SchemaContext parseYangSources(final Collection<File> files)
214             throws ReactorException, IOException, YangSyntaxErrorException {
215         return parseYangSources(files.toArray(new File[files.size()]));
216     }
217
218     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
219             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
220
221         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
222         File testSourcesDir = new File(resourceDir.toURI());
223
224         return parseYangSources(testSourcesDir.listFiles());
225     }
226
227     public static SchemaContext parseYangSource(final String yangSourceFilePath)
228             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
229
230         URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
231         File testSourcesFile = new File(resourceFile.toURI());
232
233         return parseYangSources(testSourcesFile);
234     }
235 }