Fixed incorrect path construction in JUnit tests.
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / TestUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.io.*;
13 import java.net.URI;
14 import java.text.*;
15 import java.util.*;
16
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;
20
21 final class TestUtils {
22
23     private TestUtils() {
24     }
25
26
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());
34         }
35         for (String fileName : fileList) {
36             testFiles.add(new File(testDir, fileName));
37         }
38         return parser.parseYangModels(testFiles);
39     }
40
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) {
45             stream.close();
46         }
47         return modules;
48     }
49
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));
54         stream.close();
55         return modules.iterator().next();
56     }
57
58     public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
59             throws IOException {
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));
63         stream.close();
64         Module result = null;
65         for (Module module : modules) {
66             if (module.getName().equals(name)) {
67                 result = module;
68                 break;
69             }
70         }
71         return result;
72     }
73
74     public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
75             throws IOException {
76         final YangModelParser parser = new YangParserImpl();
77         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
78         for (InputStream is : input) {
79             if (is != null) {
80                 is.close();
81             }
82         }
83         return modules;
84     }
85
86     public static Module findModule(Set<Module> modules, String moduleName) {
87         Module result = null;
88         for (Module module : modules) {
89             if (module.getName().equals(moduleName)) {
90                 result = module;
91                 break;
92             }
93         }
94         return result;
95     }
96
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;
102                 break;
103             }
104         }
105         return result;
106     }
107
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)) {
112                 result = td;
113                 break;
114             }
115         }
116         return result;
117     }
118
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));
123         }
124         return new SchemaPath(path, absolute);
125     }
126
127     public static Date createDate(String date) {
128         Date result;
129         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
130         try {
131             result = simpleDateFormat.parse(date);
132         } catch (ParseException e) {
133             result = null;
134         }
135         return result;
136     }
137
138     /**
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.
141      *
142      * @param node
143      *            node to check
144      * @param expected
145      *            expected value
146      */
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);
152             }
153         } else if (node instanceof ChoiceNode) {
154             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
155                 checkIsAugmenting(caseNode, expected);
156             }
157         }
158     }
159
160     /**
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.
163      *
164      * @param node
165      *            node to check
166      * @param expected
167      *            expected value
168      */
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);
174             }
175         } else if (node instanceof ChoiceNode) {
176             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
177                 checkIsAddedByUses(caseNode, expected);
178             }
179         }
180     }
181
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);
187             }
188         } else if (node instanceof ChoiceNode) {
189             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
190                 checkIsAddedByUses(caseNode, expected);
191             }
192         }
193     }
194
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)) {
199                 result.add(module);
200             }
201         }
202         return result;
203     }
204
205 }