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;
12 import com.google.common.io.ByteSource;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStream;
18 import java.text.DateFormat;
19 import java.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.List;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
39 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
40 import org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream;
41 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
43 final class TestUtils {
49 public static Set<Module> loadModules(final URI resourceDirectory) throws IOException {
50 final YangContextParser parser = new YangParserImpl();
51 final File testDir = new File(resourceDirectory);
52 final String[] fileList = testDir.list();
53 final List<File> testFiles = new ArrayList<>();
54 if (fileList == null) {
55 throw new FileNotFoundException(resourceDirectory.toString());
57 for (String fileName : fileList) {
58 testFiles.add(new File(testDir, fileName));
60 SchemaContext ctx = parser.parseFiles(testFiles);
61 return ctx.getModules();
64 public static Set<Module> loadModules(final List<InputStream> input) throws IOException, YangSyntaxErrorException {
65 Collection<ByteSource> sources = ParserUtils.streamsToByteSources(input);
66 final YangContextParser parser = new YangParserImpl();
67 SchemaContext ctx = parser.parseSources(sources);
68 return ctx.getModules();
71 public static Module loadModule(final InputStream stream) throws IOException, YangSyntaxErrorException {
72 final YangContextParser parser = new YangParserImpl();
73 ByteSource source = new ByteSource() {
75 public InputStream openStream() throws IOException {
76 return NamedByteArrayInputStream.create(stream);
79 final Collection<ByteSource> sources = Collections.singletonList(source);
80 SchemaContext ctx = parser.parseSources(sources);
81 return ctx.getModules().iterator().next();
84 public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
85 throws IOException, YangSyntaxErrorException {
86 final YangContextParser parser = new YangParserImpl();
87 ByteSource source = new ByteSource() {
89 public InputStream openStream() throws IOException {
90 return NamedByteArrayInputStream.create(stream);
93 final Collection<ByteSource> sources = Collections.singletonList(source);
94 SchemaContext ctx = parser.parseSources(sources, context);
95 final Set<Module> modules = ctx.getModules();
98 for (Module module : modules) {
99 if (module.getName().equals(name)) {
107 public static Set<Module> loadModulesWithContext(final Collection<InputStream> input, final SchemaContext context)
108 throws IOException, YangSyntaxErrorException {
109 Collection<ByteSource> sources = ParserUtils.streamsToByteSources(input);
110 final YangContextParser parser = new YangParserImpl();
111 SchemaContext ctx = parser.parseSources(sources, context);
112 final Set<Module> modules = ctx.getModules();
116 public static Module findModule(final Set<Module> modules, final String moduleName) {
117 Module result = null;
118 for (Module module : modules) {
119 if (module.getName().equals(moduleName)) {
127 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
128 ModuleImport result = null;
129 for (ModuleImport moduleImport : imports) {
130 if (moduleImport.getPrefix().equals(prefix)) {
131 result = moduleImport;
138 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
139 TypeDefinition<?> result = null;
140 for (TypeDefinition<?> td : typedefs) {
141 if (td.getQName().getLocalName().equals(name)) {
149 public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, final String prefix, final String... names) {
150 List<QName> path = new ArrayList<>();
151 for (String name : names) {
152 path.add(new QName(namespace, revision, prefix, name));
154 return SchemaPath.create(path, absolute);
157 public static Date createDate(final String date) {
159 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
161 result = simpleDateFormat.parse(date);
162 } catch (ParseException e) {
169 * Test if node has augmenting flag set to expected value. In case this is
170 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
177 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
178 assertEquals(expected, node.isAugmenting());
179 if (node instanceof DataNodeContainer) {
180 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
181 checkIsAugmenting(child, expected);
183 } else if (node instanceof ChoiceNode) {
184 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
185 checkIsAugmenting(caseNode, expected);
191 * Check if node has addedByUses flag set to expected value. In case this is
192 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
199 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
200 assertEquals(expected, node.isAddedByUses());
201 if (node instanceof DataNodeContainer) {
202 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
203 checkIsAddedByUses(child, expected);
205 } else if (node instanceof ChoiceNode) {
206 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
207 checkIsAddedByUses(caseNode, expected);
212 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
213 assertEquals(expected, node.isAddedByUses());
214 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
215 checkIsAddedByUses(child, expected);
219 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
220 List<Module> result = new ArrayList<>();
221 for (Module module : modules) {
222 if (module.getName().equals(moduleName)) {