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<>();
49 if (fileList == null) {
50 throw new FileNotFoundException(resourceDirectory);
52 for (String fileName : fileList) {
53 testFiles.add(new File(testDir, fileName));
55 return parser.parseYangModels(testFiles);
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) {
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));
72 return modules.iterator().next();
75 public static Module loadModuleWithContext(final InputStream stream, final SchemaContext context)
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));
81 return modules.iterator().next();
84 public static Set<Module> loadModulesWithContext(final List<InputStream> input, final SchemaContext context)
86 final YangModelParser parser = new YangParserImpl();
87 final Set<Module> modules = new HashSet<>(parser.parseYangModelsFromStreams(input, context));
88 for (InputStream is : input) {
96 public static Module findModule(Set<Module> modules, String moduleName) {
98 for (Module module : modules) {
99 if (module.getName().equals(moduleName)) {
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;
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)) {
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));
134 return new SchemaPath(path, absolute);
137 public static Date createDate(String date) {
139 final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
141 result = simpleDateFormat.parse(date);
142 } catch (ParseException e) {
149 * Test if node has augmenting flag set to true. In case this is
150 * DataNodeContainer, check his child nodes too.
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);