blob: ea45e09490bc98736b6905f3fec305c532b680b6 [file] [log] [blame]
Ray Milkeyafa00d22014-11-13 19:25:15 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Ray Milkeyafa00d22014-11-13 19:25:15 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.packet;
Ray Milkeyafa00d22014-11-13 19:25:15 -080017
Jonathan Hart584ea2d2016-10-11 10:49:16 +020018import com.google.common.testing.EqualsTester;
Ray Milkeyafa00d22014-11-13 19:25:15 -080019import org.junit.Test;
Ray Milkeyafa00d22014-11-13 19:25:15 -080020import org.onlab.packet.Ethernet;
21import org.onlab.packet.MacAddress;
Jonathan Hart584ea2d2016-10-11 10:49:16 +020022import org.onosproject.net.flow.TrafficTreatment;
23import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkeyafa00d22014-11-13 19:25:15 -080024
Jonathan Hart584ea2d2016-10-11 10:49:16 +020025import java.nio.ByteBuffer;
Ray Milkeyafa00d22014-11-13 19:25:15 -080026
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.is;
29import static org.hamcrest.Matchers.notNullValue;
30import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import static org.onosproject.net.NetTestTools.connectPoint;
32import static org.onosproject.net.NetTestTools.did;
Ray Milkeyafa00d22014-11-13 19:25:15 -080033
34/**
35 * Unit tests for the DefaultPacketContextTest.
36 */
37public 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 Milkeyafa00d22014-11-13 19:25:15 -080052 final DefaultPacketContext context1 =
Jonathan Hart584ea2d2016-10-11 10:49:16 +020053 new PacketContextAdapter(123L, inPacket, outPacket, true);
Ray Milkeyafa00d22014-11-13 19:25:15 -080054 final DefaultPacketContext sameAsContext1 =
Jonathan Hart584ea2d2016-10-11 10:49:16 +020055 new PacketContextAdapter(123L, inPacket, outPacket, true);
Ray Milkeyafa00d22014-11-13 19:25:15 -080056 final DefaultPacketContext context2 =
Jonathan Hart584ea2d2016-10-11 10:49:16 +020057 new PacketContextAdapter(123123L, inPacket, outPacket, true);
Ray Milkeyafa00d22014-11-13 19:25:15 -080058
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}