Ray Milkey
Committed by Gerrit Code Review

Unit tests for DefaultTrafficTreatment

Change-Id: I68b0773b69d85c1d05d58b32c1dceaca73c0a5e1
1 +/*
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 + */
16 +package org.onlab.onos.net.flow;
17 +
18 +import java.util.List;
19 +
20 +import org.junit.Test;
21 +import org.onlab.onos.net.PortNumber;
22 +import org.onlab.onos.net.flow.instructions.Instruction;
23 +import org.onlab.packet.IpAddress;
24 +import org.onlab.packet.MacAddress;
25 +import org.onlab.packet.VlanId;
26 +
27 +import com.google.common.testing.EqualsTester;
28 +
29 +import static org.hamcrest.CoreMatchers.equalTo;
30 +import static org.hamcrest.MatcherAssert.assertThat;
31 +import static org.hamcrest.Matchers.hasSize;
32 +import static org.hamcrest.Matchers.is;
33 +import static org.onlab.onos.net.flow.instructions.L0ModificationInstruction.L0SubType;
34 +import static org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
35 +
36 +/**
37 + * Unit tests for the DefaultTrafficTreatment class.
38 + */
39 +public class DefaultTrafficTreatmentTest {
40 +
41 + // Tests for the nested Builder class
42 +
43 + /**
44 + * Tests that the Builder constructors return equivalent objects
45 + * when given the same data.
46 + */
47 + @Test
48 + public void testTreatmentBuilderConstructors() {
49 + final TrafficTreatment treatment1 =
50 + DefaultTrafficTreatment.builder()
51 + .add(new ModLambdaInstruction(L0SubType.LAMBDA, (short) 4))
52 + .build();
53 + final TrafficTreatment treatment2 =
54 + DefaultTrafficTreatment.builder(treatment1).build();
55 + assertThat(treatment1, is(equalTo(treatment2)));
56 + }
57 +
58 + /**
59 + * Tests methods defined on the Builder.
60 + */
61 + @Test
62 + public void testBuilderMethods() {
63 + final Instruction instruction1 =
64 + new ModLambdaInstruction(L0SubType.LAMBDA, (short) 4);
65 +
66 + final TrafficTreatment.Builder builder1 =
67 + DefaultTrafficTreatment.builder()
68 + .add(instruction1)
69 + .setEthDst(MacAddress.BROADCAST)
70 + .setEthSrc(MacAddress.BROADCAST)
71 + .setIpDst(IpAddress.valueOf("1.1.1.1"))
72 + .setIpSrc(IpAddress.valueOf("2.2.2.2"))
73 + .setLambda((short) 4)
74 + .setOutput(PortNumber.portNumber(2))
75 + .setVlanId(VlanId.vlanId((short) 4))
76 + .setVlanPcp((byte) 3);
77 +
78 + final TrafficTreatment treatment1 = builder1.build();
79 +
80 + final List<Instruction> instructions1 = treatment1.instructions();
81 + assertThat(instructions1, hasSize(9));
82 +
83 + builder1.drop();
84 + builder1.add(instruction1);
85 +
86 + final List<Instruction> instructions2 = builder1.build().instructions();
87 + assertThat(instructions2, hasSize(8));
88 + }
89 +
90 + /**
91 + * Tests equals(), hashCode() and toString() methods of
92 + * DefaultTrafficTreatment.
93 + */
94 + @Test
95 + public void testEquals() {
96 + final TrafficTreatment treatment1 =
97 + DefaultTrafficTreatment.builder()
98 + .add(new ModLambdaInstruction(L0SubType.LAMBDA, (short) 4))
99 + .build();
100 + final TrafficTreatment sameAsTreatment1 =
101 + DefaultTrafficTreatment.builder()
102 + .add(new ModLambdaInstruction(L0SubType.LAMBDA, (short) 4))
103 + .build();
104 + final TrafficTreatment treatment2 =
105 + DefaultTrafficTreatment.builder()
106 + .add(new ModLambdaInstruction(L0SubType.LAMBDA, (short) 2))
107 + .build();
108 + new EqualsTester()
109 + .addEqualityGroup(treatment1, sameAsTreatment1)
110 + .addEqualityGroup(treatment2)
111 + .testEquals();
112 + }
113 +}