Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -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 | */ |
| 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.net.flow; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 18 | |
| 19 | import org.junit.Test; |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.flow.instructions.Instructions; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.intent.IntentTestsMocks; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 23 | |
| 24 | import com.google.common.testing.EqualsTester; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.is; |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 28 | import static org.hamcrest.Matchers.not; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 31 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Unit tests for the default flow rule class. |
| 35 | */ |
| 36 | public class DefaultFlowRuleTest { |
| 37 | private static final IntentTestsMocks.MockSelector SELECTOR = |
| 38 | new IntentTestsMocks.MockSelector(); |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 39 | private static final IntentTestsMocks.MockTreatment TREATMENT1 = |
| 40 | new IntentTestsMocks.MockTreatment( |
| 41 | Instructions.createOutput(PortNumber.portNumber(1))); |
| 42 | private static final IntentTestsMocks.MockTreatment TREATMENT2 = |
| 43 | new IntentTestsMocks.MockTreatment( |
| 44 | Instructions.createOutput(PortNumber.portNumber(2))); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 45 | |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 46 | final FlowRule flowRule1 = new IntentTestsMocks.MockFlowRule(1); |
| 47 | final FlowRule sameAsFlowRule1 = new IntentTestsMocks.MockFlowRule(1); |
| 48 | final FlowRule flowRule2 = new IntentTestsMocks.MockFlowRule(2); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 49 | final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1); |
| 50 | final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1); |
| 51 | final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2); |
| 52 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Checks that the DefaultFlowRule class is immutable but can be inherited |
| 55 | * from. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testImmutability() { |
| 59 | assertThatClassIsImmutableBaseClass(DefaultFlowRule.class); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 64 | */ |
| 65 | @Test |
| 66 | public void testEquals() { |
| 67 | new EqualsTester() |
| 68 | .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1) |
| 69 | .addEqualityGroup(defaultFlowRule2) |
| 70 | .testEquals(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests creation of a DefaultFlowRule using a FlowRule constructor. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testCreationFromFlowRule() { |
| 78 | assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId())); |
| 79 | assertThat(defaultFlowRule1.appId(), is(flowRule1.appId())); |
| 80 | assertThat(defaultFlowRule1.id(), is(flowRule1.id())); |
| 81 | assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent())); |
| 82 | assertThat(defaultFlowRule1.priority(), is(flowRule1.priority())); |
| 83 | assertThat(defaultFlowRule1.selector(), is(flowRule1.selector())); |
| 84 | assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment())); |
| 85 | assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout())); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests creation of a DefaultFlowRule using a FlowId constructor. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testCreationWithFlowId() { |
| 93 | final DefaultFlowRule rule = |
| 94 | new DefaultFlowRule(did("1"), SELECTOR, |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 95 | TREATMENT1, 22, 33, |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 96 | 44, false); |
| 97 | assertThat(rule.deviceId(), is(did("1"))); |
| 98 | assertThat(rule.id().value(), is(33L)); |
| 99 | assertThat(rule.isPermanent(), is(false)); |
| 100 | assertThat(rule.priority(), is(22)); |
| 101 | assertThat(rule.selector(), is(SELECTOR)); |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 102 | assertThat(rule.treatment(), is(TREATMENT1)); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 103 | assertThat(rule.timeout(), is(44)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests the creation of a DefaultFlowRule using an AppId constructor. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testCreationWithAppId() { |
| 111 | final DefaultFlowRule rule = |
| 112 | new DefaultFlowRule(did("1"), SELECTOR, |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 113 | TREATMENT1, 22, APP_ID, |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 114 | 44, false); |
| 115 | assertThat(rule.deviceId(), is(did("1"))); |
| 116 | assertThat(rule.isPermanent(), is(false)); |
| 117 | assertThat(rule.priority(), is(22)); |
| 118 | assertThat(rule.selector(), is(SELECTOR)); |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 119 | assertThat(rule.treatment(), is(TREATMENT1)); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 120 | assertThat(rule.timeout(), is(44)); |
| 121 | } |
Ayaka Koshibe | 38f8c23 | 2015-01-30 14:14:40 -0800 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Tests equality that factors in TrafficTreatment through the flowId. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testActionEquals() { |
| 128 | final DefaultFlowRule rule1 = new DefaultFlowRule(did("1"), SELECTOR, |
| 129 | TREATMENT1, 22, APP_ID, 44, false); |
| 130 | final DefaultFlowRule rule2 = new DefaultFlowRule(did("1"), SELECTOR, |
| 131 | TREATMENT2, 22, APP_ID, 44, false); |
| 132 | assertThat(rule1, not(rule2)); |
| 133 | } |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 134 | } |