Update TypeDefinition design
[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 import static org.junit.Assert.fail;
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.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
27 public class LeafrefTest {
28
29     @Test
30     public void testMethodsOfLeafrefTest() {
31         final SchemaPath schemaPath = SchemaPath.create(false, QName.create("test", "Cont1"),
32             QName.create("test", "List1"));
33         final RevisionAwareXPathImpl revision = new RevisionAwareXPathImpl("/test:Cont1/test:List1", false);
34         final RevisionAwareXPathImpl revision2 = new RevisionAwareXPathImpl("/test:Cont1/test:List2", false);
35
36         final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision)
37             .build();
38         final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision2)
39             .build();
40         final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision)
41             .build();
42         final LeafrefTypeDefinition leafref4 = leafref;
43
44         assertNotNull("Object 'leafref' shouldn't be null.", leafref);
45         assertNull("Base type of 'leafref' should be null.", leafref.getBaseType());
46         assertEquals(Optional.empty(), leafref.getUnits());
47         assertEquals(Optional.empty(), leafref.getDefaultValue());
48         assertEquals(QName.create("test", "List1"), leafref.getQName());
49         assertEquals("SchemaPath of 'leafref' is '/Cont1/List1'.", schemaPath, leafref.getPath());
50         assertFalse(leafref.getDescription().isPresent());
51         assertFalse(leafref.getReference().isPresent());
52         assertEquals("Status of 'leafref' is current.", Status.CURRENT, leafref.getStatus());
53         assertTrue("Object 'leafref' shouldn't have any unknown schema nodes.",
54                 leafref.getUnknownSchemaNodes().isEmpty());
55         assertEquals("Revision aware XPath of 'leafref' should be '/test:Cont1/test:List1'.", revision,
56                 leafref.getPathStatement());
57         assertNotNull("String representation of 'leafref' shouldn't be null.", leafref.toString());
58         assertNotEquals("Hash codes of two different object of type Leafref shouldn't be equal.", leafref.hashCode(),
59                 leafref2.hashCode());
60         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref3));
61         assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref4));
62         assertFalse("Objects of type Leafref shouldn't be equal.", leafref.equals(leafref2));
63         assertFalse("Objects shouldn't be equal.", leafref.equals(null));
64         assertFalse("Objects shouldn't be equal.", leafref.equals("test"));
65     }
66
67     @Test
68     public void testRequireInstanceSubstatement() {
69         final SchemaPath schemaPath = SchemaPath.create(true, QName.create("test", "my-cont"),
70             QName.create("test", "my-leafref"));
71         final RevisionAwareXPathImpl path = new RevisionAwareXPathImpl("../my-leaf", false);
72
73         LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(path);
74
75         leafrefTypeBuilder.setRequireInstance(false);
76         LeafrefTypeDefinition leafref = leafrefTypeBuilder.build();
77         assertFalse(leafref.requireInstance());
78
79         leafrefTypeBuilder.setRequireInstance(true);
80         leafref = leafrefTypeBuilder.build();
81         assertTrue(leafref.requireInstance());
82
83         leafrefTypeBuilder.setRequireInstance(true);
84         leafref = leafrefTypeBuilder.build();
85         assertTrue(leafref.requireInstance());
86
87         try {
88             leafrefTypeBuilder.setRequireInstance(false);
89             fail("An IllegalArgumentException should have been thrown.");
90         } catch (IllegalArgumentException ex) {
91             assertEquals(
92                 "Cannot switch off require-instance in type AbsoluteSchemaPath{path=[(test)my-cont, (test)my-leafref]}",
93                 ex.getMessage());
94         }
95
96     }
97 }