Ray Milkey

Favor adapter classes over service interfaces in unit tests

In unit tests that are mocking an entire interface, instead
extend the Adapter class so the unused method overrides
are no longer needed.

Change-Id: I6e332cc5cb59410f163deea223fbc94e73d83917
......@@ -20,10 +20,10 @@ import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.cluster.ClusterEventListener;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ClusterServiceAdapter;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.ControllerNode.State;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.event.impl.TestEventDispatcher;
......@@ -32,13 +32,14 @@ import org.onosproject.mastership.MastershipStore;
import org.onosproject.mastership.MastershipTermService;
import org.onosproject.net.DeviceId;
import org.onosproject.store.trivial.impl.SimpleMastershipStore;
import org.onlab.packet.IpAddress;
import com.google.common.collect.Sets;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.onosproject.net.MastershipRole.*;
import static org.onosproject.net.MastershipRole.MASTER;
import static org.onosproject.net.MastershipRole.NONE;
import static org.onosproject.net.MastershipRole.STANDBY;
/**
* Test codifying the mastership service contracts.
......@@ -151,7 +152,7 @@ public class MastershipManagerTest {
assertEquals("inconsistent terms: ", 3, ts.getMastershipTerm(DEV_MASTER).termNumber());
}
private final class TestClusterService implements ClusterService {
private final class TestClusterService extends ClusterServiceAdapter {
ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
......@@ -165,24 +166,6 @@ public class MastershipManagerTest {
return Sets.newHashSet();
}
@Override
public ControllerNode getNode(NodeId nodeId) {
return null;
}
@Override
public State getState(NodeId nodeId) {
return null;
}
@Override
public void addListener(ClusterEventListener listener) {
}
@Override
public void removeListener(ClusterEventListener listener) {
}
}
private final class TestSimpleMastershipStore extends SimpleMastershipStore
......
......@@ -15,42 +15,15 @@
*/
package org.onosproject.core.impl;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.core.CoreServiceAdapter;
import org.onosproject.core.IdGenerator;
import org.onosproject.core.Version;
import java.util.Set;
public class TestCoreManager implements CoreService {
@Override
public Version version() {
return null;
}
@Override
public Set<ApplicationId> getAppIds() {
return null;
}
@Override
public ApplicationId getAppId(Short id) {
return null;
}
@Override
public ApplicationId getAppId(String name) {
return null;
}
@Override
public ApplicationId registerApplication(String identifier) {
return null;
}
public class TestCoreManager extends CoreServiceAdapter {
@Override
public IdGenerator getIdGenerator(String topic) {
IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
return new BlockAllocatorBasedIdGenerator(idBlockAllocator);
}
}
......
......@@ -15,17 +15,20 @@
*/
package org.onosproject.net.device.impl;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.cluster.ClusterEventListener;
import org.onosproject.cluster.ClusterService;
import org.onlab.packet.ChassisId;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ClusterServiceAdapter;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.ControllerNode.State;
import org.onosproject.event.Event;
import org.onosproject.event.impl.TestEventDispatcher;
import org.onosproject.mastership.MastershipServiceAdapter;
......@@ -51,18 +54,22 @@ import org.onosproject.net.device.PortDescription;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.store.trivial.impl.SimpleDeviceStore;
import org.onlab.packet.ChassisId;
import org.onlab.packet.IpAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Sets;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.onosproject.net.Device.Type.SWITCH;
import static org.onosproject.net.DeviceId.deviceId;
import static org.onosproject.net.device.DeviceEvent.Type.*;
import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED;
import static org.onosproject.net.device.DeviceEvent.Type.PORT_ADDED;
import static org.onosproject.net.device.DeviceEvent.Type.PORT_REMOVED;
import static org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED;
/**
* Test codifying the device service & device provider service contracts.
......@@ -310,7 +317,7 @@ public class DeviceManagerTest {
}
// code clone
private final class TestClusterService implements ClusterService {
private final class TestClusterService extends ClusterServiceAdapter {
ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
......@@ -319,28 +326,6 @@ public class DeviceManagerTest {
return local;
}
@Override
public Set<ControllerNode> getNodes() {
return null;
}
@Override
public ControllerNode getNode(NodeId nodeId) {
return null;
}
@Override
public State getState(NodeId nodeId) {
return null;
}
@Override
public void addListener(ClusterEventListener listener) {
}
@Override
public void removeListener(ClusterEventListener listener) {
}
}
private final class TestClockProviderService implements
......
......@@ -32,10 +32,9 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.core.CoreServiceAdapter;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.IdGenerator;
import org.onosproject.core.Version;
import org.onosproject.event.impl.TestEventDispatcher;
import org.onosproject.net.DefaultDevice;
import org.onosproject.net.Device;
......@@ -219,14 +218,14 @@ public class FlowRuleManagerTest {
FlowRule r3 = flowRule(3, 3);
assertTrue("store should be empty",
Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
mgr.applyFlowRules(r1, r2, r3);
assertEquals("3 rules should exist", 3, flowCount());
assertTrue("Entries should be pending add.",
validateState(ImmutableMap.of(
r1, FlowEntryState.PENDING_ADD,
r2, FlowEntryState.PENDING_ADD,
r3, FlowEntryState.PENDING_ADD)));
validateState(ImmutableMap.of(
r1, FlowEntryState.PENDING_ADD,
r2, FlowEntryState.PENDING_ADD,
r3, FlowEntryState.PENDING_ADD)));
}
@Test
......@@ -248,10 +247,10 @@ public class FlowRuleManagerTest {
validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED);
assertEquals("3 rule should exist", 3, flowCount());
assertTrue("Entries should be pending remove.",
validateState(ImmutableMap.of(
f1, FlowEntryState.PENDING_REMOVE,
f2, FlowEntryState.PENDING_REMOVE,
f3, FlowEntryState.ADDED)));
validateState(ImmutableMap.of(
f1, FlowEntryState.PENDING_REMOVE,
f2, FlowEntryState.PENDING_REMOVE,
f3, FlowEntryState.ADDED)));
mgr.removeFlowRules(f1);
assertEquals("3 rule should still exist", 3, flowCount());
......@@ -277,7 +276,7 @@ public class FlowRuleManagerTest {
validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED,
RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
providerService.flowRemoved(fe1);
validateEvents();
......@@ -317,7 +316,7 @@ public class FlowRuleManagerTest {
f3, FlowEntryState.PENDING_ADD)));
validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED,
RULE_ADDED, RULE_ADDED);
RULE_ADDED, RULE_ADDED);
}
@Test
......@@ -374,7 +373,7 @@ public class FlowRuleManagerTest {
mgr.applyFlowRules(f1, f2);
assertTrue("should have two rules",
Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
}
@Test
......@@ -583,31 +582,7 @@ public class FlowRuleManagerTest {
}
}
private class TestCoreService implements CoreService {
@Override
public Version version() {
return null;
}
@Override
public Set<ApplicationId> getAppIds() {
return null;
}
@Override
public ApplicationId getAppId(Short id) {
return null;
}
@Override
public ApplicationId getAppId(String name) {
return null;
}
@Override
public ApplicationId registerApplication(String identifier) {
return null;
}
private class TestCoreService extends CoreServiceAdapter {
@Override
public IdGenerator getIdGenerator(String topic) {
......
......@@ -23,14 +23,13 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.flow.DefaultFlowEntry;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleListener;
import org.onosproject.net.flow.FlowRuleOperations;
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.net.flow.FlowRuleServiceAdapter;
import com.google.common.collect.Sets;
public class MockFlowRuleService implements FlowRuleService {
public class MockFlowRuleService extends FlowRuleServiceAdapter {
final Set<FlowRule> flows = Sets.newHashSet();
boolean success;
......@@ -62,16 +61,6 @@ public class MockFlowRuleService implements FlowRuleService {
}
@Override
public void addListener(FlowRuleListener listener) {
//TODO not implemented
}
@Override
public void removeListener(FlowRuleListener listener) {
//TODO not implemented
}
@Override
public int getFlowRuleCount() {
return flows.size();
}
......@@ -99,11 +88,6 @@ public class MockFlowRuleService implements FlowRuleService {
}
@Override
public void removeFlowRulesById(ApplicationId appId) {
//TODO not implemented
}
@Override
public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
return flows.stream()
.filter(flow -> flow.appId() == id.id())
......
......@@ -19,15 +19,14 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.onosproject.cluster.ClusterEventListener;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ClusterServiceAdapter;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.ControllerNode.State;
import org.onosproject.cluster.NodeId;
import com.google.common.collect.Sets;
public abstract class StaticClusterService implements ClusterService {
public abstract class StaticClusterService extends ClusterServiceAdapter {
protected final Map<NodeId, ControllerNode> nodes = new HashMap<>();
protected final Map<NodeId, ControllerNode.State> nodeStates = new HashMap<>();
......@@ -53,11 +52,4 @@ public abstract class StaticClusterService implements ClusterService {
return nodeStates.get(nodeId);
}
@Override
public void addListener(ClusterEventListener listener) {
}
@Override
public void removeListener(ClusterEventListener listener) {
}
}
......
......@@ -15,10 +15,6 @@
*/
package org.onosproject.store.mastership.impl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.onosproject.net.MastershipRole.*;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
......@@ -30,26 +26,31 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.onosproject.cluster.ClusterEventListener;
import org.onosproject.cluster.ClusterService;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ClusterServiceAdapter;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.ControllerNode.State;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.mastership.MastershipEvent;
import org.onosproject.mastership.MastershipEvent.Type;
import org.onosproject.mastership.MastershipStoreDelegate;
import org.onosproject.mastership.MastershipTerm;
import org.onosproject.mastership.MastershipEvent.Type;
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.store.hz.StoreManager;
import org.onosproject.store.hz.StoreService;
import org.onosproject.store.hz.TestStoreManager;
import org.onosproject.store.serializers.KryoSerializer;
import org.onlab.packet.IpAddress;
import com.google.common.collect.Sets;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.onosproject.net.MastershipRole.MASTER;
import static org.onosproject.net.MastershipRole.NONE;
import static org.onosproject.net.MastershipRole.STANDBY;
/**
* Test of the Hazelcast-based distributed MastershipStore implementation.
*/
......@@ -300,7 +301,7 @@ public class DistributedMastershipStoreTest {
}
}
private class TestClusterService implements ClusterService {
private class TestClusterService extends ClusterServiceAdapter {
protected ControllerNode current;
......@@ -314,24 +315,6 @@ public class DistributedMastershipStoreTest {
return Sets.newHashSet(CN1, CN2);
}
@Override
public ControllerNode getNode(NodeId nodeId) {
return null;
}
@Override
public State getState(NodeId nodeId) {
return null;
}
@Override
public void addListener(ClusterEventListener listener) {
}
@Override
public void removeListener(ClusterEventListener listener) {
}
}
}
......
......@@ -27,27 +27,24 @@ import org.onosproject.codec.impl.CodecManager;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.Path;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.net.topology.ClusterId;
import org.onosproject.net.topology.DefaultTopologyCluster;
import org.onosproject.net.topology.DefaultTopologyVertex;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyCluster;
import org.onosproject.net.topology.TopologyGraph;
import org.onosproject.net.topology.TopologyListener;
import org.onosproject.net.topology.TopologyService;
import org.onosproject.net.topology.TopologyServiceAdapter;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.google.common.collect.ImmutableSet;
import com.sun.jersey.api.client.WebResource;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.onosproject.net.NetTestTools.did;
import static org.onosproject.net.NetTestTools.link;
......@@ -89,7 +86,7 @@ public class TopologyResourceTest extends ResourceTest {
}
}
private static class MockTopologyService implements TopologyService {
private static class MockTopologyService extends TopologyServiceAdapter {
final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode"));
final Topology topology = new MockTopology();
final TopologyCluster cluster1 =
......@@ -105,16 +102,6 @@ public class TopologyResourceTest extends ResourceTest {
}
@Override
public boolean isLatest(Topology topology) {
return true;
}
@Override
public TopologyGraph getGraph(Topology topology) {
return null;
}
@Override
public Set<TopologyCluster> getClusters(Topology topology) {
return ImmutableSet.of(cluster1, cluster2);
}
......@@ -141,16 +128,6 @@ public class TopologyResourceTest extends ResourceTest {
}
@Override
public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
return null;
}
@Override
public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
return null;
}
@Override
public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
return connectPoint.elementId().toString().equals("dev2");
}
......@@ -160,15 +137,6 @@ public class TopologyResourceTest extends ResourceTest {
return connectPoint.elementId().toString().equals("dev1");
}
@Override
public void addListener(TopologyListener listener) {
}
@Override
public void removeListener(TopologyListener listener) {
}
}
/**
......