blob: b0919f6f3a91753974b1da261f8314198c88d74a [file] [log] [blame]
Ray Milkeyf410a0c2015-05-19 15:46:03 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.flowobjective;
17
18import org.junit.Test;
19import org.onosproject.net.flow.DefaultTrafficSelector;
20import org.onosproject.net.flow.DefaultTrafficTreatment;
21import org.onosproject.net.flow.TrafficSelector;
22import org.onosproject.net.flow.TrafficTreatment;
23import org.onosproject.net.flow.criteria.Criteria;
24import org.onosproject.net.flow.criteria.Criterion;
25
26import static org.hamcrest.CoreMatchers.hasItem;
27import static org.hamcrest.CoreMatchers.is;
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30import static org.onosproject.net.NetTestTools.APP_ID;
31import static org.onosproject.net.flowobjective.FilteringObjective.Type.DENY;
32import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
33import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
34
35/**
36 * Unit tests for forwarding objective class.
37 */
38public class ObjectiveTest {
39
40 private final TrafficTreatment treatment =
41 DefaultTrafficTreatment.emptyTreatment();
42 private final TrafficSelector selector =
43 DefaultTrafficSelector.emptySelector();
44 private final Criterion criterion = Criteria.dummy();
45 private final Criterion key = Criteria.dummy();
46
47 /**
48 * Mock objective context.
49 */
50 private static class MockObjectiveContext implements ObjectiveContext {
51 @Override
52 public void onSuccess(Objective objective) {
53 // stub
54 }
55
56 @Override
57 public void onError(Objective objective, ObjectiveError error) {
58 // stub
59 }
60 }
61
62 /**
63 * Checks immutability of objective classes.
64 */
65 @Test
66 public void testImmutability() {
67 assertThatClassIsImmutable(DefaultFilteringObjective.class);
68 assertThatClassIsImmutable(DefaultForwardingObjective.class);
69 assertThatClassIsImmutable(DefaultNextObjective.class);
70 }
71
72 // Forwarding Objectives
73
74 /**
75 * Makes a forwarding objective builder with a set of default values.
76 *
77 * @return forwarding objective builder
78 */
79 private ForwardingObjective.Builder baseForwardingBuilder() {
80 return DefaultForwardingObjective.builder()
81 .withSelector(selector)
82 .withTreatment(treatment)
83 .withFlag(SPECIFIC)
84 .fromApp(APP_ID)
85 .nextStep(33);
86 }
87
88 /**
89 * Checks the default values of a forwarding objective object.
90 *
91 * @param objective forwarding objective to check
92 */
93 private void checkForwardingBase(ForwardingObjective objective) {
94 assertThat(objective.selector(), is(selector));
95 assertThat(objective.treatment(), is(treatment));
96 assertThat(objective.flag(), is(SPECIFIC));
97 assertThat(objective.appId(), is(APP_ID));
98 assertThat(objective.nextId(), is(33));
99 }
100
101 /**
102 * Tests that forwarding objective objects are built correctly using the
103 * add() method.
104 */
105 @Test
106 public void testForwardingAdd() {
107 checkForwardingBase(baseForwardingBuilder().add());
108 }
109
110 /**
111 * Tests that forwarding objective objects are built correctly using the
112 * add(context) method.
113 */
114 @Test
115 public void testForwardingAddWithContext() {
116 ObjectiveContext context = new MockObjectiveContext();
117 checkForwardingBase(baseForwardingBuilder().add(context));
118 }
119
120 /**
121 * Tests that forwarding objective objects are built correctly using the
122 * remove() method.
123 */
124 @Test
125 public void testForwardingRemove() {
126 checkForwardingBase(baseForwardingBuilder().remove());
127 }
128
129 /**
130 * Tests that forwarding objective objects are built correctly using the
131 * remove(context) method.
132 */
133 @Test
134 public void testForwardingRemoveWithContext() {
135 ObjectiveContext context = new MockObjectiveContext();
136 checkForwardingBase(baseForwardingBuilder().remove(context));
137 }
138
139 // Filtering objectives
140
141 /**
142 * Makes a filtering objective builder with a set of default values.
143 *
144 * @return filtering objective builder
145 */
146 private FilteringObjective.Builder baseFilteringBuilder() {
147 return DefaultFilteringObjective.builder()
148 .withKey(key)
149 .withPriority(5)
150 .addCondition(criterion)
151 .fromApp(APP_ID)
152 .makeTemporary(2)
153 .deny();
154 }
155
156 /**
157 * Checks the default values of a filtering objective object.
158 *
159 * @param objective filtering objective to check
160 */
161 private void checkFilteringBase(FilteringObjective objective) {
162 assertThat(objective.key(), is(key));
163 assertThat(objective.conditions(), hasItem(criterion));
164 assertThat(objective.permanent(), is(false));
165 assertThat(objective.timeout(), is(2));
166 assertThat(objective.priority(), is(5));
167 assertThat(objective.appId(), is(APP_ID));
168 assertThat(objective.type(), is(DENY));
169 }
170
171 /**
172 * Tests that forwarding objective objects are built correctly using the
173 * add() method.
174 */
175 @Test
176 public void testFilteringAdd() {
177 checkFilteringBase(baseFilteringBuilder().add());
178 }
179
180 /**
181 * Tests that forwarding objective objects are built correctly using the
182 * add(context) method.
183 */
184 @Test
185 public void testFilteringAddWithContext() {
186 ObjectiveContext context = new MockObjectiveContext();
187 checkFilteringBase(baseFilteringBuilder().add(context));
188 }
189
190 /**
191 * Tests that forwarding objective objects are built correctly using the
192 * remove() method.
193 */
194 @Test
195 public void testFilteringRemove() {
196 checkFilteringBase(baseFilteringBuilder().remove());
197 }
198
199 /**
200 * Tests that forwarding objective objects are built correctly using the
201 * remove(context) method.
202 */
203 @Test
204 public void testFilteringRemoveWithContext() {
205 ObjectiveContext context = new MockObjectiveContext();
206 checkFilteringBase(baseFilteringBuilder().remove(context));
207 }
208
209 // Next objectives
210
211 /**
212 * Makes a next objective builder with a set of default values.
213 *
214 * @return next objective builder
215 */
216 private NextObjective.Builder baseNextBuilder() {
217 return DefaultNextObjective.builder()
218 .addTreatment(treatment)
219 .withId(12)
220 .withType(HASHED)
221 .fromApp(APP_ID);
222 }
223
224 /**
225 * Checks the default values of a next objective object.
226 *
227 * @param objective next objective to check
228 */
229 private void checkNextBase(NextObjective objective) {
230 assertThat(objective.id(), is(12));
231 assertThat(objective.appId(), is(APP_ID));
232 assertThat(objective.type(), is(HASHED));
233 assertThat(objective.next(), hasItem(treatment));
234 }
235
236 /**
237 * Tests that forwarding objective objects are built correctly using the
238 * add() method.
239 */
240 @Test
241 public void testNextAdd() {
242 checkNextBase(baseNextBuilder().add());
243 }
244
245 /**
246 * Tests that forwarding objective objects are built correctly using the
247 * add(context) method.
248 */
249 @Test
250 public void testNextAddWithContext() {
251 ObjectiveContext context = new MockObjectiveContext();
252 checkNextBase(baseNextBuilder().add(context));
253 }
254
255 /**
256 * Tests that forwarding objective objects are built correctly using the
257 * remove() method.
258 */
259 @Test
260 public void testNextRemove() {
261 checkNextBase(baseNextBuilder().remove());
262 }
263
264 /**
265 * Tests that forwarding objective objects are built correctly using the
266 * remove(context) method.
267 */
268 @Test
269 public void testNextRemoveWithContext() {
270 ObjectiveContext context = new MockObjectiveContext();
271 checkNextBase(baseNextBuilder().remove(context));
272 }
273}