Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.packet; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.NetTestTools.connectPoint; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 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 | } |