Bug 5947: Increasing code coverage for mdsal project
[mdsal.git] / dom / mdsal-dom-api / src / test / java / org / opendaylight / mdsal / dom / api / DOMDataTreeIdentifierTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.dom.api;
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.assertTrue;
15
16 import java.net.URI;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22
23 public class DOMDataTreeIdentifierTest {
24     private static final String REF_LISTS = "ref-lists";
25     private static final String TEST_LISTS = "test-lists";
26     private static final String COMPARE_FIRST_LISTS = "A-test-lists";
27     private static final String COMPARE_SECOND_LISTS = "B-test-lists";
28     private static final QNameModule TEST_MODULE = QNameModule.create(URI.create(
29             "urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store"), null);
30     private static final YangInstanceIdentifier REF_YII_IID = YangInstanceIdentifier.create(
31             new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_MODULE, REF_LISTS)));
32     private static final YangInstanceIdentifier TEST_YII_IID = YangInstanceIdentifier.create(
33             new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_MODULE, TEST_LISTS)));
34     private static final DOMDataTreeIdentifier REF_TREE = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
35             REF_YII_IID);
36     private static final DOMDataTreeIdentifier TEST_DIFF_TREE = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
37             TEST_YII_IID);
38
39     @Test
40     public void constructTest() {
41         assertNotNull("Instantiation", new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, REF_YII_IID));
42     }
43
44     @Test(expected = NullPointerException.class)
45     public void firstArgumentNullTest() throws Exception {
46         new DOMDataTreeIdentifier(null, REF_YII_IID);
47     }
48
49     @Test(expected = NullPointerException.class)
50     public void secondArgumentNullTest() throws Exception {
51         new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, null);
52     }
53
54     @Test
55     public void hashCodeTest() {
56         assertEquals("hashCode", REF_TREE.hashCode(), new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
57                 REF_YII_IID).hashCode());
58         assertNotEquals("hashCode", REF_TREE.hashCode(), TEST_DIFF_TREE.hashCode());
59     }
60
61     @Test
62     public void equalsTest() {
63         assertTrue("Equals same", REF_TREE.equals(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
64                 REF_YII_IID)));
65         assertFalse("Different DataStoreType", REF_TREE.equals(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
66                 REF_YII_IID)));
67         assertTrue("Equals same instance", REF_TREE.equals(REF_TREE));
68         assertFalse("Different object", REF_TREE.equals(new Object()));
69         assertFalse("Different instance", REF_TREE.equals(TEST_DIFF_TREE));
70     }
71
72     @Test
73     public void compareToTest() {
74         final YangInstanceIdentifier compareFirstIid = YangInstanceIdentifier.create(
75                 new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_MODULE, COMPARE_FIRST_LISTS)));
76         final YangInstanceIdentifier compareSecondIid = YangInstanceIdentifier.create(
77                 new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_MODULE, COMPARE_SECOND_LISTS)));
78
79         assertEquals("Compare same to same", REF_TREE.compareTo(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
80                 REF_YII_IID)), 0);
81         assertNotEquals("Compare same to same with different datastore", REF_TREE.compareTo(new DOMDataTreeIdentifier(
82                 LogicalDatastoreType.CONFIGURATION, REF_YII_IID)), 0);
83         assertEquals("Compare same to same with different datastore",
84                 new DOMDataTreeIdentifier(
85                         LogicalDatastoreType.OPERATIONAL,
86                         YangInstanceIdentifier.create(
87                                 YangInstanceIdentifier.NodeIdentifier.create(QName.create(TEST_MODULE, REF_LISTS)),
88                                 YangInstanceIdentifier.NodeIdentifier.create(QName.create(TEST_MODULE, TEST_LISTS))))
89                 .compareTo(REF_TREE), 1);
90         assertTrue("Compare first to second", new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
91                 compareFirstIid).compareTo(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
92                 compareSecondIid)) < 0);
93         assertTrue("Compare second to first", new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
94                 compareSecondIid).compareTo(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
95                 compareFirstIid)) > 0);
96     }
97
98     @Test
99     public void containsTest() {
100         assertTrue("Contains", REF_TREE.contains(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
101                 REF_YII_IID)));
102         assertEquals("Not contains", false, REF_TREE.contains(TEST_DIFF_TREE));
103     }
104
105     @Test
106     public void toStringTest() {
107         assertTrue("ToString", REF_TREE.toString().contains(REF_TREE.getRootIdentifier().toString()) && REF_TREE
108                 .toString().contains(REF_TREE.getDatastoreType().toString()));
109     }
110 }