Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 1 | /* |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 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; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 20 | import org.onosproject.core.DefaultGroupId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.IntentTestsMocks; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 22 | |
| 23 | import com.google.common.testing.EqualsTester; |
| 24 | |
| 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.Matchers.is; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 29 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Unit tests for the default flow rule class. |
| 33 | */ |
| 34 | public class DefaultFlowRuleTest { |
| 35 | private static final IntentTestsMocks.MockSelector SELECTOR = |
| 36 | new IntentTestsMocks.MockSelector(); |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 37 | private static final IntentTestsMocks.MockTreatment TREATMENT = |
| 38 | new IntentTestsMocks.MockTreatment(); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 39 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 40 | private static byte [] b = new byte[3]; |
| 41 | private static FlowRuleExtPayLoad payLoad = FlowRuleExtPayLoad.flowRuleExtPayLoad(b); |
| 42 | final FlowRule flowRule1 = new IntentTestsMocks.MockFlowRule(1, payLoad); |
| 43 | final FlowRule sameAsFlowRule1 = new IntentTestsMocks.MockFlowRule(1, payLoad); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 44 | final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1); |
| 45 | final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 46 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Checks that the DefaultFlowRule class is immutable but can be inherited |
| 49 | * from. |
| 50 | */ |
| 51 | @Test |
| 52 | public void testImmutability() { |
| 53 | assertThatClassIsImmutableBaseClass(DefaultFlowRule.class); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Tests the equals, hashCode and toString methods using Guava EqualsTester. |
| 58 | */ |
| 59 | @Test |
| 60 | public void testEquals() { |
| 61 | new EqualsTester() |
| 62 | .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1) |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 63 | .testEquals(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tests creation of a DefaultFlowRule using a FlowRule constructor. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testCreationFromFlowRule() { |
| 71 | assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId())); |
| 72 | assertThat(defaultFlowRule1.appId(), is(flowRule1.appId())); |
| 73 | assertThat(defaultFlowRule1.id(), is(flowRule1.id())); |
| 74 | assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent())); |
| 75 | assertThat(defaultFlowRule1.priority(), is(flowRule1.priority())); |
| 76 | assertThat(defaultFlowRule1.selector(), is(flowRule1.selector())); |
| 77 | assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment())); |
| 78 | assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout())); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 79 | assertThat(defaultFlowRule1.payLoad(), is(flowRule1.payLoad())); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests creation of a DefaultFlowRule using a FlowId constructor. |
| 84 | */ |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 86 | @Test |
| 87 | public void testCreationWithFlowId() { |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 88 | final FlowRule rule = |
| 89 | DefaultFlowRule.builder() |
| 90 | .forDevice(did("1")) |
| 91 | .withSelector(SELECTOR) |
| 92 | .withTreatment(TREATMENT) |
| 93 | .withPriority(22) |
| 94 | .makeTemporary(44) |
| 95 | .fromApp(APP_ID) |
| 96 | .build(); |
| 97 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 98 | assertThat(rule.deviceId(), is(did("1"))); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 99 | assertThat(rule.isPermanent(), is(false)); |
| 100 | assertThat(rule.priority(), is(22)); |
| 101 | assertThat(rule.selector(), is(SELECTOR)); |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 102 | assertThat(rule.treatment(), is(TREATMENT)); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 103 | assertThat(rule.timeout(), is(44)); |
| 104 | } |
| 105 | |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 106 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 107 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 108 | * Tests creation of a DefaultFlowRule using a PayLoad constructor. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testCreationWithPayLoadByFlowTable() { |
| 112 | final DefaultFlowRule rule = |
| 113 | new DefaultFlowRule(did("1"), null, |
| 114 | null, 22, APP_ID, |
| 115 | 44, false, payLoad); |
| 116 | assertThat(rule.deviceId(), is(did("1"))); |
| 117 | assertThat(rule.isPermanent(), is(false)); |
| 118 | assertThat(rule.priority(), is(22)); |
| 119 | assertThat(rule.timeout(), is(44)); |
| 120 | assertThat(defaultFlowRule1.payLoad(), is(payLoad)); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Tests creation of a DefaultFlowRule using a PayLoad constructor. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testCreationWithPayLoadByGroupTable() { |
| 128 | final DefaultFlowRule rule = |
| 129 | new DefaultFlowRule(did("1"), null, |
| 130 | null, 22, APP_ID, new DefaultGroupId(0), |
| 131 | 44, false, payLoad); |
| 132 | assertThat(rule.deviceId(), is(did("1"))); |
| 133 | assertThat(rule.isPermanent(), is(false)); |
| 134 | assertThat(rule.priority(), is(22)); |
| 135 | assertThat(rule.timeout(), is(44)); |
| 136 | assertThat(rule.groupId(), is(new DefaultGroupId(0))); |
| 137 | assertThat(defaultFlowRule1.payLoad(), is(payLoad)); |
| 138 | } |
| 139 | /** |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 140 | * Tests the creation of a DefaultFlowRule using an AppId constructor. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testCreationWithAppId() { |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 144 | final FlowRule rule = |
| 145 | DefaultFlowRule.builder() |
| 146 | .forDevice(did("1")) |
| 147 | .withSelector(SELECTOR) |
| 148 | .withTreatment(TREATMENT) |
| 149 | .withPriority(22) |
| 150 | .fromApp(APP_ID) |
| 151 | .makeTemporary(44) |
| 152 | .build(); |
| 153 | |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 154 | assertThat(rule.deviceId(), is(did("1"))); |
| 155 | assertThat(rule.isPermanent(), is(false)); |
| 156 | assertThat(rule.priority(), is(22)); |
| 157 | assertThat(rule.selector(), is(SELECTOR)); |
Ayaka Koshibe | b1224ab | 2015-01-31 00:48:58 +0000 | [diff] [blame] | 158 | assertThat(rule.treatment(), is(TREATMENT)); |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 159 | assertThat(rule.timeout(), is(44)); |
| 160 | } |
Ray Milkey | 1e20711 | 2014-11-11 10:38:00 -0800 | [diff] [blame] | 161 | } |