Bug 4105: Implement RegisterCandidate in EntityOwnershipShard
[controller.git] / opendaylight / md-sal / sal-common-api / src / test / java / org / opendaylight / controller / md / sal / common / 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.controller.md.sal.common.api.clustering;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import org.apache.commons.lang3.SerializationUtils;
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16
17 /**
18  * Unit tests for Entity.
19  *
20  * @author Thomas Pantelis
21  */
22 public class EntityTest {
23     static String ENTITY_TYPE1 = "type1";
24     static String ENTITY_TYPE2 = "type2";
25     static final QName QNAME1 = QName.create("test", "2015-08-14", "1");
26     static final QName QNAME2 = QName.create("test", "2015-08-14", "2");
27     static final YangInstanceIdentifier YANGID1 = YangInstanceIdentifier.of(QNAME1);
28     static final YangInstanceIdentifier YANGID2 = YangInstanceIdentifier.of(QNAME2);
29
30     @Test
31     public void testHashCode() {
32         Entity entity1 = new Entity(ENTITY_TYPE1, YANGID1);
33
34         assertEquals("hashCode", entity1.hashCode(), new Entity(ENTITY_TYPE1, YANGID1).hashCode());
35         assertNotEquals("hashCode", entity1.hashCode(), new Entity(ENTITY_TYPE2, YANGID2).hashCode());
36     }
37
38     @Test
39     public void testEquals() {
40         Entity entity1 = new Entity(ENTITY_TYPE1, YANGID1);
41
42         assertEquals("Same", true, entity1.equals(entity1));
43         assertEquals("Same", true, entity1.equals(new Entity(ENTITY_TYPE1, YANGID1)));
44         assertEquals("Different entity type", false, entity1.equals(new Entity(ENTITY_TYPE2, YANGID1)));
45         assertEquals("Different yang ID", false, entity1.equals(new Entity(ENTITY_TYPE1, YANGID2)));
46         assertEquals("Different Object", false, entity1.equals(new Object()));
47         assertEquals("Equals null", false, entity1.equals(null));
48     }
49
50     @Test
51     public void testSerialization() {
52         Entity entity = new Entity(ENTITY_TYPE1, YANGID1);
53
54         Entity clone = SerializationUtils.clone(entity);
55
56         assertEquals("getType", entity.getType(), clone.getType());
57         assertEquals("getId", entity.getId(), clone.getId());
58     }
59 }