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