Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.flow.instructions.Instruction; |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.instructions.Instructions; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | |
| 28 | import com.google.common.testing.EqualsTester; |
| 29 | |
| 30 | import static org.hamcrest.CoreMatchers.equalTo; |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.hasSize; |
| 33 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Unit tests for the DefaultTrafficTreatment class. |
| 37 | */ |
| 38 | public 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 HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 50 | .add(Instructions.modL0Lambda((short) 4)) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 51 | .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 HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 63 | Instructions.modL0Lambda((short) 4); |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 64 | |
| 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 HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 97 | .add(Instructions.modL0Lambda((short) 4)) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 98 | .build(); |
| 99 | final TrafficTreatment sameAsTreatment1 = |
| 100 | DefaultTrafficTreatment.builder() |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 101 | .add(Instructions.modL0Lambda((short) 4)) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 102 | .build(); |
| 103 | final TrafficTreatment treatment2 = |
| 104 | DefaultTrafficTreatment.builder() |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 105 | .add(Instructions.modL0Lambda((short) 2)) |
Ray Milkey | 97f8f10 | 2014-11-14 14:18:23 -0800 | [diff] [blame] | 106 | .build(); |
| 107 | new EqualsTester() |
| 108 | .addEqualityGroup(treatment1, sameAsTreatment1) |
| 109 | .addEqualityGroup(treatment2) |
| 110 | .testEquals(); |
| 111 | } |
| 112 | } |