blob: c84ac876f80a64ebbff68a2defbabb097f02b884 [file] [log] [blame]
Ray Milkey33d44c52015-06-10 16:12:02 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey33d44c52015-06-10 16:12:02 -07003 *
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 */
16package org.onosproject.net.group;
17
18import org.junit.Test;
Ray Milkey33d44c52015-06-10 16:12:02 -070019import org.onosproject.core.GroupId;
20import org.onosproject.net.NetTestTools;
Charles Chanc88ebaa2018-11-01 20:08:47 -070021import org.onosproject.net.PortNumber;
Ray Milkey33d44c52015-06-10 16:12:02 -070022import org.onosproject.net.flow.DefaultTrafficTreatment;
23
24import com.google.common.collect.ImmutableList;
25import com.google.common.testing.EqualsTester;
26
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.*;
29import static org.onosproject.net.NetTestTools.did;
30
31/**
32 * Unit tests for DefaultGroup class.
33 */
34public class DefaultGroupTest {
Yi Tsengfa394de2017-02-01 11:26:40 -080035 private final GroupId id1 = new GroupId(6);
36 private final GroupId id2 = new GroupId(7);
37 private final GroupId id3 = new GroupId(1234);
Jayasree Ghosh2d459852016-07-02 19:06:52 +053038
Charles Chanc88ebaa2018-11-01 20:08:47 -070039 private final GroupBucket failoverBucket = DefaultGroupBucket.createFailoverGroupBucket(
40 DefaultTrafficTreatment.emptyTreatment(), PortNumber.IN_PORT, id1);
41 private final GroupBuckets failoverGroupBuckets = new GroupBuckets(ImmutableList.of(failoverBucket));
Ray Milkey33d44c52015-06-10 16:12:02 -070042
Charles Chanc88ebaa2018-11-01 20:08:47 -070043 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 Ghosh2d459852016-07-02 19:06:52 +053053
Ray Milkey33d44c52015-06-10 16:12:02 -070054 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 Ghosh2d459852016-07-02 19:06:52 +053058 DefaultGroup group4 = new DefaultGroup(id3, groupDesc3);
Ray Milkey33d44c52015-06-10 16:12:02 -070059
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 Ghosh2d459852016-07-02 19:06:52 +053069 .addEqualityGroup(group4)
Ray Milkey33d44c52015-06-10 16:12:02 -070070 .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 Chanc88ebaa2018-11-01 20:08:47 -070083 assertThat(group1.buckets(), is(failoverGroupBuckets));
Ray Milkey33d44c52015-06-10 16:12:02 -070084 assertThat(group1.state(), is(Group.GroupState.PENDING_ADD));
Saurav Das137f27f2018-06-11 17:02:31 -070085 assertThat(group1.failedRetryCount(), is(0));
Ray Milkey33d44c52015-06-10 16:12:02 -070086 }
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 Chanc88ebaa2018-11-01 20:08:47 -070094 GroupDescription.Type.FAILOVER, failoverGroupBuckets);
Ray Milkey33d44c52015-06-10 16:12:02 -070095 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 Chanc88ebaa2018-11-01 20:08:47 -0700101 assertThat(group.buckets(), is(failoverGroupBuckets));
Saurav Das137f27f2018-06-11 17:02:31 -0700102 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 Milkey33d44c52015-06-10 16:12:02 -0700119 }
120}