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;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.*;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
21 final class TestUtils {
27 public static Set<Module> loadModules(URI resourceDirectory) throws FileNotFoundException {
28 final YangModelParser parser = new YangParserImpl();
29 final File testDir = new File(resourceDirectory);
30 final String[] fileList = testDir.list();
31 final List<File> testFiles = new ArrayList<>();
32 if (fileList == null) {
33 throw new FileNotFoundException(resourceDirectory.toString());
35 for (String fileName : fileList) {
36 testFiles.add(new File(testDir, fileName));
38 return parser.parseYangModels(testFiles);
41 public static Set<Module> loadModules(List<InputStream> input) throws IOException {
42 final YangModelParser parser = new YangParserImpl();
43 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
44 for (InputStream stream : input) {
50 public static Module loadModule(final InputStream stream) throws IOException {
51 final YangModelParser parser = new YangParserImpl();
52 final List<InputStream> input = Collections.singletonList(stream);
53 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
55 return modules.iterator().next();
58 public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
60 final YangModelParser parser = new YangParserImpl();
61 final List<InputStream> input = Collections.singletonList(stream);
62 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
65 for (Module module : modules) {
66 if (module.getName().equals(name)) {
74 public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
76 final YangModelParser parser = new YangParserImpl();
77 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
78 for (InputStream is : input) {
86 public static Module findModule(Set<Module> modules, String moduleName) {
88 for (Module module : modules) {
89 if (module.getName().equals(moduleName)) {
97 public static ModuleImport findImport(Set<ModuleImport> imports, String prefix) {
98 ModuleImport result = null;
99 for (ModuleImport moduleImport : imports) {
100 if (moduleImport.getPrefix().equals(prefix)) {
101 result = moduleImport;
108 public static TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs, String name) {
109 TypeDefinition<?> result = null;
110 for (TypeDefinition<?> td : typedefs) {
111 if (td.getQName().getLocalName().equals(name)) {
119 public static SchemaPath createPath(boolean absolute, URI namespace, Date revision, String prefix, String... names) {
120 List<QName> path = new ArrayList<>();
121 for (String name : names) {
122 path.add(new QName(namespace, revision, prefix, name));
124 return new SchemaPath(path, absolute);
127 public static Date createDate(String date) {
129 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
131 result = simpleDateFormat.parse(date);
132 } catch (ParseException e) {
139 * Test if node has augmenting flag set to expected value. In case this is
140 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
147 public static void checkIsAugmenting(DataSchemaNode node, boolean expected) {
148 assertEquals(expected, node.isAugmenting());
149 if (node instanceof DataNodeContainer) {
150 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
151 checkIsAugmenting(child, expected);
153 } else if (node instanceof ChoiceNode) {
154 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
155 checkIsAugmenting(caseNode, expected);
161 * Check if node has addedByUses flag set to expected value. In case this is
162 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
169 public static void checkIsAddedByUses(DataSchemaNode node, boolean expected) {
170 assertEquals(expected, node.isAddedByUses());
171 if (node instanceof DataNodeContainer) {
172 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
173 checkIsAddedByUses(child, expected);
175 } else if (node instanceof ChoiceNode) {
176 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
177 checkIsAddedByUses(caseNode, expected);
182 public static void checkIsAddedByUses(GroupingDefinition node, boolean expected) {
183 assertEquals(expected, node.isAddedByUses());
184 if (node instanceof DataNodeContainer) {
185 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
186 checkIsAddedByUses(child, expected);
188 } else if (node instanceof ChoiceNode) {
189 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
190 checkIsAddedByUses(caseNode, expected);
195 public static List<Module> findModules(Set<Module> modules, String moduleName) {
196 List<Module> result = new ArrayList<>();
197 for (Module module : modules) {
198 if (module.getName().equals(moduleName)) {