blob: c0a2d4478ce7f2478fde042dfd7b815214657ec9 [file] [log] [blame]
Ray Milkey1e207112014-11-11 10:38:00 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Ray Milkey1e207112014-11-11 10:38:00 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.net.flow;
Ray Milkey1e207112014-11-11 10:38:00 -080018
19import org.junit.Test;
Yi Tsengfa394de2017-02-01 11:26:40 -080020import org.onosproject.core.GroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkey1e207112014-11-11 10:38:00 -080022
23import com.google.common.testing.EqualsTester;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.is;
27import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import static org.onosproject.net.NetTestTools.APP_ID;
29import static org.onosproject.net.NetTestTools.did;
Ray Milkey1e207112014-11-11 10:38:00 -080030
31/**
32 * Unit tests for the default flow rule class.
33 */
34public class DefaultFlowRuleTest {
35 private static final IntentTestsMocks.MockSelector SELECTOR =
36 new IntentTestsMocks.MockSelector();
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000037 private static final IntentTestsMocks.MockTreatment TREATMENT =
38 new IntentTestsMocks.MockTreatment();
Ray Milkey1e207112014-11-11 10:38:00 -080039
Jian Li68c4fc42016-01-11 16:07:03 -080040 private static byte[] b = new byte[3];
Ray Milkey47f09c52019-02-08 18:51:34 -080041 final FlowRule flowRule1 = new IntentTestsMocks.MockFlowRule(1);
42 final FlowRule sameAsFlowRule1 = new IntentTestsMocks.MockFlowRule(1);
Ray Milkey1e207112014-11-11 10:38:00 -080043 final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1);
44 final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1);
Ray Milkey1e207112014-11-11 10:38:00 -080045
Ray Milkey1e207112014-11-11 10:38:00 -080046 /**
47 * Checks that the DefaultFlowRule class is immutable but can be inherited
48 * from.
49 */
50 @Test
51 public void testImmutability() {
52 assertThatClassIsImmutableBaseClass(DefaultFlowRule.class);
53 }
54
55 /**
56 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
57 */
58 @Test
59 public void testEquals() {
60 new EqualsTester()
61 .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1)
Ray Milkey1e207112014-11-11 10:38:00 -080062 .testEquals();
63 }
64
65 /**
66 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
67 */
68 @Test
69 public void testCreationFromFlowRule() {
70 assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId()));
71 assertThat(defaultFlowRule1.appId(), is(flowRule1.appId()));
72 assertThat(defaultFlowRule1.id(), is(flowRule1.id()));
73 assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent()));
74 assertThat(defaultFlowRule1.priority(), is(flowRule1.priority()));
75 assertThat(defaultFlowRule1.selector(), is(flowRule1.selector()));
76 assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment()));
77 assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout()));
78 }
79
80 /**
81 * Tests creation of a DefaultFlowRule using a FlowId constructor.
82 */
Ray Milkeyd13a37b2015-06-12 11:55:17 -070083
Ray Milkey1e207112014-11-11 10:38:00 -080084 @Test
85 public void testCreationWithFlowId() {
Ray Milkeyd13a37b2015-06-12 11:55:17 -070086 final FlowRule rule =
87 DefaultFlowRule.builder()
88 .forDevice(did("1"))
89 .withSelector(SELECTOR)
90 .withTreatment(TREATMENT)
91 .withPriority(22)
92 .makeTemporary(44)
93 .fromApp(APP_ID)
94 .build();
95
Ray Milkey1e207112014-11-11 10:38:00 -080096 assertThat(rule.deviceId(), is(did("1")));
Ray Milkey1e207112014-11-11 10:38:00 -080097 assertThat(rule.isPermanent(), is(false));
98 assertThat(rule.priority(), is(22));
99 assertThat(rule.selector(), is(SELECTOR));
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +0000100 assertThat(rule.treatment(), is(TREATMENT));
Ray Milkey1e207112014-11-11 10:38:00 -0800101 assertThat(rule.timeout(), is(44));
102 }
103
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700104
Ray Milkey1e207112014-11-11 10:38:00 -0800105 /**
jcc3d4e14a2015-04-21 11:32:05 +0800106 * Tests creation of a DefaultFlowRule using a PayLoad constructor.
107 */
108 @Test
109 public void testCreationWithPayLoadByFlowTable() {
Ray Milkey47f09c52019-02-08 18:51:34 -0800110 final FlowRule rule =
111 DefaultFlowRule.builder()
112 .forDevice(did("1"))
113 .withPriority(22)
114 .fromApp(APP_ID)
115 .makeTemporary(44)
116 .build();
jcc3d4e14a2015-04-21 11:32:05 +0800117 assertThat(rule.deviceId(), is(did("1")));
118 assertThat(rule.isPermanent(), is(false));
119 assertThat(rule.priority(), is(22));
120 assertThat(rule.timeout(), is(44));
jcc3d4e14a2015-04-21 11:32:05 +0800121 }
122
123 /**
124 * Tests creation of a DefaultFlowRule using a PayLoad constructor.
125 */
126 @Test
127 public void testCreationWithPayLoadByGroupTable() {
Ray Milkey47f09c52019-02-08 18:51:34 -0800128 final FlowRule rule =
129 DefaultFlowRule.builder()
130 .forDevice(did("1"))
131 .withPriority(22)
132 .fromApp(APP_ID)
133 .makeTemporary(44)
134 .build();
jcc3d4e14a2015-04-21 11:32:05 +0800135 assertThat(rule.deviceId(), is(did("1")));
136 assertThat(rule.isPermanent(), is(false));
137 assertThat(rule.priority(), is(22));
138 assertThat(rule.timeout(), is(44));
Yi Tsengfa394de2017-02-01 11:26:40 -0800139 assertThat(rule.groupId(), is(new GroupId(0)));
jcc3d4e14a2015-04-21 11:32:05 +0800140 }
141 /**
Ray Milkey1e207112014-11-11 10:38:00 -0800142 * Tests the creation of a DefaultFlowRule using an AppId constructor.
143 */
144 @Test
145 public void testCreationWithAppId() {
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700146 final FlowRule rule =
147 DefaultFlowRule.builder()
148 .forDevice(did("1"))
149 .withSelector(SELECTOR)
150 .withTreatment(TREATMENT)
151 .withPriority(22)
152 .fromApp(APP_ID)
153 .makeTemporary(44)
154 .build();
155
Ray Milkey1e207112014-11-11 10:38:00 -0800156 assertThat(rule.deviceId(), is(did("1")));
157 assertThat(rule.isPermanent(), is(false));
158 assertThat(rule.priority(), is(22));
159 assertThat(rule.selector(), is(SELECTOR));
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +0000160 assertThat(rule.treatment(), is(TREATMENT));
Ray Milkey1e207112014-11-11 10:38:00 -0800161 assertThat(rule.timeout(), is(44));
162 }
Hyunsun Moon7bfbc1c2016-04-05 16:40:43 -0700163
164 /**
165 * Tests flow ID is consistent.
166 */
167 @Test
168 public void testCreationWithConsistentFlowId() {
169 final FlowRule rule1 =
170 DefaultFlowRule.builder()
171 .forDevice(did("1"))
172 .withSelector(SELECTOR)
173 .withTreatment(TREATMENT)
174 .withPriority(22)
175 .forTable(1)
176 .fromApp(APP_ID)
177 .makeTemporary(44)
178 .build();
179
180 final FlowRule rule2 =
181 DefaultFlowRule.builder()
182 .forDevice(did("1"))
183 .withSelector(SELECTOR)
184 .withTreatment(TREATMENT)
185 .withPriority(22)
186 .forTable(1)
187 .fromApp(APP_ID)
188 .makeTemporary(44)
189 .build();
190
191 new EqualsTester().addEqualityGroup(rule1.id(), rule2.id()).testEquals();
192 }
Ray Milkey1e207112014-11-11 10:38:00 -0800193}