Migrate users of deprecated yang.common methods
[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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotEquals;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import org.junit.jupiter.api.Test;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24
25 class DOMDataTreeIdentifierTest {
26     private static final String REF_LISTS = "ref-lists";
27     private static final String TEST_LISTS = "test-lists";
28     private static final String COMPARE_FIRST_LISTS = "A-test-lists";
29     private static final String COMPARE_SECOND_LISTS = "B-test-lists";
30     private static final QNameModule TEST_MODULE =
31         QNameModule.of("urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store");
32     private static final YangInstanceIdentifier REF_YII_IID =
33         YangInstanceIdentifier.of(QName.create(TEST_MODULE, REF_LISTS));
34     private static final YangInstanceIdentifier TEST_YII_IID =
35         YangInstanceIdentifier.of(QName.create(TEST_MODULE, TEST_LISTS));
36     private static final DOMDataTreeIdentifier REF_TREE =
37         DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID);
38     private static final DOMDataTreeIdentifier TEST_DIFF_TREE =
39         DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,TEST_YII_IID);
40
41     @Test
42     void constructTest() {
43         assertNotNull(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID), "Instantiation");
44     }
45
46     @Test
47     void hashCodeTest() {
48         assertEquals(REF_TREE.hashCode(),
49             DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID).hashCode());
50         assertNotEquals(REF_TREE.hashCode(), TEST_DIFF_TREE.hashCode());
51     }
52
53     @Test
54     void equalsTest() {
55         assertEquals(REF_TREE, DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID));
56         assertNotEquals(REF_TREE, DOMDataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, REF_YII_IID));
57         assertEquals(REF_TREE, REF_TREE);
58         assertNotEquals(REF_TREE, new Object());
59         assertNotEquals(REF_TREE, TEST_DIFF_TREE);
60     }
61
62     @Test
63     void compareToTest() {
64         final var compareFirstIid = YangInstanceIdentifier.of(QName.create(TEST_MODULE, COMPARE_FIRST_LISTS));
65         final var compareSecondIid = YangInstanceIdentifier.of(QName.create(TEST_MODULE, COMPARE_SECOND_LISTS));
66
67         assertEquals(0, REF_TREE.compareTo(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID)));
68         assertNotEquals(0,
69             REF_TREE.compareTo(DOMDataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, REF_YII_IID)));
70         assertEquals(1, DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,
71             YangInstanceIdentifier.of(QName.create(TEST_MODULE, REF_LISTS), QName.create(TEST_MODULE, TEST_LISTS)))
72                 .compareTo(REF_TREE));
73         assertTrue(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, compareFirstIid)
74             .compareTo(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, compareSecondIid)) < 0);
75         assertTrue(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, compareSecondIid)
76             .compareTo(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, compareFirstIid)) > 0);
77     }
78
79     @Test
80     void containsTest() {
81         assertTrue(REF_TREE.contains(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID)));
82         assertEquals(false, REF_TREE.contains(TEST_DIFF_TREE));
83     }
84
85     @Test
86     void toStringTest() {
87         assertEquals("DOMDataTreeIdentifier{datastore=OPERATIONAL, "
88             + "root=/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store)ref-lists}",
89             REF_TREE.toString());
90     }
91
92     @Test
93     void serializationTest() throws Exception {
94         final var bos = new ByteArrayOutputStream();
95         try (var oos = new ObjectOutputStream(bos)) {
96             oos.writeObject(REF_TREE);
97         }
98
99         final var bytes = bos.toByteArray();
100         assertEquals(275, bytes.length);
101
102         try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
103             assertEquals(REF_TREE, ois.readObject());
104         }
105     }
106 }