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