Merge branch 'mdsal-trace' from controller
[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"));
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
37         TEST_DIFF_TREE = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,TEST_YII_IID);
38
39     @Test
40     public void constructTest() {
41         assertNotNull("Instantiation", new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, REF_YII_IID));
42     }
43
44     @Test
45     public void hashCodeTest() {
46         assertEquals("hashCode", REF_TREE.hashCode(), new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
47                 REF_YII_IID).hashCode());
48         assertNotEquals("hashCode", REF_TREE.hashCode(), TEST_DIFF_TREE.hashCode());
49     }
50
51     @Test
52     public void equalsTest() {
53         assertTrue("Equals same", REF_TREE.equals(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
54                 REF_YII_IID)));
55         assertFalse("Different DataStoreType", REF_TREE.equals(
56                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
57                 REF_YII_IID)));
58         assertTrue("Equals same instance", REF_TREE.equals(REF_TREE));
59         assertFalse("Different object", REF_TREE.equals(new Object()));
60         assertFalse("Different instance", REF_TREE.equals(TEST_DIFF_TREE));
61     }
62
63     @Test
64     public void compareToTest() {
65         final YangInstanceIdentifier compareFirstIid = YangInstanceIdentifier.create(
66                 new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_MODULE, COMPARE_FIRST_LISTS)));
67         final YangInstanceIdentifier compareSecondIid = YangInstanceIdentifier.create(
68                 new YangInstanceIdentifier.NodeIdentifier(QName.create(TEST_MODULE, COMPARE_SECOND_LISTS)));
69
70         assertEquals("Compare same to same", REF_TREE.compareTo(
71                 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
72                 REF_YII_IID)), 0);
73         assertNotEquals("Compare same to same with different datastore", REF_TREE.compareTo(new DOMDataTreeIdentifier(
74                 LogicalDatastoreType.CONFIGURATION, REF_YII_IID)), 0);
75         assertEquals("Compare same to same with different datastore",
76                 new DOMDataTreeIdentifier(
77                         LogicalDatastoreType.OPERATIONAL,
78                         YangInstanceIdentifier.create(
79                                 YangInstanceIdentifier.NodeIdentifier.create(QName.create(TEST_MODULE, REF_LISTS)),
80                                 YangInstanceIdentifier.NodeIdentifier.create(QName.create(TEST_MODULE, TEST_LISTS))))
81                 .compareTo(REF_TREE), 1);
82         assertTrue("Compare first to second", new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
83                 compareFirstIid).compareTo(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
84                 compareSecondIid)) < 0);
85         assertTrue("Compare second to first", new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
86                 compareSecondIid).compareTo(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
87                 compareFirstIid)) > 0);
88     }
89
90     @Test
91     public void containsTest() {
92         assertTrue("Contains", REF_TREE.contains(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
93                 REF_YII_IID)));
94         assertEquals("Not contains", false, REF_TREE.contains(TEST_DIFF_TREE));
95     }
96
97     @Test
98     public void toStringTest() {
99         assertTrue("ToString", REF_TREE.toString().contains(REF_TREE.getRootIdentifier().toString()) && REF_TREE
100                 .toString().contains(REF_TREE.getDatastoreType().toString()));
101     }
102 }