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