Ray Milkey | e8c632f | 2015-05-20 16:34:29 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | e8c632f | 2015-05-20 16:34:29 -0700 | [diff] [blame] | 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 | } |