Merge "Improvements REST documentation generation"
[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 com.google.common.io.ByteSource;
13 import com.google.common.io.ByteStreams;
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.URI;
19 import java.text.DateFormat;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Set;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
40 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
42 import org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream;
43
44 final class TestUtils {
45
46     private TestUtils() {
47     }
48
49
50     public static Set<Module> loadModules(final URI resourceDirectory) throws IOException {
51         final YangContextParser parser = new YangParserImpl();
52         final File testDir = new File(resourceDirectory);
53         final String[] fileList = testDir.list();
54         final List<File> testFiles = new ArrayList<>();
55         if (fileList == null) {
56             throw new FileNotFoundException(resourceDirectory.toString());
57         }
58         for (String fileName : fileList) {
59             testFiles.add(new File(testDir, fileName));
60         }
61         SchemaContext ctx = parser.parseFiles(testFiles);
62         return ctx.getModules();
63     }
64
65     public static Set<Module> loadModules(final List<InputStream> input) throws IOException, YangSyntaxErrorException {
66         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
67         final YangContextParser parser = new YangParserImpl();
68         SchemaContext ctx = parser.parseSources(sources);
69         return ctx.getModules();
70     }
71
72     public static Module loadModule(final InputStream stream) throws IOException, YangSyntaxErrorException {
73         final YangContextParser parser = new YangParserImpl();
74         ByteSource source = new ByteSource() {
75             @Override
76             public InputStream openStream() throws IOException {
77                 return NamedByteArrayInputStream.create(stream);
78             }
79         };
80         final Collection<ByteSource> sources = Collections.singletonList(source);
81         SchemaContext ctx = parser.parseSources(sources);
82         return ctx.getModules().iterator().next();
83     }
84
85     public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
86             throws IOException, YangSyntaxErrorException {
87         final YangContextParser parser = new YangParserImpl();
88
89         final byte[] streamContent = ByteStreams.toByteArray(stream);
90
91         ByteSource source = ByteStreams.asByteSource(streamContent);
92
93         final Collection<ByteSource> sources = Collections.singletonList(source);
94         SchemaContext ctx = parser.parseSources(sources, context);
95         final Set<Module> modules = ctx.getModules();
96         stream.close();
97         Module result = null;
98         for (Module module : modules) {
99             if (module.getName().equals(name)) {
100                 result = module;
101                 break;
102             }
103         }
104         return result;
105     }
106
107     public static Set<Module> loadModulesWithContext(final Collection<InputStream> input, final SchemaContext context)
108             throws IOException, YangSyntaxErrorException {
109         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
110         final YangContextParser parser = new YangParserImpl();
111         SchemaContext ctx = parser.parseSources(sources, context);
112         final Set<Module> modules = ctx.getModules();
113         return modules;
114     }
115
116     public static Module findModule(final Set<Module> modules, final String moduleName) {
117         Module result = null;
118         for (Module module : modules) {
119             if (module.getName().equals(moduleName)) {
120                 result = module;
121                 break;
122             }
123         }
124         return result;
125     }
126
127     public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
128         ModuleImport result = null;
129         for (ModuleImport moduleImport : imports) {
130             if (moduleImport.getPrefix().equals(prefix)) {
131                 result = moduleImport;
132                 break;
133             }
134         }
135         return result;
136     }
137
138     public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
139         TypeDefinition<?> result = null;
140         for (TypeDefinition<?> td : typedefs) {
141             if (td.getQName().getLocalName().equals(name)) {
142                 result = td;
143                 break;
144             }
145         }
146         return result;
147     }
148
149     public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, final String prefix, final String... names) {
150         List<QName> path = new ArrayList<>();
151         for (String name : names) {
152             path.add(new QName(namespace, revision, prefix, name));
153         }
154         return SchemaPath.create(path, absolute);
155     }
156
157     public static Date createDate(final String date) {
158         Date result;
159         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
160         try {
161             result = simpleDateFormat.parse(date);
162         } catch (ParseException e) {
163             result = null;
164         }
165         return result;
166     }
167
168     /**
169      * Test if node has augmenting flag set to expected value. In case this is
170      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
171      *
172      * @param node
173      *            node to check
174      * @param expected
175      *            expected value
176      */
177     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
178         assertEquals(expected, node.isAugmenting());
179         if (node instanceof DataNodeContainer) {
180             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
181                 checkIsAugmenting(child, expected);
182             }
183         } else if (node instanceof ChoiceNode) {
184             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
185                 checkIsAugmenting(caseNode, expected);
186             }
187         }
188     }
189
190     /**
191      * Check if node has addedByUses flag set to expected value. In case this is
192      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
193      *
194      * @param node
195      *            node to check
196      * @param expected
197      *            expected value
198      */
199     public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
200         assertEquals(expected, node.isAddedByUses());
201         if (node instanceof DataNodeContainer) {
202             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
203                 checkIsAddedByUses(child, expected);
204             }
205         } else if (node instanceof ChoiceNode) {
206             for (ChoiceCaseNode caseNode : ((ChoiceNode) node).getCases()) {
207                 checkIsAddedByUses(caseNode, expected);
208             }
209         }
210     }
211
212     public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
213         assertEquals(expected, node.isAddedByUses());
214         for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
215             checkIsAddedByUses(child, expected);
216         }
217     }
218
219     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
220         List<Module> result = new ArrayList<>();
221         for (Module module : modules) {
222             if (module.getName().equals(moduleName)) {
223                 result.add(module);
224             }
225         }
226         return result;
227     }
228
229 }