2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import static org.junit.Assert.assertEquals;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
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;
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;
37 final class TestUtils {
39 static final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
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);
52 for (int i = 0; i < fileList.length; i++) {
53 String fileName = fileList[i];
54 testFiles.add(new File(testDir, fileName));
56 return parser.parseYangModels(testFiles);
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) {
69 public static Module loadModule(final InputStream stream) throws
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));
76 return modules.iterator().next();
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));
84 return modules.iterator().next();
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) {
98 public static Module findModule(Set<Module> modules, String moduleName) {
100 for (Module module : modules) {
101 if (module.getName().equals(moduleName)) {
109 public static ModuleImport findImport(Set<ModuleImport> imports,
111 ModuleImport result = null;
112 for (ModuleImport moduleImport : imports) {
113 if (moduleImport.getPrefix().equals(prefix)) {
114 result = moduleImport;
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)) {
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));
139 return new SchemaPath(path, absolute);
142 public static Date createDate(String date) {
144 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
146 result = simpleDateFormat.parse(date);
147 } catch (ParseException e) {
154 * Test if node has augmenting flag set to true. In case this is
155 * DataNodeContainer, check his child nodes too.
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);