Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 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; |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 19 | import java.util.Optional; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 20 | |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | |
| 25 | import com.google.common.testing.EqualsTester; |
| 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.equalTo; |
| 29 | import static org.hamcrest.Matchers.notNullValue; |
| 30 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import static org.onosproject.net.NetTestTools.connectPoint; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Unit tests for the DefaultInboundPacket class. |
| 35 | */ |
| 36 | public class DefaultInboundPacketTest { |
| 37 | |
| 38 | final Ethernet eth = new Ethernet() |
| 39 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 40 | .setSourceMACAddress(MacAddress.BROADCAST); |
| 41 | final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize()); |
| 42 | final DefaultInboundPacket packet1 = |
| 43 | new DefaultInboundPacket(connectPoint("d1", 1), |
| 44 | eth, |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 45 | byteBuffer, |
| 46 | Optional.of(1L)); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 47 | final DefaultInboundPacket sameAsPacket1 = |
| 48 | new DefaultInboundPacket(connectPoint("d1", 1), |
| 49 | eth, |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 50 | byteBuffer, |
| 51 | Optional.of(1L)); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 52 | final DefaultInboundPacket packet2 = |
| 53 | new DefaultInboundPacket(connectPoint("d2", 1), |
| 54 | eth, |
| 55 | byteBuffer); |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 56 | final DefaultInboundPacket sameAsPacket2 = |
| 57 | new DefaultInboundPacket(connectPoint("d2", 1), |
| 58 | eth, |
| 59 | byteBuffer, |
| 60 | Optional.empty()); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Checks that the DefaultInboundPacket class is immutable. |
| 63 | */ |
| 64 | @Test |
| 65 | public void testImmutability() { |
| 66 | assertThatClassIsImmutable(DefaultInboundPacket.class); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests the equals(), hashCode() and toString() methods. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testEquals() { |
| 74 | new EqualsTester() |
| 75 | .addEqualityGroup(packet1, sameAsPacket1) |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 76 | .addEqualityGroup(packet2, sameAsPacket2) |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 77 | .testEquals(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests the object creation through the constructor. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testConstruction() { |
| 85 | assertThat(packet1.receivedFrom(), equalTo(connectPoint("d1", 1))); |
| 86 | assertThat(packet1.parsed(), equalTo(eth)); |
| 87 | assertThat(packet1.unparsed(), notNullValue()); |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 88 | assertThat(packet1.cookie(), equalTo(Optional.of(1L))); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 89 | } |
| 90 | } |