BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / EffectiveSchemaContextTest.java
index 9fb9467923a65289051ceb34def3468a45c54cac..b677e71812a468db12ddff0096d03876b930a49c 100644 (file)
@@ -14,11 +14,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import com.google.common.collect.Lists;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.text.ParseException;
@@ -27,7 +23,6 @@ import java.util.List;
 import java.util.Set;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
 import org.opendaylight.yangtools.yang.model.api.Module;
@@ -37,34 +32,30 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
 
 public class EffectiveSchemaContextTest {
 
     @Test
-    public void testEffectiveSchemaContext() throws ReactorException, ParseException, FileNotFoundException,
-            URISyntaxException {
+    public void testEffectiveSchemaContext() throws ReactorException, ParseException, URISyntaxException, IOException,
+            YangSyntaxErrorException {
         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
 
-        final File yangFile1 = new File(getClass().getResource("/effective-schema-context-test/foo.yang").toURI());
-        assertNotNull(yangFile1);
-        final File yangFile2 = new File(getClass().getResource("/effective-schema-context-test/bar.yang").toURI());
-        assertNotNull(yangFile2);
-        final File yangFile3 = new File(getClass().getResource("/effective-schema-context-test/baz.yang").toURI());
-        assertNotNull(yangFile3);
-
-        final InputStream yangInputStream1 = new FileInputStream(yangFile1);
-        assertNotNull(yangInputStream1);
-        final InputStream yangInputStream2 = new FileInputStream(yangFile2);
-        assertNotNull(yangInputStream2);
-        final InputStream yangInputStream3 = new FileInputStream(yangFile3);
-        assertNotNull(yangInputStream3);
-
-        final SchemaContext schemaContext = reactor.buildEffective(Lists.newArrayList(
-                yangInputStream1, yangInputStream2, yangInputStream3));
+        final YangStatementStreamSource source1 = YangStatementStreamSource.create(YangTextSchemaSource.forResource(
+            "/effective-schema-context-test/foo.yang"));
+        final YangStatementStreamSource source2 = YangStatementStreamSource.create(YangTextSchemaSource.forResource(
+            "/effective-schema-context-test/bar.yang"));
+        final YangStatementStreamSource source3 = YangStatementStreamSource.create(YangTextSchemaSource.forResource(
+            "/effective-schema-context-test/baz.yang"));
+
+        reactor.addSources(source1, source2, source3);
+        final SchemaContext schemaContext = reactor.buildEffective();
         assertNotNull(schemaContext);
 
         final Set<DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
@@ -87,11 +78,9 @@ public class EffectiveSchemaContextTest {
 
         assertNull(schemaContext.getDataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
 
-        assertNull(schemaContext.findModuleByName("foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-08-21")));
-        assertNull(schemaContext.findModuleByNamespaceAndRevision(
-                null, SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-21")));
-        assertNull(schemaContext.findModuleByNamespaceAndRevision(
-                URI.create("foo-namespace"), SimpleDateFormatUtil.getRevisionFormat().parse("2016-08-21")));
+        assertFalse(schemaContext.findModule("foo", QName.parseRevision("2016-08-21")).isPresent());
+        assertFalse(schemaContext.findModule(URI.create("foo-namespace"), QName.parseRevision("2016-08-21"))
+            .isPresent());
 
         assertFalse(schemaContext.isAugmenting());
         assertFalse(schemaContext.isAddedByUses());
@@ -103,16 +92,13 @@ public class EffectiveSchemaContextTest {
         assertEquals(SchemaContext.NAME, schemaContext.getQName());
         assertEquals(SchemaPath.ROOT, schemaContext.getPath());
         assertEquals(Status.CURRENT, schemaContext.getStatus());
-        assertTrue(schemaContext.getUses() instanceof Set);
+        assertNotNull(schemaContext.getUses());
         assertTrue(schemaContext.getUses().isEmpty());
-        assertTrue(schemaContext.getAvailableAugmentations() instanceof Set);
+        assertNotNull(schemaContext.getAvailableAugmentations());
         assertTrue(schemaContext.getAvailableAugmentations().isEmpty());
 
-        Module fooModule = schemaContext.findModuleByName(
-                "foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-21"));
-        assertFalse(schemaContext.getModuleSource(fooModule).isPresent());
-
-        assertEquals(3, schemaContext.getAllModuleIdentifiers().size());
+        Module fooModule = schemaContext.findModule("foo", QName.parseRevision("2016-09-21")).get();
+        assertEquals(3, schemaContext.getModules().size());
         assertEquals(3, ((EffectiveSchemaContext) schemaContext).getRootDeclaredStatements().size());
         assertEquals(3,((EffectiveSchemaContext) schemaContext).getRootEffectiveStatements().size());