Ray Milkey

Refactor tests to use shared mock implementations

Change-Id: Iba4e44b7bb88f8a31add9475a9155f85375ae61a
......@@ -15,60 +15,23 @@
*/
package org.onosproject.net.intent;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.core.IdGenerator;
import org.onosproject.net.NetTestTools;
import org.onosproject.store.Timestamp;
import com.google.common.testing.EqualsTester;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
import static org.onosproject.net.intent.IntentTestsMocks.MockTimestamp;
/**
* Unit tests for intent data objects.
*/
public class IntentDataTest {
private static class MockIntent extends Intent {
private static AtomicLong counter = new AtomicLong(0);
private final Long number;
public MockIntent(Long number) {
super(NetTestTools.APP_ID, Collections.emptyList());
this.number = number;
}
public Long number() {
return number;
}
public static Long nextId() {
return counter.getAndIncrement();
}
}
private static class MockTimestamp implements Timestamp {
final int value;
MockTimestamp(int value) {
this.value = value;
}
@Override
public int compareTo(Timestamp o) {
if (!(o instanceof MockTimestamp)) {
return -1;
}
MockTimestamp that = (MockTimestamp) o;
return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
}
}
private Timestamp timestamp1;
private Timestamp timestamp2;
......
......@@ -36,6 +36,7 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.ElementId;
import org.onosproject.net.Link;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.NetworkResource;
import org.onosproject.net.Path;
import org.onosproject.net.flow.FlowId;
import org.onosproject.net.flow.FlowRule;
......@@ -60,6 +61,8 @@ import org.onosproject.net.topology.PathService;
import org.onosproject.net.topology.TopologyVertex;
import org.onosproject.store.Timestamp;
import com.google.common.base.MoreObjects;
/**
* Common mocks used by the intent framework tests.
*/
......@@ -384,6 +387,11 @@ public class IntentTestsMocks {
this.number = number;
}
public MockIntent(Long number, Collection<NetworkResource> resources) {
super(NetTestTools.APP_ID, resources);
this.number = number;
}
public Long number() {
return number;
}
......@@ -391,6 +399,14 @@ public class IntentTestsMocks {
public static Long nextId() {
return counter.getAndIncrement();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("appId", appId())
.toString();
}
}
public static class MockTimestamp implements Timestamp {
......
......@@ -15,12 +15,14 @@
*/
package org.onosproject.net.intent.impl;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
......@@ -44,26 +46,29 @@ import org.onosproject.net.intent.IntentInstaller;
import org.onosproject.net.intent.IntentListener;
import org.onosproject.net.intent.IntentService;
import org.onosproject.net.intent.IntentState;
import org.onosproject.net.intent.IntentTestsMocks;
import org.onosproject.net.intent.Key;
import org.onosproject.net.resource.LinkResourceAllocations;
import org.onosproject.store.intent.impl.SimpleIntentStore;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.onlab.junit.TestTools.assertAfter;
import static org.onlab.util.Tools.delay;
import static org.onosproject.net.intent.IntentState.*;
import static org.onosproject.net.intent.IntentState.FAILED;
import static org.onosproject.net.intent.IntentState.INSTALLED;
import static org.onosproject.net.intent.IntentState.WITHDRAWN;
import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule;
import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
/**
* Test intent manager and transitions.
......@@ -146,25 +151,6 @@ public class IntentManagerTest {
}
}
private static class MockIntent extends Intent {
private static AtomicLong counter = new AtomicLong(0);
private final Long number;
// Nothing new here
public MockIntent(Long number) {
super(APPID, Collections.emptyList());
this.number = number;
}
public Long number() {
return number;
}
public static Long nextId() {
return counter.getAndIncrement();
}
}
private static class MockInstallableIntent extends MockIntent {
public MockInstallableIntent(Long number) {
super(number);
......@@ -195,7 +181,7 @@ public class IntentManagerTest {
private static class TestIntentInstaller implements IntentInstaller<MockInstallableIntent> {
@Override
public List<Collection<org.onosproject.net.flow.FlowRuleOperation>> install(MockInstallableIntent intent) {
FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue());
FlowRule fr = new MockFlowRule(intent.number().intValue());
Set<FlowRuleOperation> rules = ImmutableSet.of(
new FlowRuleOperation(fr, FlowRuleOperation.Type.ADD));
return Lists.newArrayList(ImmutableSet.of(rules));
......@@ -203,7 +189,7 @@ public class IntentManagerTest {
@Override
public List<Collection<FlowRuleOperation>> uninstall(MockInstallableIntent intent) {
FlowRule fr = new IntentTestsMocks.MockFlowRule(intent.number().intValue());
FlowRule fr = new MockFlowRule(intent.number().intValue());
Set<FlowRuleOperation> rules = ImmutableSet.of(
new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE));
return Lists.newArrayList(ImmutableSet.of(rules));
......@@ -212,8 +198,8 @@ public class IntentManagerTest {
@Override
public List<Collection<FlowRuleOperation>> replace(MockInstallableIntent oldIntent,
MockInstallableIntent newIntent) {
FlowRule fr = new IntentTestsMocks.MockFlowRule(oldIntent.number().intValue());
FlowRule fr2 = new IntentTestsMocks.MockFlowRule(newIntent.number().intValue());
FlowRule fr = new MockFlowRule(oldIntent.number().intValue());
FlowRule fr2 = new MockFlowRule(newIntent.number().intValue());
Set<FlowRuleOperation> rules = ImmutableSet.of(
new FlowRuleOperation(fr, FlowRuleOperation.Type.REMOVE),
new FlowRuleOperation(fr2, FlowRuleOperation.Type.ADD));
......
......@@ -15,10 +15,8 @@
*/
package org.onosproject.rest;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicLong;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
......@@ -39,11 +37,11 @@ import org.onosproject.net.NetworkResource;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentService;
import org.onosproject.net.intent.Key;
import org.onosproject.net.intent.MockIdGenerator;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.google.common.base.MoreObjects;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
......@@ -57,6 +55,7 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
/**
* Unit tests for Intents REST APIs.
......@@ -69,37 +68,6 @@ public class IntentsResourceTest extends ResourceTest {
private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test");
private IdGenerator mockGenerator;
/**
* Mock ID generator. This should be refactored to share the one in
* the core/api tests.
*/
public class MockIdGenerator implements IdGenerator {
private AtomicLong nextId = new AtomicLong(0);
@Override
public long getNewId() {
return nextId.getAndIncrement();
}
}
/**
* Mock compilable intent class.
*/
private static class MockIntent extends Intent {
public MockIntent(Collection<NetworkResource> resources) {
super(APP_ID, resources);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("appId", appId())
.toString();
}
}
private class MockResource implements NetworkResource {
int id;
......@@ -305,12 +273,12 @@ public class IntentsResourceTest extends ResourceTest {
public void testIntentsArray() {
replay(mockIntentService);
final Intent intent1 = new MockIntent(Collections.emptyList());
final Intent intent1 = new MockIntent(1L, Collections.emptyList());
final HashSet<NetworkResource> resources = new HashSet<>();
resources.add(new MockResource(1));
resources.add(new MockResource(2));
resources.add(new MockResource(3));
final Intent intent2 = new MockIntent(resources);
final Intent intent2 = new MockIntent(2L, resources);
intents.add(intent1);
intents.add(intent2);
......@@ -340,7 +308,7 @@ public class IntentsResourceTest extends ResourceTest {
resources.add(new MockResource(1));
resources.add(new MockResource(2));
resources.add(new MockResource(3));
final Intent intent = new MockIntent(resources);
final Intent intent = new MockIntent(3L, resources);
intents.add(intent);
......