Do not suppress 'uses' node effects
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.java
index 9f3112708122f372d4c3873f9ac4f0c98a393f8e..ccd2213953828ffd9969979be43cb9c1fc75b43d 100644 (file)
@@ -7,31 +7,23 @@
  */
 package org.opendaylight.yangtools.yang.stmt;
 
-import static org.junit.Assert.assertEquals;
-
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import java.util.Optional;
+import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.YangConstants;
-import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
@@ -39,51 +31,71 @@ import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransform
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.xml.sax.SAXException;
 
 public final class TestUtils {
-    private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
-
     private TestUtils() {
+        // Hidden on purpose
     }
 
-    public static EffectiveModelContext loadModules(final URI resourceDirectory)
-            throws ReactorException, IOException, YangSyntaxErrorException {
-        final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
-        File[] files = new File(resourceDirectory).listFiles();
+    public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
+            throws Exception {
+        return loadSources(TestUtils.class, resourceDirectory);
+    }
 
-        for (File file : files) {
-            if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
-                reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
-            } else {
-                LOG.info("Ignoring non-yang file {}", file);
-            }
+    public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
+            throws Exception {
+        final var files = new File(cls.getResource(resourceDirectory).toURI())
+            .listFiles(StmtTestUtils.YANG_FILE_FILTER);
+        final var sources = new ArrayList<StatementStreamSource>(files.length);
+        for (var file : files) {
+            sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
         }
+        return sources;
+    }
 
-        return reactor.buildEffective();
+    public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
+        return loadModules(TestUtils.class, resourceDirectory);
     }
 
-    public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
-            throws IOException, ReactorException, YangSyntaxErrorException {
-        final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
+    public static EffectiveModelContext loadModules(final String resourceDirectory,
+            final @Nullable Set<QName> supportedFeatures) throws Exception {
+        return loadModules(TestUtils.class, resourceDirectory, supportedFeatures);
+    }
+
+    public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
+            throws Exception {
+        return loadModules(cls, resourceDirectory, null);
+    }
 
-        for (String resourceName : resourceNames) {
-            reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
-                resourceName)));
+    public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory,
+            final @Nullable Set<QName> supportedFeatures) throws Exception {
+        final var action = RFC7950Reactors.defaultReactor().newBuild()
+            .addSources(loadSources(cls, resourceDirectory));
+        if (supportedFeatures != null) {
+            action.setSupportedFeatures(supportedFeatures);
         }
+        return action.buildEffective();
+    }
 
+    public static EffectiveModelContext parseYangSource(final String... yangSourceFilePath) throws Exception {
+        final var reactor = RFC7950Reactors.defaultReactor().newBuild();
+        for (var resourcePath : yangSourceFilePath) {
+            reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of(
+                TestUtils.class.getResource(resourcePath).toURI()))));
+        }
         return reactor.buildEffective();
     }
 
+    // FIXME: these remain unaudited
+
     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
             throws ReactorException, SAXException, IOException {
         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
 
         for (File file : new File(resourceDirectory).listFiles()) {
             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
-                YinTextSchemaSource.forFile(file))));
+                YinTextSchemaSource.forPath(file.toPath()))));
         }
 
         return reactor.buildEffective();
@@ -97,11 +109,6 @@ public final class TestUtils {
                 .getModules().iterator().next();
     }
 
-    @Deprecated(forRemoval = true)
-    public static Optional<? extends @NonNull Module> findModule(final SchemaContext context, final String moduleName) {
-        return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
-    }
-
     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
         for (ModuleImport moduleImport : imports) {
             if (moduleImport.getPrefix().equals(prefix)) {
@@ -120,78 +127,4 @@ public final class TestUtils {
         }
         return null;
     }
-
-    /**
-     * Test if node has augmenting flag set to expected value. In case this is
-     * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
-     *
-     * @param node
-     *            node to check
-     * @param expected
-     *            expected value
-     */
-    public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
-        assertEquals(expected, node.isAugmenting());
-        if (node instanceof DataNodeContainer) {
-            for (DataSchemaNode child : ((DataNodeContainer) node)
-                    .getChildNodes()) {
-                checkIsAugmenting(child, expected);
-            }
-        } else if (node instanceof ChoiceSchemaNode) {
-            for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
-                checkIsAugmenting(caseNode, expected);
-            }
-        }
-    }
-
-    public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
-        List<Module> result = new ArrayList<>();
-        for (Module module : modules) {
-            if (module.getName().equals(moduleName)) {
-                result.add(module);
-            }
-        }
-        return result;
-    }
-
-    public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
-            throws ReactorException {
-        return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
-    }
-
-    public static EffectiveModelContext parseYangSources(final File... files)
-            throws ReactorException, IOException, YangSyntaxErrorException {
-
-        StatementStreamSource[] sources = new StatementStreamSource[files.length];
-
-        for (int i = 0; i < files.length; i++) {
-            sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
-        }
-
-        return parseYangSources(sources);
-    }
-
-    public static EffectiveModelContext parseYangSources(final Collection<File> files)
-            throws ReactorException, IOException, YangSyntaxErrorException {
-        return parseYangSources(files.toArray(new File[files.size()]));
-    }
-
-    public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
-            throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
-
-        URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
-        File testSourcesDir = new File(resourceDir.toURI());
-
-        return parseYangSources(testSourcesDir.listFiles(
-            (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
-    }
-
-    public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
-            throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
-
-        URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
-        File testSourcesFile = new File(resourceFile.toURI());
-
-        return parseYangSources(testSourcesFile);
-    }
 }