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.binding.api;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotEquals;
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.binding.ChildOf;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.DataRoot;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 class DataTreeIdentifierTest {
27 private static final DataTreeIdentifier<TestDataObject1> TEST_IDENTIFIER1 = DataTreeIdentifier.create(
28 LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject1.class));
29 private static final DataTreeIdentifier<TestDataObject2> TEST_IDENTIFIER2 = DataTreeIdentifier.create(
30 LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject2.class));
33 void basicTest() throws Exception {
34 assertEquals(LogicalDatastoreType.OPERATIONAL, TEST_IDENTIFIER1.getDatastoreType());
35 assertEquals(InstanceIdentifier.create(TestDataObject1.class), TEST_IDENTIFIER1.getRootIdentifier());
40 assertTrue(TEST_IDENTIFIER1.contains(DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
41 InstanceIdentifier.create(TestDataObject1.class))), "Contains");
42 assertFalse(TEST_IDENTIFIER1.contains(TEST_IDENTIFIER2), "Not Contains");
47 assertEquals(TEST_IDENTIFIER1.hashCode(),
48 DataTreeIdentifier.create(
49 LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject1.class)).hashCode());
50 assertNotEquals(TEST_IDENTIFIER1.hashCode(), TEST_IDENTIFIER2.hashCode());
55 assertEquals(TEST_IDENTIFIER1, TEST_IDENTIFIER1, "Equals");
56 assertEquals(TEST_IDENTIFIER1, DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
57 InstanceIdentifier.create(TestDataObject1.class)), "Equals");
58 assertNotEquals(TEST_IDENTIFIER1, DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
59 InstanceIdentifier.create(TestDataObject1.class)), "Different");
60 assertNotEquals(TEST_IDENTIFIER1, TEST_IDENTIFIER2, "Different");
61 assertNotEquals(TEST_IDENTIFIER1, null, "Equals null");
62 assertNotEquals(TEST_IDENTIFIER1, new Object(), "Different object");
66 void serializationTest() throws Exception {
67 final var bos = new ByteArrayOutputStream();
68 try (var oos = new ObjectOutputStream(bos)) {
69 oos.writeObject(TEST_IDENTIFIER1);
72 final var bytes = bos.toByteArray();
73 assertEquals(450, bytes.length);
75 try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
76 assertEquals(TEST_IDENTIFIER1, ois.readObject());
80 private interface TestDataObject1 extends ChildOf<DataRoot> {
82 default Class<? extends DataObject> implementedInterface() {
83 return TestDataObject1.class;
87 private interface TestDataObject2 extends ChildOf<DataRoot> {
89 default Class<? extends DataObject> implementedInterface() {
90 return TestDataObject2.class;