blob: f8dc27aaf7e8fee48faea6a149ac2e3e83a2f07b [file] [log] [blame]
Ray Milkey930fc662014-11-11 16:07:45 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
Ray Milkey930fc662014-11-11 16:07:45 -080017
18import java.util.concurrent.TimeUnit;
19
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.intent.IntentTestsMocks;
Ray Milkey930fc662014-11-11 16:07:45 -080022
23import com.google.common.testing.EqualsTester;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.greaterThan;
27import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import static org.onosproject.net.NetTestTools.did;
Ray Milkey930fc662014-11-11 16:07:45 -080029
30/**
31 * Unit tests for the DefaultFlowEntry class.
32 */
33public class DefaultFlowEntryTest {
34 private static final IntentTestsMocks.MockSelector SELECTOR =
35 new IntentTestsMocks.MockSelector();
36 private static final IntentTestsMocks.MockTreatment TREATMENT =
37 new IntentTestsMocks.MockTreatment();
38
39 private static DefaultFlowEntry makeFlowEntry(int uniqueValue) {
40 return new DefaultFlowEntry(did("id" + Integer.toString(uniqueValue)),
41 SELECTOR,
42 TREATMENT,
43 uniqueValue,
44 FlowEntry.FlowEntryState.ADDED,
45 uniqueValue,
46 uniqueValue,
47 uniqueValue,
48 uniqueValue,
49 uniqueValue);
50 }
51
52 final DefaultFlowEntry defaultFlowEntry1 = makeFlowEntry(1);
53 final DefaultFlowEntry sameAsDefaultFlowEntry1 = makeFlowEntry(1);
54 final DefaultFlowEntry defaultFlowEntry2 = makeFlowEntry(2);
55
56 /**
57 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
58 */
59 @Test
60 public void testEquals() {
61 new EqualsTester()
62 .addEqualityGroup(defaultFlowEntry1, sameAsDefaultFlowEntry1)
63 .addEqualityGroup(defaultFlowEntry2)
64 .testEquals();
65 }
66
67 /**
68 * Tests the construction of a default flow entry from a device id.
69 */
70 @Test
71 public void testDeviceBasedObject() {
72 assertThat(defaultFlowEntry1.deviceId(), is(did("id1")));
73 assertThat(defaultFlowEntry1.selector(), is(SELECTOR));
74 assertThat(defaultFlowEntry1.treatment(), is(TREATMENT));
75 assertThat(defaultFlowEntry1.timeout(), is(1));
76 assertThat(defaultFlowEntry1.life(), is(1L));
77 assertThat(defaultFlowEntry1.packets(), is(1L));
78 assertThat(defaultFlowEntry1.bytes(), is(1L));
79 assertThat(defaultFlowEntry1.state(), is(FlowEntry.FlowEntryState.ADDED));
80 assertThat(defaultFlowEntry1.lastSeen(),
81 greaterThan(System.currentTimeMillis() -
82 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
83 }
84
85 /**
86 * Tests the setters on a default flow entry object.
87 */
88 @Test
89 public void testSetters() {
90 final DefaultFlowEntry entry = makeFlowEntry(1);
91
92 entry.setLastSeen();
93 entry.setState(FlowEntry.FlowEntryState.PENDING_REMOVE);
94 entry.setPackets(11);
95 entry.setBytes(22);
96 entry.setLife(33);
97
98 assertThat(entry.deviceId(), is(did("id1")));
99 assertThat(entry.selector(), is(SELECTOR));
100 assertThat(entry.treatment(), is(TREATMENT));
101 assertThat(entry.timeout(), is(1));
102 assertThat(entry.life(), is(33L));
103 assertThat(entry.packets(), is(11L));
104 assertThat(entry.bytes(), is(22L));
105 assertThat(entry.state(), is(FlowEntry.FlowEntryState.PENDING_REMOVE));
106 assertThat(entry.lastSeen(),
107 greaterThan(System.currentTimeMillis() -
108 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
109 }
110
111 /**
112 * Tests a default flow rule built for an error.
113 */
114 @Test
115 public void testErrorObject() {
116 final DefaultFlowEntry errorEntry =
117 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1),
118 111,
119 222);
120 assertThat(errorEntry.errType(), is(111));
121 assertThat(errorEntry.errCode(), is(222));
122 assertThat(errorEntry.state(), is(FlowEntry.FlowEntryState.FAILED));
123 assertThat(errorEntry.lastSeen(),
124 greaterThan(System.currentTimeMillis() -
125 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
126 }
127
128 /**
129 * Tests a default flow entry constructed from a flow rule.
130 */
131 @Test
132 public void testFlowBasedObject() {
133 final DefaultFlowEntry entry =
134 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(1));
135 assertThat(entry.priority(), is(1));
136 assertThat(entry.appId(), is((short) 0));
137 assertThat(entry.lastSeen(),
138 greaterThan(System.currentTimeMillis() -
139 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
140 }
141
142 /**
143 * Tests a default flow entry constructed from a flow rule plus extra
144 * parameters.
145 */
146 @Test
147 public void testFlowBasedObjectWithParameters() {
148 final DefaultFlowEntry entry =
149 new DefaultFlowEntry(new IntentTestsMocks.MockFlowRule(33),
150 FlowEntry.FlowEntryState.REMOVED,
151 101, 102, 103);
152 assertThat(entry.state(), is(FlowEntry.FlowEntryState.REMOVED));
153 assertThat(entry.life(), is(101L));
154 assertThat(entry.packets(), is(102L));
155 assertThat(entry.bytes(), is(103L));
156 assertThat(entry.lastSeen(),
157 greaterThan(System.currentTimeMillis() -
158 TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS)));
159 }
160}