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 | |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 19 | import org.junit.Test; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [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 | |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 25 | import java.nio.ByteBuffer; |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.hamcrest.Matchers.notNullValue; |
| 30 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 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 DefaultPacketContextTest. |
| 36 | */ |
| 37 | public class DefaultPacketContextTest { |
| 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 inPacket = |
| 43 | new DefaultInboundPacket(connectPoint("d1", 1), |
| 44 | eth, |
| 45 | byteBuffer); |
| 46 | final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 47 | final DefaultOutboundPacket outPacket = |
| 48 | new DefaultOutboundPacket(did("d1"), |
| 49 | treatment, |
| 50 | byteBuffer); |
| 51 | |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 52 | final DefaultPacketContext context1 = |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 53 | new PacketContextAdapter(123L, inPacket, outPacket, true); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 54 | final DefaultPacketContext sameAsContext1 = |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 55 | new PacketContextAdapter(123L, inPacket, outPacket, true); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 56 | final DefaultPacketContext context2 = |
Jonathan Hart | 584ea2d | 2016-10-11 10:49:16 +0200 | [diff] [blame] | 57 | new PacketContextAdapter(123123L, inPacket, outPacket, true); |
Ray Milkey | afa00d2 | 2014-11-13 19:25:15 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Checks that the DefaultOutboundPacket class is immutable but can be |
| 61 | * used as a base class. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testImmutability() { |
| 65 | assertThatClassIsImmutableBaseClass(DefaultPacketContext.class); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Tests the equals(), hashCode() and toString() methods. |
| 70 | */ |
| 71 | @Test |
| 72 | public void testEquals() { |
| 73 | // No hashCode() or equals() defined, object comparison is used. |
| 74 | new EqualsTester() |
| 75 | .addEqualityGroup(context1) |
| 76 | .addEqualityGroup(sameAsContext1) |
| 77 | .addEqualityGroup(context2) |
| 78 | .testEquals(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests that objects are created properly. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testConstruction() { |
| 86 | assertThat(context1.block(), is(true)); |
| 87 | assertThat(context1.inPacket(), is(inPacket)); |
| 88 | assertThat(context1.isHandled(), is(true)); |
| 89 | assertThat(context1.outPacket(), is(outPacket)); |
| 90 | assertThat(context1.time(), is(123L)); |
| 91 | assertThat(context1.treatmentBuilder(), is(notNullValue())); |
| 92 | } |
| 93 | } |