Thomas Vachuska
Committed by Gerrit Code Review

Merge "Add unit tests fir DefaultFlowEntry class"

...@@ -80,6 +80,7 @@ public class DefaultFlowEntry extends DefaultFlowRule ...@@ -80,6 +80,7 @@ public class DefaultFlowEntry extends DefaultFlowRule
80 this.state = FlowEntryState.FAILED; 80 this.state = FlowEntryState.FAILED;
81 this.errType = errType; 81 this.errType = errType;
82 this.errCode = errCode; 82 this.errCode = errCode;
83 + this.lastSeen = System.currentTimeMillis();
83 } 84 }
84 85
85 @Override 86 @Override
......
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 +package org.onlab.onos.net.flow;
17 +
18 +import java.util.concurrent.TimeUnit;
19 +
20 +import org.junit.Test;
21 +import org.onlab.onos.net.intent.IntentTestsMocks;
22 +
23 +import com.google.common.testing.EqualsTester;
24 +
25 +import static org.hamcrest.MatcherAssert.assertThat;
26 +import static org.hamcrest.Matchers.greaterThan;
27 +import static org.hamcrest.Matchers.is;
28 +import static org.onlab.onos.net.NetTestTools.did;
29 +
30 +/**
31 + * Unit tests for the DefaultFlowEntry class.
32 + */
33 +public class DefaultFlowEntryTest {
34 + private static final IntentTestsMocks.MockSelector SELECTOR =
35 + new IntentTestsMocks.MockSelector();
36 + private static final IntentTestsMocks.MockTreatment TREATMENT =
37 + new IntentTestsMocks.MockTreatment();
38 +
39 + private static DefaultFlowEntry makeFlowEntry(int uniqueValue) {
40 + return new DefaultFlowEntry(did("id" + Integer.toString(uniqueValue)),
41 + SELECTOR,
42 + TREATMENT,
43 + uniqueValue,
44 + FlowEntry.FlowEntryState.ADDED,
45 + uniqueValue,
46 + uniqueValue,
47 + uniqueValue,
48 + uniqueValue,
49 + uniqueValue);
50 + }
51 +
52 + final DefaultFlowEntry defaultFlowEntry1 = makeFlowEntry(1);
53 + final DefaultFlowEntry sameAsDefaultFlowEntry1 = makeFlowEntry(1);
54 + final DefaultFlowEntry defaultFlowEntry2 = makeFlowEntry(2);
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(defaultFlowEntry1, sameAsDefaultFlowEntry1)
63 + .addEqualityGroup(defaultFlowEntry2)
64 + .testEquals();
65 + }
66 +
67 + /**
68 + * Tests the construction of a default flow entry from a device id.
69 + */
70 + @Test
71 + public void testDeviceBasedObject() {
72 + assertThat(defaultFlowEntry1.deviceId(), is(did("id1")));
73 + assertThat(defaultFlowEntry1.selector(), is(SELECTOR));
74 + assertThat(defaultFlowEntry1.treatment(), is(TREATMENT));
75 + assertThat(defaultFlowEntry1.timeout(), is(1));
76 + assertThat(defaultFlowEntry1.life(), is(1L));
77 + assertThat(defaultFlowEntry1.packets(), is(1L));
78 + assertThat(defaultFlowEntry1.bytes(), is(1L));
79 + assertThat(defaultFlowEntry1.state(), is(FlowEntry.FlowEntryState.ADDED));
80 + assertThat(defaultFlowEntry1.lastSeen(),
81 + greaterThan(System.currentTimeMillis() -
82 + TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
83 + }
84 +
85 + /**
86 + * Tests the setters on a default flow entry object.
87 + */
88 + @Test
89 + public void testSetters() {
90 + final DefaultFlowEntry entry = makeFlowEntry(1);
91 +
92 + entry.setLastSeen();
93 + entry.setState(FlowEntry.FlowEntryState.PENDING_REMOVE);
94 + entry.setPackets(11);
95 + entry.setBytes(22);
96 + entry.setLife(33);
97 +
98 + assertThat(entry.deviceId(), is(did("id1")));
99 + assertThat(entry.selector(), is(SELECTOR));
100 + assertThat(entry.treatment(), is(TREATMENT));
101 + assertThat(entry.timeout(), is(1));
102 + assertThat(entry.life(), is(33L));
103 + assertThat(entry.packets(), is(11L));
104 + assertThat(entry.bytes(), is(22L));
105 + assertThat(entry.state(), is(FlowEntry.FlowEntryState.PENDING_REMOVE));
106 + assertThat(entry.lastSeen(),
107 + greaterThan(System.currentTimeMillis() -
108 + TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
109 + }
110 +
111 + /**
112 + * Tests a default flow rule built for an error.
113 + */
114 + @Test
115 + public void testErrorObject() {
116 + final DefaultFlowEntry errorEntry =
117 + new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1),
118 + 111,
119 + 222);
120 + assertThat(errorEntry.errType(), is(111));
121 + assertThat(errorEntry.errCode(), is(222));
122 + assertThat(errorEntry.state(), is(FlowEntry.FlowEntryState.FAILED));
123 + assertThat(errorEntry.lastSeen(),
124 + greaterThan(System.currentTimeMillis() -
125 + TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
126 + }
127 +
128 + /**
129 + * Tests a default flow entry constructed from a flow rule.
130 + */
131 + @Test
132 + public void testFlowBasedObject() {
133 + final DefaultFlowEntry entry =
134 + new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1));
135 + assertThat(entry.priority(), is(1));
136 + assertThat(entry.appId(), is((short) 0));
137 + assertThat(entry.lastSeen(),
138 + greaterThan(System.currentTimeMillis() -
139 + TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
140 + }
141 +
142 + /**
143 + * Tests a default flow entry constructed from a flow rule plus extra
144 + * parameters.
145 + */
146 + @Test
147 + public void testFlowBasedObjectWithParameters() {
148 + final DefaultFlowEntry entry =
149 + new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(33),
150 + FlowEntry.FlowEntryState.REMOVED,
151 + 101, 102, 103);
152 + assertThat(entry.state(), is(FlowEntry.FlowEntryState.REMOVED));
153 + assertThat(entry.life(), is(101L));
154 + assertThat(entry.packets(), is(102L));
155 + assertThat(entry.bytes(), is(103L));
156 + assertThat(entry.lastSeen(),
157 + greaterThan(System.currentTimeMillis() -
158 + TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
159 + }
160 +}
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
17 package org.onlab.onos.net.flow; 17 package org.onlab.onos.net.flow;
18 18
19 import org.junit.Test; 19 import org.junit.Test;
20 -import org.onlab.onos.net.DeviceId;
21 import org.onlab.onos.net.intent.IntentTestsMocks; 20 import org.onlab.onos.net.intent.IntentTestsMocks;
22 21
23 import com.google.common.testing.EqualsTester; 22 import com.google.common.testing.EqualsTester;
...@@ -25,8 +24,8 @@ import com.google.common.testing.EqualsTester; ...@@ -25,8 +24,8 @@ import com.google.common.testing.EqualsTester;
25 import static org.hamcrest.MatcherAssert.assertThat; 24 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is; 25 import static org.hamcrest.Matchers.is;
27 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; 26 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
28 -import static org.onlab.onos.net.NetTestTools.did;
29 import static org.onlab.onos.net.NetTestTools.APP_ID; 27 import static org.onlab.onos.net.NetTestTools.APP_ID;
28 +import static org.onlab.onos.net.NetTestTools.did;
30 29
31 /** 30 /**
32 * Unit tests for the default flow rule class. 31 * Unit tests for the default flow rule class.
...@@ -37,63 +36,13 @@ public class DefaultFlowRuleTest { ...@@ -37,63 +36,13 @@ public class DefaultFlowRuleTest {
37 private static final IntentTestsMocks.MockTreatment TREATMENT = 36 private static final IntentTestsMocks.MockTreatment TREATMENT =
38 new IntentTestsMocks.MockTreatment(); 37 new IntentTestsMocks.MockTreatment();
39 38
40 - final FlowRule flowRule1 = new MockFlowRule(1); 39 + final FlowRule flowRule1 = new IntentTestsMocks.MockFlowRule(1);
41 - final FlowRule sameAsFlowRule1 = new MockFlowRule(1); 40 + final FlowRule sameAsFlowRule1 = new IntentTestsMocks.MockFlowRule(1);
42 - final FlowRule flowRule2 = new MockFlowRule(2); 41 + final FlowRule flowRule2 = new IntentTestsMocks.MockFlowRule(2);
43 final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1); 42 final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1);
44 final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1); 43 final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1);
45 final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2); 44 final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2);
46 45
47 - private static class MockFlowRule implements FlowRule {
48 -
49 - int priority;
50 - MockFlowRule(int priority) {
51 - this.priority = priority;
52 - }
53 -
54 - @Override
55 - public FlowId id() {
56 - return FlowId.valueOf(1);
57 - }
58 -
59 - @Override
60 - public short appId() {
61 - return 0;
62 - }
63 -
64 - @Override
65 - public int priority() {
66 - return priority;
67 - }
68 -
69 - @Override
70 - public DeviceId deviceId() {
71 - return did("1");
72 - }
73 -
74 - @Override
75 - public TrafficSelector selector() {
76 - return SELECTOR;
77 - }
78 -
79 - @Override
80 - public TrafficTreatment treatment() {
81 - return TREATMENT;
82 - }
83 -
84 - @Override
85 - public int timeout() {
86 - return 0;
87 - }
88 -
89 - @Override
90 - public boolean isPermanent() {
91 - return false;
92 - }
93 -
94 -
95 - }
96 -
97 /** 46 /**
98 * Checks that the DefaultFlowRule class is immutable but can be inherited 47 * Checks that the DefaultFlowRule class is immutable but can be inherited
99 * from. 48 * from.
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onlab.onos.net.intent; 16 package org.onlab.onos.net.intent;
17 17
18 import static org.onlab.onos.net.NetTestTools.createPath; 18 import static org.onlab.onos.net.NetTestTools.createPath;
19 +import static org.onlab.onos.net.NetTestTools.did;
19 import static org.onlab.onos.net.NetTestTools.link; 20 import static org.onlab.onos.net.NetTestTools.link;
20 21
21 import java.util.ArrayList; 22 import java.util.ArrayList;
...@@ -31,6 +32,8 @@ import org.onlab.onos.net.DeviceId; ...@@ -31,6 +32,8 @@ import org.onlab.onos.net.DeviceId;
31 import org.onlab.onos.net.ElementId; 32 import org.onlab.onos.net.ElementId;
32 import org.onlab.onos.net.Link; 33 import org.onlab.onos.net.Link;
33 import org.onlab.onos.net.Path; 34 import org.onlab.onos.net.Path;
35 +import org.onlab.onos.net.flow.FlowId;
36 +import org.onlab.onos.net.flow.FlowRule;
34 import org.onlab.onos.net.flow.TrafficSelector; 37 import org.onlab.onos.net.flow.TrafficSelector;
35 import org.onlab.onos.net.flow.TrafficTreatment; 38 import org.onlab.onos.net.flow.TrafficTreatment;
36 import org.onlab.onos.net.flow.criteria.Criterion; 39 import org.onlab.onos.net.flow.criteria.Criterion;
...@@ -271,4 +274,60 @@ public class IntentTestsMocks { ...@@ -271,4 +274,60 @@ public class IntentTestsMocks {
271 } 274 }
272 } 275 }
273 276
277 + private static final IntentTestsMocks.MockSelector SELECTOR =
278 + new IntentTestsMocks.MockSelector();
279 + private static final IntentTestsMocks.MockTreatment TREATMENT =
280 + new IntentTestsMocks.MockTreatment();
281 +
282 + public static class MockFlowRule implements FlowRule {
283 +
284 + int priority;
285 + public MockFlowRule(int priority) {
286 + this.priority = priority;
287 + }
288 +
289 + @Override
290 + public FlowId id() {
291 + return FlowId.valueOf(1);
292 + }
293 +
294 + @Override
295 + public short appId() {
296 + return 0;
297 + }
298 +
299 + @Override
300 + public int priority() {
301 + return priority;
302 + }
303 +
304 + @Override
305 + public DeviceId deviceId() {
306 + return did("1");
307 + }
308 +
309 + @Override
310 + public TrafficSelector selector() {
311 + return SELECTOR;
312 + }
313 +
314 + @Override
315 + public TrafficTreatment treatment() {
316 + return TREATMENT;
317 + }
318 +
319 + @Override
320 + public int timeout() {
321 + return 0;
322 + }
323 +
324 + @Override
325 + public boolean isPermanent() {
326 + return false;
327 + }
328 +
329 +
330 + }
331 +
332 +
274 } 333 }
......