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 org.eclipse.jdt.annotation.NonNull;
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.xml.sax.SAXException;
41 public final class TestUtils {
46 public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
48 return loadSources(TestUtils.class, resourceDirectory);
51 public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
53 final var files = new File(cls.getResource(resourceDirectory).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())));
62 public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
63 return loadModules(TestUtils.class, resourceDirectory);
66 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
68 return RFC7950Reactors.defaultReactor().newBuild()
69 .addSources(loadSources(cls, resourceDirectory))
73 public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
74 throws IOException, ReactorException, YangSyntaxErrorException {
75 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
77 for (String resourceName : resourceNames) {
78 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
82 return reactor.buildEffective();
85 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
86 throws ReactorException, SAXException, IOException {
87 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
89 for (File file : new File(resourceDirectory).listFiles()) {
90 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
91 YinTextSchemaSource.forPath(file.toPath()))));
94 return reactor.buildEffective();
97 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
99 return RFC7950Reactors.defaultReactor().newBuild()
100 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
102 .getModules().iterator().next();
105 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
106 for (ModuleImport moduleImport : imports) {
107 if (moduleImport.getPrefix().equals(prefix)) {
114 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
116 for (TypeDefinition<?> td : typedefs) {
117 if (td.getQName().getLocalName().equals(name)) {
125 * Test if node has augmenting flag set to expected value. In case this is
126 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
133 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
134 assertEquals(expected, node.isAugmenting());
135 if (node instanceof DataNodeContainer) {
136 for (DataSchemaNode child : ((DataNodeContainer) node)
138 checkIsAugmenting(child, expected);
140 } else if (node instanceof ChoiceSchemaNode) {
141 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
142 checkIsAugmenting(caseNode, expected);
147 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
148 throws ReactorException {
149 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
152 public static EffectiveModelContext parseYangSources(final File... files)
153 throws ReactorException, IOException, YangSyntaxErrorException {
155 StatementStreamSource[] sources = new StatementStreamSource[files.length];
157 for (int i = 0; i < files.length; i++) {
158 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forPath(files[i].toPath()));
161 return parseYangSources(sources);
164 public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
165 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
167 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
168 File testSourcesFile = new File(resourceFile.toURI());
170 return parseYangSources(testSourcesFile);