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.InputStream;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
38 final class TestUtils {
43 public static Set<Module> loadModules(final URI resourceDirectory) throws SourceException, ReactorException {
44 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
45 File[] files = new File(resourceDirectory).listFiles();
47 for (File file : files) {
48 addSources(reactor, new YangStatementSourceImpl(file.getPath(), true));
51 EffectiveSchemaContext ctx = reactor.buildEffective();
52 return ctx.getModules();
55 public static Set<Module> loadModules(final List<InputStream> streams) throws SourceException, ReactorException {
56 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57 for (InputStream inputStream : streams) {
58 addSources(reactor, new YangStatementSourceImpl(inputStream));
61 EffectiveSchemaContext ctx = reactor.buildEffective();
62 return ctx.getModules();
65 public static Module loadModule(final InputStream stream) throws SourceException, ReactorException {
66 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
67 addSources(reactor, new YangStatementSourceImpl(stream));
68 EffectiveSchemaContext ctx = reactor.buildEffective();
69 return ctx.getModules().iterator().next();
72 public static Module findModule(final Set<Module> modules, final String moduleName) {
74 for (Module module : modules) {
75 if (module.getName().equals(moduleName)) {
83 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
84 ModuleImport result = null;
85 for (ModuleImport moduleImport : imports) {
86 if (moduleImport.getPrefix().equals(prefix)) {
87 result = moduleImport;
94 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
95 TypeDefinition<?> result = null;
96 for (TypeDefinition<?> td : typedefs) {
97 if (td.getQName().getLocalName().equals(name)) {
105 public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision,
106 final String prefix, final String... names) {
107 List<QName> path = new ArrayList<>();
108 for (String name : names) {
109 path.add(QName.create(namespace, revision, name));
111 return SchemaPath.create(path, absolute);
114 public static Date createDate(final String date) {
116 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
118 result = simpleDateFormat.parse(date);
119 } catch (ParseException e) {
126 * Test if node has augmenting flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its
127 * child nodes/case nodes too.
134 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
135 assertEquals(expected, node.isAugmenting());
136 if (node instanceof DataNodeContainer) {
137 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
138 checkIsAugmenting(child, expected);
140 } else if (node instanceof ChoiceSchemaNode) {
141 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
142 checkIsAugmenting(caseNode, expected);
148 * Check if node has addedByUses flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its
149 * child nodes/case nodes too.
156 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
157 assertEquals(expected, node.isAddedByUses());
158 if (node instanceof DataNodeContainer) {
159 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
160 checkIsAddedByUses(child, expected);
162 } else if (node instanceof ChoiceSchemaNode) {
163 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
164 checkIsAddedByUses(caseNode, expected);
169 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
170 assertEquals(expected, node.isAddedByUses());
171 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
172 checkIsAddedByUses(child, expected);
176 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
177 List<Module> result = new ArrayList<>();
178 for (Module module : modules) {
179 if (module.getName().equals(moduleName)) {
186 private static void addSources(CrossSourceStatementReactor.BuildAction reactor, YangStatementSourceImpl... sources) {
187 for (YangStatementSourceImpl source : sources) {
188 reactor.addSource(source);