2 * Copyright (c) 2013 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.retest;
10 import static org.junit.Assert.assertEquals;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.net.URISyntaxException;
16 import java.util.Collection;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
19 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
22 import java.io.InputStream;
24 import java.text.DateFormat;
25 import java.text.ParseException;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.List;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
48 final class TestUtils {
53 public static Set<Module> loadModules(final URI resourceDirectory)
54 throws SourceException, ReactorException {
55 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
57 File[] files = new File(resourceDirectory).listFiles();
59 for (File file : files) {
60 addSources(reactor, new YangStatementSourceImpl(file.getPath(),
64 EffectiveSchemaContext ctx = reactor.buildEffective();
65 return ctx.getModules();
68 public static Set<Module> loadModules(final List<InputStream> streams)
69 throws SourceException, ReactorException {
70 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
72 for (InputStream inputStream : streams) {
73 addSources(reactor, new YangStatementSourceImpl(inputStream));
76 EffectiveSchemaContext ctx = reactor.buildEffective();
77 return ctx.getModules();
80 public static Module loadModule(final InputStream stream)
81 throws SourceException, ReactorException {
82 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
84 addSources(reactor, new YangStatementSourceImpl(stream));
85 EffectiveSchemaContext ctx = reactor.buildEffective();
86 return ctx.getModules().iterator().next();
89 public static Module findModule(final Set<Module> modules,
90 final String moduleName) {
92 for (Module module : modules) {
93 if (module.getName().equals(moduleName)) {
101 public static ModuleImport findImport(final Set<ModuleImport> imports,
102 final String prefix) {
103 ModuleImport result = null;
104 for (ModuleImport moduleImport : imports) {
105 if (moduleImport.getPrefix().equals(prefix)) {
106 result = moduleImport;
113 public static TypeDefinition<?> findTypedef(
114 final Set<TypeDefinition<?>> typedefs, final String name) {
115 TypeDefinition<?> result = null;
116 for (TypeDefinition<?> td : typedefs) {
117 if (td.getQName().getLocalName().equals(name)) {
125 public static SchemaPath createPath(final boolean absolute,
126 final URI namespace, final Date revision, final String prefix,
127 final String... names) {
128 List<QName> path = new ArrayList<>();
129 for (String name : names) {
130 path.add(QName.create(namespace, revision, name));
132 return SchemaPath.create(path, absolute);
135 public static Date createDate(final String date) {
137 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
139 result = simpleDateFormat.parse(date);
140 } catch (ParseException e) {
147 * Test if node has augmenting flag set to expected value. In case this is
148 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
155 public static void checkIsAugmenting(final DataSchemaNode node,
156 final boolean expected) {
157 assertEquals(expected, node.isAugmenting());
158 if (node instanceof DataNodeContainer) {
159 for (DataSchemaNode child : ((DataNodeContainer) node)
161 checkIsAugmenting(child, expected);
163 } else if (node instanceof ChoiceSchemaNode) {
164 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
165 checkIsAugmenting(caseNode, expected);
171 * Check if node has addedByUses flag set to expected value. In case this is
172 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
179 public static void checkIsAddedByUses(final DataSchemaNode node,
180 final boolean expected) {
181 assertEquals(expected, node.isAddedByUses());
182 if (node instanceof DataNodeContainer) {
183 for (DataSchemaNode child : ((DataNodeContainer) node)
185 checkIsAddedByUses(child, expected);
187 } else if (node instanceof ChoiceSchemaNode) {
188 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
189 checkIsAddedByUses(caseNode, expected);
194 public static void checkIsAddedByUses(final GroupingDefinition node,
195 final boolean expected) {
196 assertEquals(expected, node.isAddedByUses());
197 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
198 checkIsAddedByUses(child, expected);
202 public static List<Module> findModules(final Set<Module> modules,
203 final String moduleName) {
204 List<Module> result = new ArrayList<>();
205 for (Module module : modules) {
206 if (module.getName().equals(moduleName)) {
213 private static void addSources(
214 CrossSourceStatementReactor.BuildAction reactor,
215 YangStatementSourceImpl... sources) {
216 for (YangStatementSourceImpl source : sources) {
217 reactor.addSource(source);
221 public static SchemaContext parseYangSources(
222 StatementStreamSource... sources) throws SourceException,
225 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
227 reactor.addSources(sources);
229 return reactor.buildEffective();
232 public static SchemaContext parseYangSources(File... files)
233 throws SourceException, ReactorException, FileNotFoundException {
235 StatementStreamSource[] sources = new StatementStreamSource[files.length];
237 for (int i = 0; i < files.length; i++) {
238 sources[i] = new YangStatementSourceImpl(new FileInputStream(
242 return parseYangSources(sources);
245 public static SchemaContext parseYangSources(Collection<File> files)
246 throws SourceException, ReactorException, FileNotFoundException {
247 return parseYangSources(files.toArray(new File[files.size()]));
250 public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
251 throws SourceException, ReactorException, FileNotFoundException,
254 URL resourceDir = StmtTestUtils.class
255 .getResource(yangSourcesDirectoryPath);
256 File testSourcesDir = new File(resourceDir.toURI());
258 return parseYangSources(testSourcesDir.listFiles());
261 public static SchemaContext parseYangSource(String yangSourceFilePath)
262 throws SourceException, ReactorException, FileNotFoundException,
265 URL resourceFile = StmtTestUtils.class
266 .getResource(yangSourceFilePath);
267 File testSourcesFile = new File(resourceFile.toURI());
269 return parseYangSources(testSourcesFile);