5d690280a04543d43387730a974cb5a1ecda3e23
[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<File>();
49         if(fileList == null) {
50             throw new FileNotFoundException(resourceDirectory);
51         }
52         for (int i = 0; i < fileList.length; i++) {
53             String fileName = fileList[i];
54             testFiles.add(new File(testDir, fileName));
55         }
56         return parser.parseYangModels(testFiles);
57     }
58
59     public static Set<Module> loadModules(List<InputStream> input) throws IOException {
60         final YangModelParser parser = new YangParserImpl();
61         final Set<Module> modules = new HashSet<Module>(
62                 parser.parseYangModelsFromStreams(input));
63         for(InputStream stream : input) {
64             stream.close();
65         }
66         return modules;
67     }
68
69     public static Module loadModule(final InputStream stream) throws
70             IOException {
71         final YangModelParser parser = new YangParserImpl();
72         final List<InputStream> input = Collections.singletonList(stream);
73         final Set<Module> modules = new HashSet<Module>(
74                 parser.parseYangModelsFromStreams(input));
75         stream.close();
76         return modules.iterator().next();
77     }
78
79     public static Module loadModuleWithContext(final InputStream stream, final SchemaContext context) throws IOException {
80         final YangModelParser parser = new YangParserImpl();
81         final List<InputStream> input = Collections.singletonList(stream);
82         final Set<Module> modules = new HashSet<Module>(parser.parseYangModelsFromStreams(input, context));
83         stream.close();
84         return modules.iterator().next();
85     }
86
87     public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context) throws IOException {
88         final YangModelParser parser = new YangParserImpl();
89         final Set<Module> modules = new HashSet<Module>(parser.parseYangModelsFromStreams(input, context));
90         for(InputStream is : input) {
91             if(is != null) {
92                 is.close();
93             }
94         }
95         return modules;
96     }
97
98     public static Module findModule(Set<Module> modules, String moduleName) {
99         Module result = null;
100         for (Module module : modules) {
101             if (module.getName().equals(moduleName)) {
102                 result = module;
103                 break;
104             }
105         }
106         return result;
107     }
108
109     public static ModuleImport findImport(Set<ModuleImport> imports,
110             String prefix) {
111         ModuleImport result = null;
112         for (ModuleImport moduleImport : imports) {
113             if (moduleImport.getPrefix().equals(prefix)) {
114                 result = moduleImport;
115                 break;
116             }
117         }
118         return result;
119     }
120
121     public static TypeDefinition<?> findTypedef(
122             Set<TypeDefinition<?>> typedefs, String name) {
123         TypeDefinition<?> result = null;
124         for (TypeDefinition<?> td : typedefs) {
125             if (td.getQName().getLocalName().equals(name)) {
126                 result = td;
127                 break;
128             }
129         }
130         return result;
131     }
132
133     public static SchemaPath createPath(boolean absolute, URI namespace,
134             Date revision, String prefix, String... names) {
135         List<QName> path = new ArrayList<QName>();
136         for (String name : names) {
137             path.add(new QName(namespace, revision, prefix, name));
138         }
139         return new SchemaPath(path, absolute);
140     }
141
142     public static Date createDate(String date) {
143         Date result;
144         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
145         try {
146             result = simpleDateFormat.parse(date);
147         } catch (ParseException e) {
148             result = null;
149         }
150         return result;
151     }
152
153     /**
154      * Test if node has augmenting flag set to true. In case this is
155      * DataNodeContainer, check his child nodes too.
156      *
157      * @param node
158      *            node to check
159      */
160     public static void checkIsAugmenting(DataSchemaNode node, boolean expected) {
161         assertEquals(expected, node.isAugmenting());
162         if (node instanceof DataNodeContainer) {
163             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
164                 checkIsAugmenting(child, expected);
165             }
166         }
167     }
168
169 }