Committed by
Gerrit Code Review
Unit tests to improve coverage in intents module
Change-Id: Ic544114a8d3065157b0abd09632a2dc5ff8b708d
Showing
6 changed files
with
380 additions
and
4 deletions
| ... | @@ -23,7 +23,7 @@ import static com.google.common.base.MoreObjects.toStringHelper; | ... | @@ -23,7 +23,7 @@ import static com.google.common.base.MoreObjects.toStringHelper; |
| 23 | /** | 23 | /** |
| 24 | * Abstraction of an intent-related operation, e.g. add, remove, replace. | 24 | * Abstraction of an intent-related operation, e.g. add, remove, replace. |
| 25 | */ | 25 | */ |
| 26 | -public class IntentOperation { | 26 | +public final class IntentOperation { |
| 27 | 27 | ||
| 28 | private final Type type; | 28 | private final Type type; |
| 29 | private final IntentId intentId; | 29 | private final IntentId intentId; | ... | ... |
| ... | @@ -15,14 +15,17 @@ | ... | @@ -15,14 +15,17 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onlab.onos.net.intent; | 16 | package org.onlab.onos.net.intent; |
| 17 | 17 | ||
| 18 | -import com.google.common.collect.ImmutableList; | ||
| 19 | - | ||
| 20 | import java.util.List; | 18 | import java.util.List; |
| 21 | import java.util.Objects; | 19 | import java.util.Objects; |
| 22 | 20 | ||
| 21 | +import com.google.common.collect.ImmutableList; | ||
| 22 | + | ||
| 23 | import static com.google.common.base.MoreObjects.toStringHelper; | 23 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; | 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | -import static org.onlab.onos.net.intent.IntentOperation.Type.*; | 25 | +import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE; |
| 26 | +import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT; | ||
| 27 | +import static org.onlab.onos.net.intent.IntentOperation.Type.UPDATE; | ||
| 28 | +import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW; | ||
| 26 | 29 | ||
| 27 | /** | 30 | /** |
| 28 | * Batch of intent submit/withdraw/replace operations. | 31 | * Batch of intent submit/withdraw/replace operations. | ... | ... |
| 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.intent; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | +import org.onlab.onos.net.HostId; | ||
| 20 | +import org.onlab.onos.net.flow.TrafficSelector; | ||
| 21 | + | ||
| 22 | +import com.google.common.testing.EqualsTester; | ||
| 23 | + | ||
| 24 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
| 25 | +import static org.onlab.onos.net.NetTestTools.APP_ID; | ||
| 26 | +import static org.onlab.onos.net.NetTestTools.hid; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * Unit tests for the HostToHostIntent class. | ||
| 30 | + */ | ||
| 31 | +public class HostToHostIntentTest { | ||
| 32 | + final TrafficSelector selector = new IntentTestsMocks.MockSelector(); | ||
| 33 | + final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Checks that the HostToHostIntent class is immutable. | ||
| 37 | + */ | ||
| 38 | + @Test | ||
| 39 | + public void testImmutability() { | ||
| 40 | + assertThatClassIsImmutable(HostToHostIntent.class); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Tests equals(), hashCode() and toString() methods. | ||
| 45 | + */ | ||
| 46 | + @Test | ||
| 47 | + public void testEquals() { | ||
| 48 | + final HostId id1 = hid("12:34:56:78:91:ab/1"); | ||
| 49 | + final HostId id2 = hid("12:34:56:78:92:ab/1"); | ||
| 50 | + final HostId id3 = hid("12:34:56:78:93:ab/1"); | ||
| 51 | + | ||
| 52 | + final HostToHostIntent intent1 = new HostToHostIntent(APP_ID, | ||
| 53 | + id1, | ||
| 54 | + id2, | ||
| 55 | + selector, | ||
| 56 | + treatment); | ||
| 57 | + final HostToHostIntent sameAsIntent1 = new HostToHostIntent(APP_ID, | ||
| 58 | + id1, | ||
| 59 | + id2, | ||
| 60 | + selector, | ||
| 61 | + treatment); | ||
| 62 | + final HostToHostIntent intent2 = new HostToHostIntent(APP_ID, | ||
| 63 | + id2, | ||
| 64 | + id3, | ||
| 65 | + selector, | ||
| 66 | + treatment); | ||
| 67 | + | ||
| 68 | + new EqualsTester() | ||
| 69 | + .addEqualityGroup(intent1, sameAsIntent1) | ||
| 70 | + .addEqualityGroup(intent2) | ||
| 71 | + .testEquals(); | ||
| 72 | + } | ||
| 73 | +} |
| ... | @@ -17,6 +17,8 @@ package org.onlab.onos.net.intent; | ... | @@ -17,6 +17,8 @@ package org.onlab.onos.net.intent; |
| 17 | 17 | ||
| 18 | import org.junit.Test; | 18 | import org.junit.Test; |
| 19 | 19 | ||
| 20 | +import com.google.common.testing.EqualsTester; | ||
| 21 | + | ||
| 20 | import static org.hamcrest.Matchers.is; | 22 | import static org.hamcrest.Matchers.is; |
| 21 | import static org.hamcrest.Matchers.not; | 23 | import static org.hamcrest.Matchers.not; |
| 22 | import static org.junit.Assert.assertEquals; | 24 | import static org.junit.Assert.assertEquals; |
| ... | @@ -64,4 +66,30 @@ public class IntentIdTest { | ... | @@ -64,4 +66,30 @@ public class IntentIdTest { |
| 64 | assertEquals("incorrect valueOf", id, IntentId.valueOf(0xdeadbeefL)); | 66 | assertEquals("incorrect valueOf", id, IntentId.valueOf(0xdeadbeefL)); |
| 65 | } | 67 | } |
| 66 | 68 | ||
| 69 | + /** | ||
| 70 | + * Tests the equals(), hashCode() and toString() methods. | ||
| 71 | + */ | ||
| 72 | + @Test | ||
| 73 | + public void testEquals() { | ||
| 74 | + final IntentId id1 = new IntentId(11111L); | ||
| 75 | + final IntentId sameAsId1 = new IntentId(11111L); | ||
| 76 | + final IntentId id2 = new IntentId(22222L); | ||
| 77 | + | ||
| 78 | + new EqualsTester() | ||
| 79 | + .addEqualityGroup(id1, sameAsId1) | ||
| 80 | + .addEqualityGroup(id2) | ||
| 81 | + .testEquals(); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Tests construction of an IntentId object. | ||
| 86 | + */ | ||
| 87 | + @Test | ||
| 88 | + public void testConstruction() { | ||
| 89 | + final IntentId id1 = new IntentId(987654321L); | ||
| 90 | + assertEquals(id1.fingerprint(), 987654321L); | ||
| 91 | + | ||
| 92 | + final IntentId emptyId = new IntentId(); | ||
| 93 | + assertEquals(emptyId.fingerprint(), 0L); | ||
| 94 | + } | ||
| 67 | } | 95 | } | ... | ... |
| 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.intent; | ||
| 17 | + | ||
| 18 | +import java.util.List; | ||
| 19 | + | ||
| 20 | +import org.junit.Test; | ||
| 21 | +import org.onlab.onos.net.ConnectPoint; | ||
| 22 | +import org.onlab.onos.net.NetTestTools; | ||
| 23 | +import org.onlab.onos.net.flow.TrafficSelector; | ||
| 24 | + | ||
| 25 | +import com.google.common.testing.EqualsTester; | ||
| 26 | + | ||
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 28 | +import static org.hamcrest.Matchers.hasSize; | ||
| 29 | +import static org.hamcrest.Matchers.is; | ||
| 30 | +import static org.hamcrest.Matchers.isOneOf; | ||
| 31 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * Tests for the IntentOperations class. | ||
| 35 | + */ | ||
| 36 | +public class IntentOperationsTest { | ||
| 37 | + | ||
| 38 | + final ConnectPoint egress = NetTestTools.connectPoint("egress", 3); | ||
| 39 | + final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 3); | ||
| 40 | + final TrafficSelector selector = new IntentTestsMocks.MockSelector(); | ||
| 41 | + final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); | ||
| 42 | + | ||
| 43 | + final Intent intent = new PointToPointIntent(NetTestTools.APP_ID, | ||
| 44 | + selector, | ||
| 45 | + treatment, | ||
| 46 | + ingress, | ||
| 47 | + egress); | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Checks that the IntentOperation and IntentOperations classes are immutable. | ||
| 51 | + */ | ||
| 52 | + @Test | ||
| 53 | + public void testImmutability() { | ||
| 54 | + assertThatClassIsImmutable(IntentOperations.class); | ||
| 55 | + assertThatClassIsImmutable(IntentOperations.Builder.class); | ||
| 56 | + assertThatClassIsImmutable(IntentOperation.class); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Tests equals(), hashCode() and toString() methods. | ||
| 61 | + */ | ||
| 62 | + @Test | ||
| 63 | + public void testEquals() { | ||
| 64 | + final IntentOperations operations1 = | ||
| 65 | + IntentOperations.builder() | ||
| 66 | + .addSubmitOperation(intent) | ||
| 67 | + .build(); | ||
| 68 | + final IntentOperations sameAsOperations1 = | ||
| 69 | + IntentOperations.builder() | ||
| 70 | + .addSubmitOperation(intent) | ||
| 71 | + .build(); | ||
| 72 | + final IntentOperations operations2 = | ||
| 73 | + IntentOperations.builder() | ||
| 74 | + .addReplaceOperation(intent.id(), intent) | ||
| 75 | + .build(); | ||
| 76 | + | ||
| 77 | + new EqualsTester() | ||
| 78 | + .addEqualityGroup(operations1, sameAsOperations1) | ||
| 79 | + .addEqualityGroup(operations2) | ||
| 80 | + .testEquals(); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * Checks that objects are created correctly. | ||
| 85 | + */ | ||
| 86 | + @Test | ||
| 87 | + public void testConstruction() { | ||
| 88 | + final IntentOperations operations = | ||
| 89 | + IntentOperations.builder() | ||
| 90 | + .addUpdateOperation(intent.id()) | ||
| 91 | + .addWithdrawOperation(intent.id()) | ||
| 92 | + .build(); | ||
| 93 | + final List<IntentOperation> operationList = operations.operations(); | ||
| 94 | + assertThat(operationList, hasSize(2)); | ||
| 95 | + for (final IntentOperation operation : operationList) { | ||
| 96 | + assertThat(operation.type(), | ||
| 97 | + isOneOf(IntentOperation.Type.UPDATE, | ||
| 98 | + IntentOperation.Type.WITHDRAW)); | ||
| 99 | + assertThat(operation.intent(), is((Intent) null)); | ||
| 100 | + assertThat(operation.intentId(), is(intent.id())); | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | +} |
| 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.intent; | ||
| 17 | + | ||
| 18 | +import java.util.HashSet; | ||
| 19 | +import java.util.LinkedList; | ||
| 20 | +import java.util.List; | ||
| 21 | +import java.util.Set; | ||
| 22 | + | ||
| 23 | +import org.junit.Test; | ||
| 24 | +import org.onlab.onos.net.ConnectPoint; | ||
| 25 | +import org.onlab.onos.net.Link; | ||
| 26 | +import org.onlab.onos.net.NetTestTools; | ||
| 27 | +import org.onlab.onos.net.flow.TrafficSelector; | ||
| 28 | +import org.onlab.onos.net.intent.constraint.LambdaConstraint; | ||
| 29 | +import org.onlab.onos.net.resource.Lambda; | ||
| 30 | + | ||
| 31 | +import com.google.common.testing.EqualsTester; | ||
| 32 | + | ||
| 33 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 34 | +import static org.hamcrest.Matchers.hasSize; | ||
| 35 | +import static org.hamcrest.Matchers.is; | ||
| 36 | +import static org.hamcrest.Matchers.nullValue; | ||
| 37 | +import static org.hamcrest.Matchers.startsWith; | ||
| 38 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
| 39 | +import static org.onlab.onos.net.NetTestTools.APP_ID; | ||
| 40 | +import static org.onlab.onos.net.NetTestTools.link; | ||
| 41 | + | ||
| 42 | +/** | ||
| 43 | + * Unit tests for the LinkCollectionIntent class. | ||
| 44 | + */ | ||
| 45 | +public class LinkCollectionIntentTest { | ||
| 46 | + | ||
| 47 | + final ConnectPoint egress = NetTestTools.connectPoint("egress", 3); | ||
| 48 | + final TrafficSelector selector = new IntentTestsMocks.MockSelector(); | ||
| 49 | + final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Checks that the LinkCollectionIntent class is immutable. | ||
| 53 | + */ | ||
| 54 | + @Test | ||
| 55 | + public void testImmutability() { | ||
| 56 | + assertThatClassIsImmutable(LinkCollectionIntent.class); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Tests equals(), hashCode() and toString() methods. | ||
| 61 | + */ | ||
| 62 | + @Test | ||
| 63 | + public void testEquals() { | ||
| 64 | + | ||
| 65 | + final HashSet<Link> links1 = new HashSet<>(); | ||
| 66 | + links1.add(link("src", 1, "dst", 2)); | ||
| 67 | + final LinkCollectionIntent collectionIntent1 = | ||
| 68 | + new LinkCollectionIntent(APP_ID, | ||
| 69 | + selector, | ||
| 70 | + treatment, | ||
| 71 | + links1, | ||
| 72 | + egress); | ||
| 73 | + final LinkCollectionIntent sameAsCollectionIntent1 = | ||
| 74 | + new LinkCollectionIntent(APP_ID, | ||
| 75 | + selector, | ||
| 76 | + treatment, | ||
| 77 | + links1, | ||
| 78 | + egress); | ||
| 79 | + | ||
| 80 | + final HashSet<Link> links2 = new HashSet<>(); | ||
| 81 | + links2.add(link("src", 1, "dst", 3)); | ||
| 82 | + final LinkCollectionIntent collectionIntent2 = | ||
| 83 | + new LinkCollectionIntent(APP_ID, | ||
| 84 | + selector, | ||
| 85 | + treatment, | ||
| 86 | + links2, | ||
| 87 | + egress); | ||
| 88 | + | ||
| 89 | + new EqualsTester() | ||
| 90 | + .addEqualityGroup(collectionIntent1, sameAsCollectionIntent1) | ||
| 91 | + .addEqualityGroup(collectionIntent2) | ||
| 92 | + .testEquals(); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * Tests constructor without constraints. | ||
| 97 | + */ | ||
| 98 | + @Test | ||
| 99 | + public void testConstructor() { | ||
| 100 | + final HashSet<Link> links1 = new HashSet<>(); | ||
| 101 | + links1.add(link("src", 1, "dst", 2)); | ||
| 102 | + final LinkCollectionIntent collectionIntent = | ||
| 103 | + new LinkCollectionIntent(APP_ID, | ||
| 104 | + selector, | ||
| 105 | + treatment, | ||
| 106 | + links1, | ||
| 107 | + egress); | ||
| 108 | + | ||
| 109 | + final Set<Link> createdLinks = collectionIntent.links(); | ||
| 110 | + assertThat(createdLinks, hasSize(1)); | ||
| 111 | + assertThat(collectionIntent.isInstallable(), is(true)); | ||
| 112 | + assertThat(collectionIntent.treatment(), is(treatment)); | ||
| 113 | + assertThat(collectionIntent.selector(), is(selector)); | ||
| 114 | + assertThat(collectionIntent.egressPoint(), is(egress)); | ||
| 115 | + assertThat(collectionIntent.resources(), hasSize(1)); | ||
| 116 | + final List<Constraint> createdConstraints = collectionIntent.constraints(); | ||
| 117 | + assertThat(createdConstraints, hasSize(0)); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * Tests constructor with constraints. | ||
| 122 | + */ | ||
| 123 | + @Test | ||
| 124 | + public void testConstructorWithConstraints() { | ||
| 125 | + final HashSet<Link> links1 = new HashSet<>(); | ||
| 126 | + final LinkedList<Constraint> constraints = new LinkedList<>(); | ||
| 127 | + | ||
| 128 | + links1.add(link("src", 1, "dst", 2)); | ||
| 129 | + constraints.add(new LambdaConstraint(Lambda.valueOf(23))); | ||
| 130 | + final LinkCollectionIntent collectionIntent = | ||
| 131 | + new LinkCollectionIntent(APP_ID, | ||
| 132 | + selector, | ||
| 133 | + treatment, | ||
| 134 | + links1, | ||
| 135 | + egress, | ||
| 136 | + constraints); | ||
| 137 | + | ||
| 138 | + final Set<Link> createdLinks = collectionIntent.links(); | ||
| 139 | + assertThat(createdLinks, hasSize(1)); | ||
| 140 | + assertThat(collectionIntent.isInstallable(), is(true)); | ||
| 141 | + assertThat(collectionIntent.treatment(), is(treatment)); | ||
| 142 | + assertThat(collectionIntent.selector(), is(selector)); | ||
| 143 | + assertThat(collectionIntent.egressPoint(), is(egress)); | ||
| 144 | + | ||
| 145 | + final List<Constraint> createdConstraints = collectionIntent.constraints(); | ||
| 146 | + assertThat(createdConstraints, hasSize(1)); | ||
| 147 | + assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint")); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * Tests constructor with constraints. | ||
| 152 | + */ | ||
| 153 | + @Test | ||
| 154 | + public void testSerializerConstructor() { | ||
| 155 | + | ||
| 156 | + final LinkCollectionIntent collectionIntent = | ||
| 157 | + new LinkCollectionIntent(); | ||
| 158 | + | ||
| 159 | + final Set<Link> createdLinks = collectionIntent.links(); | ||
| 160 | + assertThat(createdLinks, nullValue()); | ||
| 161 | + assertThat(collectionIntent.isInstallable(), is(true)); | ||
| 162 | + assertThat(collectionIntent.treatment(), nullValue()); | ||
| 163 | + assertThat(collectionIntent.selector(), nullValue()); | ||
| 164 | + assertThat(collectionIntent.egressPoint(), nullValue()); | ||
| 165 | + | ||
| 166 | + final List<Constraint> createdConstraints = collectionIntent.constraints(); | ||
| 167 | + assertThat(createdConstraints, hasSize(0)); | ||
| 168 | + } | ||
| 169 | +} |
-
Please register or login to post a comment