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;
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.builder.impl.BuilderUtils;
41 import org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream;
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 = BuilderUtils.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();
88 final byte[] streamContent = ByteStreams.toByteArray(stream);
90 ByteSource source = ByteStreams.asByteSource(streamContent);
92 final Collection<ByteSource> sources = Collections.singletonList(source);
93 SchemaContext ctx = parser.parseSources(sources, context);
94 final Set<Module> modules = ctx.getModules();
97 for (Module module : modules) {
98 if (module.getName().equals(name)) {
106 public static Set<Module> loadModulesWithContext(final Collection<InputStream> input, final SchemaContext context)
107 throws IOException, YangSyntaxErrorException {
108 Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
109 final YangContextParser parser = new YangParserImpl();
110 SchemaContext ctx = parser.parseSources(sources, context);
111 final Set<Module> modules = ctx.getModules();
115 public static Module findModule(final Set<Module> modules, final String moduleName) {
116 Module result = null;
117 for (Module module : modules) {
118 if (module.getName().equals(moduleName)) {
126 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
127 ModuleImport result = null;
128 for (ModuleImport moduleImport : imports) {
129 if (moduleImport.getPrefix().equals(prefix)) {
130 result = moduleImport;
137 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
138 TypeDefinition<?> result = null;
139 for (TypeDefinition<?> td : typedefs) {
140 if (td.getQName().getLocalName().equals(name)) {
148 public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, final String prefix, final String... names) {
149 List<QName> path = new ArrayList<>();
150 for (String name : names) {
151 path.add(QName.create(namespace, revision, name));
153 return SchemaPath.create(path, absolute);
156 public static Date createDate(final String date) {
158 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
160 result = simpleDateFormat.parse(date);
161 } catch (ParseException e) {
168 * Test if node has augmenting flag set to expected value. In case this is
169 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
176 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
177 assertEquals(expected, node.isAugmenting());
178 if (node instanceof DataNodeContainer) {
179 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
180 checkIsAugmenting(child, expected);
182 } else if (node instanceof ChoiceNode) {
183 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
184 checkIsAugmenting(caseNode, expected);
190 * Check if node has addedByUses flag set to expected value. In case this is
191 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
198 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
199 assertEquals(expected, node.isAddedByUses());
200 if (node instanceof DataNodeContainer) {
201 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
202 checkIsAddedByUses(child, expected);
204 } else if (node instanceof ChoiceNode) {
205 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
206 checkIsAddedByUses(caseNode, expected);
211 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
212 assertEquals(expected, node.isAddedByUses());
213 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
214 checkIsAddedByUses(child, expected);
218 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
219 List<Module> result = new ArrayList<>();
220 for (Module module : modules) {
221 if (module.getName().equals(moduleName)) {