Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / 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.stmt.retest;
9
10 import static org.junit.Assert.assertEquals;
11 import java.io.File;
12 import java.io.InputStream;
13 import java.net.URI;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.List;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
37
38 final class TestUtils {
39
40     private TestUtils() {
41     }
42
43     public static Set<Module> loadModules(final URI resourceDirectory) throws SourceException, ReactorException {
44         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
45         File[] files = new File(resourceDirectory).listFiles();
46
47         for (File file : files) {
48             addSources(reactor, new YangStatementSourceImpl(file.getPath(), true));
49         }
50
51         EffectiveSchemaContext ctx = reactor.buildEffective();
52         return ctx.getModules();
53     }
54
55     public static Set<Module> loadModules(final List<InputStream> streams) throws SourceException, ReactorException {
56         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57         for (InputStream inputStream : streams) {
58             addSources(reactor, new YangStatementSourceImpl(inputStream));
59         }
60
61         EffectiveSchemaContext ctx = reactor.buildEffective();
62         return ctx.getModules();
63     }
64
65     public static Module loadModule(final InputStream stream) throws SourceException, ReactorException {
66         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
67         addSources(reactor, new YangStatementSourceImpl(stream));
68         EffectiveSchemaContext ctx = reactor.buildEffective();
69         return ctx.getModules().iterator().next();
70     }
71
72     public static Module findModule(final Set<Module> modules, final String moduleName) {
73         Module result = null;
74         for (Module module : modules) {
75             if (module.getName().equals(moduleName)) {
76                 result = module;
77                 break;
78             }
79         }
80         return result;
81     }
82
83     public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
84         ModuleImport result = null;
85         for (ModuleImport moduleImport : imports) {
86             if (moduleImport.getPrefix().equals(prefix)) {
87                 result = moduleImport;
88                 break;
89             }
90         }
91         return result;
92     }
93
94     public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
95         TypeDefinition<?> result = null;
96         for (TypeDefinition<?> td : typedefs) {
97             if (td.getQName().getLocalName().equals(name)) {
98                 result = td;
99                 break;
100             }
101         }
102         return result;
103     }
104
105     public static SchemaPath createPath(final boolean absolute, final URI namespace, final Date revision,
106             final String prefix, final String... names) {
107         List<QName> path = new ArrayList<>();
108         for (String name : names) {
109             path.add(QName.create(namespace, revision, name));
110         }
111         return SchemaPath.create(path, absolute);
112     }
113
114     public static Date createDate(final String date) {
115         Date result;
116         final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
117         try {
118             result = simpleDateFormat.parse(date);
119         } catch (ParseException e) {
120             result = null;
121         }
122         return result;
123     }
124
125     /**
126      * Test if node has augmenting flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its
127      * child nodes/case nodes too.
128      *
129      * @param node
130      *            node to check
131      * @param expected
132      *            expected value
133      */
134     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
135         assertEquals(expected, node.isAugmenting());
136         if (node instanceof DataNodeContainer) {
137             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
138                 checkIsAugmenting(child, expected);
139             }
140         } else if (node instanceof ChoiceSchemaNode) {
141             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
142                 checkIsAugmenting(caseNode, expected);
143             }
144         }
145     }
146
147     /**
148      * Check if node has addedByUses flag set to expected value. In case this is DataNodeContainer/ChoiceNode, check its
149      * child nodes/case nodes too.
150      *
151      * @param node
152      *            node to check
153      * @param expected
154      *            expected value
155      */
156     public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
157         assertEquals(expected, node.isAddedByUses());
158         if (node instanceof DataNodeContainer) {
159             for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
160                 checkIsAddedByUses(child, expected);
161             }
162         } else if (node instanceof ChoiceSchemaNode) {
163             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
164                 checkIsAddedByUses(caseNode, expected);
165             }
166         }
167     }
168
169     public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
170         assertEquals(expected, node.isAddedByUses());
171         for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) {
172             checkIsAddedByUses(child, expected);
173         }
174     }
175
176     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
177         List<Module> result = new ArrayList<>();
178         for (Module module : modules) {
179             if (module.getName().equals(moduleName)) {
180                 result.add(module);
181             }
182         }
183         return result;
184     }
185
186     private static void addSources(CrossSourceStatementReactor.BuildAction reactor, YangStatementSourceImpl... sources) {
187         for (YangStatementSourceImpl source : sources) {
188             reactor.addSource(source);
189         }
190     }
191
192 }