Ray Milkey
Committed by Gerrit Code Review

Unit tests to improve coverage in intents module

Change-Id: Ic544114a8d3065157b0abd09632a2dc5ff8b708d
......@@ -23,7 +23,7 @@ import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Abstraction of an intent-related operation, e.g. add, remove, replace.
*/
public class IntentOperation {
public final class IntentOperation {
private final Type type;
private final IntentId intentId;
......
......@@ -15,14 +15,17 @@
*/
package org.onlab.onos.net.intent;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import com.google.common.collect.ImmutableList;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.onos.net.intent.IntentOperation.Type.*;
import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE;
import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT;
import static org.onlab.onos.net.intent.IntentOperation.Type.UPDATE;
import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW;
/**
* Batch of intent submit/withdraw/replace operations.
......
/*
* 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.intent;
import org.junit.Test;
import org.onlab.onos.net.HostId;
import org.onlab.onos.net.flow.TrafficSelector;
import com.google.common.testing.EqualsTester;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onlab.onos.net.NetTestTools.APP_ID;
import static org.onlab.onos.net.NetTestTools.hid;
/**
* Unit tests for the HostToHostIntent class.
*/
public class HostToHostIntentTest {
final TrafficSelector selector = new IntentTestsMocks.MockSelector();
final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
/**
* Checks that the HostToHostIntent class is immutable.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(HostToHostIntent.class);
}
/**
* Tests equals(), hashCode() and toString() methods.
*/
@Test
public void testEquals() {
final HostId id1 = hid("12:34:56:78:91:ab/1");
final HostId id2 = hid("12:34:56:78:92:ab/1");
final HostId id3 = hid("12:34:56:78:93:ab/1");
final HostToHostIntent intent1 = new HostToHostIntent(APP_ID,
id1,
id2,
selector,
treatment);
final HostToHostIntent sameAsIntent1 = new HostToHostIntent(APP_ID,
id1,
id2,
selector,
treatment);
final HostToHostIntent intent2 = new HostToHostIntent(APP_ID,
id2,
id3,
selector,
treatment);
new EqualsTester()
.addEqualityGroup(intent1, sameAsIntent1)
.addEqualityGroup(intent2)
.testEquals();
}
}
......@@ -17,6 +17,8 @@ package org.onlab.onos.net.intent;
import org.junit.Test;
import com.google.common.testing.EqualsTester;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
......@@ -64,4 +66,30 @@ public class IntentIdTest {
assertEquals("incorrect valueOf", id, IntentId.valueOf(0xdeadbeefL));
}
/**
* Tests the equals(), hashCode() and toString() methods.
*/
@Test
public void testEquals() {
final IntentId id1 = new IntentId(11111L);
final IntentId sameAsId1 = new IntentId(11111L);
final IntentId id2 = new IntentId(22222L);
new EqualsTester()
.addEqualityGroup(id1, sameAsId1)
.addEqualityGroup(id2)
.testEquals();
}
/**
* Tests construction of an IntentId object.
*/
@Test
public void testConstruction() {
final IntentId id1 = new IntentId(987654321L);
assertEquals(id1.fingerprint(), 987654321L);
final IntentId emptyId = new IntentId();
assertEquals(emptyId.fingerprint(), 0L);
}
}
......
/*
* 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.intent;
import java.util.List;
import org.junit.Test;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.NetTestTools;
import org.onlab.onos.net.flow.TrafficSelector;
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.hamcrest.Matchers.isOneOf;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Tests for the IntentOperations class.
*/
public class IntentOperationsTest {
final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 3);
final TrafficSelector selector = new IntentTestsMocks.MockSelector();
final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
final Intent intent = new PointToPointIntent(NetTestTools.APP_ID,
selector,
treatment,
ingress,
egress);
/**
* Checks that the IntentOperation and IntentOperations classes are immutable.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(IntentOperations.class);
assertThatClassIsImmutable(IntentOperations.Builder.class);
assertThatClassIsImmutable(IntentOperation.class);
}
/**
* Tests equals(), hashCode() and toString() methods.
*/
@Test
public void testEquals() {
final IntentOperations operations1 =
IntentOperations.builder()
.addSubmitOperation(intent)
.build();
final IntentOperations sameAsOperations1 =
IntentOperations.builder()
.addSubmitOperation(intent)
.build();
final IntentOperations operations2 =
IntentOperations.builder()
.addReplaceOperation(intent.id(), intent)
.build();
new EqualsTester()
.addEqualityGroup(operations1, sameAsOperations1)
.addEqualityGroup(operations2)
.testEquals();
}
/**
* Checks that objects are created correctly.
*/
@Test
public void testConstruction() {
final IntentOperations operations =
IntentOperations.builder()
.addUpdateOperation(intent.id())
.addWithdrawOperation(intent.id())
.build();
final List<IntentOperation> operationList = operations.operations();
assertThat(operationList, hasSize(2));
for (final IntentOperation operation : operationList) {
assertThat(operation.type(),
isOneOf(IntentOperation.Type.UPDATE,
IntentOperation.Type.WITHDRAW));
assertThat(operation.intent(), is((Intent) null));
assertThat(operation.intentId(), is(intent.id()));
}
}
}
/*
* 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.intent;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.NetTestTools;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.intent.constraint.LambdaConstraint;
import org.onlab.onos.net.resource.Lambda;
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.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
import static org.onlab.onos.net.NetTestTools.APP_ID;
import static org.onlab.onos.net.NetTestTools.link;
/**
* Unit tests for the LinkCollectionIntent class.
*/
public class LinkCollectionIntentTest {
final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
final TrafficSelector selector = new IntentTestsMocks.MockSelector();
final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
/**
* Checks that the LinkCollectionIntent class is immutable.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(LinkCollectionIntent.class);
}
/**
* Tests equals(), hashCode() and toString() methods.
*/
@Test
public void testEquals() {
final HashSet<Link> links1 = new HashSet<>();
links1.add(link("src", 1, "dst", 2));
final LinkCollectionIntent collectionIntent1 =
new LinkCollectionIntent(APP_ID,
selector,
treatment,
links1,
egress);
final LinkCollectionIntent sameAsCollectionIntent1 =
new LinkCollectionIntent(APP_ID,
selector,
treatment,
links1,
egress);
final HashSet<Link> links2 = new HashSet<>();
links2.add(link("src", 1, "dst", 3));
final LinkCollectionIntent collectionIntent2 =
new LinkCollectionIntent(APP_ID,
selector,
treatment,
links2,
egress);
new EqualsTester()
.addEqualityGroup(collectionIntent1, sameAsCollectionIntent1)
.addEqualityGroup(collectionIntent2)
.testEquals();
}
/**
* Tests constructor without constraints.
*/
@Test
public void testConstructor() {
final HashSet<Link> links1 = new HashSet<>();
links1.add(link("src", 1, "dst", 2));
final LinkCollectionIntent collectionIntent =
new LinkCollectionIntent(APP_ID,
selector,
treatment,
links1,
egress);
final Set<Link> createdLinks = collectionIntent.links();
assertThat(createdLinks, hasSize(1));
assertThat(collectionIntent.isInstallable(), is(true));
assertThat(collectionIntent.treatment(), is(treatment));
assertThat(collectionIntent.selector(), is(selector));
assertThat(collectionIntent.egressPoint(), is(egress));
assertThat(collectionIntent.resources(), hasSize(1));
final List<Constraint> createdConstraints = collectionIntent.constraints();
assertThat(createdConstraints, hasSize(0));
}
/**
* Tests constructor with constraints.
*/
@Test
public void testConstructorWithConstraints() {
final HashSet<Link> links1 = new HashSet<>();
final LinkedList<Constraint> constraints = new LinkedList<>();
links1.add(link("src", 1, "dst", 2));
constraints.add(new LambdaConstraint(Lambda.valueOf(23)));
final LinkCollectionIntent collectionIntent =
new LinkCollectionIntent(APP_ID,
selector,
treatment,
links1,
egress,
constraints);
final Set<Link> createdLinks = collectionIntent.links();
assertThat(createdLinks, hasSize(1));
assertThat(collectionIntent.isInstallable(), is(true));
assertThat(collectionIntent.treatment(), is(treatment));
assertThat(collectionIntent.selector(), is(selector));
assertThat(collectionIntent.egressPoint(), is(egress));
final List<Constraint> createdConstraints = collectionIntent.constraints();
assertThat(createdConstraints, hasSize(1));
assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint"));
}
/**
* Tests constructor with constraints.
*/
@Test
public void testSerializerConstructor() {
final LinkCollectionIntent collectionIntent =
new LinkCollectionIntent();
final Set<Link> createdLinks = collectionIntent.links();
assertThat(createdLinks, nullValue());
assertThat(collectionIntent.isInstallable(), is(true));
assertThat(collectionIntent.treatment(), nullValue());
assertThat(collectionIntent.selector(), nullValue());
assertThat(collectionIntent.egressPoint(), nullValue());
final List<Constraint> createdConstraints = collectionIntent.constraints();
assertThat(createdConstraints, hasSize(0));
}
}