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