2 * Copyright (c) 2015 Brocade Communications 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.eos.binding.api;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
14 import org.apache.commons.lang3.SerializationUtils;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.Identifier;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 * Unit tests for Entity.
23 * @author Thomas Pantelis
25 public class EntityTest {
27 static String ENTITY_TYPE1 = "type1";
28 static String ENTITY_TYPE2 = "type2";
29 static final InstanceIdentifier<TestDataObject1> ID1 = InstanceIdentifier.create(TestDataObject1.class);
30 static final InstanceIdentifier<TestDataObject2> ID2 = InstanceIdentifier.create(TestDataObject2.class);
33 public void testHashCode() {
34 Entity entity1 = new Entity(ENTITY_TYPE1, ID1);
36 assertEquals("hashCode", entity1.hashCode(), new Entity(ENTITY_TYPE1, ID1).hashCode());
37 assertNotEquals("hashCode", entity1.hashCode(), new Entity(ENTITY_TYPE2, ID2).hashCode());
41 public void testEquals() {
42 Entity entity1 = new Entity(ENTITY_TYPE1, ID1);
44 assertEquals("Same", true, entity1.equals(entity1));
45 assertEquals("Same", true, entity1.equals(new Entity(ENTITY_TYPE1, ID1)));
46 assertEquals("Different entity type", false, entity1.equals(new Entity(ENTITY_TYPE2, ID1)));
47 assertEquals("Different entity ID", false, entity1.equals(new Entity(ENTITY_TYPE1, ID2)));
48 assertEquals("Different Object", false, entity1.equals(new Object()));
49 assertEquals("Equals null", false, entity1.equals(null));
53 public void testSerialization() {
54 Entity entity = new Entity(ENTITY_TYPE1, ID1);
56 Entity clone = SerializationUtils.clone(entity);
58 assertEquals("getType", entity.getType(), clone.getType());
59 assertEquals("getId", entity.getIdentifier(), clone.getIdentifier());
63 public void testEntityNameConstructor() {
64 Entity entity = new Entity(ENTITY_TYPE1, "foo");
66 Identifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
67 .mdsal.core.general.entity.rev150930.Entity> keyID = entity.getIdentifier().firstKeyOf(
68 org.opendaylight.yang.gen.v1.urn
69 .opendaylight.params.xml.ns.yang.mdsal.core.general.entity.rev150930.Entity.class);
70 assertNotNull("List key not found", keyID);
73 static class TestDataObject1 implements DataObject {
76 public Class<? extends DataObject> getImplementedInterface() {
77 return DataObject.class;
81 public Class<? extends DataObject> implementedInterface() {
82 return DataObject.class;
86 static class TestDataObject2 implements DataObject {
89 public Class<? extends DataObject> getImplementedInterface() {
90 return DataObject.class;
94 public Class<? extends DataObject> implementedInterface() {
95 return DataObject.class;