Committed by
Gerrit Code Review
Unit tests for GroupBucket and GroupOperation
Also added the group type to the equals() method for GroupOperation. Change-Id: I733f32ee740fd17dd6c3d488abbb8b1656514073
Showing
3 changed files
with
223 additions
and
0 deletions
| ... | @@ -171,6 +171,7 @@ public final class GroupOperation { | ... | @@ -171,6 +171,7 @@ public final class GroupOperation { |
| 171 | if (obj instanceof GroupOperation) { | 171 | if (obj instanceof GroupOperation) { |
| 172 | GroupOperation that = (GroupOperation) obj; | 172 | GroupOperation that = (GroupOperation) obj; |
| 173 | return Objects.equals(groupId, that.groupId) && | 173 | return Objects.equals(groupId, that.groupId) && |
| 174 | + Objects.equals(groupType, that.groupType) && | ||
| 174 | Objects.equals(opType, that.opType) && | 175 | Objects.equals(opType, that.opType) && |
| 175 | Objects.equals(buckets, that.buckets); | 176 | Objects.equals(buckets, that.buckets); |
| 176 | 177 | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2015 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.onosproject.net.group; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | +import org.onosproject.core.DefaultGroupId; | ||
| 20 | +import org.onosproject.core.GroupId; | ||
| 21 | +import org.onosproject.net.PortNumber; | ||
| 22 | +import org.onosproject.net.flow.DefaultTrafficTreatment; | ||
| 23 | +import org.onosproject.net.flow.TrafficTreatment; | ||
| 24 | + | ||
| 25 | +import com.google.common.testing.EqualsTester; | ||
| 26 | + | ||
| 27 | +import static org.hamcrest.CoreMatchers.is; | ||
| 28 | +import static org.hamcrest.CoreMatchers.nullValue; | ||
| 29 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 30 | +import static org.onosproject.net.group.GroupDescription.Type.FAILOVER; | ||
| 31 | +import static org.onosproject.net.group.GroupDescription.Type.INDIRECT; | ||
| 32 | +import static org.onosproject.net.group.GroupDescription.Type.SELECT; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * Unit tests for the group bucket class. | ||
| 36 | + */ | ||
| 37 | +public class GroupBucketTest { | ||
| 38 | + | ||
| 39 | + private final GroupId groupId = new DefaultGroupId(7); | ||
| 40 | + private final GroupId nullGroup = null; | ||
| 41 | + | ||
| 42 | + private final PortNumber nullPort = null; | ||
| 43 | + | ||
| 44 | + private final TrafficTreatment treatment = | ||
| 45 | + DefaultTrafficTreatment.emptyTreatment(); | ||
| 46 | + private final GroupBucket selectGroupBucket = | ||
| 47 | + DefaultGroupBucket.createSelectGroupBucket(treatment); | ||
| 48 | + private final GroupBucket sameAsSelectGroupBucket = | ||
| 49 | + DefaultGroupBucket.createSelectGroupBucket(treatment); | ||
| 50 | + private final GroupBucket failoverGroupBucket = | ||
| 51 | + DefaultGroupBucket.createFailoverGroupBucket(treatment, | ||
| 52 | + PortNumber.IN_PORT, groupId); | ||
| 53 | + private final GroupBucket indirectGroupBucket = | ||
| 54 | + DefaultGroupBucket.createIndirectGroupBucket(treatment); | ||
| 55 | + private final GroupBucket selectGroupBucketWithWeight = | ||
| 56 | + DefaultGroupBucket.createSelectGroupBucket(treatment, (short) 5); | ||
| 57 | + | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Tests for proper operation of equals(), hashCode() and toString() methods. | ||
| 61 | + */ | ||
| 62 | + @Test | ||
| 63 | + public void checkEquals() { | ||
| 64 | + new EqualsTester() | ||
| 65 | + .addEqualityGroup(selectGroupBucket, | ||
| 66 | + sameAsSelectGroupBucket, | ||
| 67 | + selectGroupBucketWithWeight) | ||
| 68 | + .addEqualityGroup(failoverGroupBucket) | ||
| 69 | + .addEqualityGroup(indirectGroupBucket) | ||
| 70 | + .testEquals(); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + private void checkValues(GroupBucket bucket, GroupDescription.Type type, | ||
| 74 | + long bytes, long packets, short weight, | ||
| 75 | + GroupId groupId, PortNumber portNumber) { | ||
| 76 | + assertThat(bucket.type(), is(type)); | ||
| 77 | + assertThat(bucket.bytes(), is(bytes)); | ||
| 78 | + assertThat(bucket.packets(), is(packets)); | ||
| 79 | + assertThat(bucket.treatment(), is(treatment)); | ||
| 80 | + assertThat(bucket.weight(), is(weight)); | ||
| 81 | + assertThat(bucket.watchGroup(), is(groupId)); | ||
| 82 | + assertThat(bucket.watchPort(), is(portNumber)); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * Checks that construction of a select group was correct. | ||
| 87 | + */ | ||
| 88 | + @Test | ||
| 89 | + public void checkSelectGroup() { | ||
| 90 | + // Casting needed because only the store accesses the set methods. | ||
| 91 | + ((DefaultGroupBucket) selectGroupBucket).setBytes(4); | ||
| 92 | + ((DefaultGroupBucket) selectGroupBucket).setPackets(44); | ||
| 93 | + | ||
| 94 | + checkValues(selectGroupBucket, SELECT, 4L, 44L, (short) 1, | ||
| 95 | + nullGroup, nullPort); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + /** | ||
| 99 | + * Checks that construction of a select group with a weight was correct. | ||
| 100 | + */ | ||
| 101 | + @Test | ||
| 102 | + public void checkSelectGroupWithPriority() { | ||
| 103 | + checkValues(selectGroupBucketWithWeight, SELECT, 0L, 0L, (short) 5, | ||
| 104 | + nullGroup, nullPort); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + /** | ||
| 108 | + * Checks that construction of an indirect group was correct. | ||
| 109 | + */ | ||
| 110 | + @Test | ||
| 111 | + public void checkFailoverGroup() { | ||
| 112 | + checkValues(failoverGroupBucket, FAILOVER, 0L, 0L, (short) -1, | ||
| 113 | + groupId, PortNumber.IN_PORT); | ||
| 114 | + } | ||
| 115 | + /** | ||
| 116 | + * Checks that construction of an indirect group was correct. | ||
| 117 | + */ | ||
| 118 | + @Test | ||
| 119 | + public void checkIndirectGroup() { | ||
| 120 | + checkValues(indirectGroupBucket, INDIRECT, 0L, 0L, (short) -1, | ||
| 121 | + nullGroup, nullPort); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * Checks that a weight of 0 results in no group getting created. | ||
| 126 | + */ | ||
| 127 | + @Test | ||
| 128 | + public void checkZeroWeight() { | ||
| 129 | + GroupBucket bucket = | ||
| 130 | + DefaultGroupBucket.createSelectGroupBucket(treatment, (short) 0); | ||
| 131 | + assertThat(bucket, nullValue()); | ||
| 132 | + } | ||
| 133 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2015 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.onosproject.net.group; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | +import org.onosproject.core.DefaultGroupId; | ||
| 20 | +import org.onosproject.core.GroupId; | ||
| 21 | +import org.onosproject.net.flow.DefaultTrafficTreatment; | ||
| 22 | +import org.onosproject.net.flow.TrafficTreatment; | ||
| 23 | + | ||
| 24 | +import com.google.common.collect.ImmutableList; | ||
| 25 | +import com.google.common.testing.EqualsTester; | ||
| 26 | + | ||
| 27 | +import static org.hamcrest.CoreMatchers.is; | ||
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 29 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
| 30 | +import static org.onosproject.net.group.GroupDescription.Type.ALL; | ||
| 31 | +import static org.onosproject.net.group.GroupDescription.Type.INDIRECT; | ||
| 32 | +import static org.onosproject.net.group.GroupOperation.Type.ADD; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * Tests for the group operation class. | ||
| 36 | + */ | ||
| 37 | +public class GroupOperationTest { | ||
| 38 | + | ||
| 39 | + private final GroupId groupId = new DefaultGroupId(6); | ||
| 40 | + private final TrafficTreatment treatment = | ||
| 41 | + DefaultTrafficTreatment.emptyTreatment(); | ||
| 42 | + private final GroupBucket bucket = | ||
| 43 | + DefaultGroupBucket.createSelectGroupBucket(treatment); | ||
| 44 | + private final GroupBuckets groupBuckets = | ||
| 45 | + new GroupBuckets(ImmutableList.of(bucket)); | ||
| 46 | + private final GroupOperation op1 = | ||
| 47 | + GroupOperation.createAddGroupOperation(groupId, ALL, groupBuckets); | ||
| 48 | + private final GroupOperation sameAsOp1 = | ||
| 49 | + GroupOperation.createAddGroupOperation(groupId, ALL, groupBuckets); | ||
| 50 | + private final GroupOperation op2 = | ||
| 51 | + GroupOperation.createAddGroupOperation(groupId, INDIRECT, groupBuckets); | ||
| 52 | + private final GroupOperation op3 = | ||
| 53 | + GroupOperation.createDeleteGroupOperation(groupId, INDIRECT); | ||
| 54 | + private final GroupOperation op4 = | ||
| 55 | + GroupOperation.createModifyGroupOperation(groupId, INDIRECT, groupBuckets); | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * Checks that the GroupOperation class is immutable. | ||
| 59 | + */ | ||
| 60 | + @Test | ||
| 61 | + public void testImmutability() { | ||
| 62 | + assertThatClassIsImmutable(GroupOperation.class); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + /** | ||
| 66 | + * Tests for proper operation of equals(), hashCode() and toString() methods. | ||
| 67 | + */ | ||
| 68 | + @Test | ||
| 69 | + public void checkEquals() { | ||
| 70 | + new EqualsTester() | ||
| 71 | + .addEqualityGroup(op1, sameAsOp1) | ||
| 72 | + .addEqualityGroup(op2) | ||
| 73 | + .addEqualityGroup(op3) | ||
| 74 | + .addEqualityGroup(op4) | ||
| 75 | + .testEquals(); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * Checks that the construction of the add operation is correct. | ||
| 80 | + */ | ||
| 81 | + @Test | ||
| 82 | + public void testAddGroupOperation() { | ||
| 83 | + assertThat(op1.buckets(), is(groupBuckets)); | ||
| 84 | + assertThat(op1.groupId(), is(groupId)); | ||
| 85 | + assertThat(op1.groupType(), is(ALL)); | ||
| 86 | + assertThat(op1.opType(), is(ADD)); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | +} |
-
Please register or login to post a comment