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