Address FIXME for QueuedNotificationManager
[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 import com.google.common.io.ByteSource;
12 import com.google.common.io.ByteStreams;
13 import java.io.ByteArrayInputStream;
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.ChoiceSchemaNode;
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     public static SchemaContext loadSchemaContext(final URI resourceDirectory)
50             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         return parser.parseFiles(testFiles);
62     }
63
64     public static Set<Module> loadModules(final URI resourceDirectory)
65             throws IOException {
66         return loadSchemaContext(resourceDirectory).getModules();
67     }
68
69     public static Set<Module> loadModules(final List<InputStream> input) throws IOException, YangSyntaxErrorException {
70         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
71         final YangContextParser parser = new YangParserImpl();
72         SchemaContext ctx = parser.parseSources(sources);
73         return ctx.getModules();
74     }
75
76     public static Module loadModule(final InputStream stream) throws IOException, YangSyntaxErrorException {
77         final YangContextParser parser = new YangParserImpl();
78         ByteSource source = new ByteSource() {
79             @Override
80             public InputStream openStream() throws IOException {
81                 return NamedByteArrayInputStream.create(stream);
82             }
83         };
84         final Collection<ByteSource> sources = Collections.singletonList(source);
85         SchemaContext ctx = parser.parseSources(sources);
86         return ctx.getModules().iterator().next();
87     }
88
89     public static Module loadModuleWithContext(final String name, final InputStream stream, final SchemaContext context)
90             throws IOException, YangSyntaxErrorException {
91         final YangContextParser parser = new YangParserImpl();
92
93         final byte[] streamContent = ByteStreams.toByteArray(stream);
94
95         ByteSource source = new ByteSource() {
96             @Override
97             public InputStream openStream() throws IOException {
98                 return new ByteArrayInputStream(streamContent);
99             }
100         };
101
102         final Collection<ByteSource> sources = Collections.singletonList(source);
103         SchemaContext ctx = parser.parseSources(sources, context);
104         final Set<Module> modules = ctx.getModules();
105         stream.close();
106         Module result = null;
107         for (Module module : modules) {
108             if (module.getName().equals(name)) {
109                 result = module;
110                 break;
111             }
112         }
113         return result;
114     }
115
116     public static Set<Module> loadModulesWithContext(final Collection<InputStream> input, final SchemaContext context)
117             throws IOException, YangSyntaxErrorException {
118         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(input);
119         final YangContextParser parser = new YangParserImpl();
120         SchemaContext ctx = parser.parseSources(sources, context);
121         final Set<Module> modules = ctx.getModules();
122         return modules;
123     }
124
125     public static Module findModule(final Set<Module> modules, final String moduleName) {
126         Module result = null;
127         for (Module module : modules) {
128             if (module.getName().equals(moduleName)) {
129                 result = module;
130                 break;
131             }
132         }
133         return result;
134     }
135
136     public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
137         ModuleImport result = null;
138         for (ModuleImport moduleImport : imports) {
139             if (moduleImport.getPrefix().equals(prefix)) {
140                 result = moduleImport;
141                 break;
142             }
143         }
144         return result;
145     }
146
147     public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
148         TypeDefinition<?> result = null;
149         for (TypeDefinition<?> td : typedefs) {
150             if (td.getQName().getLocalName().equals(name)) {
151                 result = td;
152                 break;
153             }
154         }
155         return result;
156     }
157
158     public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision, final String prefix, final String... names) {
159         List<QName> path = new ArrayList<>();
160         for (String name : names) {
161             path.add(QName.create(namespace, revision, name));
162         }
163         return SchemaPath.create(path, absolute);
164     }
165
166     public static Date createDate(final String date) {
167         Date result;
168         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
169         try {
170             result = simpleDateFormat.parse(date);
171         } catch (ParseException e) {
172             result = null;
173         }
174         return result;
175     }
176
177     /**
178      * Test if node has augmenting flag set to expected value. In case this is
179      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
180      *
181      * @param node
182      *            node to check
183      * @param expected
184      *            expected value
185      */
186     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
187         assertEquals(expected, node.isAugmenting());
188         if (node instanceof DataNodeContainer) {
189             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
190                 checkIsAugmenting(child, expected);
191             }
192         } else if (node instanceof ChoiceSchemaNode) {
193             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
194                 checkIsAugmenting(caseNode, expected);
195             }
196         }
197     }
198
199     /**
200      * Check if node has addedByUses flag set to expected value. In case this is
201      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
202      *
203      * @param node
204      *            node to check
205      * @param expected
206      *            expected value
207      */
208     public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
209         assertEquals(expected, node.isAddedByUses());
210         if (node instanceof DataNodeContainer) {
211             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
212                 checkIsAddedByUses(child, expected);
213             }
214         } else if (node instanceof ChoiceSchemaNode) {
215             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
216                 checkIsAddedByUses(caseNode, expected);
217             }
218         }
219     }
220
221     public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
222         assertEquals(expected, node.isAddedByUses());
223         for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
224             checkIsAddedByUses(child, expected);
225         }
226     }
227
228     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
229         List<Module> result = new ArrayList<>();
230         for (Module module : modules) {
231             if (module.getName().equals(moduleName)) {
232                 result.add(module);
233             }
234         }
235         return result;
236     }
237
238 }