blob: cddaf23e2461f54f5593f5d9789422faeb758f06 [file] [log] [blame]
Ray Milkey1e207112014-11-11 10:38:00 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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
17package org.onlab.onos.net.flow;
18
19import org.junit.Test;
20import org.onlab.onos.net.DeviceId;
21import org.onlab.onos.net.intent.IntentTestsMocks;
22
23import com.google.common.testing.EqualsTester;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.is;
27import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
28import static org.onlab.onos.net.NetTestTools.did;
29import static org.onlab.onos.net.NetTestTools.APP_ID;
30
31/**
32 * Unit tests for the default flow rule class.
33 */
34public class DefaultFlowRuleTest {
35 private static final IntentTestsMocks.MockSelector SELECTOR =
36 new IntentTestsMocks.MockSelector();
37 private static final IntentTestsMocks.MockTreatment TREATMENT =
38 new IntentTestsMocks.MockTreatment();
39
40 final FlowRule flowRule1 = new MockFlowRule(1);
41 final FlowRule sameAsFlowRule1 = new MockFlowRule(1);
42 final FlowRule flowRule2 = new MockFlowRule(2);
43 final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1);
44 final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1);
45 final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2);
46
47 private static class MockFlowRule implements FlowRule {
48
49 int priority;
50 MockFlowRule(int priority) {
51 this.priority = priority;
52 }
53
54 @Override
55 public FlowId id() {
56 return FlowId.valueOf(1);
57 }
58
59 @Override
60 public short appId() {
61 return 0;
62 }
63
64 @Override
65 public int priority() {
66 return priority;
67 }
68
69 @Override
70 public DeviceId deviceId() {
71 return did("1");
72 }
73
74 @Override
75 public TrafficSelector selector() {
76 return SELECTOR;
77 }
78
79 @Override
80 public TrafficTreatment treatment() {
81 return TREATMENT;
82 }
83
84 @Override
85 public int timeout() {
86 return 0;
87 }
88
89 @Override
90 public boolean isPermanent() {
91 return false;
92 }
93
94
95 }
96
97 /**
98 * Checks that the DefaultFlowRule class is immutable but can be inherited
99 * from.
100 */
101 @Test
102 public void testImmutability() {
103 assertThatClassIsImmutableBaseClass(DefaultFlowRule.class);
104 }
105
106 /**
107 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
108 */
109 @Test
110 public void testEquals() {
111 new EqualsTester()
112 .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1)
113 .addEqualityGroup(defaultFlowRule2)
114 .testEquals();
115 }
116
117 /**
118 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
119 */
120 @Test
121 public void testCreationFromFlowRule() {
122 assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId()));
123 assertThat(defaultFlowRule1.appId(), is(flowRule1.appId()));
124 assertThat(defaultFlowRule1.id(), is(flowRule1.id()));
125 assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent()));
126 assertThat(defaultFlowRule1.priority(), is(flowRule1.priority()));
127 assertThat(defaultFlowRule1.selector(), is(flowRule1.selector()));
128 assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment()));
129 assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout()));
130 }
131
132 /**
133 * Tests creation of a DefaultFlowRule using a FlowId constructor.
134 */
135 @Test
136 public void testCreationWithFlowId() {
137 final DefaultFlowRule rule =
138 new DefaultFlowRule(did("1"), SELECTOR,
139 TREATMENT, 22, 33,
140 44, false);
141 assertThat(rule.deviceId(), is(did("1")));
142 assertThat(rule.id().value(), is(33L));
143 assertThat(rule.isPermanent(), is(false));
144 assertThat(rule.priority(), is(22));
145 assertThat(rule.selector(), is(SELECTOR));
146 assertThat(rule.treatment(), is(TREATMENT));
147 assertThat(rule.timeout(), is(44));
148 }
149
150 /**
151 * Tests the creation of a DefaultFlowRule using an AppId constructor.
152 */
153 @Test
154 public void testCreationWithAppId() {
155 final DefaultFlowRule rule =
156 new DefaultFlowRule(did("1"), SELECTOR,
157 TREATMENT, 22, APP_ID,
158 44, false);
159 assertThat(rule.deviceId(), is(did("1")));
160 assertThat(rule.isPermanent(), is(false));
161 assertThat(rule.priority(), is(22));
162 assertThat(rule.selector(), is(SELECTOR));
163 assertThat(rule.treatment(), is(TREATMENT));
164 assertThat(rule.timeout(), is(44));
165 }
166}