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