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;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import java.text.DateFormat;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashSet;
24 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.YangModelParser;
40 final class TestUtils {
46 public static Set<Module> loadModules(final URI resourceDirectory) throws FileNotFoundException {
47 final YangModelParser parser = new YangParserImpl();
48 final File testDir = new File(resourceDirectory);
49 final String[] fileList = testDir.list();
50 final List<File> testFiles = new ArrayList<>();
51 if (fileList == null) {
52 throw new FileNotFoundException(resourceDirectory.toString());
54 for (String fileName : fileList) {
55 testFiles.add(new File(testDir, fileName));
57 return parser.parseYangModels(testFiles);
60 public static Set<Module> loadModules(final List<InputStream> input) throws IOException {
61 final YangModelParser parser = new YangParserImpl();
62 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
63 for (InputStream stream : input) {
69 public static Module loadModule(final InputStream stream) throws IOException {
70 final YangModelParser parser = new YangParserImpl();
71 final List<InputStream> input = Collections.singletonList(stream);
72 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
74 return modules.iterator().next();
77 public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
79 final YangModelParser parser = new YangParserImpl();
80 final List<InputStream> input = Collections.singletonList(stream);
81 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
84 for (Module module : modules) {
85 if (module.getName().equals(name)) {
93 public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
95 final YangModelParser parser = new YangParserImpl();
96 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
97 for (InputStream is : input) {
105 public static Module findModule(final Set<Module> modules, final String moduleName) {
106 Module result = null;
107 for (Module module : modules) {
108 if (module.getName().equals(moduleName)) {
116 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
117 ModuleImport result = null;
118 for (ModuleImport moduleImport : imports) {
119 if (moduleImport.getPrefix().equals(prefix)) {
120 result = moduleImport;
127 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
128 TypeDefinition<?> result = null;
129 for (TypeDefinition<?> td : typedefs) {
130 if (td.getQName().getLocalName().equals(name)) {
138 public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, final String prefix, final String... names) {
139 List<QName> path = new ArrayList<>();
140 for (String name : names) {
141 path.add(new QName(namespace, revision, prefix, name));
143 return SchemaPath.create(path, absolute);
146 public static Date createDate(final String date) {
148 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
150 result = simpleDateFormat.parse(date);
151 } catch (ParseException e) {
158 * Test if node has augmenting flag set to expected value. In case this is
159 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
166 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
167 assertEquals(expected, node.isAugmenting());
168 if (node instanceof DataNodeContainer) {
169 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
170 checkIsAugmenting(child, expected);
172 } else if (node instanceof ChoiceNode) {
173 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
174 checkIsAugmenting(caseNode, expected);
180 * Check if node has addedByUses flag set to expected value. In case this is
181 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
188 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
189 assertEquals(expected, node.isAddedByUses());
190 if (node instanceof DataNodeContainer) {
191 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
192 checkIsAddedByUses(child, expected);
194 } else if (node instanceof ChoiceNode) {
195 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
196 checkIsAddedByUses(caseNode, expected);
201 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
202 assertEquals(expected, node.isAddedByUses());
203 if (node instanceof DataNodeContainer) {
204 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
205 checkIsAddedByUses(child, expected);
207 } else if (node instanceof ChoiceNode) {
208 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
209 checkIsAddedByUses(caseNode, expected);
214 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
215 List<Module> result = new ArrayList<>();
216 for (Module module : modules) {
217 if (module.getName().equals(moduleName)) {