Do not suppress 'uses' node effects
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.java
index 65378f78da4d14735c8e56ccd2ac6b7b7d752ba5..ccd2213953828ffd9969979be43cb9c1fc75b43d 100644 (file)
@@ -7,29 +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.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.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;
@@ -37,13 +31,9 @@ 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
     }
@@ -68,39 +58,37 @@ public final class TestUtils {
         return loadModules(TestUtils.class, resourceDirectory);
     }
 
+    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.getResource(resourceDirectory).toURI());
+        return loadModules(cls, resourceDirectory, null);
     }
 
-    public static EffectiveModelContext loadModules(final URI resourceDirectory)
-            throws ReactorException, IOException, YangSyntaxErrorException {
-        final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
-        File[] files = new File(resourceDirectory).listFiles();
-
-        for (File file : files) {
-            if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
-                reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
-            } else {
-                LOG.info("Ignoring non-yang file {}", file);
-            }
+    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 reactor.buildEffective();
+        return action.buildEffective();
     }
 
-    public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
-            throws IOException, ReactorException, YangSyntaxErrorException {
-        final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
-
-        for (String resourceName : resourceNames) {
-            reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
-                resourceName)));
+    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();
@@ -139,58 +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 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.forPath(files[i].toPath()));
-        }
-
-        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 parseYangSource(final String yangSourceFilePath)
-            throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
-
-        URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
-        File testSourcesFile = new File(resourceFile.toURI());
-
-        return parseYangSources(testSourcesFile);
-    }
 }