blob: 691852dd985291e866e3d3f17441519cbfd798da [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;
19import org.onosproject.core.DefaultGroupId;
20import org.onosproject.core.GroupId;
21import org.onosproject.net.NetTestTools;
22import 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 {
35 private final GroupId id1 = new DefaultGroupId(6);
36 private final GroupId id2 = new DefaultGroupId(7);
Jayasree Ghosh2d459852016-07-02 19:06:52 +053037 private final GroupId id3 = new DefaultGroupId(1234);
38
Ray Milkey33d44c52015-06-10 16:12:02 -070039 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 Ghosh2d459852016-07-02 19:06:52 +053053 private final GroupDescription groupDesc3 =
54 new DefaultGroupDescription(did("3"),
55 GroupDescription.Type.INDIRECT,
56 groupBuckets);
57
Ray Milkey33d44c52015-06-10 16:12:02 -070058 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 Ghosh2d459852016-07-02 19:06:52 +053062 DefaultGroup group4 = new DefaultGroup(id3, groupDesc3);
Ray Milkey33d44c52015-06-10 16:12:02 -070063
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 Ghosh2d459852016-07-02 19:06:52 +053073 .addEqualityGroup(group4)
Ray Milkey33d44c52015-06-10 16:12:02 -070074 .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 Ghosh2d459852016-07-02 19:06:52 +053097 GroupDescription.Type.ALL, groupBuckets);
Ray Milkey33d44c52015-06-10 16:12:02 -070098 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}