Merge "BUG-981: remove deprecated elements"
[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.File;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URI;
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;
25 import java.util.Set;
26
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;
39
40 final class TestUtils {
41
42     private TestUtils() {
43     }
44
45
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());
53         }
54         for (String fileName : fileList) {
55             testFiles.add(new File(testDir, fileName));
56         }
57         return parser.parseYangModels(testFiles);
58     }
59
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) {
64             stream.close();
65         }
66         return modules;
67     }
68
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));
73         stream.close();
74         return modules.iterator().next();
75     }
76
77     public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
78             throws IOException {
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));
82         stream.close();
83         Module result = null;
84         for (Module module : modules) {
85             if (module.getName().equals(name)) {
86                 result = module;
87                 break;
88             }
89         }
90         return result;
91     }
92
93     public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
94             throws IOException {
95         final YangModelParser parser = new YangParserImpl();
96         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
97         for (InputStream is : input) {
98             if (is != null) {
99                 is.close();
100             }
101         }
102         return modules;
103     }
104
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)) {
109                 result = module;
110                 break;
111             }
112         }
113         return result;
114     }
115
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;
121                 break;
122             }
123         }
124         return result;
125     }
126
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)) {
131                 result = td;
132                 break;
133             }
134         }
135         return result;
136     }
137
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));
142         }
143         return SchemaPath.create(path, absolute);
144     }
145
146     public static Date createDate(final String date) {
147         Date result;
148         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
149         try {
150             result = simpleDateFormat.parse(date);
151         } catch (ParseException e) {
152             result = null;
153         }
154         return result;
155     }
156
157     /**
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.
160      *
161      * @param node
162      *            node to check
163      * @param expected
164      *            expected value
165      */
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);
171             }
172         } else if (node instanceof ChoiceNode) {
173             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
174                 checkIsAugmenting(caseNode, expected);
175             }
176         }
177     }
178
179     /**
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.
182      *
183      * @param node
184      *            node to check
185      * @param expected
186      *            expected value
187      */
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);
193             }
194         } else if (node instanceof ChoiceNode) {
195             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
196                 checkIsAddedByUses(caseNode, expected);
197             }
198         }
199     }
200
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);
206             }
207         } else if (node instanceof ChoiceNode) {
208             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
209                 checkIsAddedByUses(caseNode, expected);
210             }
211         }
212     }
213
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)) {
218                 result.add(module);
219             }
220         }
221         return result;
222     }
223
224 }