Fix SchemaContextUtil RevisionAwareXPath resolution
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / LeafrefTest.java
index 1972d94c25ef6d601b7ca8afc495563d0ff6e814..313ad5ace13394a1fc61c17856a0dd73dd0f6167 100644 (file)
@@ -13,43 +13,81 @@ import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+
+import java.util.Optional;
 import org.junit.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.LeafrefTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
 
 public class LeafrefTest {
 
     @Test
     public void testMethodsOfLeafrefTest() {
-        final SchemaPath schemaPath = SchemaPath.create(false, QName.create("Cont1"), QName.create("List1"));
+        final SchemaPath schemaPath = SchemaPath.create(false, QName.create("test", "Cont1"),
+            QName.create("test", "List1"));
         final RevisionAwareXPathImpl revision = new RevisionAwareXPathImpl("/test:Cont1/test:List1", false);
         final RevisionAwareXPathImpl revision2 = new RevisionAwareXPathImpl("/test:Cont1/test:List2", false);
 
-        final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision).build();
-        final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision2).build();
-        final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision).build();
+        final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision)
+            .build();
+        final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision2)
+            .build();
+        final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision)
+            .build();
         final LeafrefTypeDefinition leafref4 = leafref;
 
         assertNotNull("Object 'leafref' shouldn't be null.", leafref);
         assertNull("Base type of 'leafref' should be null.", leafref.getBaseType());
-        assertNull("Units of 'leafref' should be empty.", leafref.getUnits());
-        assertNull("Leafref does not have a default value", leafref.getDefaultValue());
-        assertEquals(QName.create("List1"), leafref.getQName());
+        assertEquals(Optional.empty(), leafref.getUnits());
+        assertEquals(Optional.empty(), leafref.getDefaultValue());
+        assertEquals(QName.create("test", "List1"), leafref.getQName());
         assertEquals("SchemaPath of 'leafref' is '/Cont1/List1'.", schemaPath, leafref.getPath());
-        assertNull(leafref.getDescription());
-        assertNull(leafref.getReference());
+        assertFalse(leafref.getDescription().isPresent());
+        assertFalse(leafref.getReference().isPresent());
         assertEquals("Status of 'leafref' is current.", Status.CURRENT, leafref.getStatus());
-        assertTrue("Object 'leafref' shouldn't have any unknown schema nodes.", leafref.getUnknownSchemaNodes().isEmpty());
-        assertEquals("Revision aware XPath of 'leafref' should be '/test:Cont1/test:List1'.", revision, leafref.getPathStatement());
+        assertTrue("Object 'leafref' shouldn't have any unknown schema nodes.",
+                leafref.getUnknownSchemaNodes().isEmpty());
+        assertEquals("Revision aware XPath of 'leafref' should be '/test:Cont1/test:List1'.", revision,
+                leafref.getPathStatement());
         assertNotNull("String representation of 'leafref' shouldn't be null.", leafref.toString());
-        assertNotEquals("Hash codes of two different object of type Leafref shouldn't be equal.", leafref.hashCode(), leafref2.hashCode());
+        assertNotEquals("Hash codes of two different object of type Leafref shouldn't be equal.", leafref.hashCode(),
+                leafref2.hashCode());
         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref3));
         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref4));
         assertFalse("Objects of type Leafref shouldn't be equal.", leafref.equals(leafref2));
         assertFalse("Objects shouldn't be equal.", leafref.equals(null));
         assertFalse("Objects shouldn't be equal.", leafref.equals("test"));
     }
+
+    @Test
+    public void testRequireInstanceSubstatement() {
+        final SchemaPath schemaPath = SchemaPath.create(true, QName.create("test", "my-cont"),
+            QName.create("test", "my-leafref"));
+        final RevisionAwareXPathImpl path = new RevisionAwareXPathImpl("../my-leaf", false);
+        final LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(path);
+
+        assertTrue(leafrefTypeBuilder.build().requireInstance());
+
+        leafrefTypeBuilder.setRequireInstance(false);
+        final LeafrefTypeDefinition falseLeafref = leafrefTypeBuilder.build();
+        assertFalse(falseLeafref.requireInstance());
+
+        leafrefTypeBuilder.setRequireInstance(true);
+        final LeafrefTypeDefinition trueLeafref = leafrefTypeBuilder.build();
+        assertTrue(trueLeafref.requireInstance());
+
+        final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> falseBuilder =
+                RestrictedTypes.newLeafrefBuilder(falseLeafref, schemaPath);
+        assertFalse(falseBuilder.build().requireInstance());
+
+        final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> trueBuilder =
+                RestrictedTypes.newLeafrefBuilder(trueLeafref, schemaPath);
+        assertTrue(trueBuilder.build().requireInstance());
+    }
 }