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