2 * Copyright (c) 2016 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.mdsal.dom.api;
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;
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;
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);
43 void constructTest() {
44 assertNotNull(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID), "Instantiation");
49 assertEquals(REF_TREE.hashCode(),
50 DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID).hashCode());
51 assertNotEquals(REF_TREE.hashCode(), TEST_DIFF_TREE.hashCode());
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);
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));
68 assertEquals(0, REF_TREE.compareTo(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID)));
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);
82 assertTrue(REF_TREE.contains(DOMDataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, REF_YII_IID)));
83 assertEquals(false, REF_TREE.contains(TEST_DIFF_TREE));
88 assertEquals("DOMDataTreeIdentifier{datastore=OPERATIONAL, "
89 + "root=/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store)ref-lists}",
94 void serializationTest() throws Exception {
95 final var bos = new ByteArrayOutputStream();
96 try (var oos = new ObjectOutputStream(bos)) {
97 oos.writeObject(REF_TREE);
100 final var bytes = bos.toByteArray();
101 assertEquals(275, bytes.length);
103 try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
104 assertEquals(REF_TREE, ois.readObject());