Add Unit tests for some org.onlab.onos.net.flow classes
Change-Id: Iac13ce237245d08e42a25cd8e01dee6a30ee2b20
diff --git a/core/api/src/test/java/org/onlab/onos/net/flow/BatchOperationTest.java b/core/api/src/test/java/org/onlab/onos/net/flow/BatchOperationTest.java
new file mode 100644
index 0000000..7cdb1be
--- /dev/null
+++ b/core/api/src/test/java/org/onlab/onos/net/flow/BatchOperationTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.onos.net.flow;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Test;
+
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
+
+/**
+ * Unit tests for the BatchOperationTest object.
+ */
+public class BatchOperationTest {
+
+ private enum TestType {
+ OP1,
+ OP2,
+ OP3
+ }
+
+ final TestEntry entry1 = new TestEntry(TestType.OP1, new TestTarget(1));
+ final TestEntry entry2 = new TestEntry(TestType.OP2, new TestTarget(2));
+
+
+ private static final class TestTarget implements BatchOperationTarget {
+ private int id;
+
+ private TestTarget(int id) {
+ this.id = id;
+ }
+
+ @Override
+ public int hashCode() {
+ return id;
+ }
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+
+ if (o == null) {
+ return false;
+ }
+
+ if (getClass() != o.getClass()) {
+ return false;
+ }
+ TestTarget that = (TestTarget) o;
+ return this.id == that.id;
+ }
+
+ }
+
+ private static final class TestEntry extends BatchOperationEntry<TestType, TestTarget> {
+ public TestEntry(TestType operator, TestTarget target) {
+ super(operator, target);
+ }
+ }
+
+ private static final class TestOperation extends BatchOperation<TestEntry> {
+ private TestOperation() {
+ super();
+ }
+
+ private TestOperation(Collection<TestEntry> batchOperations) {
+ super(batchOperations);
+ }
+ }
+
+ /**
+ * Checks that the DefaultFlowRule class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutableBaseClass(BatchOperation.class);
+ }
+
+ /**
+ * Tests the equals(), hashCode() and toString() operations.
+ */
+ @Test
+ public void testEquals() {
+ final List<TestEntry> ops1 = new LinkedList<>();
+ ops1.add(entry1);
+ final List<TestEntry> ops2 = new LinkedList<>();
+ ops2.add(entry2);
+
+ final TestOperation op1 = new TestOperation(ops1);
+ final TestOperation sameAsOp1 = new TestOperation(ops1);
+ final TestOperation op2 = new TestOperation(ops2);
+
+ new EqualsTester()
+ .addEqualityGroup(op1, sameAsOp1)
+ .addEqualityGroup(op2)
+ .testEquals();
+ }
+
+ /**
+ * Tests the constructors for a BatchOperation.
+ */
+ @Test
+ public void testConstruction() {
+ final List<TestEntry> ops = new LinkedList<>();
+ ops.add(entry2);
+
+ final TestOperation op1 = new TestOperation();
+ assertThat(op1.size(), is(0));
+ assertThat(op1.getOperations(), hasSize(0));
+
+ final TestOperation op2 = new TestOperation(ops);
+ op1.addOperation(entry1);
+ op1.addAll(op2);
+ assertThat(op1.size(), is(2));
+ assertThat(op1.getOperations(), hasSize(2));
+
+ op2.clear();
+ assertThat(op2.size(), is(0));
+ assertThat(op2.getOperations(), hasSize(0));
+ }
+
+ /**
+ * Tests the constructor for BatchOperationEntries.
+ */
+ @Test
+ public void testEntryConstruction() {
+ final TestEntry entry = new TestEntry(TestType.OP3, new TestTarget(3));
+
+ assertThat(entry.getOperator(), is(TestType.OP3));
+ assertThat(entry.getTarget().id, is(3));
+ }
+}
diff --git a/core/api/src/test/java/org/onlab/onos/net/flow/DefaultTrafficSelectorTest.java b/core/api/src/test/java/org/onlab/onos/net/flow/DefaultTrafficSelectorTest.java
new file mode 100644
index 0000000..b3e2acf
--- /dev/null
+++ b/core/api/src/test/java/org/onlab/onos/net/flow/DefaultTrafficSelectorTest.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.onos.net.flow;
+
+import java.util.Set;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.junit.Test;
+import org.onlab.onos.net.PortNumber;
+import org.onlab.onos.net.flow.criteria.Criteria;
+import org.onlab.onos.net.flow.criteria.Criterion;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.onlab.onos.net.flow.criteria.Criterion.Type;
+
+/**
+ * Unit tests for default traffic selector class.
+ */
+public class DefaultTrafficSelectorTest {
+
+ /**
+ * Checks that the DefaultFlowRule class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultTrafficSelector.class);
+ }
+
+ /**
+ * Tests equals(), hashCode() and toString() methods.
+ */
+ @Test
+ public void testEquals() {
+ final short one = 1;
+ final short two = 2;
+
+ final TrafficSelector selector1 =
+ DefaultTrafficSelector.builder().matchLambda(one).build();
+ final TrafficSelector sameAsSelector1 =
+ DefaultTrafficSelector.builder().matchLambda(one).build();
+ final TrafficSelector selector2 =
+ DefaultTrafficSelector.builder().matchLambda(two).build();
+
+ new EqualsTester()
+ .addEqualityGroup(selector1, sameAsSelector1)
+ .addEqualityGroup(selector2)
+ .testEquals();
+ }
+
+ /**
+ * Hamcrest matcher to check that a collection of Intents contains an
+ * Intent with the specified Intent Id.
+ */
+ public static final class CriterionExistsMatcher
+ extends TypeSafeMatcher<TrafficSelector> {
+ private final Criterion.Type type;
+
+ public CriterionExistsMatcher(Criterion.Type typeValue) {
+ type = typeValue;
+ }
+
+ @Override
+ public boolean matchesSafely(TrafficSelector selector) {
+ final Set<Criterion> criteria = selector.criteria();
+
+ return notNullValue().matches(criteria) &&
+ hasSize(1).matches(criteria) &&
+ notNullValue().matches(selector.getCriterion(type));
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a criterion with type \" ").
+ appendText(type.toString()).
+ appendText("\"");
+ }
+ }
+
+
+ /**
+ * Factory method to create a criterion type matcher. Returns a matcher
+ * for a criterion with the given type.
+ *
+ * @param type type of Criterion to match
+ * @return Matcher object
+ */
+ @Factory
+ public static Matcher<TrafficSelector> hasCriterionWithType(Criterion.Type type) {
+ return new CriterionExistsMatcher(type);
+ }
+
+
+ /**
+ * Tests the builder functions that add specific criteria.
+ */
+ @Test
+ public void testCriteriaCreation() {
+ TrafficSelector selector;
+
+ final short shortValue = 33;
+ final byte byteValue = 44;
+ final IpPrefix ipPrefixValue = IpPrefix.valueOf("192.168.1.0/24");
+
+ selector = DefaultTrafficSelector.builder()
+ .matchInport(PortNumber.portNumber(11)).build();
+ assertThat(selector, hasCriterionWithType(Type.IN_PORT));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchEthSrc(MacAddress.BROADCAST).build();
+ assertThat(selector, hasCriterionWithType(Type.ETH_SRC));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchEthDst(MacAddress.BROADCAST).build();
+ assertThat(selector, hasCriterionWithType(Type.ETH_DST));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchEthType(shortValue).build();
+ assertThat(selector, hasCriterionWithType(Type.ETH_TYPE));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchVlanId(VlanId.vlanId(shortValue)).build();
+ assertThat(selector, hasCriterionWithType(Type.VLAN_VID));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchVlanPcp(byteValue).build();
+ assertThat(selector, hasCriterionWithType(Type.VLAN_PCP));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchIPProtocol(byteValue).build();
+ assertThat(selector, hasCriterionWithType(Type.IP_PROTO));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchIPSrc(ipPrefixValue).build();
+ assertThat(selector, hasCriterionWithType(Type.IPV4_SRC));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchIPDst(ipPrefixValue).build();
+ assertThat(selector, hasCriterionWithType(Type.IPV4_DST));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchTcpSrc(shortValue).build();
+ assertThat(selector, hasCriterionWithType(Type.TCP_SRC));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchTcpDst(shortValue).build();
+ assertThat(selector, hasCriterionWithType(Type.TCP_DST));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchMplsLabel(3).build();
+ assertThat(selector, hasCriterionWithType(Type.MPLS_LABEL));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchLambda(shortValue).build();
+ assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
+
+ selector = DefaultTrafficSelector.builder()
+ .matchOpticalSignalType(shortValue).build();
+ assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
+
+ final TrafficSelector baseSelector = DefaultTrafficSelector.builder()
+ .matchOpticalSignalType(shortValue).build();
+ selector = DefaultTrafficSelector.builder(baseSelector)
+ .build();
+ assertThat(selector, hasCriterionWithType(Type.OCH_SIGTYPE));
+
+ final Criterion criterion = Criteria.matchLambda(shortValue);
+ selector = DefaultTrafficSelector.builder()
+ .add(criterion).build();
+ assertThat(selector, hasCriterionWithType(Type.OCH_SIGID));
+ }
+}
diff --git a/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleBatchOperationTest.java b/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleBatchOperationTest.java
new file mode 100644
index 0000000..2b8908b
--- /dev/null
+++ b/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleBatchOperationTest.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.onos.net.flow;
+
+import java.util.LinkedList;
+
+import org.junit.Test;
+import org.onlab.onos.net.intent.IntentTestsMocks;
+
+import com.google.common.testing.EqualsTester;
+
+/**
+ * Unit tests for flow rule batch classes.
+ */
+public class FlowRuleBatchOperationTest {
+
+ /**
+ * Tests the equals(), hashCode() and toString() methods.
+ */
+ @Test
+ public void testEquals() {
+ final FlowRule rule = new IntentTestsMocks.MockFlowRule(1);
+ final FlowRuleBatchEntry entry1 = new FlowRuleBatchEntry(
+ FlowRuleBatchEntry.FlowRuleOperation.ADD, rule);
+ final FlowRuleBatchEntry entry2 = new FlowRuleBatchEntry(
+ FlowRuleBatchEntry.FlowRuleOperation.MODIFY, rule);
+ final FlowRuleBatchEntry entry3 = new FlowRuleBatchEntry(
+ FlowRuleBatchEntry.FlowRuleOperation.REMOVE, rule);
+ final LinkedList<FlowRuleBatchEntry> ops1 = new LinkedList<>();
+ ops1.add(entry1);
+ final LinkedList<FlowRuleBatchEntry> ops2 = new LinkedList<>();
+ ops1.add(entry2);
+ final LinkedList<FlowRuleBatchEntry> ops3 = new LinkedList<>();
+ ops3.add(entry3);
+
+ final FlowRuleBatchOperation operation1 = new FlowRuleBatchOperation(ops1);
+ final FlowRuleBatchOperation sameAsOperation1 = new FlowRuleBatchOperation(ops1);
+ final FlowRuleBatchOperation operation2 = new FlowRuleBatchOperation(ops2);
+ final FlowRuleBatchOperation operation3 = new FlowRuleBatchOperation(ops3);
+
+ new EqualsTester()
+ .addEqualityGroup(operation1, sameAsOperation1)
+ .addEqualityGroup(operation2)
+ .addEqualityGroup(operation3)
+ .testEquals();
+ }
+}
diff --git a/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleBatchRequestTest.java b/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleBatchRequestTest.java
new file mode 100644
index 0000000..7541692
--- /dev/null
+++ b/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleBatchRequestTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.onos.net.flow;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Test;
+import org.onlab.onos.net.intent.IntentTestsMocks;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Unit tests for the FlowRuleBatchRequest class.
+ */
+public class FlowRuleBatchRequestTest {
+
+ /**
+ * Tests that construction of FlowRuleBatchRequest objects returns the
+ * correct objects.
+ */
+ @Test
+ public void testConstruction() {
+ final FlowRule rule1 = new IntentTestsMocks.MockFlowRule(1);
+ final FlowRule rule2 = new IntentTestsMocks.MockFlowRule(2);
+ final List<FlowRule> toAdd = new LinkedList<>();
+ toAdd.add(rule1);
+ final List<FlowRule> toRemove = new LinkedList<>();
+ toRemove.add(rule2);
+
+
+ final FlowRuleBatchRequest request =
+ new FlowRuleBatchRequest(1, toAdd, toRemove);
+
+ assertThat(request.toAdd(), hasSize(1));
+ assertThat(request.toAdd().get(0), is(rule1));
+ assertThat(request.toRemove(), hasSize(1));
+ assertThat(request.toRemove().get(0), is(rule2));
+ assertThat(request.batchId(), is(1));
+
+ final FlowRuleBatchOperation op = request.asBatchOperation();
+ assertThat(op.size(), is(2));
+
+ final List<FlowRuleBatchEntry> ops = op.getOperations();
+ assertThat(ops, hasSize(2));
+ }
+
+}
diff --git a/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleEventTest.java b/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleEventTest.java
index 90cd0c0..2fc1ff8 100644
--- a/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleEventTest.java
+++ b/core/api/src/test/java/org/onlab/onos/net/flow/FlowRuleEventTest.java
@@ -15,6 +15,8 @@
*/
package org.onlab.onos.net.flow;
+import java.util.concurrent.TimeUnit;
+
import org.junit.Test;
import org.onlab.onos.event.AbstractEventTest;
import org.onlab.onos.net.intent.IntentTestsMocks;
@@ -68,7 +70,8 @@
final long time = System.currentTimeMillis();
final FlowRule flowRule = new IntentTestsMocks.MockFlowRule(1);
final FlowRuleEvent event =
- new FlowRuleEvent(FlowRuleEvent.Type.RULE_UPDATED, flowRule, time);
- validateEvent(event, FlowRuleEvent.Type.RULE_UPDATED, flowRule, time);
+ new FlowRuleEvent(FlowRuleEvent.Type.RULE_UPDATED, flowRule);
+ validateEvent(event, FlowRuleEvent.Type.RULE_UPDATED, flowRule, time,
+ time + TimeUnit.SECONDS.toMillis(30));
}
}