Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / 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.stmt.retest;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.util.Collection;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
19 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
20
21 import java.io.File;
22 import java.io.InputStream;
23 import java.net.URI;
24 import java.text.DateFormat;
25 import java.text.ParseException;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.Set;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
47
48 final class TestUtils {
49
50     private TestUtils() {
51     }
52
53     public static Set<Module> loadModules(final URI resourceDirectory)
54             throws SourceException, ReactorException {
55         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
56                 .newBuild();
57         File[] files = new File(resourceDirectory).listFiles();
58
59         for (File file : files) {
60             addSources(reactor, new YangStatementSourceImpl(file.getPath(),
61                     true));
62         }
63
64         EffectiveSchemaContext ctx = reactor.buildEffective();
65         return ctx.getModules();
66     }
67
68     public static Set<Module> loadModules(final List<InputStream> streams)
69             throws SourceException, ReactorException {
70         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
71                 .newBuild();
72         for (InputStream inputStream : streams) {
73             addSources(reactor, new YangStatementSourceImpl(inputStream));
74         }
75
76         EffectiveSchemaContext ctx = reactor.buildEffective();
77         return ctx.getModules();
78     }
79
80     public static Module loadModule(final InputStream stream)
81             throws SourceException, ReactorException {
82         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
83                 .newBuild();
84         addSources(reactor, new YangStatementSourceImpl(stream));
85         EffectiveSchemaContext ctx = reactor.buildEffective();
86         return ctx.getModules().iterator().next();
87     }
88
89     public static Module findModule(final Set<Module> modules,
90             final String moduleName) {
91         Module result = null;
92         for (Module module : modules) {
93             if (module.getName().equals(moduleName)) {
94                 result = module;
95                 break;
96             }
97         }
98         return result;
99     }
100
101     public static ModuleImport findImport(final Set<ModuleImport> imports,
102             final String prefix) {
103         ModuleImport result = null;
104         for (ModuleImport moduleImport : imports) {
105             if (moduleImport.getPrefix().equals(prefix)) {
106                 result = moduleImport;
107                 break;
108             }
109         }
110         return result;
111     }
112
113     public static TypeDefinition<?> findTypedef(
114             final Set<TypeDefinition<?>> typedefs, final String name) {
115         TypeDefinition<?> result = null;
116         for (TypeDefinition<?> td : typedefs) {
117             if (td.getQName().getLocalName().equals(name)) {
118                 result = td;
119                 break;
120             }
121         }
122         return result;
123     }
124
125     public static SchemaPath createPath(final boolean absolute,
126             final URI namespace, final Date revision, final String prefix,
127             final String... names) {
128         List<QName> path = new ArrayList<>();
129         for (String name : names) {
130             path.add(QName.create(namespace, revision, name));
131         }
132         return SchemaPath.create(path, absolute);
133     }
134
135     public static Date createDate(final String date) {
136         Date result;
137         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
138         try {
139             result = simpleDateFormat.parse(date);
140         } catch (ParseException e) {
141             result = null;
142         }
143         return result;
144     }
145
146     /**
147      * Test if node has augmenting flag set to expected value. In case this is
148      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
149      *
150      * @param node
151      *            node to check
152      * @param expected
153      *            expected value
154      */
155     public static void checkIsAugmenting(final DataSchemaNode node,
156             final boolean expected) {
157         assertEquals(expected, node.isAugmenting());
158         if (node instanceof DataNodeContainer) {
159             for (DataSchemaNode child : ((DataNodeContainer) node)
160                     .getChildNodes()) {
161                 checkIsAugmenting(child, expected);
162             }
163         } else if (node instanceof ChoiceSchemaNode) {
164             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
165                 checkIsAugmenting(caseNode, expected);
166             }
167         }
168     }
169
170     /**
171      * Check if node has addedByUses flag set to expected value. In case this is
172      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
173      *
174      * @param node
175      *            node to check
176      * @param expected
177      *            expected value
178      */
179     public static void checkIsAddedByUses(final DataSchemaNode node,
180             final boolean expected) {
181         assertEquals(expected, node.isAddedByUses());
182         if (node instanceof DataNodeContainer) {
183             for (DataSchemaNode child : ((DataNodeContainer) node)
184                     .getChildNodes()) {
185                 checkIsAddedByUses(child, expected);
186             }
187         } else if (node instanceof ChoiceSchemaNode) {
188             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
189                 checkIsAddedByUses(caseNode, expected);
190             }
191         }
192     }
193
194     public static void checkIsAddedByUses(final GroupingDefinition node,
195             final boolean expected) {
196         assertEquals(expected, node.isAddedByUses());
197         for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
198             checkIsAddedByUses(child, expected);
199         }
200     }
201
202     public static List<Module> findModules(final Set<Module> modules,
203             final String moduleName) {
204         List<Module> result = new ArrayList<>();
205         for (Module module : modules) {
206             if (module.getName().equals(moduleName)) {
207                 result.add(module);
208             }
209         }
210         return result;
211     }
212
213     private static void addSources(
214             CrossSourceStatementReactor.BuildAction reactor,
215             YangStatementSourceImpl... sources) {
216         for (YangStatementSourceImpl source : sources) {
217             reactor.addSource(source);
218         }
219     }
220
221     public static SchemaContext parseYangSources(
222             StatementStreamSource... sources) throws SourceException,
223             ReactorException {
224
225         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
226                 .newBuild();
227         reactor.addSources(sources);
228
229         return reactor.buildEffective();
230     }
231
232     public static SchemaContext parseYangSources(File... files)
233             throws SourceException, ReactorException, FileNotFoundException {
234
235         StatementStreamSource[] sources = new StatementStreamSource[files.length];
236
237         for (int i = 0; i < files.length; i++) {
238             sources[i] = new YangStatementSourceImpl(new FileInputStream(
239                     files[i]));
240         }
241
242         return parseYangSources(sources);
243     }
244
245     public static SchemaContext parseYangSources(Collection<File> files)
246             throws SourceException, ReactorException, FileNotFoundException {
247         return parseYangSources(files.toArray(new File[files.size()]));
248     }
249
250     public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
251             throws SourceException, ReactorException, FileNotFoundException,
252             URISyntaxException {
253
254         URL resourceDir = StmtTestUtils.class
255                 .getResource(yangSourcesDirectoryPath);
256         File testSourcesDir = new File(resourceDir.toURI());
257
258         return parseYangSources(testSourcesDir.listFiles());
259     }
260
261     public static SchemaContext parseYangSource(String yangSourceFilePath)
262             throws SourceException, ReactorException, FileNotFoundException,
263             URISyntaxException {
264
265         URL resourceFile = StmtTestUtils.class
266                 .getResource(yangSourceFilePath);
267         File testSourcesFile = new File(resourceFile.toURI());
268
269         return parseYangSources(testSourcesFile);
270     }
271
272 }