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;
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.ChoiceCaseNode;
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.impl.DefaultReactors;
39 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
41 import org.opendaylight.yangtools.yang.parser.rfc6020.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 = DefaultReactors.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 = DefaultReactors.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 = DefaultReactors.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 = DefaultReactors.defaultReactor().newBuild()
98 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
100 return ctx.getModules().iterator().next();
103 public static Optional<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 Set<ModuleImport> imports, final String prefix) {
108 ModuleImport result = null;
109 for (ModuleImport moduleImport : imports) {
110 if (moduleImport.getPrefix().equals(prefix)) {
111 result = moduleImport;
118 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
119 TypeDefinition<?> result = null;
120 for (TypeDefinition<?> td : typedefs) {
121 if (td.getQName().getLocalName().equals(name)) {
129 public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
130 List<QName> path = new ArrayList<>(names.length);
131 for (String name : names) {
132 path.add(QName.create(module, name));
134 return SchemaPath.create(path, absolute);
138 * Test if node has augmenting flag set to expected value. In case this is
139 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
146 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
147 assertEquals(expected, node.isAugmenting());
148 if (node instanceof DataNodeContainer) {
149 for (DataSchemaNode child : ((DataNodeContainer) node)
151 checkIsAugmenting(child, expected);
153 } else if (node instanceof ChoiceSchemaNode) {
154 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
155 checkIsAugmenting(caseNode, expected);
161 * Check if node has addedByUses flag set to expected value. In case this is
162 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
169 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
170 assertEquals(expected, node.isAddedByUses());
171 if (node instanceof DataNodeContainer) {
172 for (DataSchemaNode child : ((DataNodeContainer) node)
174 checkIsAddedByUses(child, expected);
176 } else if (node instanceof ChoiceSchemaNode) {
177 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
178 checkIsAddedByUses(caseNode, expected);
183 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
184 assertEquals(expected, node.isAddedByUses());
185 for (DataSchemaNode child : node.getChildNodes()) {
186 checkIsAddedByUses(child, expected);
190 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
191 List<Module> result = new ArrayList<>();
192 for (Module module : modules) {
193 if (module.getName().equals(moduleName)) {
200 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
201 return DefaultReactors.defaultReactor().newBuild().addSources(sources).buildEffective();
204 public static SchemaContext parseYangSources(final File... files)
205 throws ReactorException, IOException, YangSyntaxErrorException {
207 StatementStreamSource[] sources = new StatementStreamSource[files.length];
209 for (int i = 0; i < files.length; i++) {
210 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
213 return parseYangSources(sources);
216 public static SchemaContext parseYangSources(final Collection<File> files)
217 throws ReactorException, IOException, YangSyntaxErrorException {
218 return parseYangSources(files.toArray(new File[files.size()]));
221 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
222 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
224 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
225 File testSourcesDir = new File(resourceDir.toURI());
227 return parseYangSources(testSourcesDir.listFiles());
230 public static SchemaContext parseYangSource(final String yangSourceFilePath)
231 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
233 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
234 File testSourcesFile = new File(resourceFile.toURI());
236 return parseYangSources(testSourcesFile);