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.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;
48 public final class TestUtils {
49 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
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();
59 for (File file : files) {
60 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
61 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
63 LOG.info("Ignoring non-yang file {}", file);
67 return reactor.buildEffective();
70 public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
71 throws IOException, ReactorException, YangSyntaxErrorException {
72 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
74 for (String resourceName : resourceNames) {
75 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
79 return reactor.buildEffective();
82 public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
84 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
86 for (File file : new File(resourceDirectory).listFiles()) {
87 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
88 YinTextSchemaSource.forFile(file))));
91 return reactor.buildEffective();
94 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
96 final SchemaContext ctx = RFC7950Reactors.defaultReactor().newBuild()
97 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
99 return ctx.getModules().iterator().next();
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();
106 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
107 for (ModuleImport moduleImport : imports) {
108 if (moduleImport.getPrefix().equals(prefix)) {
115 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
117 for (TypeDefinition<?> td : typedefs) {
118 if (td.getQName().getLocalName().equals(name)) {
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));
130 return SchemaPath.create(path, absolute);
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.
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)
147 checkIsAugmenting(child, expected);
149 } else if (node instanceof ChoiceSchemaNode) {
150 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
151 checkIsAugmenting(caseNode, expected);
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.
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)
170 checkIsAddedByUses(child, expected);
172 } else if (node instanceof ChoiceSchemaNode) {
173 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
174 checkIsAddedByUses(caseNode, expected);
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);
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)) {
196 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
197 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
200 public static SchemaContext parseYangSources(final File... files)
201 throws ReactorException, IOException, YangSyntaxErrorException {
203 StatementStreamSource[] sources = new StatementStreamSource[files.length];
205 for (int i = 0; i < files.length; i++) {
206 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
209 return parseYangSources(sources);
212 public static SchemaContext parseYangSources(final Collection<File> files)
213 throws ReactorException, IOException, YangSyntaxErrorException {
214 return parseYangSources(files.toArray(new File[files.size()]));
217 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
218 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
220 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
221 File testSourcesDir = new File(resourceDir.toURI());
223 return parseYangSources(testSourcesDir.listFiles());
226 public static SchemaContext parseYangSource(final String yangSourceFilePath)
227 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
229 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
230 File testSourcesFile = new File(resourceFile.toURI());
232 return parseYangSources(testSourcesFile);