2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
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;
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;
27 public class LeafrefTest {
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);
36 final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision)
38 final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision2)
40 final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(revision)
42 final LeafrefTypeDefinition leafref4 = leafref;
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(),
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"));
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);
73 LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(schemaPath).setPathStatement(path);
75 leafrefTypeBuilder.setRequireInstance(false);
76 LeafrefTypeDefinition leafref = leafrefTypeBuilder.build();
77 assertFalse(leafref.requireInstance());
79 leafrefTypeBuilder.setRequireInstance(true);
80 leafref = leafrefTypeBuilder.build();
81 assertTrue(leafref.requireInstance());
83 leafrefTypeBuilder.setRequireInstance(true);
84 leafref = leafrefTypeBuilder.build();
85 assertTrue(leafref.requireInstance());
88 leafrefTypeBuilder.setRequireInstance(false);
89 fail("An IllegalArgumentException should have been thrown.");
90 } catch (IllegalArgumentException ex) {
92 "Cannot switch off require-instance in type AbsoluteSchemaPath{path=[(test)my-cont, (test)my-leafref]}",