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; |
| 19 | |
| 20 | import org.junit.Test; |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.TrafficTreatment; |
| 23 | import org.onosproject.net.intent.IntentTestsMocks; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | |
| 27 | import com.google.common.testing.EqualsTester; |
| 28 | |
| 29 | import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | import static org.hamcrest.Matchers.equalTo; |
| 31 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Unit tests for the DefaultOutboundPacketTest class. |
| 36 | */ |
| 37 | public class DefaultOutboundPacketTest { |
| 38 | final Ethernet eth = new Ethernet() |
| 39 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 40 | .setSourceMACAddress(MacAddress.BROADCAST); |
| 41 | final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize()); |
| 42 | final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 43 | final DefaultOutboundPacket packet1 = |
| 44 | new DefaultOutboundPacket(did("d1"), |
| 45 | treatment, |
| 46 | byteBuffer); |
| 47 | final DefaultOutboundPacket sameAsPacket1 = |
| 48 | new DefaultOutboundPacket(did("d1"), |
| 49 | treatment, |
| 50 | byteBuffer); |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 51 | private final PortNumber inPort = PortNumber.portNumber(1); |
| 52 | final DefaultOutboundPacket packet1WithInPort = |
| 53 | new DefaultOutboundPacket(did("d1"), |
| 54 | treatment, |
| 55 | byteBuffer, |
| 56 | inPort); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 57 | final DefaultOutboundPacket packet2 = |
| 58 | new DefaultOutboundPacket(did("d2"), |
| 59 | treatment, |
| 60 | byteBuffer); |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 61 | |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Checks that the DefaultOutboundPacket class is immutable. |
| 64 | */ |
| 65 | @Test |
| 66 | public void testImmutability() { |
| 67 | assertThatClassIsImmutable(DefaultOutboundPacket.class); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests the equals(), hashCode() and toString() methods. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testEquals() { |
| 75 | new EqualsTester() |
| 76 | .addEqualityGroup(packet1, sameAsPacket1) |
| 77 | .addEqualityGroup(packet2) |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 78 | .addEqualityGroup(packet1WithInPort) |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 79 | .testEquals(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests the object creation through the constructor. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testConstruction() { |
| 87 | assertThat(packet1.sendThrough(), equalTo(did("d1"))); |
| 88 | assertThat(packet1.data(), equalTo(byteBuffer)); |
| 89 | assertThat(packet1.treatment(), equalTo(treatment)); |
Thiago Santos | a4a17cb | 2019-01-30 14:23:58 -0800 | [diff] [blame] | 90 | assertThat(packet1.inPort(), equalTo(null)); |
| 91 | assertThat(packet1WithInPort.inPort(), equalTo(inPort)); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 92 | } |
| 93 | } |