blob: e7dcb2f99250578f8f292973a4af1abd5eb38280 [file] [log] [blame]
Ray Milkey33d44c52015-06-10 16:12:02 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
21import org.onosproject.net.flow.DefaultTrafficTreatment;
22
23import com.google.common.collect.ImmutableList;
24import com.google.common.testing.EqualsTester;
25
26import static org.hamcrest.Matchers.is;
27import static org.junit.Assert.*;
28import static org.onosproject.net.NetTestTools.did;
29
30/**
31 * Unit tests for DefaultGroup class.
32 */
33public class DefaultGroupTest {
Yi Tsengfa394de2017-02-01 11:26:40 -080034 private final GroupId id1 = new GroupId(6);
35 private final GroupId id2 = new GroupId(7);
36 private final GroupId id3 = new GroupId(1234);
Jayasree Ghosh2d459852016-07-02 19:06:52 +053037
Ray Milkey33d44c52015-06-10 16:12:02 -070038 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 Ghosh2d459852016-07-02 19:06:52 +053052 private final GroupDescription groupDesc3 =
53 new DefaultGroupDescription(did("3"),
54 GroupDescription.Type.INDIRECT,
55 groupBuckets);
56
Ray Milkey33d44c52015-06-10 16:12:02 -070057 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 Ghosh2d459852016-07-02 19:06:52 +053061 DefaultGroup group4 = new DefaultGroup(id3, groupDesc3);
Ray Milkey33d44c52015-06-10 16:12:02 -070062
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 Ghosh2d459852016-07-02 19:06:52 +053072 .addEqualityGroup(group4)
Ray Milkey33d44c52015-06-10 16:12:02 -070073 .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));
88 }
89
90 /**
91 * Tests that objects are created properly using the device based constructor.
92 */
93 @Test
94 public void checkConstructionWithDid() {
95 DefaultGroup group = new DefaultGroup(id2, NetTestTools.did("1"),
Jayasree Ghosh2d459852016-07-02 19:06:52 +053096 GroupDescription.Type.ALL, groupBuckets);
Ray Milkey33d44c52015-06-10 16:12:02 -070097 assertThat(group.id(), is(id2));
98 assertThat(group.bytes(), is(0L));
99 assertThat(group.life(), is(0L));
100 assertThat(group.packets(), is(0L));
101 assertThat(group.referenceCount(), is(0L));
102 assertThat(group.deviceId(), is(NetTestTools.did("1")));
103 assertThat(group.buckets(), is(groupBuckets));
104 }
105}