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