Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -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 | 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; |
| 19 | import org.onosproject.core.DefaultGroupId; |
| 20 | import org.onosproject.core.GroupId; |
| 21 | import org.onosproject.net.NetTestTools; |
| 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 { |
| 35 | private final GroupId id1 = new DefaultGroupId(6); |
| 36 | private final GroupId id2 = new DefaultGroupId(7); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 37 | private final GroupId id3 = new DefaultGroupId(1234); |
| 38 | |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 39 | private final GroupBucket bucket = |
| 40 | DefaultGroupBucket.createSelectGroupBucket( |
| 41 | DefaultTrafficTreatment.emptyTreatment()); |
| 42 | private final GroupBuckets groupBuckets = |
| 43 | new GroupBuckets(ImmutableList.of(bucket)); |
| 44 | private final GroupDescription groupDesc1 = |
| 45 | new DefaultGroupDescription(did("1"), |
| 46 | GroupDescription.Type.FAILOVER, |
| 47 | groupBuckets); |
| 48 | private final GroupDescription groupDesc2 = |
| 49 | new DefaultGroupDescription(did("2"), |
| 50 | GroupDescription.Type.FAILOVER, |
| 51 | groupBuckets); |
| 52 | |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 53 | private final GroupDescription groupDesc3 = |
| 54 | new DefaultGroupDescription(did("3"), |
| 55 | GroupDescription.Type.INDIRECT, |
| 56 | groupBuckets); |
| 57 | |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 58 | DefaultGroup group1 = new DefaultGroup(id1, groupDesc1); |
| 59 | DefaultGroup sameAsGroup1 = new DefaultGroup(id1, groupDesc1); |
| 60 | DefaultGroup group2 = new DefaultGroup(id1, groupDesc2); |
| 61 | DefaultGroup group3 = new DefaultGroup(id2, groupDesc2); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 62 | DefaultGroup group4 = new DefaultGroup(id3, groupDesc3); |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Tests for proper operation of equals(), hashCode() and toString() methods. |
| 66 | */ |
| 67 | @Test |
| 68 | public void checkEquals() { |
| 69 | new EqualsTester() |
| 70 | .addEqualityGroup(group1, sameAsGroup1) |
| 71 | .addEqualityGroup(group2) |
| 72 | .addEqualityGroup(group3) |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 73 | .addEqualityGroup(group4) |
Ray Milkey | 33d44c5 | 2015-06-10 16:12:02 -0700 | [diff] [blame] | 74 | .testEquals(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests that objects are created properly. |
| 79 | */ |
| 80 | @Test |
| 81 | public void checkConstruction() { |
| 82 | assertThat(group1.id(), is(id1)); |
| 83 | assertThat(group1.bytes(), is(0L)); |
| 84 | assertThat(group1.life(), is(0L)); |
| 85 | assertThat(group1.packets(), is(0L)); |
| 86 | assertThat(group1.referenceCount(), is(0L)); |
| 87 | assertThat(group1.buckets(), is(groupBuckets)); |
| 88 | assertThat(group1.state(), is(Group.GroupState.PENDING_ADD)); |
| 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)); |
| 105 | } |
| 106 | } |