Remove unused TestUtils methods
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.java
1 /*
2  * Copyright (c) 2016 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;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
39 import org.xml.sax.SAXException;
40
41 public final class TestUtils {
42     private TestUtils() {
43         // Hidden on purpose
44     }
45
46     public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
47             throws Exception {
48         return loadSources(TestUtils.class, resourceDirectory);
49     }
50
51     public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
52             throws Exception {
53         final var files = new File(cls.getResource(resourceDirectory).toURI())
54             .listFiles(StmtTestUtils.YANG_FILE_FILTER);
55         final var sources = new ArrayList<StatementStreamSource>(files.length);
56         for (var file : files) {
57             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
58         }
59         return sources;
60     }
61
62     public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
63         return loadModules(TestUtils.class, resourceDirectory);
64     }
65
66     public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
67             throws Exception {
68         return RFC7950Reactors.defaultReactor().newBuild()
69             .addSources(loadSources(cls, resourceDirectory))
70             .buildEffective();
71     }
72
73     public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
74             throws IOException, ReactorException, YangSyntaxErrorException {
75         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
76
77         for (String resourceName : resourceNames) {
78             reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
79                 resourceName)));
80         }
81
82         return reactor.buildEffective();
83     }
84
85     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
86             throws ReactorException, SAXException, IOException {
87         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
88
89         for (File file : new File(resourceDirectory).listFiles()) {
90             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
91                 YinTextSchemaSource.forPath(file.toPath()))));
92         }
93
94         return reactor.buildEffective();
95     }
96
97     public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
98             IOException {
99         return RFC7950Reactors.defaultReactor().newBuild()
100                 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
101                 .buildEffective()
102                 .getModules().iterator().next();
103     }
104
105     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
106         for (ModuleImport moduleImport : imports) {
107             if (moduleImport.getPrefix().equals(prefix)) {
108                 return moduleImport;
109             }
110         }
111         return null;
112     }
113
114     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
115             final String name) {
116         for (TypeDefinition<?> td : typedefs) {
117             if (td.getQName().getLocalName().equals(name)) {
118                 return td;
119             }
120         }
121         return null;
122     }
123
124     /**
125      * Test if node has augmenting flag set to expected value. In case this is
126      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
127      *
128      * @param node
129      *            node to check
130      * @param expected
131      *            expected value
132      */
133     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
134         assertEquals(expected, node.isAugmenting());
135         if (node instanceof DataNodeContainer) {
136             for (DataSchemaNode child : ((DataNodeContainer) node)
137                     .getChildNodes()) {
138                 checkIsAugmenting(child, expected);
139             }
140         } else if (node instanceof ChoiceSchemaNode) {
141             for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
142                 checkIsAugmenting(caseNode, expected);
143             }
144         }
145     }
146
147     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
148             throws ReactorException {
149         return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
150     }
151
152     public static EffectiveModelContext parseYangSources(final File... files)
153             throws ReactorException, IOException, YangSyntaxErrorException {
154
155         StatementStreamSource[] sources = new StatementStreamSource[files.length];
156
157         for (int i = 0; i < files.length; i++) {
158             sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forPath(files[i].toPath()));
159         }
160
161         return parseYangSources(sources);
162     }
163
164     public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
165             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
166
167         URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
168         File testSourcesFile = new File(resourceFile.toURI());
169
170         return parseYangSources(testSourcesFile);
171     }
172 }