Populate model/ hierarchy
[yangtools.git] / model / yang-model-ri / src / test / java / org / opendaylight / yangtools / yang / model / ri / type / 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.ri.type;
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 import static org.mockito.Mockito.mock;
17
18 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.PathExpression;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
24
25 public class LeafrefTest {
26     @Test
27     public void testMethodsOfLeafrefTest() {
28         final QName qname = QName.create("test", "List1");
29         final PathExpression revision = mock(PathExpression.class);
30         final PathExpression revision2 = mock(PathExpression.class);
31
32         final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision).build();
33         final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision2).build();
34         final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision).build();
35         final LeafrefTypeDefinition leafref4 = leafref;
36
37         assertNotNull("Object 'leafref' shouldn't be null.", leafref);
38         assertNull("Base type of 'leafref' should be null.", leafref.getBaseType());
39         assertEquals(Optional.empty(), leafref.getUnits());
40         assertEquals(Optional.empty(), leafref.getDefaultValue());
41         assertEquals(qname, leafref.getQName());
42         assertFalse(leafref.getDescription().isPresent());
43         assertFalse(leafref.getReference().isPresent());
44         assertEquals("Status of 'leafref' is current.", Status.CURRENT, leafref.getStatus());
45         assertTrue("Object 'leafref' shouldn't have any unknown schema nodes.",
46                 leafref.getUnknownSchemaNodes().isEmpty());
47         assertEquals("Revision aware XPath of 'leafref' should be '/test:Cont1/test:List1'.", revision,
48                 leafref.getPathStatement());
49         assertNotNull("String representation of 'leafref' shouldn't be null.", leafref.toString());
50         assertNotEquals("Hash codes of two different object of type Leafref shouldn't be equal.", leafref.hashCode(),
51                 leafref2.hashCode());
52         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref3));
53         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref4));
54         assertFalse("Objects of type Leafref shouldn't be equal.", leafref.equals(leafref2));
55         assertFalse("Objects shouldn't be equal.", leafref.equals(null));
56         assertFalse("Objects shouldn't be equal.", leafref.equals("test"));
57     }
58
59     @Test
60     public void testRequireInstanceSubstatement() {
61         final QName qname = QName.create("test", "my-leafref");
62         final PathExpression path = mock(PathExpression.class);
63         final LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(path);
64
65         assertTrue(leafrefTypeBuilder.build().requireInstance());
66
67         leafrefTypeBuilder.setRequireInstance(false);
68         final LeafrefTypeDefinition falseLeafref = leafrefTypeBuilder.build();
69         assertFalse(falseLeafref.requireInstance());
70
71         leafrefTypeBuilder.setRequireInstance(true);
72         final LeafrefTypeDefinition trueLeafref = leafrefTypeBuilder.build();
73         assertTrue(trueLeafref.requireInstance());
74
75         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> falseBuilder =
76                 RestrictedTypes.newLeafrefBuilder(falseLeafref, qname);
77         assertFalse(falseBuilder.build().requireInstance());
78
79         final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> trueBuilder =
80                 RestrictedTypes.newLeafrefBuilder(trueLeafref, qname);
81         assertTrue(trueBuilder.build().requireInstance());
82     }
83 }