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.parser.impl;
10 import static org.junit.Assert.assertEquals;
11 import com.google.common.io.ByteSource;
12 import com.google.common.io.ByteStreams;
13 import java.io.ByteArrayInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
19 import java.text.DateFormat;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
28 import org.opendaylight.yangtools.yang.common.QName;
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.YangContextParser;
40 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
42 import org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream;
44 final class TestUtils {
49 public static SchemaContext loadSchemaContext(final URI resourceDirectory)
51 final YangContextParser parser = new YangParserImpl();
52 final File testDir = new File(resourceDirectory);
53 final String[] fileList = testDir.list();
54 final List<File> testFiles = new ArrayList<>();
55 if (fileList == null) {
56 throw new FileNotFoundException(resourceDirectory.toString());
58 for (String fileName : fileList) {
59 testFiles.add(new File(testDir, fileName));
61 return parser.parseFiles(testFiles);
64 public static Set<Module> loadModules(final URI resourceDirectory)
66 return loadSchemaContext(resourceDirectory).getModules();
69 public static Set<Module> loadModules(final List<InputStream> input) throws IOException, YangSyntaxErrorException {
70 Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
71 final YangContextParser parser = new YangParserImpl();
72 SchemaContext ctx = parser.parseSources(sources);
73 return ctx.getModules();
76 public static Module loadModule(final InputStream stream) throws IOException, YangSyntaxErrorException {
77 final YangContextParser parser = new YangParserImpl();
78 ByteSource source = new ByteSource() {
80 public InputStream openStream() throws IOException {
81 return NamedByteArrayInputStream.create(stream);
84 final Collection<ByteSource> sources = Collections.singletonList(source);
85 SchemaContext ctx = parser.parseSources(sources);
86 return ctx.getModules().iterator().next();
89 public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
90 throws IOException, YangSyntaxErrorException {
91 final YangContextParser parser = new YangParserImpl();
93 final byte[] streamContent = ByteStreams.toByteArray(stream);
95 ByteSource source = new ByteSource() {
97 public InputStream openStream() throws IOException {
98 return new ByteArrayInputStream(streamContent);
102 final Collection<ByteSource> sources = Collections.singletonList(source);
103 SchemaContext ctx = parser.parseSources(sources, context);
104 final Set<Module> modules = ctx.getModules();
106 Module result = null;
107 for (Module module : modules) {
108 if (module.getName().equals(name)) {
116 public static Set<Module> loadModulesWithContext(final Collection<InputStream> input, final SchemaContext context)
117 throws IOException, YangSyntaxErrorException {
118 Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
119 final YangContextParser parser = new YangParserImpl();
120 SchemaContext ctx = parser.parseSources(sources, context);
121 final Set<Module> modules = ctx.getModules();
125 public static Module findModule(final Set<Module> modules, final String moduleName) {
126 Module result = null;
127 for (Module module : modules) {
128 if (module.getName().equals(moduleName)) {
136 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
137 ModuleImport result = null;
138 for (ModuleImport moduleImport : imports) {
139 if (moduleImport.getPrefix().equals(prefix)) {
140 result = moduleImport;
147 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
148 TypeDefinition<?> result = null;
149 for (TypeDefinition<?> td : typedefs) {
150 if (td.getQName().getLocalName().equals(name)) {
158 public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, final String prefix, final String... names) {
159 List<QName> path = new ArrayList<>();
160 for (String name : names) {
161 path.add(QName.create(namespace, revision, name));
163 return SchemaPath.create(path, absolute);
166 public static Date createDate(final String date) {
168 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
170 result = simpleDateFormat.parse(date);
171 } catch (ParseException e) {
178 * Test if node has augmenting flag set to expected value. In case this is
179 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
186 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
187 assertEquals(expected, node.isAugmenting());
188 if (node instanceof DataNodeContainer) {
189 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
190 checkIsAugmenting(child, expected);
192 } else if (node instanceof ChoiceSchemaNode) {
193 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
194 checkIsAugmenting(caseNode, expected);
200 * Check if node has addedByUses flag set to expected value. In case this is
201 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
208 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
209 assertEquals(expected, node.isAddedByUses());
210 if (node instanceof DataNodeContainer) {
211 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
212 checkIsAddedByUses(child, expected);
214 } else if (node instanceof ChoiceSchemaNode) {
215 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
216 checkIsAddedByUses(caseNode, expected);
221 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
222 assertEquals(expected, node.isAddedByUses());
223 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
224 checkIsAddedByUses(child, expected);
228 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
229 List<Module> result = new ArrayList<>();
230 for (Module module : modules) {
231 if (module.getName().equals(moduleName)) {