0863db37aeead4d153af4b83fb1de42ed30d734d
[openflowjava.git] / openflow-protocol-api / src / test / java / org / opendaylight / openflowjava / protocol / api / keys / MatchEntrySerializerKeyTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.api.keys;
10
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPhyPort;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Nxm0Class;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
18
19 /**
20  * @author michal.polkorab
21  *
22  */
23 public class MatchEntrySerializerKeyTest {
24
25     /**
26      * Test MatchEntrySerializerKey equals and hashCode
27      */
28     @Test
29     public void test() {
30         MatchEntrySerializerKey<?, ?> key1 = new MatchEntrySerializerKey<>
31                 (EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, InPort.class);
32         MatchEntrySerializerKey<?, ?> key2 = new MatchEntrySerializerKey<>
33                 (EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, InPort.class);
34         Assert.assertTrue("Wrong equals", key1.equals(key2));
35         Assert.assertTrue("Wrong hashCode", key1.hashCode() == key2.hashCode());
36         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
37                 OpenflowBasicClass.class, InPhyPort.class);
38         Assert.assertFalse("Wrong equals", key1.equals(key2));
39         Assert.assertFalse("Wrong hashCode", key1.hashCode() == key2.hashCode());
40         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
41                 Nxm0Class.class, InPort.class);
42         Assert.assertFalse("Wrong equals", key1.equals(key2));
43         Assert.assertFalse("Wrong hashCode", key1.hashCode() == key2.hashCode());
44         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF10_VERSION_ID,
45                 OpenflowBasicClass.class, InPhyPort.class);
46         Assert.assertFalse("Wrong equals", key1.equals(key2));
47         Assert.assertFalse("Wrong hashCode", key1.hashCode() == key2.hashCode());
48         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
49                 OpenflowBasicClass.class, null);
50         Assert.assertFalse("Wrong equals", key1.equals(key2));
51         Assert.assertFalse("Wrong hashCode", key1.hashCode() == key2.hashCode());
52         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
53                 null, InPhyPort.class);
54         Assert.assertFalse("Wrong equals", key1.equals(key2));
55         Assert.assertFalse("Wrong hashCode", key1.hashCode() == key2.hashCode());
56         key2.setExperimenterId(42L);
57         Assert.assertFalse("Wrong hashCode", key1.hashCode() == key2.hashCode());
58     }
59     
60     /**
61      * Test MatchEntrySerializerKey equals - additional test 
62      */
63     @Test
64     public void testEquals(){
65         MatchEntrySerializerKey<?, ?> key1;
66         MatchEntrySerializerKey<?, ?> key2;
67         key1 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, InPort.class);
68         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, InPort.class);
69         Assert.assertTrue("Wrong equal to identical object.", key1.equals(key1));
70         Assert.assertFalse("Wrong equal to different class.", key1.equals(new Object()));
71         
72         Long expId1 = 987654331L;
73         Long expId2 = 123456789L;
74         
75         key1.setExperimenterId(null);
76         key2.setExperimenterId(expId2);
77         Assert.assertFalse("Wrong equal by experimenterId", key1.equals(key2));
78         key1.setExperimenterId(expId1);
79         Assert.assertFalse("Wrong equal by experimenterId", key1.equals(key2));
80         key1 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, null, InPort.class);
81         key1.setExperimenterId(expId2);
82         Assert.assertFalse("Wrong equal by oxmClass", key1.equals(key2));
83         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, null, InPort.class);
84         key2.setExperimenterId(expId2);
85         Assert.assertTrue("Wrong equal by oxmClass", key1.equals(key2));
86         key1 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, null);
87         key1.setExperimenterId(expId2);
88         Assert.assertFalse("Wrong equal by oxmField", key1.equals(key2));
89         key2 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, null);
90         key2.setExperimenterId(expId2);
91         Assert.assertTrue("Wrong equal by oxmField", key1.equals(key2));
92     }
93
94     /**
95      * Test MatchEntrySerializerKey toString()
96      */
97     @Test
98     public void testToString(){
99         MatchEntrySerializerKey<?, ?> key1;
100         key1 = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID, OpenflowBasicClass.class, InPort.class);
101
102         Assert.assertEquals("Wrong toString()", "msgVersion: 4 objectType: org.opendaylight.yang.gen.v1.urn.opendaylight"
103                 + ".openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries oxm_class: org.opendaylight.yang.gen.v1.urn"
104                 + ".opendaylight.openflow.oxm.rev130731.OpenflowBasicClass oxm_field: org.opendaylight.yang.gen.v1.urn"
105                 + ".opendaylight.openflow.oxm.rev130731.InPort experimenterID: null", key1.toString());
106     }
107 }