Enforce InstanceIdentifier creation
[mdsal.git] / entityownership / mdsal-eos-binding-api / src / test / java / org / opendaylight / mdsal / eos / binding / api / EntityTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.eos.binding.api;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import org.apache.commons.lang3.SerializationUtils;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.binding.ChildOf;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.DataRoot;
19 import org.opendaylight.yangtools.yang.binding.Identifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21
22 /**
23  * Unit tests for Entity.
24  *
25  * @author Thomas Pantelis
26  */
27 public class EntityTest {
28
29     static final String ENTITY_TYPE1 = "type1";
30     static final String ENTITY_TYPE2 = "type2";
31     static final InstanceIdentifier<TestDataObject1> ID1 = InstanceIdentifier.create(TestDataObject1.class);
32     static final InstanceIdentifier<TestDataObject2> ID2 = InstanceIdentifier.create(TestDataObject2.class);
33
34     @Test
35     public void testHashCode() {
36         Entity entity1 = new Entity(ENTITY_TYPE1, ID1);
37
38         assertEquals("hashCode", entity1.hashCode(), new Entity(ENTITY_TYPE1, ID1).hashCode());
39         assertNotEquals("hashCode", entity1.hashCode(), new Entity(ENTITY_TYPE2, ID2).hashCode());
40     }
41
42     @Test
43     public void testEquals() {
44         Entity entity1 = new Entity(ENTITY_TYPE1, ID1);
45
46         assertEquals("Same", true, entity1.equals(entity1));
47         assertEquals("Same", true, entity1.equals(new Entity(ENTITY_TYPE1, ID1)));
48         assertEquals("Different entity type", false, entity1.equals(new Entity(ENTITY_TYPE2, ID1)));
49         assertEquals("Different entity ID", false, entity1.equals(new Entity(ENTITY_TYPE1, ID2)));
50         assertEquals("Different Object", false, entity1.equals(new Object()));
51         assertEquals("Equals null", false, entity1.equals(null));
52     }
53
54     @Test
55     public void testSerialization() {
56         Entity entity = new Entity(ENTITY_TYPE1, ID1);
57
58         Entity clone = SerializationUtils.clone(entity);
59
60         assertEquals("getType", entity.getType(), clone.getType());
61         assertEquals("getId", entity.getIdentifier(), clone.getIdentifier());
62     }
63
64     @Test
65     public void testEntityNameConstructor() {
66         Entity entity = new Entity(ENTITY_TYPE1, "foo");
67
68         Identifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
69             .mdsal.core.general.entity.rev150930.Entity> keyID = entity.getIdentifier().firstKeyOf(
70                 org.opendaylight.yang.gen.v1.urn
71                     .opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity.class);
72         assertNotNull("List key not found", keyID);
73     }
74
75     interface TestDataObject1 extends ChildOf<DataRoot> {
76         @Override
77         default Class<? extends DataObject> implementedInterface() {
78             return TestDataObject1.class;
79         }
80     }
81
82     interface TestDataObject2 extends ChildOf<DataRoot> {
83         @Override
84         default Class<? extends DataObject> implementedInterface() {
85             return TestDataObject2.class;
86         }
87     }
88 }