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.text.DateFormat;
18 import java.text.ParseException;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.Optional;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
28 import org.opendaylight.yangtools.yang.common.YangConstants;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
40 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
41 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
42 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
43 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
44 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinTextToDomTransformer;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
46 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
47 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
48 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
49 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.xml.sax.SAXException;
55 public final class TestUtils {
56 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
61 public static SchemaContext loadModules(final URI resourceDirectory)
62 throws ReactorException, IOException, YangSyntaxErrorException {
63 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
65 File[] files = new File(resourceDirectory).listFiles();
67 for (File file : files) {
68 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
69 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
71 LOG.info("Ignoring non-yang file {}", file);
75 return reactor.buildEffective();
78 public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
79 throws IOException, ReactorException, YangSyntaxErrorException {
80 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
82 for (String resourceName : resourceNames) {
83 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass, resourceName)));
86 return reactor.buildEffective();
89 public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
91 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
93 for (File file : new File(resourceDirectory).listFiles()) {
94 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
95 YinTextSchemaSource.forFile(file))));
98 return reactor.buildEffective();
101 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException, IOException {
102 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
103 reactor.addSources(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)));
104 SchemaContext ctx = reactor.buildEffective();
105 return ctx.getModules().iterator().next();
108 public static Optional<Module> findModule(final SchemaContext context, final String moduleName) {
109 return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
112 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
113 ModuleImport result = null;
114 for (ModuleImport moduleImport : imports) {
115 if (moduleImport.getPrefix().equals(prefix)) {
116 result = moduleImport;
123 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
124 TypeDefinition<?> result = null;
125 for (TypeDefinition<?> td : typedefs) {
126 if (td.getQName().getLocalName().equals(name)) {
134 public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
135 List<QName> path = new ArrayList<>(names.length);
136 for (String name : names) {
137 path.add(QName.create(module, name));
139 return SchemaPath.create(path, absolute);
142 public static Date createDate(final String date) {
144 final DateFormat simpleDateFormat = SimpleDateFormatUtil.getRevisionFormat();
146 result = simpleDateFormat.parse(date);
147 } catch (ParseException e) {
154 * Test if node has augmenting flag set to expected value. In case this is
155 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
162 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
163 assertEquals(expected, node.isAugmenting());
164 if (node instanceof DataNodeContainer) {
165 for (DataSchemaNode child : ((DataNodeContainer) node)
167 checkIsAugmenting(child, expected);
169 } else if (node instanceof ChoiceSchemaNode) {
170 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
171 checkIsAugmenting(caseNode, expected);
177 * Check if node has addedByUses flag set to expected value. In case this is
178 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
185 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
186 assertEquals(expected, node.isAddedByUses());
187 if (node instanceof DataNodeContainer) {
188 for (DataSchemaNode child : ((DataNodeContainer) node)
190 checkIsAddedByUses(child, expected);
192 } else if (node instanceof ChoiceSchemaNode) {
193 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
194 checkIsAddedByUses(caseNode, expected);
199 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
200 assertEquals(expected, node.isAddedByUses());
201 for (DataSchemaNode child : node.getChildNodes()) {
202 checkIsAddedByUses(child, expected);
206 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
207 List<Module> result = new ArrayList<>();
208 for (Module module : modules) {
209 if (module.getName().equals(moduleName)) {
216 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
219 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
221 reactor.addSources(sources);
223 return reactor.buildEffective();
226 public static SchemaContext parseYangSources(final File... files)
227 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
229 StatementStreamSource[] sources = new StatementStreamSource[files.length];
231 for (int i = 0; i < files.length; i++) {
232 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
235 return parseYangSources(sources);
238 public static SchemaContext parseYangSources(final Collection<File> files)
239 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
240 return parseYangSources(files.toArray(new File[files.size()]));
243 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
244 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
246 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
247 File testSourcesDir = new File(resourceDir.toURI());
249 return parseYangSources(testSourcesDir.listFiles());
252 public static SchemaContext parseYangSource(final String yangSourceFilePath)
253 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
255 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
256 File testSourcesFile = new File(resourceFile.toURI());
258 return parseYangSources(testSourcesFile);