blob: 7ad8f7070e240e04821c0631ef10c2b9407ae672 [file] [log] [blame]
Ray Milkey1e207112014-11-11 10:38:00 -08001/*
jcc3d4e14a2015-04-21 11:32:05 +08002 * Copyright 2014 Open Networking Laboratory
Ray Milkey1e207112014-11-11 10:38:00 -08003 *
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
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.net.flow;
Ray Milkey1e207112014-11-11 10:38:00 -080018
19import org.junit.Test;
jcc3d4e14a2015-04-21 11:32:05 +080020import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkey1e207112014-11-11 10:38:00 -080022
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import static org.onosproject.net.NetTestTools.APP_ID;
29import static org.onosproject.net.NetTestTools.did;
Ray Milkey1e207112014-11-11 10:38:00 -080030
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();
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000037 private static final IntentTestsMocks.MockTreatment TREATMENT =
38 new IntentTestsMocks.MockTreatment();
Ray Milkey1e207112014-11-11 10:38:00 -080039
jcc3d4e14a2015-04-21 11:32:05 +080040 private static byte [] b = new byte[3];
41 private static FlowRuleExtPayLoad payLoad = FlowRuleExtPayLoad.flowRuleExtPayLoad(b);
42 final FlowRule flowRule1 = new IntentTestsMocks.MockFlowRule(1, payLoad);
43 final FlowRule sameAsFlowRule1 = new IntentTestsMocks.MockFlowRule(1, payLoad);
44 final FlowRule flowRule2 = new IntentTestsMocks.MockFlowRule(2, payLoad);
Ray Milkey1e207112014-11-11 10:38:00 -080045 final DefaultFlowRule defaultFlowRule1 = new DefaultFlowRule(flowRule1);
46 final DefaultFlowRule sameAsDefaultFlowRule1 = new DefaultFlowRule(sameAsFlowRule1);
47 final DefaultFlowRule defaultFlowRule2 = new DefaultFlowRule(flowRule2);
48
Ray Milkey1e207112014-11-11 10:38:00 -080049 /**
50 * Checks that the DefaultFlowRule class is immutable but can be inherited
51 * from.
52 */
53 @Test
54 public void testImmutability() {
55 assertThatClassIsImmutableBaseClass(DefaultFlowRule.class);
56 }
57
58 /**
59 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
60 */
61 @Test
62 public void testEquals() {
63 new EqualsTester()
64 .addEqualityGroup(defaultFlowRule1, sameAsDefaultFlowRule1)
Ray Milkey1e207112014-11-11 10:38:00 -080065 .testEquals();
66 }
67
68 /**
69 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
70 */
71 @Test
72 public void testCreationFromFlowRule() {
73 assertThat(defaultFlowRule1.deviceId(), is(flowRule1.deviceId()));
74 assertThat(defaultFlowRule1.appId(), is(flowRule1.appId()));
75 assertThat(defaultFlowRule1.id(), is(flowRule1.id()));
76 assertThat(defaultFlowRule1.isPermanent(), is(flowRule1.isPermanent()));
77 assertThat(defaultFlowRule1.priority(), is(flowRule1.priority()));
78 assertThat(defaultFlowRule1.selector(), is(flowRule1.selector()));
79 assertThat(defaultFlowRule1.treatment(), is(flowRule1.treatment()));
80 assertThat(defaultFlowRule1.timeout(), is(flowRule1.timeout()));
jcc3d4e14a2015-04-21 11:32:05 +080081 assertThat(defaultFlowRule1.payLoad(), is(flowRule1.payLoad()));
Ray Milkey1e207112014-11-11 10:38:00 -080082 }
83
84 /**
85 * Tests creation of a DefaultFlowRule using a FlowId constructor.
86 */
87 @Test
88 public void testCreationWithFlowId() {
89 final DefaultFlowRule rule =
90 new DefaultFlowRule(did("1"), SELECTOR,
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000091 TREATMENT, 22, 33,
Ray Milkey1e207112014-11-11 10:38:00 -080092 44, false);
93 assertThat(rule.deviceId(), is(did("1")));
94 assertThat(rule.id().value(), is(33L));
95 assertThat(rule.isPermanent(), is(false));
96 assertThat(rule.priority(), is(22));
97 assertThat(rule.selector(), is(SELECTOR));
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +000098 assertThat(rule.treatment(), is(TREATMENT));
Ray Milkey1e207112014-11-11 10:38:00 -080099 assertThat(rule.timeout(), is(44));
100 }
101
102 /**
jcc3d4e14a2015-04-21 11:32:05 +0800103 * Tests creation of a DefaultFlowRule using a PayLoad constructor.
104 */
105 @Test
106 public void testCreationWithPayLoadByFlowTable() {
107 final DefaultFlowRule rule =
108 new DefaultFlowRule(did("1"), null,
109 null, 22, APP_ID,
110 44, false, payLoad);
111 assertThat(rule.deviceId(), is(did("1")));
112 assertThat(rule.isPermanent(), is(false));
113 assertThat(rule.priority(), is(22));
114 assertThat(rule.timeout(), is(44));
115 assertThat(defaultFlowRule1.payLoad(), is(payLoad));
116 }
117
118 /**
119 * Tests creation of a DefaultFlowRule using a PayLoad constructor.
120 */
121 @Test
122 public void testCreationWithPayLoadByGroupTable() {
123 final DefaultFlowRule rule =
124 new DefaultFlowRule(did("1"), null,
125 null, 22, APP_ID, new DefaultGroupId(0),
126 44, false, payLoad);
127 assertThat(rule.deviceId(), is(did("1")));
128 assertThat(rule.isPermanent(), is(false));
129 assertThat(rule.priority(), is(22));
130 assertThat(rule.timeout(), is(44));
131 assertThat(rule.groupId(), is(new DefaultGroupId(0)));
132 assertThat(defaultFlowRule1.payLoad(), is(payLoad));
133 }
134 /**
Ray Milkey1e207112014-11-11 10:38:00 -0800135 * Tests the creation of a DefaultFlowRule using an AppId constructor.
136 */
137 @Test
138 public void testCreationWithAppId() {
139 final DefaultFlowRule rule =
140 new DefaultFlowRule(did("1"), SELECTOR,
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +0000141 TREATMENT, 22, APP_ID,
Ray Milkey1e207112014-11-11 10:38:00 -0800142 44, false);
143 assertThat(rule.deviceId(), is(did("1")));
144 assertThat(rule.isPermanent(), is(false));
145 assertThat(rule.priority(), is(22));
146 assertThat(rule.selector(), is(SELECTOR));
Ayaka Koshibeb1224ab2015-01-31 00:48:58 +0000147 assertThat(rule.treatment(), is(TREATMENT));
Ray Milkey1e207112014-11-11 10:38:00 -0800148 assertThat(rule.timeout(), is(44));
149 }
Ray Milkey1e207112014-11-11 10:38:00 -0800150}