Use java.nio.file.Path in YangTextFileSchemaSource
[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 yangFilesDirectoryPath)
52             throws Exception {
53         final var files = new File(TestUtils.class.getResource(yangFilesDirectoryPath).toURI())
54             .listFiles(StmtTestUtils.YANG_FILE_FILTER);
55         final var sources = new ArrayList<StatementStreamSource>(files.length);
56         for (var file : files) {
57             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
58         }
59         return sources;
60     }
61
62     public static EffectiveModelContext loadModules(final URI resourceDirectory)
63             throws ReactorException, IOException, YangSyntaxErrorException {
64         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
65         File[] files = new File(resourceDirectory).listFiles();
66
67         for (File file : files) {
68             if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
69                 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
70             } else {
71                 LOG.info("Ignoring non-yang file {}", file);
72             }
73         }
74
75         return reactor.buildEffective();
76     }
77
78     public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
79             throws IOException, ReactorException, YangSyntaxErrorException {
80         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
81
82         for (String resourceName : resourceNames) {
83             reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
84                 resourceName)));
85         }
86
87         return reactor.buildEffective();
88     }
89
90     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
91             throws ReactorException, SAXException, IOException {
92         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
93
94         for (File file : new File(resourceDirectory).listFiles()) {
95             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
96                 YinTextSchemaSource.forPath(file.toPath()))));
97         }
98
99         return reactor.buildEffective();
100     }
101
102     public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
103             IOException {
104         return RFC7950Reactors.defaultReactor().newBuild()
105                 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
106                 .buildEffective()
107                 .getModules().iterator().next();
108     }
109
110     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
111         for (ModuleImport moduleImport : imports) {
112             if (moduleImport.getPrefix().equals(prefix)) {
113                 return moduleImport;
114             }
115         }
116         return null;
117     }
118
119     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
120             final String name) {
121         for (TypeDefinition<?> td : typedefs) {
122             if (td.getQName().getLocalName().equals(name)) {
123                 return td;
124             }
125         }
126         return null;
127     }
128
129     /**
130      * Test if node has augmenting flag set to expected value. In case this is
131      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
132      *
133      * @param node
134      *            node to check
135      * @param expected
136      *            expected value
137      */
138     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
139         assertEquals(expected, node.isAugmenting());
140         if (node instanceof DataNodeContainer) {
141             for (DataSchemaNode child : ((DataNodeContainer) node)
142                     .getChildNodes()) {
143                 checkIsAugmenting(child, expected);
144             }
145         } else if (node instanceof ChoiceSchemaNode) {
146             for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
147                 checkIsAugmenting(caseNode, expected);
148             }
149         }
150     }
151
152     public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
153         List<Module> result = new ArrayList<>();
154         for (Module module : modules) {
155             if (module.getName().equals(moduleName)) {
156                 result.add(module);
157             }
158         }
159         return result;
160     }
161
162     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
163             throws ReactorException {
164         return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
165     }
166
167     public static EffectiveModelContext parseYangSources(final File... files)
168             throws ReactorException, IOException, YangSyntaxErrorException {
169
170         StatementStreamSource[] sources = new StatementStreamSource[files.length];
171
172         for (int i = 0; i < files.length; i++) {
173             sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forPath(files[i].toPath()));
174         }
175
176         return parseYangSources(sources);
177     }
178
179     public static EffectiveModelContext parseYangSources(final Collection<File> files)
180             throws ReactorException, IOException, YangSyntaxErrorException {
181         return parseYangSources(files.toArray(new File[files.size()]));
182     }
183
184     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
185             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
186
187         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
188         File testSourcesDir = new File(resourceDir.toURI());
189
190         return parseYangSources(testSourcesDir.listFiles(
191             (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
192     }
193
194     public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
195             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
196
197         URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
198         File testSourcesFile = new File(resourceFile.toURI());
199
200         return parseYangSources(testSourcesFile);
201     }
202 }