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