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