Yang parser refactoring.
[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     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);
33         }
34         for (String fileName : fileList) {
35             testFiles.add(new File(testDir, fileName));
36         }
37         return parser.parseYangModels(testFiles);
38     }
39
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) {
44             stream.close();
45         }
46         return modules;
47     }
48
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));
53         stream.close();
54         return modules.iterator().next();
55     }
56
57     public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
58             throws IOException {
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));
62         stream.close();
63         Module result = null;
64         for (Module module : modules) {
65             if (module.getName().equals(name)) {
66                 result = module;
67                 break;
68             }
69         }
70         return result;
71     }
72
73     public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
74             throws IOException {
75         final YangModelParser parser = new YangParserImpl();
76         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
77         for (InputStream is : input) {
78             if (is != null) {
79                 is.close();
80             }
81         }
82         return modules;
83     }
84
85     public static Module findModule(Set<Module> modules, String moduleName) {
86         Module result = null;
87         for (Module module : modules) {
88             if (module.getName().equals(moduleName)) {
89                 result = module;
90                 break;
91             }
92         }
93         return result;
94     }
95
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;
101                 break;
102             }
103         }
104         return result;
105     }
106
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)) {
111                 result = td;
112                 break;
113             }
114         }
115         return result;
116     }
117
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));
122         }
123         return new SchemaPath(path, absolute);
124     }
125
126     public static Date createDate(String date) {
127         Date result;
128         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
129         try {
130             result = simpleDateFormat.parse(date);
131         } catch (ParseException e) {
132             result = null;
133         }
134         return result;
135     }
136
137     /**
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.
140      *
141      * @param node
142      *            node to check
143      * @param expected
144      *            expected value
145      */
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);
151             }
152         } else if (node instanceof ChoiceNode) {
153             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
154                 checkIsAugmenting(caseNode, expected);
155             }
156         }
157     }
158
159     /**
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.
162      *
163      * @param node
164      *            node to check
165      * @param expected
166      *            expected value
167      */
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);
173             }
174         } else if (node instanceof ChoiceNode) {
175             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
176                 checkIsAddedByUses(caseNode, expected);
177             }
178         }
179     }
180
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);
186             }
187         } else if (node instanceof ChoiceNode) {
188             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
189                 checkIsAddedByUses(caseNode, expected);
190             }
191         }
192     }
193
194 }