Remove SchemaPath from TypeDefinition implementations
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / LeafrefTest.java
index f1ea79a28fb931c130784d41f45a41497e93b20c..7663342738a1b7faf0cb40fec356846aa02d69cf 100644 (file)
@@ -14,43 +14,73 @@ 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.PathExpression;
 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 RevisionAwareXPathImpl revision = new RevisionAwareXPathImpl("/test:Cont1/test:List1", false);
-        final RevisionAwareXPathImpl revision2 = new RevisionAwareXPathImpl("/test:Cont1/test:List2", false);
+        final QName qname = QName.create("test", "List1");
+        final PathExpression revision = new PathExpressionImpl("/test:Cont1/test:List1", false);
+        final PathExpression revision2 = new PathExpressionImpl("/test:Cont1/test:List2", false);
 
-        final Leafref leafref = Leafref.create(schemaPath, revision);
-        final Leafref leafref2 = Leafref.create(schemaPath, revision2);
-        final Leafref leafref3 = Leafref.create(schemaPath, revision);
-        final Leafref leafref4 = leafref;
+        final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision).build();
+        final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision2).build();
+        final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(qname).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());
-        assertTrue("Units of 'leafref' should be empty.", leafref.getUnits().isEmpty());
-        assertEquals("Default value of 'leafref' is 'leafref' itself.", leafref, leafref.getDefaultValue());
-        assertEquals("QName of 'leafref' is value '(urn:ietf:params:xml:ns:yang:1)leafref'.",
-                BaseTypes.constructQName("leafref"), leafref.getQName());
-        assertEquals("SchemaPath of 'leafref' is '/Cont1/List1'.", schemaPath, leafref.getPath());
-        assertEquals("Description of 'leafref' is 'The leafref type is used to reference a particular leaf instance in the data tree.'",
-                "The leafref type is used to reference a particular leaf instance in the data tree.", leafref.getDescription());
-        assertEquals("Reference of 'leafref' is 'https://tools.ietf.org/html/rfc6020#section-9.9'.", "https://tools.ietf.org/html/rfc6020#section-9.9", leafref.getReference());
+        assertEquals(Optional.empty(), leafref.getUnits());
+        assertEquals(Optional.empty(), leafref.getDefaultValue());
+        assertEquals(qname, leafref.getQName());
+        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 QName qname = QName.create("test", "my-leafref");
+        final PathExpression path = new PathExpressionImpl("../my-leaf", false);
+        final LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(qname).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, qname);
+        assertFalse(falseBuilder.build().requireInstance());
+
+        final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> trueBuilder =
+                RestrictedTypes.newLeafrefBuilder(trueLeafref, qname);
+        assertTrue(trueBuilder.build().requireInstance());
+    }
 }