Split out yang-model-ri
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtilTest.java
index 50785ae53c0e0db577fc9a3d840b1acda574276b..be19429bea127177652c97860e2c7329b328d2db 100644 (file)
@@ -9,88 +9,72 @@ package org.opendaylight.yangtools.yang.model.util;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
-import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
-import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
+import org.opendaylight.yangtools.yang.model.api.PathExpression;
 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.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class SchemaContextUtilTest {
-    private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
-    private static final URI NAMESPACE = URI.create("abc");
-
-    // The idea is:
-    // container baz {
-    //     leaf xyzzy {
-    //         type leafref;
-    //     }
-    //     leaf foo {
-    //         type string;
-    //     }
-    //     leaf bar {
-    //         type string;
-    //     }
-    // }
-    private static final QName FOO = QName.create(NAMESPACE, "foo");
-    private static final QName BAR = QName.create(NAMESPACE, "bar");
-    private static final QName BAZ = QName.create(NAMESPACE, "baz");
-    private static final QName XYZZY = QName.create(NAMESPACE, "xyzzy");
+    public static final Splitter SPACE_SPLITTER = Splitter.on(' ');
+    public static final XMLNamespace NAMESPACE = XMLNamespace.of("abc");
 
     @Mock
-    private SchemaContext mockSchemaContext;
+    public SchemaContext mockSchemaContext;
     @Mock
-    private Module mockModule;
-
+    public Module mockModule;
     @Mock
-    private SchemaNode schemaNode;
+    public SchemaNode schemaNode;
 
-    @Before
-    public void before() {
+    @Test
+    @Ignore
+    // FIXME: YANGTOOLS-1127: rewrite this test in terms of a real PathExpression
+    public void testFindDummyData() {
         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
 
         doReturn("test").when(mockModule).getName();
         doReturn("test").when(mockModule).getPrefix();
-        doReturn(NAMESPACE).when(mockModule).getNamespace();
         doReturn(QNameModule.create(NAMESPACE)).when(mockModule).getQNameModule();
-        doReturn(Optional.empty()).when(mockModule).getRevision();
-
-        doReturn(SchemaPath.create(true, BAZ, XYZZY)).when(schemaNode).getPath();
-    }
-
-    @Test
-    public void testFindDummyData() {
 
         QName qname = QName.create("namespace", "localname");
         SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
         assertNull("Should be null. Module TestQName not found",
                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
 
-        RevisionAwareXPath xpath = new RevisionAwareXPathImpl("/test:bookstore/test:book/test:title", true);
+        PathExpression xpath = new PathExpressionImpl("/test:bookstore/test:book/test:title", true);
         assertNull("Should be null. Module bookstore not found",
                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
 
+
+        final PathExpression xPath = new PathExpressionImpl("/bookstore/book/title", true);
+        assertEquals("Should be null. Module bookstore not found", null,
+                SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xPath));
+
         SchemaNode int32node = BaseTypes.int32Type();
-        RevisionAwareXPath xpathRelative = new RevisionAwareXPathImpl("../prefix", false);
+        PathExpression xpathRelative = new PathExpressionImpl("../prefix", false);
         assertNull("Should be null, Module prefix not found",
                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
                         mockSchemaContext, mockModule, int32node, xpathRelative));
@@ -102,10 +86,68 @@ public class SchemaContextUtilTest {
     }
 
     @Test
-    public void testDeref() {
-        RevisionAwareXPath xpath = new RevisionAwareXPathImpl("deref(../foo)/../bar", false);
-        assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
-            xpath));
+    public void findDataSchemaNodeFromXPathIllegalArgumentTest() {
+        assertThrows(NullPointerException.class,
+            () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), mock(Module.class), null));
+    }
+
+    @Test
+    public void findDataSchemaNodeFromXPathIllegalArgumentTest2() {
+        final SchemaContext mockContext = mock(SchemaContext.class);
+        final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
+
+        assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(mockContext, null, xpath));
+    }
+
+    @Test
+    public void findDataSchemaNodeFromXPathIllegalArgumentTest3() {
+        final Module module = mock(Module.class);
+        final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", true);
+
+        assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null, module, xpath));
+    }
+
+    @Test
+    public void findDataSchemaNodeFromXPathIllegalArgumentTest4() {
+        final SchemaContext mockContext = mock(SchemaContext.class);
+        final Module module = mock(Module.class);
+        final PathExpression xpath = new PathExpressionImpl("my:my-grouping[@con='NULL']/my:my-leaf-in-gouping2", true);
+
+        assertThrows(IllegalArgumentException.class,
+            () -> SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
+    }
+
+    @Test
+    public void findParentModuleIllegalArgumentTest() {
+        assertThrows(NullPointerException.class,
+            () -> SchemaContextUtil.findParentModule(mock(SchemaContext.class), null));
+    }
+
+    @Test
+    public void findParentModuleIllegalArgumentTest2() {
+        doReturn(QName.create("foo", "bar")).when(schemaNode).getQName();
+        assertThrows(NullPointerException.class, () -> SchemaContextUtil.findParentModule(null, schemaNode));
+    }
+
+    @Test
+    public void findDataSchemaNodeIllegalArgumentTest() {
+        assertThrows(NullPointerException.class,
+            () -> SchemaContextUtil.findDataSchemaNode(mock(SchemaContext.class), (SchemaPath) null));
+    }
+
+    @Test
+    public void findDataSchemaNodeIllegalArgumentTest2() {
+        assertThrows(NullPointerException.class, () -> SchemaContextUtil.findDataSchemaNode(null,
+            SchemaPath.create(true, QName.create(XMLNamespace.of("uri:my-module"), Revision.of("2014-10-07"), "foo"))));
+    }
+
+    @Test
+    public void findDataSchemaNodeFromXPathNullTest2() {
+        final SchemaContext mockContext = mock(SchemaContext.class);
+        final Module module = mock(Module.class);
+        final PathExpression xpath = new PathExpressionImpl("my:my-grouping/my:my-leaf-in-gouping2", false);
+
+        assertNull(SchemaContextUtil.findDataSchemaNode(mockContext, module, xpath));
     }
 
     @Test