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