blob: 3f7b29903ca507b802a4c15c47f776ff76851359 [file] [log] [blame]
Ray Milkeyafa00d22014-11-13 19:25:15 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
18import java.nio.ByteBuffer;
19
20import org.junit.Test;
Thiago Santosa4a17cb2019-01-30 14:23:58 -080021import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.flow.TrafficTreatment;
23import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkeyafa00d22014-11-13 19:25:15 -080024import org.onlab.packet.Ethernet;
25import org.onlab.packet.MacAddress;
26
27import com.google.common.testing.EqualsTester;
28
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.equalTo;
31import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import static org.onosproject.net.NetTestTools.did;
Ray Milkeyafa00d22014-11-13 19:25:15 -080033
34/**
35 * Unit tests for the DefaultOutboundPacketTest class.
36 */
37public 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 Santosa4a17cb2019-01-30 14:23:58 -080051 private final PortNumber inPort = PortNumber.portNumber(1);
52 final DefaultOutboundPacket packet1WithInPort =
53 new DefaultOutboundPacket(did("d1"),
54 treatment,
55 byteBuffer,
56 inPort);
Ray Milkeyafa00d22014-11-13 19:25:15 -080057 final DefaultOutboundPacket packet2 =
58 new DefaultOutboundPacket(did("d2"),
59 treatment,
60 byteBuffer);
Thiago Santosa4a17cb2019-01-30 14:23:58 -080061
Ray Milkeyafa00d22014-11-13 19:25:15 -080062 /**
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 Santosa4a17cb2019-01-30 14:23:58 -080078 .addEqualityGroup(packet1WithInPort)
Ray Milkeyafa00d22014-11-13 19:25:15 -080079 .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 Santosa4a17cb2019-01-30 14:23:58 -080090 assertThat(packet1.inPort(), equalTo(null));
91 assertThat(packet1WithInPort.inPort(), equalTo(inPort));
Ray Milkeyafa00d22014-11-13 19:25:15 -080092 }
93}