Enable checkstyle in yang-model-util
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / LeafrefTest.java
index f1ea79a28fb931c130784d41f45a41497e93b20c..2e4be322a1cfd2859e0f1b6afd2daa85d262befd 100644 (file)
@@ -13,11 +13,15 @@ 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 static org.junit.Assert.fail;
 
 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;
 
 public class LeafrefTest {
 
@@ -27,30 +31,63 @@ public class LeafrefTest {
         final RevisionAwareXPathImpl revision = new RevisionAwareXPathImpl("/test:Cont1/test:List1", false);
         final RevisionAwareXPathImpl revision2 = new RevisionAwareXPathImpl("/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(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());
-        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());
+        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("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());
+        assertNull(leafref.getDescription());
+        assertNull(leafref.getReference());
         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("my-cont"), QName.create("my-leafref"));
+        final RevisionAwareXPathImpl path = new RevisionAwareXPathImpl("../my-leaf", false);
+
+        LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(path);
+
+        leafrefTypeBuilder.setRequireInstance(false);
+        LeafrefTypeDefinition leafref = leafrefTypeBuilder.build();
+        assertFalse(leafref.requireInstance());
+
+        leafrefTypeBuilder.setRequireInstance(true);
+        leafref = leafrefTypeBuilder.build();
+        assertTrue(leafref.requireInstance());
+
+        leafrefTypeBuilder.setRequireInstance(true);
+        leafref = leafrefTypeBuilder.build();
+        assertTrue(leafref.requireInstance());
+
+        try {
+            leafrefTypeBuilder.setRequireInstance(false);
+            fail("An IllegalArgumentException should have been thrown.");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Cannot switch off require-instance in type AbsoluteSchemaPath{path=[my-cont, my-leafref]}",
+                    ex.getMessage());
+        }
+
+    }
 }