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.spi.type;
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.mockito.Mockito.mock;
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.PathExpression;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 public class LeafrefTest {
27 public void testMethodsOfLeafrefTest() {
28 final QName qname = QName.create("test", "List1");
29 final PathExpression revision = mock(PathExpression.class);
30 final PathExpression revision2 = mock(PathExpression.class);
32 final LeafrefTypeDefinition leafref = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision).build();
33 final LeafrefTypeDefinition leafref2 = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision2).build();
34 final LeafrefTypeDefinition leafref3 = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(revision).build();
35 final LeafrefTypeDefinition leafref4 = leafref;
37 assertNotNull("Object 'leafref' shouldn't be null.", leafref);
38 assertNull("Base type of 'leafref' should be null.", leafref.getBaseType());
39 assertEquals(Optional.empty(), leafref.getUnits());
40 assertEquals(Optional.empty(), leafref.getDefaultValue());
41 assertEquals(qname, leafref.getQName());
42 assertFalse(leafref.getDescription().isPresent());
43 assertFalse(leafref.getReference().isPresent());
44 assertEquals("Status of 'leafref' is current.", Status.CURRENT, leafref.getStatus());
45 assertTrue("Object 'leafref' shouldn't have any unknown schema nodes.",
46 leafref.getUnknownSchemaNodes().isEmpty());
47 assertEquals("Revision aware XPath of 'leafref' should be '/test:Cont1/test:List1'.", revision,
48 leafref.getPathStatement());
49 assertNotNull("String representation of 'leafref' shouldn't be null.", leafref.toString());
50 assertNotEquals("Hash codes of two different object of type Leafref shouldn't be equal.", leafref.hashCode(),
52 assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref3));
53 assertTrue("Objects of type Leafref should be equal.", leafref.equals(leafref4));
54 assertFalse("Objects of type Leafref shouldn't be equal.", leafref.equals(leafref2));
55 assertFalse("Objects shouldn't be equal.", leafref.equals(null));
56 assertFalse("Objects shouldn't be equal.", leafref.equals("test"));
60 public void testRequireInstanceSubstatement() {
61 final QName qname = QName.create("test", "my-leafref");
62 final PathExpression path = mock(PathExpression.class);
63 final LeafrefTypeBuilder leafrefTypeBuilder = BaseTypes.leafrefTypeBuilder(qname).setPathStatement(path);
65 assertTrue(leafrefTypeBuilder.build().requireInstance());
67 leafrefTypeBuilder.setRequireInstance(false);
68 final LeafrefTypeDefinition falseLeafref = leafrefTypeBuilder.build();
69 assertFalse(falseLeafref.requireInstance());
71 leafrefTypeBuilder.setRequireInstance(true);
72 final LeafrefTypeDefinition trueLeafref = leafrefTypeBuilder.build();
73 assertTrue(trueLeafref.requireInstance());
75 final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> falseBuilder =
76 RestrictedTypes.newLeafrefBuilder(falseLeafref, qname);
77 assertFalse(falseBuilder.build().requireInstance());
79 final RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> trueBuilder =
80 RestrictedTypes.newLeafrefBuilder(trueLeafref, qname);
81 assertTrue(trueBuilder.build().requireInstance());