Fixed bug in resolving groupings.
[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.File;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URI;
17 import java.text.DateFormat;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
36
37 final class TestUtils {
38
39     static final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
40
41     private TestUtils() {
42     }
43
44     public static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
45         YangModelParser parser = new YangParserImpl();
46         final File testDir = new File(resourceDirectory);
47         final String[] fileList = testDir.list();
48         final List<File> testFiles = new ArrayList<>();
49         if (fileList == null) {
50             throw new FileNotFoundException(resourceDirectory);
51         }
52         for (String fileName : fileList) {
53             testFiles.add(new File(testDir, fileName));
54         }
55         return parser.parseYangModels(testFiles);
56     }
57
58     public static Set<Module> loadModules(List<InputStream> input) throws IOException {
59         final YangModelParser parser = new YangParserImpl();
60         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
61         for (InputStream stream : input) {
62             stream.close();
63         }
64         return modules;
65     }
66
67     public static Module loadModule(final InputStream stream) throws IOException {
68         final YangModelParser parser = new YangParserImpl();
69         final List<InputStream> input = Collections.singletonList(stream);
70         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input));
71         stream.close();
72         return modules.iterator().next();
73     }
74
75     public static Module loadModuleWithContext(final InputStream stream, final SchemaContext context)
76             throws IOException {
77         final YangModelParser parser = new YangParserImpl();
78         final List<InputStream> input = Collections.singletonList(stream);
79         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
80         stream.close();
81         return modules.iterator().next();
82     }
83
84     public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
85             throws IOException {
86         final YangModelParser parser = new YangParserImpl();
87         final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
88         for (InputStream is : input) {
89             if (is != null) {
90                 is.close();
91             }
92         }
93         return modules;
94     }
95
96     public static Module findModule(Set<Module> modules, String moduleName) {
97         Module result = null;
98         for (Module module : modules) {
99             if (module.getName().equals(moduleName)) {
100                 result = module;
101                 break;
102             }
103         }
104         return result;
105     }
106
107     public static ModuleImport findImport(Set<ModuleImport> imports, String prefix) {
108         ModuleImport result = null;
109         for (ModuleImport moduleImport : imports) {
110             if (moduleImport.getPrefix().equals(prefix)) {
111                 result = moduleImport;
112                 break;
113             }
114         }
115         return result;
116     }
117
118     public static TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs, String name) {
119         TypeDefinition<?> result = null;
120         for (TypeDefinition<?> td : typedefs) {
121             if (td.getQName().getLocalName().equals(name)) {
122                 result = td;
123                 break;
124             }
125         }
126         return result;
127     }
128
129     public static SchemaPath createPath(boolean absolute, URI namespace, Date revision, String prefix, String... names) {
130         List<QName> path = new ArrayList<>();
131         for (String name : names) {
132             path.add(new QName(namespace, revision, prefix, name));
133         }
134         return new SchemaPath(path, absolute);
135     }
136
137     public static Date createDate(String date) {
138         Date result;
139         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
140         try {
141             result = simpleDateFormat.parse(date);
142         } catch (ParseException e) {
143             result = null;
144         }
145         return result;
146     }
147
148     /**
149      * Test if node has augmenting flag set to true. In case this is
150      * DataNodeContainer, check his child nodes too.
151      *
152      * @param node
153      *            node to check
154      */
155     public static void checkIsAugmenting(DataSchemaNode node, boolean expected) {
156         assertEquals(expected, node.isAugmenting());
157         if (node instanceof DataNodeContainer) {
158             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
159                 checkIsAugmenting(child, expected);
160             }
161         }
162     }
163
164 }