Fix leafref require-instance implementation
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / LeafrefTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.model.api.Status;
21 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
23 import org.opendaylight.yangtools.yang.model.util.type.LeafrefTypeBuilder;
24 import org.opendaylight.yangtools.yang.model.util.type.RequireInstanceRestrictedTypeBuilder;
25 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
26
27 public class LeafrefTest {
28
29     @Test
30     public void testMethodsOfLeafrefTest() {
31         final SchemaPath schemaPath = SchemaPath.create(false, QName.create("Cont1"), QName.create("List1"));
32         final RevisionAwareXPathImpl revision = new RevisionAwareXPathImpl("/test:Cont1/test:List1", false);
33         final RevisionAwareXPathImpl revision2 = new RevisionAwareXPathImpl("/test:Cont1/test:List2", false);
34
35         final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision).build();
36         final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision2).build();
37         final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision).build();
38         final LeafrefTypeDefinition leafref4 = leafref;
39
40         assertNotNull("Object 'leafref' shouldn't be null.", leafref);
41         assertNull("Base type of 'leafref' should be null.", leafref.getBaseType());
42         assertNull("Units of 'leafref' should be empty.", leafref.getUnits());
43         assertNull("Leafref does not have a default value", leafref.getDefaultValue());
44         assertEquals(QName.create("List1"), leafref.getQName());
45         assertEquals("SchemaPath of 'leafref' is '/Cont1/List1'.", schemaPath, leafref.getPath());
46         assertNull(leafref.getDescription());
47         assertNull(leafref.getReference());
48         assertEquals("Status of 'leafref' is current.", Status.CURRENT, leafref.getStatus());
49         assertTrue("Object 'leafref' shouldn't have any unknown schema nodes.", leafref.getUnknownSchemaNodes().isEmpty());
50         assertEquals("Revision aware XPath of 'leafref' should be '/test:Cont1/test:List1'.", revision, leafref.getPathStatement());
51         assertNotNull("String representation of 'leafref' shouldn't be null.", leafref.toString());
52         assertNotEquals("Hash codes of two different object of type Leafref shouldn't be equal.", leafref.hashCode(), leafref2.hashCode());
53         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref3));
54         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref4));
55         assertFalse("Objects of type Leafref shouldn't be equal.", leafref.equals(leafref2));
56         assertFalse("Objects shouldn't be equal.", leafref.equals(null));
57         assertFalse("Objects shouldn't be equal.", leafref.equals("test"));
58     }
59
60     @Test
61     public void testRequireInstanceSubstatement() {
62         final SchemaPath schemaPath = SchemaPath.create(true, QName.create("my-cont"), QName.create("my-leafref"));
63         final RevisionAwareXPathImpl path = new RevisionAwareXPathImpl("../my-leaf", false);
64         final LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(path);
65
66         assertTrue(leafrefTypeBuilder.build().requireInstance());
67
68         leafrefTypeBuilder.setRequireInstance(false);
69         final LeafrefTypeDefinition falseLeafref = leafrefTypeBuilder.build();
70         assertFalse(falseLeafref.requireInstance());
71
72         leafrefTypeBuilder.setRequireInstance(true);
73         final LeafrefTypeDefinition trueLeafref = leafrefTypeBuilder.build();
74         assertTrue(trueLeafref.requireInstance());
75
76         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> falseBuilder =
77                 RestrictedTypes.newLeafrefBuilder(falseLeafref, schemaPath);
78         assertFalse(falseBuilder.build().requireInstance());
79
80         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> trueBuilder =
81                 RestrictedTypes.newLeafrefBuilder(trueLeafref, schemaPath);
82         assertTrue(trueBuilder.build().requireInstance());
83     }
84 }