2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertEquals;
13 import java.io.IOException;
15 import java.net.URISyntaxException;
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;
49 public final class TestUtils {
50 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
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();
60 for (File file : files) {
61 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
62 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
64 LOG.info("Ignoring non-yang file {}", file);
68 return reactor.buildEffective();
71 public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
72 throws IOException, ReactorException, YangSyntaxErrorException {
73 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
75 for (String resourceName : resourceNames) {
76 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
80 return reactor.buildEffective();
83 public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
85 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
87 for (File file : new File(resourceDirectory).listFiles()) {
88 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
89 YinTextSchemaSource.forFile(file))));
92 return reactor.buildEffective();
95 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
97 final SchemaContext ctx = RFC7950Reactors.defaultReactor().newBuild()
98 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
100 return ctx.getModules().iterator().next();
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();
107 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
108 for (ModuleImport moduleImport : imports) {
109 if (moduleImport.getPrefix().equals(prefix)) {
116 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
118 for (TypeDefinition<?> td : typedefs) {
119 if (td.getQName().getLocalName().equals(name)) {
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));
131 return SchemaPath.create(path, absolute);
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.
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)
148 checkIsAugmenting(child, expected);
150 } else if (node instanceof ChoiceSchemaNode) {
151 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
152 checkIsAugmenting(caseNode, expected);
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.
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)
171 checkIsAddedByUses(child, expected);
173 } else if (node instanceof ChoiceSchemaNode) {
174 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
175 checkIsAddedByUses(caseNode, expected);
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);
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)) {
197 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
198 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
201 public static SchemaContext parseYangSources(final File... files)
202 throws ReactorException, IOException, YangSyntaxErrorException {
204 StatementStreamSource[] sources = new StatementStreamSource[files.length];
206 for (int i = 0; i < files.length; i++) {
207 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
210 return parseYangSources(sources);
213 public static SchemaContext parseYangSources(final Collection<File> files)
214 throws ReactorException, IOException, YangSyntaxErrorException {
215 return parseYangSources(files.toArray(new File[files.size()]));
218 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
219 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
221 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
222 File testSourcesDir = new File(resourceDir.toURI());
224 return parseYangSources(testSourcesDir.listFiles());
227 public static SchemaContext parseYangSource(final String yangSourceFilePath)
228 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
230 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
231 File testSourcesFile = new File(resourceFile.toURI());
233 return parseYangSources(testSourcesFile);