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 {
26 public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
27 YangModelParser parser = new YangParserImpl();
28 final File testDir = new File(resourceDirectory);
29 final String[] fileList = testDir.list();
30 final List<File> testFiles = new ArrayList<>();
31 if (fileList == null) {
32 throw new FileNotFoundException(resourceDirectory);
34 for (String fileName : fileList) {
35 testFiles.add(new File(testDir, fileName));
37 return parser.parseYangModels(testFiles);
40 public static Set<Module> loadModules(List<InputStream> input) throws IOException {
41 final YangModelParser parser = new YangParserImpl();
42 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
43 for (InputStream stream : input) {
49 public static Module loadModule(final InputStream stream) throws IOException {
50 final YangModelParser parser = new YangParserImpl();
51 final List<InputStream> input = Collections.singletonList(stream);
52 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
54 return modules.iterator().next();
57 public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
59 final YangModelParser parser = new YangParserImpl();
60 final List<InputStream> input = Collections.singletonList(stream);
61 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
64 for (Module module : modules) {
65 if (module.getName().equals(name)) {
73 public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
75 final YangModelParser parser = new YangParserImpl();
76 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
77 for (InputStream is : input) {
85 public static Module findModule(Set<Module> modules, String moduleName) {
87 for (Module module : modules) {
88 if (module.getName().equals(moduleName)) {
96 public static ModuleImport findImport(Set<ModuleImport> imports, String prefix) {
97 ModuleImport result = null;
98 for (ModuleImport moduleImport : imports) {
99 if (moduleImport.getPrefix().equals(prefix)) {
100 result = moduleImport;
107 public static TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs, String name) {
108 TypeDefinition<?> result = null;
109 for (TypeDefinition<?> td : typedefs) {
110 if (td.getQName().getLocalName().equals(name)) {
118 public static SchemaPath createPath(boolean absolute, URI namespace, Date revision, String prefix, String... names) {
119 List<QName> path = new ArrayList<>();
120 for (String name : names) {
121 path.add(new QName(namespace, revision, prefix, name));
123 return new SchemaPath(path, absolute);
126 public static Date createDate(String date) {
128 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
130 result = simpleDateFormat.parse(date);
131 } catch (ParseException e) {
138 * Test if node has augmenting flag set to expected value. In case this is
139 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
146 public static void checkIsAugmenting(DataSchemaNode node, boolean expected) {
147 assertEquals(expected, node.isAugmenting());
148 if (node instanceof DataNodeContainer) {
149 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
150 checkIsAugmenting(child, expected);
152 } else if (node instanceof ChoiceNode) {
153 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
154 checkIsAugmenting(caseNode, expected);
160 * Check if node has addedByUses flag set to expected value. In case this is
161 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
168 public static void checkIsAddedByUses(DataSchemaNode node, boolean expected) {
169 assertEquals(expected, node.isAddedByUses());
170 if (node instanceof DataNodeContainer) {
171 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
172 checkIsAddedByUses(child, expected);
174 } else if (node instanceof ChoiceNode) {
175 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
176 checkIsAddedByUses(caseNode, expected);
181 public static void checkIsAddedByUses(GroupingDefinition node, boolean expected) {
182 assertEquals(expected, node.isAddedByUses());
183 if (node instanceof DataNodeContainer) {
184 for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
185 checkIsAddedByUses(child, expected);
187 } else if (node instanceof ChoiceNode) {
188 for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
189 checkIsAddedByUses(caseNode, expected);