Inline Bug7480Test model assembly 04/97704/7
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Oct 2021 20:51:35 +0000 (22:51 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 2 Oct 2021 11:02:55 +0000 (13:02 +0200)
We have a few utilities which are noize in StmtTestUtils, move them to
heir sole user. This makes it wee bit more obvious what is going on.
This creates a very useful litte utility in TestUtils.

Change-Id: Ic6fe1a9e36eafc1cde1e42c3b1c2895ccef3e5fb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7480Test.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java

index a3668a2bd7e00db6a6d12d57e4cd78fe36ac6569..bea668a07d1c488ce580ed5dd66e8bb424cb401a 100644 (file)
@@ -7,15 +7,18 @@
  */
 package org.opendaylight.yangtools.yang.stmt;
 
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.util.Collection;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.Submodule;
@@ -25,7 +28,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedEx
 public class Bug7480Test {
     @Test
     public void libSourcesTest() throws Exception {
-        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
+        final SchemaContext context = parseYangSources("/bugs/bug7480/files", "/bugs/bug7480/lib");
         assertNotNull(context);
 
         final Collection<? extends Module> modules = context.getModules();
@@ -48,14 +51,11 @@ public class Bug7480Test {
 
     @Test
     public void missingRelevantImportTest() throws Exception {
-        try {
-            StmtTestUtils.parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2");
-            fail("Test should fail due to missing import of required yang source from library");
-        } catch (final SomeModifiersUnresolvedException e) {
-            final String message = e.getSuppressed().length > 0 ? e.getSuppressed()[0].getCause().getMessage() : e
-                    .getCause().getCause().getMessage();
-            assertTrue(message.startsWith("Imported module [missing-lib] was not found."));
-        }
+        final var ex = assertThrows(SomeModifiersUnresolvedException.class,
+            () -> parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2"));
+        final String message = ex.getSuppressed().length > 0
+            ? ex.getSuppressed()[0].getCause().getMessage() : ex.getCause().getCause().getMessage();
+        assertThat(message, startsWith("Imported module [missing-lib] was not found."));
     }
 
     @Test
@@ -63,14 +63,14 @@ public class Bug7480Test {
         // parent module as main source and as lib source at the same time
         // parser should remove it from the required lib sources and thus avoid module namespace collision
         final SchemaContext schemaContext =  RFC7950Reactors.defaultReactor().newBuild()
-                .addSource(StmtTestUtils.sourceForResource(
-                        "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
-                .addLibSources(
-                    StmtTestUtils.sourceForResource(
-                        "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
-                    StmtTestUtils.sourceForResource(
-                        "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
-                .buildEffective();
+            .addSource(StmtTestUtils.sourceForResource(
+                "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
+            .addLibSources(
+                StmtTestUtils.sourceForResource(
+                    "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"),
+                StmtTestUtils.sourceForResource(
+                    "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"))
+            .buildEffective();
         assertNotNull(schemaContext);
     }
 
@@ -79,14 +79,22 @@ public class Bug7480Test {
         // submodule as main source and as lib source at the same time
         // parser should remove it from the required lib sources and thus avoid submodule name collision
         final SchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
-                .addSource(StmtTestUtils.sourceForResource(
-                        "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
-                .addLibSources(
-                    StmtTestUtils.sourceForResource(
-                            "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
-                    StmtTestUtils.sourceForResource(
-                            "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
-                .buildEffective();
+            .addSource(StmtTestUtils.sourceForResource(
+                "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
+            .addLibSources(
+                StmtTestUtils.sourceForResource(
+                    "/bugs/bug7480/main-source-lib-source-conflict-test/parent-module.yang"),
+                StmtTestUtils.sourceForResource(
+                    "/bugs/bug7480/main-source-lib-source-conflict-test/child-module.yang"))
+            .buildEffective();
         assertNotNull(schemaContext);
     }
+
+    private static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
+            final String yangLibsDirectoryPath) throws Exception {
+        return RFC7950Reactors.defaultReactor().newBuild()
+            .addSources(TestUtils.loadSources(yangFilesDirectoryPath))
+            .addLibSources(TestUtils.loadSources(yangLibsDirectoryPath))
+            .buildEffective();
+    }
 }
index 6b0d4d3b30c2a6c1c8b83292f2b1f07836aaaaeb..5390bc4a96807a9e63d5ef3ee6f99efd1ca967d2 100644 (file)
@@ -191,49 +191,6 @@ public final class StmtTestUtils {
         return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
     }
 
-    public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
-            final String yangLibsDirectoryPath)
-            throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
-        return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
-    }
-
-    public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
-            final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
-            ReactorException, IOException, YangSyntaxErrorException {
-        final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
-        final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
-
-        return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
-                supportedFeatures);
-    }
-
-    private static EffectiveModelContext parseYangSources(final File[] yangFiles, final File[] libFiles,
-            final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
-        final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
-        for (int i = 0; i < yangFiles.length; i++) {
-            yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
-        }
-
-        final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
-        for (int i = 0; i < libFiles.length; i++) {
-            libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
-        }
-
-        return parseYangSources(yangSources, libSources, supportedFeatures);
-    }
-
-    private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
-            final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
-
-        final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
-                .addSources(yangSources).addLibSources(libSources);
-        if (supportedFeatures != null) {
-            reactor.setSupportedFeatures(supportedFeatures);
-        }
-
-        return reactor.buildEffective();
-    }
-
     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
             throws URISyntaxException, SAXException, IOException, ReactorException {
         return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
index a0b7e8cc7958bbd1bc40c0ec07d1cf4407d098bd..4a3a009c88ac3b9eb4b1ced4d60173c303d43b09 100644 (file)
@@ -17,6 +17,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+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;
@@ -44,6 +45,18 @@ public final class TestUtils {
     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
 
     private TestUtils() {
+        // Hidden on purpose
+    }
+
+    public static @NonNull List<StatementStreamSource> loadSources(final String yangFilesDirectoryPath)
+            throws Exception {
+        final var files = new File(TestUtils.class.getResource(yangFilesDirectoryPath).toURI())
+            .listFiles(StmtTestUtils.YANG_FILE_FILTER);
+        final var sources = new ArrayList<StatementStreamSource>(files.length);
+        for (var file : files) {
+            sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
+        }
+        return sources;
     }
 
     public static EffectiveModelContext loadModules(final URI resourceDirectory)