f879b96d902361b6a4c87e4d67981525c4c58f9c
[mdsal.git] / binding / mdsal-binding-api / src / test / java / org / opendaylight / mdsal / binding / api / DataTreeIdentifierTest.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.binding.api;
9
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;
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.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;
25
26 class DataTreeIdentifierTest {
27     private static final DataTreeIdentifier<TestDataObject1> TEST_IDENTIFIER1 =
28         DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject1.class));
29     private static final DataTreeIdentifier<TestDataObject2> TEST_IDENTIFIER2 =
30         DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject2.class));
31
32     @Test
33     void basicTest() throws Exception {
34         assertEquals(LogicalDatastoreType.OPERATIONAL, TEST_IDENTIFIER1.datastore());
35         assertEquals(InstanceIdentifier.create(TestDataObject1.class), TEST_IDENTIFIER1.path());
36     }
37
38     @Test
39     void containsTest() {
40         assertTrue(TEST_IDENTIFIER1.contains(
41             DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject1.class))));
42         assertFalse(TEST_IDENTIFIER1.contains(TEST_IDENTIFIER2));
43     }
44
45     @Test
46     void hashCodeTest() {
47         assertEquals(TEST_IDENTIFIER1.hashCode(),
48             DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(TestDataObject1.class))
49                 .hashCode());
50         assertNotEquals(TEST_IDENTIFIER1.hashCode(), TEST_IDENTIFIER2.hashCode());
51     }
52
53     @Test
54     void equalsTest() {
55         assertEquals(TEST_IDENTIFIER1, TEST_IDENTIFIER1);
56         assertEquals(TEST_IDENTIFIER1,
57             DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,InstanceIdentifier.create(TestDataObject1.class)));
58         assertNotEquals(TEST_IDENTIFIER1,
59             DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION,
60                 InstanceIdentifier.create(TestDataObject1.class)));
61         assertNotEquals(TEST_IDENTIFIER1, TEST_IDENTIFIER2);
62         assertNotEquals(TEST_IDENTIFIER1, null);
63         assertNotEquals(TEST_IDENTIFIER1, new Object());
64     }
65
66     @Test
67     void serializationTest() throws Exception {
68         final var bos = new ByteArrayOutputStream();
69         try (var oos = new ObjectOutputStream(bos)) {
70             oos.writeObject(TEST_IDENTIFIER1);
71         }
72
73         final var bytes = bos.toByteArray();
74         assertEquals(450, bytes.length);
75
76         try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
77             assertEquals(TEST_IDENTIFIER1, ois.readObject());
78         }
79     }
80
81     private interface TestDataObject1 extends ChildOf<DataRoot> {
82         @Override
83         default Class<? extends DataObject> implementedInterface() {
84             return TestDataObject1.class;
85         }
86     }
87
88     private interface TestDataObject2 extends ChildOf<DataRoot> {
89         @Override
90         default Class<? extends DataObject> implementedInterface() {
91             return TestDataObject2.class;
92         }
93     }
94 }