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