Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -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; |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 19 | import org.onosproject.core.GroupId; |
| 20 | import org.onosproject.net.NetTestTools; |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.testing.EqualsTester; |
| 26 | |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.junit.Assert.*; |
| 29 | import static org.onosproject.net.NetTestTools.did; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for DefaultGroup class. |
| 33 | */ |
| 34 | public class DefaultGroupTest { |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 35 | private final GroupId id1 = new GroupId(6); |
| 36 | private final GroupId id2 = new GroupId(7); |
| 37 | private final GroupId id3 = new GroupId(1234); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 38 | |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 39 | private final GroupBucket failoverBucket = DefaultGroupBucket.createFailoverGroupBucket( |
| 40 | DefaultTrafficTreatment.emptyTreatment(), PortNumber.IN_PORT, id1); |
| 41 | private final GroupBuckets failoverGroupBuckets = new GroupBuckets(ImmutableList.of(failoverBucket)); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 42 | |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 43 | private final GroupBucket indirectBucket = |
| 44 | DefaultGroupBucket.createIndirectGroupBucket(DefaultTrafficTreatment.emptyTreatment()); |
| 45 | private final GroupBuckets indirectGroupBuckets = new GroupBuckets(ImmutableList.of(indirectBucket)); |
| 46 | |
| 47 | private final GroupDescription groupDesc1 = |
| 48 | new DefaultGroupDescription(did("1"), GroupDescription.Type.FAILOVER, failoverGroupBuckets); |
| 49 | private final GroupDescription groupDesc2 = |
| 50 | new DefaultGroupDescription(did("2"), GroupDescription.Type.FAILOVER, failoverGroupBuckets); |
| 51 | private final GroupDescription groupDesc3 = |
| 52 | new DefaultGroupDescription(did("3"), GroupDescription.Type.INDIRECT, indirectGroupBuckets); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 53 | |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 54 | DefaultGroup group1 = new DefaultGroup(id1, groupDesc1); |
| 55 | DefaultGroup sameAsGroup1 = new DefaultGroup(id1, groupDesc1); |
| 56 | DefaultGroup group2 = new DefaultGroup(id1, groupDesc2); |
| 57 | DefaultGroup group3 = new DefaultGroup(id2, groupDesc2); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 58 | DefaultGroup group4 = new DefaultGroup(id3, groupDesc3); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 62 | */ |
| 63 | @Test |
| 64 | public void checkEquals() { |
| 65 | new EqualsTester() |
| 66 | .addEqualityGroup(group1, sameAsGroup1) |
| 67 | .addEqualityGroup(group2) |
| 68 | .addEqualityGroup(group3) |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 69 | .addEqualityGroup(group4) |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 70 | .testEquals(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests that objects are created properly. |
| 75 | */ |
| 76 | @Test |
| 77 | public void checkConstruction() { |
| 78 | assertThat(group1.id(), is(id1)); |
| 79 | assertThat(group1.bytes(), is(0L)); |
| 80 | assertThat(group1.life(), is(0L)); |
| 81 | assertThat(group1.packets(), is(0L)); |
| 82 | assertThat(group1.referenceCount(), is(0L)); |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 83 | assertThat(group1.buckets(), is(failoverGroupBuckets)); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 84 | assertThat(group1.state(), is(Group.GroupState.PENDING_ADD)); |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 85 | assertThat(group1.failedRetryCount(), is(0)); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests that objects are created properly using the device based constructor. |
| 90 | */ |
| 91 | @Test |
| 92 | public void checkConstructionWithDid() { |
| 93 | DefaultGroup group = new DefaultGroup(id2, NetTestTools.did("1"), |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 94 | GroupDescription.Type.FAILOVER, failoverGroupBuckets); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 95 | assertThat(group.id(), is(id2)); |
| 96 | assertThat(group.bytes(), is(0L)); |
| 97 | assertThat(group.life(), is(0L)); |
| 98 | assertThat(group.packets(), is(0L)); |
| 99 | assertThat(group.referenceCount(), is(0L)); |
| 100 | assertThat(group.deviceId(), is(NetTestTools.did("1"))); |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 101 | assertThat(group.buckets(), is(failoverGroupBuckets)); |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 102 | assertThat(group.failedRetryCount(), is(0)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Test failedRetryCount field. |
| 107 | */ |
| 108 | @Test |
| 109 | public void checkFailedRetyCount() { |
| 110 | assertThat(group1.failedRetryCount(), is(0)); |
| 111 | group1.incrFailedRetryCount(); |
| 112 | assertThat(group1.failedRetryCount(), is(1)); |
| 113 | group1.setFailedRetryCount(3); |
| 114 | assertThat(group1.failedRetryCount(), is(3)); |
| 115 | group1.incrFailedRetryCount(); |
| 116 | assertThat(group1.failedRetryCount(), is(4)); |
| 117 | group1.setFailedRetryCount(0); |
| 118 | assertThat(group1.failedRetryCount(), is(0)); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 119 | } |
| 120 | } |