Ray Milkey
Committed by Gerrit Code Review

Add Unit tests for some org.onlab.onos.net.flow classes

Change-Id: Iac13ce237245d08e42a25cd8e01dee6a30ee2b20
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.Collection;
19 +import java.util.LinkedList;
20 +import java.util.List;
21 +
22 +import org.junit.Test;
23 +
24 +import com.google.common.testing.EqualsTester;
25 +
26 +import static org.hamcrest.MatcherAssert.assertThat;
27 +import static org.hamcrest.Matchers.hasSize;
28 +import static org.hamcrest.Matchers.is;
29 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
30 +
31 +/**
32 + * Unit tests for the BatchOperationTest object.
33 + */
34 +public class BatchOperationTest {
35 +
36 + private enum TestType {
37 + OP1,
38 + OP2,
39 + OP3
40 + }
41 +
42 + final TestEntry entry1 = new TestEntry(TestType.OP1, new TestTarget(1));
43 + final TestEntry entry2 = new TestEntry(TestType.OP2, new TestTarget(2));
44 +
45 +
46 + private static final class TestTarget implements BatchOperationTarget {
47 + private int id;
48 +
49 + private TestTarget(int id) {
50 + this.id = id;
51 + }
52 +
53 + @Override
54 + public int hashCode() {
55 + return id;
56 + }
57 + @Override
58 + public boolean equals(Object o) {
59 + if (this == o) {
60 + return true;
61 + }
62 +
63 + if (o == null) {
64 + return false;
65 + }
66 +
67 + if (getClass() != o.getClass()) {
68 + return false;
69 + }
70 + TestTarget that = (TestTarget) o;
71 + return this.id == that.id;
72 + }
73 +
74 + }
75 +
76 + private static final class TestEntry extends BatchOperationEntry<TestType, TestTarget> {
77 + public TestEntry(TestType operator, TestTarget target) {
78 + super(operator, target);
79 + }
80 + }
81 +
82 + private static final class TestOperation extends BatchOperation<TestEntry> {
83 + private TestOperation() {
84 + super();
85 + }
86 +
87 + private TestOperation(Collection<TestEntry> batchOperations) {
88 + super(batchOperations);
89 + }
90 + }
91 +
92 + /**
93 + * Checks that the DefaultFlowRule class is immutable.
94 + */
95 + @Test
96 + public void testImmutability() {
97 + assertThatClassIsImmutableBaseClass(BatchOperation.class);
98 + }
99 +
100 + /**
101 + * Tests the equals(), hashCode() and toString() operations.
102 + */
103 + @Test
104 + public void testEquals() {
105 + final List<TestEntry> ops1 = new LinkedList<>();
106 + ops1.add(entry1);
107 + final List<TestEntry> ops2 = new LinkedList<>();
108 + ops2.add(entry2);
109 +
110 + final TestOperation op1 = new TestOperation(ops1);
111 + final TestOperation sameAsOp1 = new TestOperation(ops1);
112 + final TestOperation op2 = new TestOperation(ops2);
113 +
114 + new EqualsTester()
115 + .addEqualityGroup(op1, sameAsOp1)
116 + .addEqualityGroup(op2)
117 + .testEquals();
118 + }
119 +
120 + /**
121 + * Tests the constructors for a BatchOperation.
122 + */
123 + @Test
124 + public void testConstruction() {
125 + final List<TestEntry> ops = new LinkedList<>();
126 + ops.add(entry2);
127 +
128 + final TestOperation op1 = new TestOperation();
129 + assertThat(op1.size(), is(0));
130 + assertThat(op1.getOperations(), hasSize(0));
131 +
132 + final TestOperation op2 = new TestOperation(ops);
133 + op1.addOperation(entry1);
134 + op1.addAll(op2);
135 + assertThat(op1.size(), is(2));
136 + assertThat(op1.getOperations(), hasSize(2));
137 +
138 + op2.clear();
139 + assertThat(op2.size(), is(0));
140 + assertThat(op2.getOperations(), hasSize(0));
141 + }
142 +
143 + /**
144 + * Tests the constructor for BatchOperationEntries.
145 + */
146 + @Test
147 + public void testEntryConstruction() {
148 + final TestEntry entry = new TestEntry(TestType.OP3, new TestTarget(3));
149 +
150 + assertThat(entry.getOperator(), is(TestType.OP3));
151 + assertThat(entry.getTarget().id, is(3));
152 + }
153 +}
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.Set;
19 +
20 +import org.hamcrest.Description;
21 +import org.hamcrest.Factory;
22 +import org.hamcrest.Matcher;
23 +import org.hamcrest.TypeSafeMatcher;
24 +import org.junit.Test;
25 +import org.onlab.onos.net.PortNumber;
26 +import org.onlab.onos.net.flow.criteria.Criteria;
27 +import org.onlab.onos.net.flow.criteria.Criterion;
28 +import org.onlab.packet.IpPrefix;
29 +import org.onlab.packet.MacAddress;
30 +import org.onlab.packet.VlanId;
31 +
32 +import com.google.common.testing.EqualsTester;
33 +
34 +import static org.hamcrest.MatcherAssert.assertThat;
35 +import static org.hamcrest.Matchers.hasSize;
36 +import static org.hamcrest.Matchers.notNullValue;
37 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
38 +import static org.onlab.onos.net.flow.criteria.Criterion.Type;
39 +
40 +/**
41 + * Unit tests for default traffic selector class.
42 + */
43 +public class DefaultTrafficSelectorTest {
44 +
45 + /**
46 + * Checks that the DefaultFlowRule class is immutable.
47 + */
48 + @Test
49 + public void testImmutability() {
50 + assertThatClassIsImmutable(DefaultTrafficSelector.class);
51 + }
52 +
53 + /**
54 + * Tests equals(), hashCode() and toString() methods.
55 + */
56 + @Test
57 + public void testEquals() {
58 + final short one = 1;
59 + final short two = 2;
60 +
61 + final TrafficSelector selector1 =
62 + DefaultTrafficSelector.builder().matchLambda(one).build();
63 + final TrafficSelector sameAsSelector1 =
64 + DefaultTrafficSelector.builder().matchLambda(one).build();
65 + final TrafficSelector selector2 =
66 + DefaultTrafficSelector.builder().matchLambda(two).build();
67 +
68 + new EqualsTester()
69 + .addEqualityGroup(selector1, sameAsSelector1)
70 + .addEqualityGroup(selector2)
71 + .testEquals();
72 + }
73 +
74 + /**
75 + * Hamcrest matcher to check that a collection of Intents contains an
76 + * Intent with the specified Intent Id.
77 + */
78 + public static final class CriterionExistsMatcher
79 + extends TypeSafeMatcher<TrafficSelector> {
80 + private final Criterion.Type type;
81 +
82 + public CriterionExistsMatcher(Criterion.Type typeValue) {
83 + type = typeValue;
84 + }
85 +
86 + @Override
87 + public boolean matchesSafely(TrafficSelector selector) {
88 + final Set<Criterion> criteria = selector.criteria();
89 +
90 + return notNullValue().matches(criteria) &&
91 + hasSize(1).matches(criteria) &&
92 + notNullValue().matches(selector.getCriterion(type));
93 + }
94 +
95 + @Override
96 + public void describeTo(Description description) {
97 + description.appendText("a criterion with type \" ").
98 + appendText(type.toString()).
99 + appendText("\"");
100 + }
101 + }
102 +
103 +
104 + /**
105 + * Factory method to create a criterion type matcher. Returns a matcher
106 + * for a criterion with the given type.
107 + *
108 + * @param type type of Criterion to match
109 + * @return Matcher object
110 + */
111 + @Factory
112 + public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
113 + return new CriterionExistsMatcher(type);
114 + }
115 +
116 +
117 + /**
118 + * Tests the builder functions that add specific criteria.
119 + */
120 + @Test
121 + public void testCriteriaCreation() {
122 + TrafficSelector selector;
123 +
124 + final short shortValue = 33;
125 + final byte byteValue = 44;
126 + final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
127 +
128 + selector = DefaultTrafficSelector.builder()
129 + .matchInport(PortNumber.portNumber(11)).build();
130 + assertThat(selector, hasCriterionWithType(Type.IN_PORT));
131 +
132 + selector = DefaultTrafficSelector.builder()
133 + .matchEthSrc(MacAddress.BROADCAST).build();
134 + assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
135 +
136 + selector = DefaultTrafficSelector.builder()
137 + .matchEthDst(MacAddress.BROADCAST).build();
138 + assertThat(selector, hasCriterionWithType(Type.ETH_DST));
139 +
140 + selector = DefaultTrafficSelector.builder()
141 + .matchEthType(shortValue).build();
142 + assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
143 +
144 + selector = DefaultTrafficSelector.builder()
145 + .matchVlanId(VlanId.vlanId(shortValue)).build();
146 + assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
147 +
148 + selector = DefaultTrafficSelector.builder()
149 + .matchVlanPcp(byteValue).build();
150 + assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
151 +
152 + selector = DefaultTrafficSelector.builder()
153 + .matchIPProtocol(byteValue).build();
154 + assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
155 +
156 + selector = DefaultTrafficSelector.builder()
157 + .matchIPSrc(ipPrefixValue).build();
158 + assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
159 +
160 + selector = DefaultTrafficSelector.builder()
161 + .matchIPDst(ipPrefixValue).build();
162 + assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
163 +
164 + selector = DefaultTrafficSelector.builder()
165 + .matchTcpSrc(shortValue).build();
166 + assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
167 +
168 + selector = DefaultTrafficSelector.builder()
169 + .matchTcpDst(shortValue).build();
170 + assertThat(selector, hasCriterionWithType(Type.TCP_DST));
171 +
172 + selector = DefaultTrafficSelector.builder()
173 + .matchMplsLabel(3).build();
174 + assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
175 +
176 + selector = DefaultTrafficSelector.builder()
177 + .matchLambda(shortValue).build();
178 + assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
179 +
180 + selector = DefaultTrafficSelector.builder()
181 + .matchOpticalSignalType(shortValue).build();
182 + assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
183 +
184 + final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
185 + .matchOpticalSignalType(shortValue).build();
186 + selector = DefaultTrafficSelector.builder(baseSelector)
187 + .build();
188 + assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
189 +
190 + final Criterion criterion = Criteria.matchLambda(shortValue);
191 + selector = DefaultTrafficSelector.builder()
192 + .add(criterion).build();
193 + assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
194 + }
195 +}
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.LinkedList;
19 +
20 +import org.junit.Test;
21 +import org.onlab.onos.net.intent.IntentTestsMocks;
22 +
23 +import com.google.common.testing.EqualsTester;
24 +
25 +/**
26 + * Unit tests for flow rule batch classes.
27 + */
28 +public class FlowRuleBatchOperationTest {
29 +
30 + /**
31 + * Tests the equals(), hashCode() and toString() methods.
32 + */
33 + @Test
34 + public void testEquals() {
35 + final FlowRule rule = new IntentTestsMocks.MockFlowRule(1);
36 + final FlowRuleBatchEntry entry1 = new FlowRuleBatchEntry(
37 + FlowRuleBatchEntry.FlowRuleOperation.ADD, rule);
38 + final FlowRuleBatchEntry entry2 = new FlowRuleBatchEntry(
39 + FlowRuleBatchEntry.FlowRuleOperation.MODIFY, rule);
40 + final FlowRuleBatchEntry entry3 = new FlowRuleBatchEntry(
41 + FlowRuleBatchEntry.FlowRuleOperation.REMOVE, rule);
42 + final LinkedList<FlowRuleBatchEntry> ops1 = new LinkedList<>();
43 + ops1.add(entry1);
44 + final LinkedList<FlowRuleBatchEntry> ops2 = new LinkedList<>();
45 + ops1.add(entry2);
46 + final LinkedList<FlowRuleBatchEntry> ops3 = new LinkedList<>();
47 + ops3.add(entry3);
48 +
49 + final FlowRuleBatchOperation operation1 = new FlowRuleBatchOperation(ops1);
50 + final FlowRuleBatchOperation sameAsOperation1 = new FlowRuleBatchOperation(ops1);
51 + final FlowRuleBatchOperation operation2 = new FlowRuleBatchOperation(ops2);
52 + final FlowRuleBatchOperation operation3 = new FlowRuleBatchOperation(ops3);
53 +
54 + new EqualsTester()
55 + .addEqualityGroup(operation1, sameAsOperation1)
56 + .addEqualityGroup(operation2)
57 + .addEqualityGroup(operation3)
58 + .testEquals();
59 + }
60 +}
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.LinkedList;
19 +import java.util.List;
20 +
21 +import org.junit.Test;
22 +import org.onlab.onos.net.intent.IntentTestsMocks;
23 +
24 +import static org.hamcrest.MatcherAssert.assertThat;
25 +import static org.hamcrest.Matchers.hasSize;
26 +import static org.hamcrest.Matchers.is;
27 +
28 +/**
29 + * Unit tests for the FlowRuleBatchRequest class.
30 + */
31 +public class FlowRuleBatchRequestTest {
32 +
33 + /**
34 + * Tests that construction of FlowRuleBatchRequest objects returns the
35 + * correct objects.
36 + */
37 + @Test
38 + public void testConstruction() {
39 + final FlowRule rule1 = new IntentTestsMocks.MockFlowRule(1);
40 + final FlowRule rule2 = new IntentTestsMocks.MockFlowRule(2);
41 + final List<FlowRule> toAdd = new LinkedList<>();
42 + toAdd.add(rule1);
43 + final List<FlowRule> toRemove = new LinkedList<>();
44 + toRemove.add(rule2);
45 +
46 +
47 + final FlowRuleBatchRequest request =
48 + new FlowRuleBatchRequest(1, toAdd, toRemove);
49 +
50 + assertThat(request.toAdd(), hasSize(1));
51 + assertThat(request.toAdd().get(0), is(rule1));
52 + assertThat(request.toRemove(), hasSize(1));
53 + assertThat(request.toRemove().get(0), is(rule2));
54 + assertThat(request.batchId(), is(1));
55 +
56 + final FlowRuleBatchOperation op = request.asBatchOperation();
57 + assertThat(op.size(), is(2));
58 +
59 + final List<FlowRuleBatchEntry> ops = op.getOperations();
60 + assertThat(ops, hasSize(2));
61 + }
62 +
63 +}
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onlab.onos.net.flow; 16 package org.onlab.onos.net.flow;
17 17
18 +import java.util.concurrent.TimeUnit;
19 +
18 import org.junit.Test; 20 import org.junit.Test;
19 import org.onlab.onos.event.AbstractEventTest; 21 import org.onlab.onos.event.AbstractEventTest;
20 import org.onlab.onos.net.intent.IntentTestsMocks; 22 import org.onlab.onos.net.intent.IntentTestsMocks;
...@@ -68,7 +70,8 @@ public class FlowRuleEventTest extends AbstractEventTest { ...@@ -68,7 +70,8 @@ public class FlowRuleEventTest extends AbstractEventTest {
68 final long time = System.currentTimeMillis(); 70 final long time = System.currentTimeMillis();
69 final FlowRule flowRule = new IntentTestsMocks.MockFlowRule(1); 71 final FlowRule flowRule = new IntentTestsMocks.MockFlowRule(1);
70 final FlowRuleEvent event = 72 final FlowRuleEvent event =
71 - new FlowRuleEvent(FlowRuleEvent.Type.RULE_UPDATED, flowRule, time); 73 + new FlowRuleEvent(FlowRuleEvent.Type.RULE_UPDATED, flowRule);
72 - validateEvent(event, FlowRuleEvent.Type.RULE_UPDATED, flowRule, time); 74 + validateEvent(event, FlowRuleEvent.Type.RULE_UPDATED, flowRule, time,
75 + time + TimeUnit.SECONDS.toMillis(30));
73 } 76 }
74 } 77 }
......