blob: 9aad3dfc7adcfb6e86a897da7cb728e319a4a41c [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
18import java.nio.ByteBuffer;
Charles Chancb13d122016-02-10 13:03:40 -080019import java.util.Optional;
Ray Milkeyafa00d22014-11-13 19:25:15 -080020
21import org.junit.Test;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.MacAddress;
24
25import com.google.common.testing.EqualsTester;
26
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.equalTo;
29import static org.hamcrest.Matchers.notNullValue;
30import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import static org.onosproject.net.NetTestTools.connectPoint;
Ray Milkeyafa00d22014-11-13 19:25:15 -080032
33/**
34 * Unit tests for the DefaultInboundPacket class.
35 */
36public 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 Chancb13d122016-02-10 13:03:40 -080045 byteBuffer,
46 Optional.of(1L));
Ray Milkeyafa00d22014-11-13 19:25:15 -080047 final DefaultInboundPacket sameAsPacket1 =
48 new DefaultInboundPacket(connectPoint("d1", 1),
49 eth,
Charles Chancb13d122016-02-10 13:03:40 -080050 byteBuffer,
51 Optional.of(1L));
Ray Milkeyafa00d22014-11-13 19:25:15 -080052 final DefaultInboundPacket packet2 =
53 new DefaultInboundPacket(connectPoint("d2", 1),
54 eth,
55 byteBuffer);
Charles Chancb13d122016-02-10 13:03:40 -080056 final DefaultInboundPacket sameAsPacket2 =
57 new DefaultInboundPacket(connectPoint("d2", 1),
58 eth,
59 byteBuffer,
60 Optional.empty());
Ray Milkeyafa00d22014-11-13 19:25:15 -080061 /**
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 Chancb13d122016-02-10 13:03:40 -080076 .addEqualityGroup(packet2, sameAsPacket2)
Ray Milkeyafa00d22014-11-13 19:25:15 -080077 .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 Chancb13d122016-02-10 13:03:40 -080088 assertThat(packet1.cookie(), equalTo(Optional.of(1L)));
Ray Milkeyafa00d22014-11-13 19:25:15 -080089 }
90}