blob: a06783b7c2189307321a59a1057dcfa9da9053b7 [file] [log] [blame]
Ray Milkeye8c632f2015-05-20 16:34:29 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeye8c632f2015-05-20 16:34:29 -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 Milkeye8c632f2015-05-20 16:34:29 -070019import org.onosproject.core.GroupId;
20import org.onosproject.net.PortNumber;
21import org.onosproject.net.flow.DefaultTrafficTreatment;
22import org.onosproject.net.flow.TrafficTreatment;
23
24import com.google.common.testing.EqualsTester;
25
26import static org.hamcrest.CoreMatchers.is;
27import static org.hamcrest.CoreMatchers.nullValue;
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.onosproject.net.group.GroupDescription.Type.FAILOVER;
30import static org.onosproject.net.group.GroupDescription.Type.INDIRECT;
31import static org.onosproject.net.group.GroupDescription.Type.SELECT;
32
33/**
34 * Unit tests for the group bucket class.
35 */
36public class GroupBucketTest {
37
Yi Tsengfa394de2017-02-01 11:26:40 -080038 private final GroupId groupId = new GroupId(7);
Ray Milkeye8c632f2015-05-20 16:34:29 -070039 private final GroupId nullGroup = null;
40
41 private final PortNumber nullPort = null;
42
43 private final TrafficTreatment treatment =
44 DefaultTrafficTreatment.emptyTreatment();
45 private final GroupBucket selectGroupBucket =
46 DefaultGroupBucket.createSelectGroupBucket(treatment);
47 private final GroupBucket sameAsSelectGroupBucket =
48 DefaultGroupBucket.createSelectGroupBucket(treatment);
49 private final GroupBucket failoverGroupBucket =
50 DefaultGroupBucket.createFailoverGroupBucket(treatment,
51 PortNumber.IN_PORT, groupId);
52 private final GroupBucket indirectGroupBucket =
53 DefaultGroupBucket.createIndirectGroupBucket(treatment);
54 private final GroupBucket selectGroupBucketWithWeight =
55 DefaultGroupBucket.createSelectGroupBucket(treatment, (short) 5);
56
57
58 /**
59 * Tests for proper operation of equals(), hashCode() and toString() methods.
60 */
61 @Test
62 public void checkEquals() {
63 new EqualsTester()
64 .addEqualityGroup(selectGroupBucket,
65 sameAsSelectGroupBucket,
66 selectGroupBucketWithWeight)
67 .addEqualityGroup(failoverGroupBucket)
68 .addEqualityGroup(indirectGroupBucket)
69 .testEquals();
70 }
71
72 private void checkValues(GroupBucket bucket, GroupDescription.Type type,
73 long bytes, long packets, short weight,
74 GroupId groupId, PortNumber portNumber) {
75 assertThat(bucket.type(), is(type));
76 assertThat(bucket.bytes(), is(bytes));
77 assertThat(bucket.packets(), is(packets));
78 assertThat(bucket.treatment(), is(treatment));
79 assertThat(bucket.weight(), is(weight));
80 assertThat(bucket.watchGroup(), is(groupId));
81 assertThat(bucket.watchPort(), is(portNumber));
82 }
83
84 /**
85 * Checks that construction of a select group was correct.
86 */
87 @Test
88 public void checkSelectGroup() {
89 // Casting needed because only the store accesses the set methods.
90 ((DefaultGroupBucket) selectGroupBucket).setBytes(4);
91 ((DefaultGroupBucket) selectGroupBucket).setPackets(44);
92
93 checkValues(selectGroupBucket, SELECT, 4L, 44L, (short) 1,
94 nullGroup, nullPort);
95 }
96
97 /**
98 * Checks that construction of a select group with a weight was correct.
99 */
100 @Test
101 public void checkSelectGroupWithPriority() {
102 checkValues(selectGroupBucketWithWeight, SELECT, 0L, 0L, (short) 5,
103 nullGroup, nullPort);
104 }
105
106 /**
107 * Checks that construction of an indirect group was correct.
108 */
109 @Test
110 public void checkFailoverGroup() {
111 checkValues(failoverGroupBucket, FAILOVER, 0L, 0L, (short) -1,
112 groupId, PortNumber.IN_PORT);
113 }
114 /**
115 * Checks that construction of an indirect group was correct.
116 */
117 @Test
118 public void checkIndirectGroup() {
119 checkValues(indirectGroupBucket, INDIRECT, 0L, 0L, (short) -1,
120 nullGroup, nullPort);
121 }
122
123 /**
124 * Checks that a weight of 0 results in no group getting created.
125 */
126 @Test
127 public void checkZeroWeight() {
128 GroupBucket bucket =
129 DefaultGroupBucket.createSelectGroupBucket(treatment, (short) 0);
130 assertThat(bucket, nullValue());
131 }
132}