BUG-7052: extract SimpleSchemaContext
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / MoreRevisionsTest.java
index 4002451dfe658feea225dd4a2ade9ae72b07dede..f18c46d7694822918b0e24424a85c4ff9c235d1f 100644 (file)
@@ -10,8 +10,10 @@ package org.opendaylight.yangtools.yang.stmt;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-import java.io.FileNotFoundException;
+import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
+
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.text.ParseException;
@@ -21,57 +23,53 @@ import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 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.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
 
 public class MoreRevisionsTest {
 
-    private static final YangStatementSourceImpl REVFILE = new YangStatementSourceImpl(
-            "/semantic-statement-parser/revisions/more-revisions-test.yang",
-            false);
+    private static final StatementStreamSource REVFILE = sourceForResource(
+            "/semantic-statement-parser/revisions/more-revisions-test.yang");
 
-    private static final YangStatementSourceImpl TED_20130712 = new YangStatementSourceImpl(
-            "/semantic-statement-parser/two-revisions/ted@2013-07-12.yang",
-            false);
+    private static final StatementStreamSource TED_20130712 = sourceForResource(
+            "/semantic-statement-parser/two-revisions/ted@2013-07-12.yang");
 
-    private static final YangStatementSourceImpl TED_20131021 = new YangStatementSourceImpl(
-            "/semantic-statement-parser/two-revisions/ted@2013-10-21.yang",
-            false);
+    private static final StatementStreamSource TED_20131021 = sourceForResource(
+            "/semantic-statement-parser/two-revisions/ted@2013-10-21.yang");
 
-    private static final YangStatementSourceImpl IETF_TYPES = new YangStatementSourceImpl(
-            "/ietf/ietf-inet-types@2010-09-24.yang", false);
+    private static final StatementStreamSource IETF_TYPES = sourceForResource(
+            "/ietf/ietf-inet-types@2010-09-24.yang");
 
-    private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20130712 = new YangStatementSourceImpl(
-            "/ietf/network-topology@2013-07-12.yang", false);
+    private static final StatementStreamSource NETWORK_TOPOLOGY_20130712 = sourceForResource(
+            "/ietf/network-topology@2013-07-12.yang");
 
-    private static final YangStatementSourceImpl NETWORK_TOPOLOGY_20131021 = new YangStatementSourceImpl(
-            "/ietf/network-topology@2013-10-21.yang", false);
+    private static final StatementStreamSource NETWORK_TOPOLOGY_20131021 = sourceForResource(
+            "/ietf/network-topology@2013-10-21.yang");
 
-    private static final YangStatementSourceImpl ISIS_20130712 = new YangStatementSourceImpl(
-            "/semantic-statement-parser/two-revisions/isis-topology@2013-07-12.yang",
-            false);
+    private static final StatementStreamSource ISIS_20130712 = sourceForResource(
+            "/semantic-statement-parser/two-revisions/isis-topology@2013-07-12.yang");
 
-    private static final YangStatementSourceImpl ISIS_20131021 = new YangStatementSourceImpl(
-            "/semantic-statement-parser/two-revisions/isis-topology@2013-10-21.yang",
-            false);
+    private static final StatementStreamSource ISIS_20131021 = sourceForResource(
+            "/semantic-statement-parser/two-revisions/isis-topology@2013-10-21.yang");
 
-    private static final YangStatementSourceImpl L3_20130712 = new YangStatementSourceImpl(
-            "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-07-12.yang",
-            false);
+    private static final StatementStreamSource L3_20130712 = sourceForResource(
+            "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-07-12.yang");
 
-    private static final YangStatementSourceImpl L3_20131021 = new YangStatementSourceImpl(
-            "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-10-21.yang",
-            false);
+    private static final StatementStreamSource L3_20131021 = sourceForResource(
+            "/semantic-statement-parser/two-revisions/l3-unicast-igp-topology@2013-10-21.yang");
 
     @Test
     public void readAndParseYangFileTest() throws SourceException,
@@ -79,7 +77,7 @@ public class MoreRevisionsTest {
         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
                 .newBuild();
         reactor.addSource(REVFILE);
-        EffectiveSchemaContext result = reactor.buildEffective();
+        SchemaContext result = reactor.buildEffective();
         assertNotNull(result);
         final Module moduleByName = result.getModules().iterator().next();
         final QNameModule qNameModule = moduleByName.getQNameModule();
@@ -94,7 +92,7 @@ public class MoreRevisionsTest {
 
         reactor.addSources(TED_20130712, TED_20131021, IETF_TYPES);
 
-        EffectiveSchemaContext result = reactor.buildEffective();
+        SchemaContext result = reactor.buildEffective();
         assertNotNull(result);
 
     }
@@ -107,7 +105,7 @@ public class MoreRevisionsTest {
         reactor.addSources(NETWORK_TOPOLOGY_20130712,
                 NETWORK_TOPOLOGY_20131021, IETF_TYPES);
 
-        EffectiveSchemaContext result = reactor.buildEffective();
+        SchemaContext result = reactor.buildEffective();
         assertNotNull(result);
         Set<Module> modules = result.getModules();
 
@@ -126,13 +124,12 @@ public class MoreRevisionsTest {
                 ISIS_20131021, L3_20130712, L3_20131021, IETF_TYPES,
                 NETWORK_TOPOLOGY_20130712, NETWORK_TOPOLOGY_20131021);
 
-        EffectiveSchemaContext result = reactor.buildEffective();
+        SchemaContext result = reactor.buildEffective();
         assertNotNull(result);
     }
 
     @Test
-    public void multipleRevisionsTest() throws SourceException,
-            ReactorException, FileNotFoundException, URISyntaxException {
+    public void multipleRevisionsTest() throws Exception {
         for (int i = 0; i < 100; i++) {
             SchemaContext context = StmtTestUtils
                     .parseYangSources("/semantic-statement-parser/multiple-revisions");
@@ -141,8 +138,7 @@ public class MoreRevisionsTest {
     }
 
     @Test
-    public void multipleRevisionsFullTest() throws SourceException,
-            ReactorException, FileNotFoundException, URISyntaxException,
+    public void multipleRevisionsFullTest() throws Exception,
             ParseException {
         for (int i = 0; i < 100; i++) {
             SchemaContext context = StmtTestUtils
@@ -153,8 +149,7 @@ public class MoreRevisionsTest {
         }
     }
 
-    private static void checkContentFullTest(final SchemaContext context) throws ParseException,
-            URISyntaxException {
+    private static void checkContentFullTest(final SchemaContext context) throws ParseException, URISyntaxException {
 
         String yangTypesNSStr = "urn:ietf:params:xml:ns:yang:ietf-yang-types";
         URI yangTypesNS = new URI(yangTypesNSStr);
@@ -242,12 +237,10 @@ public class MoreRevisionsTest {
     }
 
     @Test
-    public void multipleRevisionsSimpleTest() throws SourceException,
-            ReactorException, FileNotFoundException, URISyntaxException,
-            ParseException {
+    public void multipleRevisionsSimpleTest() throws Exception {
         for (int i = 0; i < 1000; i++) {
-            SchemaContext context = StmtTestUtils
-                    .parseYangSources("/semantic-statement-parser/multiple-revisions/simple");
+            SchemaContext context = StmtTestUtils.parseYangSources(
+                "/semantic-statement-parser/multiple-revisions/simple");
             assertNotNull(context);
             assertEquals(5, context.getModules().size());
             checkContentSimpleTest(context);
@@ -379,4 +372,23 @@ public class MoreRevisionsTest {
         }
         return false;
     }
+
+    @Test
+    public void nodeTest() throws Exception {
+        SchemaContext context = StmtTestUtils.parseYangSources("/semantic-statement-parser/multiple-revisions/node-test");
+        assertNotNull(context);
+
+        QName root = QName.create("foo", "2016-04-06", "foo-root");
+        QName container20160404 = QName.create("foo", "2016-04-06", "con20160404");
+        SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, root, container20160404));
+        assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
+
+        QName container20160405 = QName.create("foo", "2016-04-06", "con20160405");
+        findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, root, container20160405));
+        assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
+
+        QName container20160406 = QName.create("foo", "2016-04-06", "con20160406");
+        findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, SchemaPath.create(true, root, container20160406));
+        assertNull(findDataSchemaNode);
+    }
 }