blob: 7d187492c764de4cfb9a1930fe1c73eed55bce60 [file] [log] [blame]
Ray Milkey97f8f102014-11-14 14:18:23 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
Ray Milkey97f8f102014-11-14 14:18:23 -080017
18import java.util.List;
19
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.PortNumber;
22import org.onosproject.net.flow.instructions.Instruction;
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080023import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey97f8f102014-11-14 14:18:23 -080024import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27
28import com.google.common.testing.EqualsTester;
29
30import static org.hamcrest.CoreMatchers.equalTo;
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.hasSize;
33import static org.hamcrest.Matchers.is;
Ray Milkey97f8f102014-11-14 14:18:23 -080034
35/**
36 * Unit tests for the DefaultTrafficTreatment class.
37 */
38public class DefaultTrafficTreatmentTest {
39
40 // Tests for the nested Builder class
41
42 /**
43 * Tests that the Builder constructors return equivalent objects
44 * when given the same data.
45 */
46 @Test
47 public void testTreatmentBuilderConstructors() {
48 final TrafficTreatment treatment1 =
49 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080050 .add(Instructions.modL0Lambda((short) 4))
Ray Milkey97f8f102014-11-14 14:18:23 -080051 .build();
52 final TrafficTreatment treatment2 =
53 DefaultTrafficTreatment.builder(treatment1).build();
54 assertThat(treatment1, is(equalTo(treatment2)));
55 }
56
57 /**
58 * Tests methods defined on the Builder.
59 */
60 @Test
61 public void testBuilderMethods() {
62 final Instruction instruction1 =
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080063 Instructions.modL0Lambda((short) 4);
Ray Milkey97f8f102014-11-14 14:18:23 -080064
65 final TrafficTreatment.Builder builder1 =
66 DefaultTrafficTreatment.builder()
67 .add(instruction1)
68 .setEthDst(MacAddress.BROADCAST)
69 .setEthSrc(MacAddress.BROADCAST)
70 .setIpDst(IpAddress.valueOf("1.1.1.1"))
71 .setIpSrc(IpAddress.valueOf("2.2.2.2"))
72 .setLambda((short) 4)
73 .setOutput(PortNumber.portNumber(2))
74 .setVlanId(VlanId.vlanId((short) 4))
75 .setVlanPcp((byte) 3);
76
77 final TrafficTreatment treatment1 = builder1.build();
78
79 final List<Instruction> instructions1 = treatment1.instructions();
80 assertThat(instructions1, hasSize(9));
81
82 builder1.drop();
83 builder1.add(instruction1);
84
85 final List<Instruction> instructions2 = builder1.build().instructions();
86 assertThat(instructions2, hasSize(8));
87 }
88
89 /**
90 * Tests equals(), hashCode() and toString() methods of
91 * DefaultTrafficTreatment.
92 */
93 @Test
94 public void testEquals() {
95 final TrafficTreatment treatment1 =
96 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -080097 .add(Instructions.modL0Lambda((short) 4))
Ray Milkey97f8f102014-11-14 14:18:23 -080098 .build();
99 final TrafficTreatment sameAsTreatment1 =
100 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800101 .add(Instructions.modL0Lambda((short) 4))
Ray Milkey97f8f102014-11-14 14:18:23 -0800102 .build();
103 final TrafficTreatment treatment2 =
104 DefaultTrafficTreatment.builder()
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800105 .add(Instructions.modL0Lambda((short) 2))
Ray Milkey97f8f102014-11-14 14:18:23 -0800106 .build();
107 new EqualsTester()
108 .addEqualityGroup(treatment1, sameAsTreatment1)
109 .addEqualityGroup(treatment2)
110 .testEquals();
111 }
112}