Ray Milkey

Packet class unit tests

Added unit tests for DefaultInboundPacket, DefaultOutboundPacket,
and DefaultPacketContext.

Made DefaultInboundPacket and DefaultOutboundPacket proper immutable classes

Added equals() and hashCode() to DefaultOutboundPacket

Change-Id: Ibb1199a5282743ccaf0844c0b7ccfcc4cb13f52e
...@@ -26,7 +26,7 @@ import static com.google.common.base.MoreObjects.toStringHelper; ...@@ -26,7 +26,7 @@ import static com.google.common.base.MoreObjects.toStringHelper;
26 /** 26 /**
27 * Default implementation of an immutable inbound packet. 27 * Default implementation of an immutable inbound packet.
28 */ 28 */
29 -public class DefaultInboundPacket implements InboundPacket { 29 +public final class DefaultInboundPacket implements InboundPacket {
30 30
31 private final ConnectPoint receivedFrom; 31 private final ConnectPoint receivedFrom;
32 private final Ethernet parsed; 32 private final Ethernet parsed;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onlab.onos.net.packet; 16 package org.onlab.onos.net.packet;
17 17
18 import java.nio.ByteBuffer; 18 import java.nio.ByteBuffer;
19 +import java.util.Objects;
19 20
20 import org.onlab.onos.net.DeviceId; 21 import org.onlab.onos.net.DeviceId;
21 import org.onlab.onos.net.flow.TrafficTreatment; 22 import org.onlab.onos.net.flow.TrafficTreatment;
...@@ -25,7 +26,7 @@ import com.google.common.base.MoreObjects; ...@@ -25,7 +26,7 @@ import com.google.common.base.MoreObjects;
25 /** 26 /**
26 * Default implementation of an immutable outbound packet. 27 * Default implementation of an immutable outbound packet.
27 */ 28 */
28 -public class DefaultOutboundPacket implements OutboundPacket { 29 +public final class DefaultOutboundPacket implements OutboundPacket {
29 private final DeviceId sendThrough; 30 private final DeviceId sendThrough;
30 private final TrafficTreatment treatment; 31 private final TrafficTreatment treatment;
31 private final ByteBuffer data; 32 private final ByteBuffer data;
...@@ -61,6 +62,24 @@ public class DefaultOutboundPacket implements OutboundPacket { ...@@ -61,6 +62,24 @@ public class DefaultOutboundPacket implements OutboundPacket {
61 } 62 }
62 63
63 @Override 64 @Override
65 + public int hashCode() {
66 + return Objects.hash(sendThrough, treatment, data);
67 + }
68 +
69 + @Override
70 + public boolean equals(Object obj) {
71 + if (this == obj) {
72 + return true;
73 + }
74 + if (obj instanceof OutboundPacket) {
75 + final DefaultOutboundPacket other = (DefaultOutboundPacket) obj;
76 + return Objects.equals(this.sendThrough, other.sendThrough) &&
77 + Objects.equals(this.treatment, other.treatment) &&
78 + Objects.equals(this.data, other.data);
79 + }
80 + return false;
81 + }
82 + @Override
64 public String toString() { 83 public String toString() {
65 return MoreObjects.toStringHelper(this) 84 return MoreObjects.toStringHelper(this)
66 .add("sendThrough", sendThrough) 85 .add("sendThrough", sendThrough)
......
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onlab.onos.net.packet;
17 +
18 +import java.nio.ByteBuffer;
19 +
20 +import org.junit.Test;
21 +import org.onlab.packet.Ethernet;
22 +import org.onlab.packet.MacAddress;
23 +
24 +import com.google.common.testing.EqualsTester;
25 +
26 +import static org.hamcrest.MatcherAssert.assertThat;
27 +import static org.hamcrest.Matchers.equalTo;
28 +import static org.hamcrest.Matchers.notNullValue;
29 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30 +import static org.onlab.onos.net.NetTestTools.connectPoint;
31 +
32 +/**
33 + * Unit tests for the DefaultInboundPacket class.
34 + */
35 +public class DefaultInboundPacketTest {
36 +
37 + final Ethernet eth = new Ethernet()
38 + .setDestinationMACAddress(MacAddress.BROADCAST)
39 + .setSourceMACAddress(MacAddress.BROADCAST);
40 + final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize());
41 + final DefaultInboundPacket packet1 =
42 + new DefaultInboundPacket(connectPoint("d1", 1),
43 + eth,
44 + byteBuffer);
45 + final DefaultInboundPacket sameAsPacket1 =
46 + new DefaultInboundPacket(connectPoint("d1", 1),
47 + eth,
48 + byteBuffer);
49 + final DefaultInboundPacket packet2 =
50 + new DefaultInboundPacket(connectPoint("d2", 1),
51 + eth,
52 + byteBuffer);
53 + /**
54 + * Checks that the DefaultInboundPacket class is immutable.
55 + */
56 + @Test
57 + public void testImmutability() {
58 + assertThatClassIsImmutable(DefaultInboundPacket.class);
59 + }
60 +
61 + /**
62 + * Tests the equals(), hashCode() and toString() methods.
63 + */
64 + @Test
65 + public void testEquals() {
66 + new EqualsTester()
67 + .addEqualityGroup(packet1, sameAsPacket1)
68 + .addEqualityGroup(packet2)
69 + .testEquals();
70 + }
71 +
72 + /**
73 + * Tests the object creation through the constructor.
74 + */
75 + @Test
76 + public void testConstruction() {
77 + assertThat(packet1.receivedFrom(), equalTo(connectPoint("d1", 1)));
78 + assertThat(packet1.parsed(), equalTo(eth));
79 + assertThat(packet1.unparsed(), notNullValue());
80 + }
81 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onlab.onos.net.packet;
17 +
18 +import java.nio.ByteBuffer;
19 +
20 +import org.junit.Test;
21 +import org.onlab.onos.net.flow.TrafficTreatment;
22 +import org.onlab.onos.net.intent.IntentTestsMocks;
23 +import org.onlab.packet.Ethernet;
24 +import org.onlab.packet.MacAddress;
25 +
26 +import com.google.common.testing.EqualsTester;
27 +
28 +import static org.hamcrest.MatcherAssert.assertThat;
29 +import static org.hamcrest.Matchers.equalTo;
30 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
31 +import static org.onlab.onos.net.NetTestTools.did;
32 +
33 +/**
34 + * Unit tests for the DefaultOutboundPacketTest class.
35 + */
36 +public class DefaultOutboundPacketTest {
37 + final Ethernet eth = new Ethernet()
38 + .setDestinationMACAddress(MacAddress.BROADCAST)
39 + .setSourceMACAddress(MacAddress.BROADCAST);
40 + final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize());
41 + final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
42 + final DefaultOutboundPacket packet1 =
43 + new DefaultOutboundPacket(did("d1"),
44 + treatment,
45 + byteBuffer);
46 + final DefaultOutboundPacket sameAsPacket1 =
47 + new DefaultOutboundPacket(did("d1"),
48 + treatment,
49 + byteBuffer);
50 + final DefaultOutboundPacket packet2 =
51 + new DefaultOutboundPacket(did("d2"),
52 + treatment,
53 + byteBuffer);
54 + /**
55 + * Checks that the DefaultOutboundPacket class is immutable.
56 + */
57 + @Test
58 + public void testImmutability() {
59 + assertThatClassIsImmutable(DefaultOutboundPacket.class);
60 + }
61 +
62 + /**
63 + * Tests the equals(), hashCode() and toString() methods.
64 + */
65 + @Test
66 + public void testEquals() {
67 + new EqualsTester()
68 + .addEqualityGroup(packet1, sameAsPacket1)
69 + .addEqualityGroup(packet2)
70 + .testEquals();
71 + }
72 +
73 + /**
74 + * Tests the object creation through the constructor.
75 + */
76 + @Test
77 + public void testConstruction() {
78 + assertThat(packet1.sendThrough(), equalTo(did("d1")));
79 + assertThat(packet1.data(), equalTo(byteBuffer));
80 + assertThat(packet1.treatment(), equalTo(treatment));
81 + }
82 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onlab.onos.net.packet;
17 +
18 +import java.nio.ByteBuffer;
19 +
20 +import org.junit.Test;
21 +import org.onlab.onos.net.flow.TrafficTreatment;
22 +import org.onlab.onos.net.intent.IntentTestsMocks;
23 +import org.onlab.packet.Ethernet;
24 +import org.onlab.packet.MacAddress;
25 +
26 +import com.google.common.testing.EqualsTester;
27 +
28 +import static org.hamcrest.MatcherAssert.assertThat;
29 +import static org.hamcrest.Matchers.is;
30 +import static org.hamcrest.Matchers.notNullValue;
31 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
32 +import static org.onlab.onos.net.NetTestTools.connectPoint;
33 +import static org.onlab.onos.net.NetTestTools.did;
34 +
35 +/**
36 + * Unit tests for the DefaultPacketContextTest.
37 + */
38 +public class DefaultPacketContextTest {
39 + final Ethernet eth = new Ethernet()
40 + .setDestinationMACAddress(MacAddress.BROADCAST)
41 + .setSourceMACAddress(MacAddress.BROADCAST);
42 + final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize());
43 + final DefaultInboundPacket inPacket =
44 + new DefaultInboundPacket(connectPoint("d1", 1),
45 + eth,
46 + byteBuffer);
47 + final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
48 + final DefaultOutboundPacket outPacket =
49 + new DefaultOutboundPacket(did("d1"),
50 + treatment,
51 + byteBuffer);
52 +
53 + static class MockPacketContext extends DefaultPacketContext {
54 +
55 + protected MockPacketContext(long time, InboundPacket inPkt,
56 + OutboundPacket outPkt, boolean block) {
57 + super(time, inPkt, outPkt, block);
58 + }
59 +
60 + @Override
61 + public void send() {
62 +
63 + }
64 +
65 + @Override
66 + public boolean block() {
67 + return super.block();
68 + }
69 + }
70 +
71 + final DefaultPacketContext context1 =
72 + new MockPacketContext(123L, inPacket, outPacket, true);
73 + final DefaultPacketContext sameAsContext1 =
74 + new MockPacketContext(123L, inPacket, outPacket, true);
75 + final DefaultPacketContext context2 =
76 + new MockPacketContext(123123L, inPacket, outPacket, true);
77 +
78 + /**
79 + * Checks that the DefaultOutboundPacket class is immutable but can be
80 + * used as a base class.
81 + */
82 + @Test
83 + public void testImmutability() {
84 + assertThatClassIsImmutableBaseClass(DefaultPacketContext.class);
85 + }
86 +
87 + /**
88 + * Tests the equals(), hashCode() and toString() methods.
89 + */
90 + @Test
91 + public void testEquals() {
92 + // No hashCode() or equals() defined, object comparison is used.
93 + new EqualsTester()
94 + .addEqualityGroup(context1)
95 + .addEqualityGroup(sameAsContext1)
96 + .addEqualityGroup(context2)
97 + .testEquals();
98 + }
99 +
100 + /**
101 + * Tests that objects are created properly.
102 + */
103 + @Test
104 + public void testConstruction() {
105 + assertThat(context1.block(), is(true));
106 + assertThat(context1.inPacket(), is(inPacket));
107 + assertThat(context1.isHandled(), is(true));
108 + assertThat(context1.outPacket(), is(outPacket));
109 + assertThat(context1.time(), is(123L));
110 + assertThat(context1.treatmentBuilder(), is(notNullValue()));
111 + }
112 +}