Fixup xpath resolution
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtilTest.java
index 4e27022d4d3163d6ce3ba96a44b5a74d7c79840e..50785ae53c0e0db577fc9a3d840b1acda574276b 100644 (file)
@@ -8,15 +8,22 @@
 package org.opendaylight.yangtools.yang.model.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 
+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.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.Module;
@@ -26,16 +33,38 @@ 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;
 
+@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");
+
     @Mock
     private SchemaContext mockSchemaContext;
     @Mock
     private Module mockModule;
 
-    @Test
-    public void testFindDummyData() {
-        MockitoAnnotations.initMocks(this);
+    @Mock
+    private SchemaNode schemaNode;
+
+    @Before
+    public void before() {
         doReturn(Optional.empty()).when(mockSchemaContext).findModule(any(QNameModule.class));
         doReturn(Optional.empty()).when(mockSchemaContext).findDataTreeChild(any(Iterable.class));
 
@@ -45,24 +74,56 @@ public class SchemaContextUtilTest {
         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);
-        assertEquals("Should be null. Module TestQName not found", null,
+        assertNull("Should be null. Module TestQName not found",
                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, schemaPath));
 
         RevisionAwareXPath xpath = new RevisionAwareXPathImpl("/test:bookstore/test:book/test:title", true);
-        assertEquals("Should be null. Module bookstore not found", null,
+        assertNull("Should be null. Module bookstore not found",
                 SchemaContextUtil.findDataSchemaNode(mockSchemaContext, mockModule, xpath));
 
-        SchemaNode schemaNode = BaseTypes.int32Type();
+        SchemaNode int32node = BaseTypes.int32Type();
         RevisionAwareXPath xpathRelative = new RevisionAwareXPathImpl("../prefix", false);
-        assertEquals("Should be null, Module prefix not found", null,
+        assertNull("Should be null, Module prefix not found",
                 SchemaContextUtil.findDataSchemaNodeForRelativeXPath(
-                        mockSchemaContext, mockModule, schemaNode, xpathRelative));
+                        mockSchemaContext, mockModule, int32node, xpathRelative));
 
-        assertEquals("Should be null. Module TestQName not found", null,
+        assertNull("Should be null. Module TestQName not found",
                 SchemaContextUtil.findNodeInSchemaContext(mockSchemaContext, Collections.singleton(qname)));
 
-        assertEquals("Should be null.", null, SchemaContextUtil.findParentModule(mockSchemaContext, schemaNode));
+        assertNull("Should be null.", SchemaContextUtil.findParentModule(mockSchemaContext, int32node));
+    }
+
+    @Test
+    public void testDeref() {
+        RevisionAwareXPath xpath = new RevisionAwareXPathImpl("deref(../foo)/../bar", false);
+        assertNull(SchemaContextUtil.findDataSchemaNodeForRelativeXPath(mockSchemaContext, mockModule, schemaNode,
+            xpath));
+    }
+
+    @Test
+    public void testNormalizeXPath() {
+        assertNormalizedPath(0, ImmutableList.of(""), "");
+        assertNormalizedPath(0, ImmutableList.of("a"), "a");
+        assertNormalizedPath(0, ImmutableList.of("a", "b"), "a b");
+        assertNormalizedPath(1, ImmutableList.of("..", "b"), ".. b");
+        assertNormalizedPath(0, ImmutableList.of(), "a ..");
+        assertNormalizedPath(0, ImmutableList.of("b"), "a .. b");
+        assertNormalizedPath(2, ImmutableList.of("..", "..", "a", "c"), ".. .. a b .. c");
+        assertNormalizedPath(3, ImmutableList.of("..", "..", "..", "b"), ".. .. a .. .. b");
+    }
+
+    private static void assertNormalizedPath(final int expectedLead, final List<String> expectedList,
+            final String input) {
+        final List<String> list = new ArrayList<>(SPACE_SPLITTER.splitToList(input));
+        assertEquals(expectedLead, SchemaContextUtil.normalizeXPath(list));
+        assertEquals(expectedList, list);
     }
 }