Ray Milkey

Refactor tests to use shared mock implementations

Change-Id: Iba4e44b7bb88f8a31add9475a9155f85375ae61a
...@@ -15,60 +15,23 @@ ...@@ -15,60 +15,23 @@
15 */ 15 */
16 package org.onosproject.net.intent; 16 package org.onosproject.net.intent;
17 17
18 -import java.util.Collections;
19 -import java.util.concurrent.atomic.AtomicLong;
20 -
21 import org.junit.After; 18 import org.junit.After;
22 import org.junit.Before; 19 import org.junit.Before;
23 import org.junit.Test; 20 import org.junit.Test;
24 import org.onosproject.core.IdGenerator; 21 import org.onosproject.core.IdGenerator;
25 -import org.onosproject.net.NetTestTools;
26 import org.onosproject.store.Timestamp; 22 import org.onosproject.store.Timestamp;
27 23
28 import com.google.common.testing.EqualsTester; 24 import com.google.common.testing.EqualsTester;
29 25
30 import static org.hamcrest.MatcherAssert.assertThat; 26 import static org.hamcrest.MatcherAssert.assertThat;
31 import static org.hamcrest.Matchers.is; 27 import static org.hamcrest.Matchers.is;
28 +import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
29 +import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
32 30
33 /** 31 /**
34 * Unit tests for intent data objects. 32 * Unit tests for intent data objects.
35 */ 33 */
36 public class IntentDataTest { 34 public class IntentDataTest {
37 - private static class MockIntent extends Intent {
38 - private static AtomicLong counter = new AtomicLong(0);
39 -
40 - private final Long number;
41 -
42 - public MockIntent(Long number) {
43 - super(NetTestTools.APP_ID, Collections.emptyList());
44 - this.number = number;
45 - }
46 -
47 - public Long number() {
48 - return number;
49 - }
50 -
51 - public static Long nextId() {
52 - return counter.getAndIncrement();
53 - }
54 - }
55 -
56 - private static class MockTimestamp implements Timestamp {
57 - final int value;
58 -
59 - MockTimestamp(int value) {
60 - this.value = value;
61 - }
62 -
63 - @Override
64 - public int compareTo(Timestamp o) {
65 - if (!(o instanceof MockTimestamp)) {
66 - return -1;
67 - }
68 - MockTimestamp that = (MockTimestamp) o;
69 - return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
70 - }
71 - }
72 35
73 private Timestamp timestamp1; 36 private Timestamp timestamp1;
74 private Timestamp timestamp2; 37 private Timestamp timestamp2;
......
...@@ -36,6 +36,7 @@ import org.onosproject.net.DeviceId; ...@@ -36,6 +36,7 @@ import org.onosproject.net.DeviceId;
36 import org.onosproject.net.ElementId; 36 import org.onosproject.net.ElementId;
37 import org.onosproject.net.Link; 37 import org.onosproject.net.Link;
38 import org.onosproject.net.NetTestTools; 38 import org.onosproject.net.NetTestTools;
39 +import org.onosproject.net.NetworkResource;
39 import org.onosproject.net.Path; 40 import org.onosproject.net.Path;
40 import org.onosproject.net.flow.FlowId; 41 import org.onosproject.net.flow.FlowId;
41 import org.onosproject.net.flow.FlowRule; 42 import org.onosproject.net.flow.FlowRule;
...@@ -60,6 +61,8 @@ import org.onosproject.net.topology.PathService; ...@@ -60,6 +61,8 @@ import org.onosproject.net.topology.PathService;
60 import org.onosproject.net.topology.TopologyVertex; 61 import org.onosproject.net.topology.TopologyVertex;
61 import org.onosproject.store.Timestamp; 62 import org.onosproject.store.Timestamp;
62 63
64 +import com.google.common.base.MoreObjects;
65 +
63 /** 66 /**
64 * Common mocks used by the intent framework tests. 67 * Common mocks used by the intent framework tests.
65 */ 68 */
...@@ -384,6 +387,11 @@ public class IntentTestsMocks { ...@@ -384,6 +387,11 @@ public class IntentTestsMocks {
384 this.number = number; 387 this.number = number;
385 } 388 }
386 389
390 + public MockIntent(Long number, Collection<NetworkResource> resources) {
391 + super(NetTestTools.APP_ID, resources);
392 + this.number = number;
393 + }
394 +
387 public Long number() { 395 public Long number() {
388 return number; 396 return number;
389 } 397 }
...@@ -391,6 +399,14 @@ public class IntentTestsMocks { ...@@ -391,6 +399,14 @@ public class IntentTestsMocks {
391 public static Long nextId() { 399 public static Long nextId() {
392 return counter.getAndIncrement(); 400 return counter.getAndIncrement();
393 } 401 }
402 +
403 + @Override
404 + public String toString() {
405 + return MoreObjects.toStringHelper(getClass())
406 + .add("id", id())
407 + .add("appId", appId())
408 + .toString();
409 + }
394 } 410 }
395 411
396 public static class MockTimestamp implements Timestamp { 412 public static class MockTimestamp implements Timestamp {
......
...@@ -15,12 +15,14 @@ ...@@ -15,12 +15,14 @@
15 */ 15 */
16 package org.onosproject.net.intent.impl; 16 package org.onosproject.net.intent.impl;
17 17
18 -import com.google.common.collect.HashMultimap; 18 +import java.util.Collection;
19 -import com.google.common.collect.ImmutableSet; 19 +import java.util.Collections;
20 -import com.google.common.collect.Lists; 20 +import java.util.List;
21 -import com.google.common.collect.Maps; 21 +import java.util.Map;
22 -import com.google.common.collect.Multimap; 22 +import java.util.Set;
23 -import com.google.common.collect.Sets; 23 +import java.util.concurrent.CountDownLatch;
24 +import java.util.concurrent.TimeUnit;
25 +
24 import org.hamcrest.Description; 26 import org.hamcrest.Description;
25 import org.hamcrest.TypeSafeMatcher; 27 import org.hamcrest.TypeSafeMatcher;
26 import org.junit.After; 28 import org.junit.After;
...@@ -44,26 +46,29 @@ import org.onosproject.net.intent.IntentInstaller; ...@@ -44,26 +46,29 @@ import org.onosproject.net.intent.IntentInstaller;
44 import org.onosproject.net.intent.IntentListener; 46 import org.onosproject.net.intent.IntentListener;
45 import org.onosproject.net.intent.IntentService; 47 import org.onosproject.net.intent.IntentService;
46 import org.onosproject.net.intent.IntentState; 48 import org.onosproject.net.intent.IntentState;
47 -import org.onosproject.net.intent.IntentTestsMocks;
48 import org.onosproject.net.intent.Key; 49 import org.onosproject.net.intent.Key;
49 import org.onosproject.net.resource.LinkResourceAllocations; 50 import org.onosproject.net.resource.LinkResourceAllocations;
50 import org.onosproject.store.intent.impl.SimpleIntentStore; 51 import org.onosproject.store.intent.impl.SimpleIntentStore;
51 52
52 -import java.util.Collection; 53 +import com.google.common.collect.HashMultimap;
53 -import java.util.Collections; 54 +import com.google.common.collect.ImmutableSet;
54 -import java.util.List; 55 +import com.google.common.collect.Lists;
55 -import java.util.Map; 56 +import com.google.common.collect.Maps;
56 -import java.util.Set; 57 +import com.google.common.collect.Multimap;
57 -import java.util.concurrent.CountDownLatch; 58 +import com.google.common.collect.Sets;
58 -import java.util.concurrent.TimeUnit;
59 -import java.util.concurrent.atomic.AtomicLong;
60 59
61 import static org.hamcrest.MatcherAssert.assertThat; 60 import static org.hamcrest.MatcherAssert.assertThat;
62 import static org.hamcrest.Matchers.hasSize; 61 import static org.hamcrest.Matchers.hasSize;
63 -import static org.junit.Assert.*; 62 +import static org.junit.Assert.assertEquals;
63 +import static org.junit.Assert.assertNotNull;
64 +import static org.junit.Assert.assertTrue;
64 import static org.onlab.junit.TestTools.assertAfter; 65 import static org.onlab.junit.TestTools.assertAfter;
65 import static org.onlab.util.Tools.delay; 66 import static org.onlab.util.Tools.delay;
66 -import static org.onosproject.net.intent.IntentState.*; 67 +import static org.onosproject.net.intent.IntentState.FAILED;
68 +import static org.onosproject.net.intent.IntentState.INSTALLED;
69 +import static org.onosproject.net.intent.IntentState.WITHDRAWN;
70 +import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule;
71 +import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
67 72
68 /** 73 /**
69 * Test intent manager and transitions. 74 * Test intent manager and transitions.
...@@ -146,25 +151,6 @@ public class IntentManagerTest { ...@@ -146,25 +151,6 @@ public class IntentManagerTest {
146 } 151 }
147 } 152 }
148 153
149 - private static class MockIntent extends Intent {
150 - private static AtomicLong counter = new AtomicLong(0);
151 -
152 - private final Long number;
153 - // Nothing new here
154 - public MockIntent(Long number) {
155 - super(APPID, Collections.emptyList());
156 - this.number = number;
157 - }
158 -
159 - public Long number() {
160 - return number;
161 - }
162 -
163 - public static Long nextId() {
164 - return counter.getAndIncrement();
165 - }
166 - }
167 -
168 private static class MockInstallableIntent extends MockIntent { 154 private static class MockInstallableIntent extends MockIntent {
169 public MockInstallableIntent(Long number) { 155 public MockInstallableIntent(Long number) {
170 super(number); 156 super(number);
...@@ -195,7 +181,7 @@ public class IntentManagerTest { ...@@ -195,7 +181,7 @@ public class IntentManagerTest {
195 private static class TestIntentInstaller implements IntentInstaller<MockInstallableIntent> { 181 private static class TestIntentInstaller implements IntentInstaller<MockInstallableIntent> {
196 @Override 182 @Override
197 public List<Collection<org.onosproject.net.flow.FlowRuleOperation>> install(MockInstallableIntent intent) { 183 public List<Collection<org.onosproject.net.flow.FlowRuleOperation>> install(MockInstallableIntent intent) {
198 - FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue()); 184 + FlowRule fr = new MockFlowRule(intent.number().intValue());
199 Set<FlowRuleOperation> rules = ImmutableSet.of( 185 Set<FlowRuleOperation> rules = ImmutableSet.of(
200 new FlowRuleOperation(fr, FlowRuleOperation.Type.ADD)); 186 new FlowRuleOperation(fr, FlowRuleOperation.Type.ADD));
201 return Lists.newArrayList(ImmutableSet.of(rules)); 187 return Lists.newArrayList(ImmutableSet.of(rules));
...@@ -203,7 +189,7 @@ public class IntentManagerTest { ...@@ -203,7 +189,7 @@ public class IntentManagerTest {
203 189
204 @Override 190 @Override
205 public List<Collection<FlowRuleOperation>> uninstall(MockInstallableIntent intent) { 191 public List<Collection<FlowRuleOperation>> uninstall(MockInstallableIntent intent) {
206 - FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue()); 192 + FlowRule fr = new MockFlowRule(intent.number().intValue());
207 Set<FlowRuleOperation> rules = ImmutableSet.of( 193 Set<FlowRuleOperation> rules = ImmutableSet.of(
208 new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE)); 194 new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE));
209 return Lists.newArrayList(ImmutableSet.of(rules)); 195 return Lists.newArrayList(ImmutableSet.of(rules));
...@@ -212,8 +198,8 @@ public class IntentManagerTest { ...@@ -212,8 +198,8 @@ public class IntentManagerTest {
212 @Override 198 @Override
213 public List<Collection<FlowRuleOperation>> replace(MockInstallableIntent oldIntent, 199 public List<Collection<FlowRuleOperation>> replace(MockInstallableIntent oldIntent,
214 MockInstallableIntent newIntent) { 200 MockInstallableIntent newIntent) {
215 - FlowRule fr = new IntentTestsMocks.MockFlowRule(oldIntent.number().intValue()); 201 + FlowRule fr = new MockFlowRule(oldIntent.number().intValue());
216 - FlowRule fr2 = new IntentTestsMocks.MockFlowRule(newIntent.number().intValue()); 202 + FlowRule fr2 = new MockFlowRule(newIntent.number().intValue());
217 Set<FlowRuleOperation> rules = ImmutableSet.of( 203 Set<FlowRuleOperation> rules = ImmutableSet.of(
218 new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE), 204 new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE),
219 new FlowRuleOperation(fr2, FlowRuleOperation.Type.ADD)); 205 new FlowRuleOperation(fr2, FlowRuleOperation.Type.ADD));
......
...@@ -15,10 +15,8 @@ ...@@ -15,10 +15,8 @@
15 */ 15 */
16 package org.onosproject.rest; 16 package org.onosproject.rest;
17 17
18 -import java.util.Collection;
19 import java.util.Collections; 18 import java.util.Collections;
20 import java.util.HashSet; 19 import java.util.HashSet;
21 -import java.util.concurrent.atomic.AtomicLong;
22 20
23 import org.hamcrest.Description; 21 import org.hamcrest.Description;
24 import org.hamcrest.TypeSafeMatcher; 22 import org.hamcrest.TypeSafeMatcher;
...@@ -39,11 +37,11 @@ import org.onosproject.net.NetworkResource; ...@@ -39,11 +37,11 @@ import org.onosproject.net.NetworkResource;
39 import org.onosproject.net.intent.Intent; 37 import org.onosproject.net.intent.Intent;
40 import org.onosproject.net.intent.IntentService; 38 import org.onosproject.net.intent.IntentService;
41 import org.onosproject.net.intent.Key; 39 import org.onosproject.net.intent.Key;
40 +import org.onosproject.net.intent.MockIdGenerator;
42 41
43 import com.eclipsesource.json.JsonArray; 42 import com.eclipsesource.json.JsonArray;
44 import com.eclipsesource.json.JsonObject; 43 import com.eclipsesource.json.JsonObject;
45 import com.eclipsesource.json.JsonValue; 44 import com.eclipsesource.json.JsonValue;
46 -import com.google.common.base.MoreObjects;
47 import com.sun.jersey.api.client.UniformInterfaceException; 45 import com.sun.jersey.api.client.UniformInterfaceException;
48 import com.sun.jersey.api.client.WebResource; 46 import com.sun.jersey.api.client.WebResource;
49 47
...@@ -57,6 +55,7 @@ import static org.hamcrest.Matchers.is; ...@@ -57,6 +55,7 @@ import static org.hamcrest.Matchers.is;
57 import static org.hamcrest.Matchers.notNullValue; 55 import static org.hamcrest.Matchers.notNullValue;
58 import static org.junit.Assert.assertThat; 56 import static org.junit.Assert.assertThat;
59 import static org.junit.Assert.fail; 57 import static org.junit.Assert.fail;
58 +import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
60 59
61 /** 60 /**
62 * Unit tests for Intents REST APIs. 61 * Unit tests for Intents REST APIs.
...@@ -69,37 +68,6 @@ public class IntentsResourceTest extends ResourceTest { ...@@ -69,37 +68,6 @@ public class IntentsResourceTest extends ResourceTest {
69 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test"); 68 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test");
70 private IdGenerator mockGenerator; 69 private IdGenerator mockGenerator;
71 70
72 - /**
73 - * Mock ID generator. This should be refactored to share the one in
74 - * the core/api tests.
75 - */
76 - public class MockIdGenerator implements IdGenerator {
77 - private AtomicLong nextId = new AtomicLong(0);
78 -
79 - @Override
80 - public long getNewId() {
81 - return nextId.getAndIncrement();
82 - }
83 - }
84 -
85 - /**
86 - * Mock compilable intent class.
87 - */
88 - private static class MockIntent extends Intent {
89 -
90 - public MockIntent(Collection<NetworkResource> resources) {
91 - super(APP_ID, resources);
92 - }
93 -
94 - @Override
95 - public String toString() {
96 - return MoreObjects.toStringHelper(getClass())
97 - .add("id", id())
98 - .add("appId", appId())
99 - .toString();
100 - }
101 - }
102 -
103 private class MockResource implements NetworkResource { 71 private class MockResource implements NetworkResource {
104 int id; 72 int id;
105 73
...@@ -305,12 +273,12 @@ public class IntentsResourceTest extends ResourceTest { ...@@ -305,12 +273,12 @@ public class IntentsResourceTest extends ResourceTest {
305 public void testIntentsArray() { 273 public void testIntentsArray() {
306 replay(mockIntentService); 274 replay(mockIntentService);
307 275
308 - final Intent intent1 = new MockIntent(Collections.emptyList()); 276 + final Intent intent1 = new MockIntent(1L, Collections.emptyList());
309 final HashSet<NetworkResource> resources = new HashSet<>(); 277 final HashSet<NetworkResource> resources = new HashSet<>();
310 resources.add(new MockResource(1)); 278 resources.add(new MockResource(1));
311 resources.add(new MockResource(2)); 279 resources.add(new MockResource(2));
312 resources.add(new MockResource(3)); 280 resources.add(new MockResource(3));
313 - final Intent intent2 = new MockIntent(resources); 281 + final Intent intent2 = new MockIntent(2L, resources);
314 282
315 intents.add(intent1); 283 intents.add(intent1);
316 intents.add(intent2); 284 intents.add(intent2);
...@@ -340,7 +308,7 @@ public class IntentsResourceTest extends ResourceTest { ...@@ -340,7 +308,7 @@ public class IntentsResourceTest extends ResourceTest {
340 resources.add(new MockResource(1)); 308 resources.add(new MockResource(1));
341 resources.add(new MockResource(2)); 309 resources.add(new MockResource(2));
342 resources.add(new MockResource(3)); 310 resources.add(new MockResource(3));
343 - final Intent intent = new MockIntent(resources); 311 + final Intent intent = new MockIntent(3L, resources);
344 312
345 intents.add(intent); 313 intents.add(intent);
346 314
......