blob: 238bcce7d1ab68ab343e287975aa1451d8af1f98 [file] [log] [blame]
Ray Milkeyafa00d22014-11-13 19:25:15 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
16package org.onlab.onos.net.packet;
17
18import java.nio.ByteBuffer;
19
20import org.junit.Test;
21import org.onlab.onos.net.flow.TrafficTreatment;
22import org.onlab.onos.net.intent.IntentTestsMocks;
23import org.onlab.packet.Ethernet;
24import org.onlab.packet.MacAddress;
25
26import com.google.common.testing.EqualsTester;
27
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.notNullValue;
31import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
32import static org.onlab.onos.net.NetTestTools.connectPoint;
33import static org.onlab.onos.net.NetTestTools.did;
34
35/**
36 * Unit tests for the DefaultPacketContextTest.
37 */
38public class DefaultPacketContextTest {
39 final Ethernet eth = new Ethernet()
40 .setDestinationMACAddress(MacAddress.BROADCAST)
41 .setSourceMACAddress(MacAddress.BROADCAST);
42 final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize());
43 final DefaultInboundPacket inPacket =
44 new DefaultInboundPacket(connectPoint("d1", 1),
45 eth,
46 byteBuffer);
47 final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
48 final DefaultOutboundPacket outPacket =
49 new DefaultOutboundPacket(did("d1"),
50 treatment,
51 byteBuffer);
52
53 static class MockPacketContext extends DefaultPacketContext {
54
55 protected MockPacketContext(long time, InboundPacket inPkt,
56 OutboundPacket outPkt, boolean block) {
57 super(time, inPkt, outPkt, block);
58 }
59
60 @Override
61 public void send() {
62
63 }
64
65 @Override
66 public boolean block() {
67 return super.block();
68 }
69 }
70
71 final DefaultPacketContext context1 =
72 new MockPacketContext(123L, inPacket, outPacket, true);
73 final DefaultPacketContext sameAsContext1 =
74 new MockPacketContext(123L, inPacket, outPacket, true);
75 final DefaultPacketContext context2 =
76 new MockPacketContext(123123L, inPacket, outPacket, true);
77
78 /**
79 * Checks that the DefaultOutboundPacket class is immutable but can be
80 * used as a base class.
81 */
82 @Test
83 public void testImmutability() {
84 assertThatClassIsImmutableBaseClass(DefaultPacketContext.class);
85 }
86
87 /**
88 * Tests the equals(), hashCode() and toString() methods.
89 */
90 @Test
91 public void testEquals() {
92 // No hashCode() or equals() defined, object comparison is used.
93 new EqualsTester()
94 .addEqualityGroup(context1)
95 .addEqualityGroup(sameAsContext1)
96 .addEqualityGroup(context2)
97 .testEquals();
98 }
99
100 /**
101 * Tests that objects are created properly.
102 */
103 @Test
104 public void testConstruction() {
105 assertThat(context1.block(), is(true));
106 assertThat(context1.inPacket(), is(inPacket));
107 assertThat(context1.isHandled(), is(true));
108 assertThat(context1.outPacket(), is(outPacket));
109 assertThat(context1.time(), is(123L));
110 assertThat(context1.treatmentBuilder(), is(notNullValue()));
111 }
112}