Refactor Unit Tests handling of Metrics and web server removal
- Created a new base class for Unit tests (UnitTest.java).
This handles destruction of Metrics in an @After method
so individual tests don't need to know about the Metrics
lifecycle.
- Added @After methods to REST unit test base classes
to shut down the web server.
- Removed dependencies on PowerMock for tests that are not using mocking
Change-Id: Ib46e8675998c0520977e59b749cbba89456100ab
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRest.java b/src/test/java/net/onrc/onos/api/rest/TestRest.java
index 76792ab..d6b64c4 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRest.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRest.java
@@ -3,9 +3,12 @@
import com.codahale.metrics.MetricFilter;
import net.floodlightcontroller.restserver.RestletRoutable;
import net.onrc.onos.core.metrics.OnosMetrics;
+import net.onrc.onos.core.util.UnitTest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import org.junit.After;
+import org.junit.Before;
import org.restlet.resource.ClientResource;
import java.util.LinkedList;
@@ -22,7 +25,7 @@
* See TestRestIntent as an example of an implementation which uses the
* TestRest framework.
*/
-public class TestRest {
+public class TestRest extends UnitTest {
private final List<RestletRoutable> restlets = new LinkedList<>();
private TestRestApiServer restApiServer;
@@ -60,7 +63,9 @@
/**
* Set up the REST API web server and start it.
*/
+ @Before
public void setUp() {
+ setRestPort(generateRandomPort());
restApiServer = new TestRestApiServer(restPort);
restApiServer.startServer(restlets);
}
@@ -69,8 +74,8 @@
* Remove anything that will interfere with the next test running correctly.
* Shuts down the test REST web server.
*/
-
- public void tearDown() {
+ @After
+ public void tearDownRest() {
getRestApiServer().stopServer();
OnosMetrics.removeMatching(MetricFilter.ALL);
}
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestIntent.java b/src/test/java/net/onrc/onos/api/rest/TestRestIntent.java
index 8d6dafd..dabcd1a 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestIntent.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestIntent.java
@@ -8,6 +8,7 @@
import net.onrc.onos.core.intent.runtime.IntentTestMocks;
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable;
+import org.junit.After;
import org.restlet.resource.ClientResource;
import java.util.Collection;
@@ -71,10 +72,9 @@
* Remove anything that will interfere with the next test running correctly.
* Shuts down the test REST web server and removes the mocks.
*/
- @Override
- public void tearDown() {
+ @After
+ public void tearDownMocks() {
getMocks().tearDownIntentMocks();
- super.tearDown();
}
/**
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighDelete.java b/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighDelete.java
index 89ab771..d91ead8 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighDelete.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighDelete.java
@@ -5,9 +5,7 @@
import net.onrc.onos.core.intent.IntentOperationList;
import net.onrc.onos.core.intent.ShortestPathIntent;
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
-import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -32,28 +30,6 @@
private static final Long LOCAL_PORT = 0xFFFEL;
private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
-
- /**
- * Create the web server, PathCalcRuntime, and mocks required for
- * all of the tests.
- */
- @Before
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- public void afterTest() {
- tearDown();
- }
-
-
/**
* Make a set of Intents that can be used as test data.
*/
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighGet.java b/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighGet.java
index 58e9bfa..d87325a 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighGet.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighGet.java
@@ -6,9 +6,7 @@
import net.onrc.onos.core.intent.IntentOperationList;
import net.onrc.onos.core.intent.ShortestPathIntent;
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
-import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -41,27 +39,6 @@
private static final String IP_ADDRESS_3 = "127.0.0.3";
/**
- * Create the web server, PathCalcRuntime, and mocks required for
- * all of the tests.
- */
- @Before
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- public void afterTest() {
- tearDown();
- }
-
-
- /**
* Make a set of Intents that can be used as test data.
*/
private void makeDefaultIntents() {
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighPost.java b/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighPost.java
index ce6d461..5e0ca9d 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighPost.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestIntentHighPost.java
@@ -3,8 +3,6 @@
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
import org.json.JSONArray;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -42,27 +40,6 @@
"}" +
"]";
-
- /**
- * Create the web server, PathCalcRuntime, and mocks required for
- * all of the tests.
- */
- @Before
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- public void afterTest() {
- tearDown();
- }
-
/**
* Test that a POST operation to create a high level Intent
* creates an object correctly.
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestIntentLowGet.java b/src/test/java/net/onrc/onos/api/rest/TestRestIntentLowGet.java
index a13c75d..065f342 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestIntentLowGet.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestIntentLowGet.java
@@ -9,9 +9,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -40,27 +38,6 @@
private static final String IP_ADDRESS_3 = "127.0.0.3";
/**
- * Create the web server, PathCalcRuntime, and mocks required for
- * all of the tests.
- */
- @Before
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- public void afterTest() {
- tearDown();
- }
-
-
- /**
* Make a set of Intents that can be used as test data.
*/
private void makeDefaultIntents() {
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetrics.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetrics.java
index 6796578..c5d0e2f 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetrics.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetrics.java
@@ -28,16 +28,6 @@
}
/**
- * Removes anything that will interfere with the next test running
- * correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @Override
- public void tearDown() {
- super.tearDown();
- }
-
- /**
* Fetches the base URL for Metrics REST APIs.
*
* @return base URL
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java
index 3eef047..8d2142f 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsCounters.java
@@ -5,11 +5,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -20,30 +16,8 @@
/**
* Unit tests for REST APIs for Counter Metrics.
*/
-@RunWith(PowerMockRunner.class)
public class TestRestMetricsCounters extends TestRestMetrics {
- /**
- * Create the web server and mocks required for
- * all of the tests.
- */
- @Before
- @SuppressWarnings("ununsed")
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- tearDown();
- }
-
// Test Counter data objects
private static final OnosMetrics.MetricsComponent COMPONENT =
OnosMetrics.registerComponent("MetricsUnitTests");
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java
index 4be1a5c..caded72 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsFilters.java
@@ -1,16 +1,12 @@
package net.onrc.onos.api.rest;
import com.codahale.metrics.Gauge;
-import com.codahale.metrics.MetricFilter;
import net.onrc.onos.core.metrics.OnosMetrics;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -21,8 +17,6 @@
/**
* Unit tests for filtering REST APIs for Timer Metrics.
*/
-
-@RunWith(PowerMockRunner.class)
public class TestRestMetricsFilters extends TestRestMetrics {
/**
@@ -32,22 +26,10 @@
@Before
@SuppressWarnings("ununsed")
public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
// Make some test data
createMetrics();
}
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- destroyMetrics();
- tearDown();
- }
// Test data objects
private static final OnosMetrics.MetricsComponent COMPONENT =
@@ -186,13 +168,6 @@
}
/**
- * Removes the Metrics to clean up for the next test run.
- */
- private void destroyMetrics() {
- OnosMetrics.removeMatching(MetricFilter.ALL);
- }
-
- /**
* Tests that query of non existant name returns nothing.
*
* @throws JSONException if any of the JSON processing fails.
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java
index 205f527..481f86c 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsGauges.java
@@ -5,11 +5,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -20,30 +16,8 @@
/**
* Unit tests for REST APIs for Gauges Metrics.
*/
-@RunWith(PowerMockRunner.class)
public class TestRestMetricsGauges extends TestRestMetrics {
- /**
- * Create the web server and mocks required for
- * all of the tests.
- */
- @Before
- @SuppressWarnings("ununsed")
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- tearDown();
- }
-
// Test data for Gauges
private static final OnosMetrics.MetricsComponent COMPONENT =
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java
index c3fd01c..083edbb 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsHistograms.java
@@ -5,11 +5,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
import java.util.Arrays;
@@ -25,30 +21,8 @@
/**
* Unit tests for REST APIs for Histogram Metrics.
*/
-@RunWith(PowerMockRunner.class)
public class TestRestMetricsHistograms extends TestRestMetrics {
- /**
- * Create the web server and mocks required for
- * all of the tests.
- */
- @Before
- @SuppressWarnings("ununsed")
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- tearDown();
- }
-
// Test data for Histograms
private static final OnosMetrics.MetricsComponent COMPONENT =
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java
index 88b459b..c8d5d8e 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsMeters.java
@@ -5,11 +5,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -23,33 +19,10 @@
/**
* Unit tests for REST APIs for Meter Metrics.
*/
-@RunWith(PowerMockRunner.class)
public class TestRestMetricsMeters extends TestRestMetrics {
- /**
- * Create the web server and mocks required for
- * all of the tests.
- */
- @Before
- @SuppressWarnings("ununsed")
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- tearDown();
- }
-
// Test data for Meters
-
private static final OnosMetrics.MetricsComponent COMPONENT =
OnosMetrics.registerComponent("MetricsUnitTest");
private static final OnosMetrics.MetricsFeature FEATURE =
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java
index 6aec6ee..fc75cc8 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestMetricsTimers.java
@@ -7,11 +7,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.modules.junit4.PowerMockRunner;
import org.restlet.resource.ClientResource;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -25,20 +21,8 @@
/**
* Unit tests for REST APIs for Timer Metrics.
*/
-@RunWith(PowerMockRunner.class)
public class TestRestMetricsTimers extends TestRestMetrics {
- /**
- * Create the web server and mocks required for
- * all of the tests.
- */
- @Before
- @SuppressWarnings("ununsed")
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
// Test data objects for Timers
private static final OnosMetrics.MetricsComponent COMPONENT =
@@ -58,8 +42,8 @@
private static final String TIMER2_NAME = "timer2";
private static final String TIMER2_FULL_NAME =
OnosMetrics.generateName(COMPONENT,
- FEATURE,
- TIMER2_NAME);
+ FEATURE,
+ TIMER2_NAME);
private static final int TIMER2_COUNT = 10;
private static final int RESERVOIR_SIZE = 100;
@@ -97,9 +81,9 @@
timer1);
OnosMetrics.registerMetric(COMPONENT,
- FEATURE,
- TIMER2_NAME,
- timer2);
+ FEATURE,
+ TIMER2_NAME,
+ timer2);
}
/**
@@ -135,16 +119,6 @@
}
/**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- tearDown();
- }
-
- /**
* Unit test for the REST APIs for Metrics Timers.
*
* @throws JSONException if any of the JSON processing fails.
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestTopology.java b/src/test/java/net/onrc/onos/api/rest/TestRestTopology.java
index aef7262..d0f578c 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestTopology.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestTopology.java
@@ -6,6 +6,7 @@
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.topology.web.TopologyWebRoutable;
+import org.junit.After;
/**
* Test harness for Topology based REST API tests. This class maintains the
@@ -53,10 +54,9 @@
* Remove anything that will interfere with the next test running correctly.
* Shuts down the test REST web server and removes the mocks.
*/
- @Override
- public void tearDown() {
+ @After
+ public void tearDownMocks() {
getMocks().tearDownIntentMocks();
- super.tearDown();
}
/**
diff --git a/src/test/java/net/onrc/onos/api/rest/TestRestTopologyGet.java b/src/test/java/net/onrc/onos/api/rest/TestRestTopologyGet.java
index 4eba584..35ed2ec 100644
--- a/src/test/java/net/onrc/onos/api/rest/TestRestTopologyGet.java
+++ b/src/test/java/net/onrc/onos/api/rest/TestRestTopologyGet.java
@@ -1,12 +1,10 @@
package net.onrc.onos.api.rest;
+import com.google.common.collect.ImmutableList;
import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
-
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
@@ -14,14 +12,16 @@
import org.restlet.data.Status;
import org.restlet.resource.ClientResource;
-import com.google.common.collect.ImmutableList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItems;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
/**
* Tests for topology REST get operations.
@@ -31,28 +31,6 @@
public class TestRestTopologyGet extends TestRestTopology {
/**
- * Create the web server and mocks required for
- * all of the tests.
- */
- @Before
- @SuppressWarnings("ununsed")
- public void beforeTest() {
- setRestPort(generateRandomPort());
- setUp();
- }
-
-
- /**
- * Remove anything that will interfere with the next test running correctly.
- * Shuts down the test REST web server and removes the mocks.
- */
- @After
- @SuppressWarnings("unused")
- public void afterTest() {
- tearDown();
- }
-
- /**
* Check that the JSON array returned for the switches element matches
* the data in the mocked topology.
*
diff --git a/src/test/java/net/onrc/onos/core/intent/runtime/PathCalcRuntimeModuleTest.java b/src/test/java/net/onrc/onos/core/intent/runtime/PathCalcRuntimeModuleTest.java
index 425646d..184499f 100644
--- a/src/test/java/net/onrc/onos/core/intent/runtime/PathCalcRuntimeModuleTest.java
+++ b/src/test/java/net/onrc/onos/core/intent/runtime/PathCalcRuntimeModuleTest.java
@@ -18,7 +18,6 @@
import net.onrc.onos.core.intent.IntentOperation.Operator;
import net.onrc.onos.core.intent.IntentOperationList;
import net.onrc.onos.core.intent.ShortestPathIntent;
-import net.onrc.onos.core.metrics.OnosMetrics;
import net.onrc.onos.core.topology.HostEvent;
import net.onrc.onos.core.topology.LinkEvent;
import net.onrc.onos.core.topology.MockTopology;
@@ -28,7 +27,7 @@
import net.onrc.onos.core.topology.TopologyEvents;
import net.onrc.onos.core.util.SwitchPort;
-import com.codahale.metrics.MetricFilter;
+import net.onrc.onos.core.util.UnitTest;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
@@ -50,7 +49,7 @@
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(PathCalcRuntimeModule.class)
-public class PathCalcRuntimeModuleTest {
+public class PathCalcRuntimeModuleTest extends UnitTest {
private static final Long LOCAL_PORT = 0xFFFEL;
private IntentTestMocks mocks;
@@ -171,7 +170,6 @@
@After
public void tearDown() {
mocks.tearDownIntentMocks();
- OnosMetrics.removeMatching(MetricFilter.ALL);
}
private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
diff --git a/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java b/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java
index 87e096f..990ca6b 100644
--- a/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java
+++ b/src/test/java/net/onrc/onos/core/intent/runtime/UseCaseTest.java
@@ -28,7 +28,6 @@
import net.onrc.onos.core.intent.PathIntentMap;
import net.onrc.onos.core.intent.ShortestPathIntent;
import net.onrc.onos.core.intent.runtime.web.IntentWebRoutable;
-import net.onrc.onos.core.metrics.OnosMetrics;
import net.onrc.onos.core.registry.IControllerRegistryService;
import net.onrc.onos.core.topology.HostEvent;
import net.onrc.onos.core.topology.ITopologyListener;
@@ -41,8 +40,7 @@
import net.onrc.onos.core.topology.TopologyEvents;
import net.onrc.onos.core.util.SwitchPort;
-import com.codahale.metrics.MetricFilter;
-
+import net.onrc.onos.core.util.UnitTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -60,7 +58,7 @@
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(PathCalcRuntimeModule.class)
-public class UseCaseTest {
+public class UseCaseTest extends UnitTest {
private static final Logger log = LoggerFactory.getLogger(UseCaseTest.class);
private Topology topology;
@@ -137,7 +135,6 @@
verify(controllerRegistryService);
PowerMock.verify(persistIntent, PersistIntent.class);
verify(restApi);
- OnosMetrics.removeMatching(MetricFilter.ALL);
}
private void showResult(PathIntentMap intents) {
diff --git a/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java b/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
index ae0a936..9bdffd5 100644
--- a/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
+++ b/src/test/java/net/onrc/onos/core/topology/TopologyManagerTest.java
@@ -1,45 +1,47 @@
package net.onrc.onos.core.topology;
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.verify;
-import static org.junit.Assert.*;
-import static org.hamcrest.Matchers.*;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-
import net.floodlightcontroller.core.IFloodlightProviderService.Role;
import net.floodlightcontroller.util.MACAddress;
import net.onrc.onos.core.datagrid.IDatagridService;
import net.onrc.onos.core.datagrid.IEventChannel;
import net.onrc.onos.core.datagrid.IEventChannelListener;
-import net.onrc.onos.core.metrics.OnosMetrics;
import net.onrc.onos.core.registry.IControllerRegistryService;
import net.onrc.onos.core.util.Dpid;
import net.onrc.onos.core.util.OnosInstanceId;
import net.onrc.onos.core.util.PortNumber;
import net.onrc.onos.core.util.SwitchPort;
import net.onrc.onos.core.util.TestUtils;
-
-import com.codahale.metrics.MetricFilter;
+import net.onrc.onos.core.util.UnitTest;
import org.easymock.EasyMock;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
/**
* Unit tests for the TopologyManager class in the Topology module.
* These test cases only check the sanity of functions in the TopologyManager.
* Note that we do not test the eventHandler functions in the TopologyManager class.
* DatagridService, DataStoreService, eventChannel, and controllerRegistryService are mocked out.
*/
-public class TopologyManagerTest {
+public class TopologyManagerTest extends UnitTest {
private TopologyManager theTopologyManager;
private final String eventChannelName = "onos.topology";
private IEventChannel<byte[], TopologyEvent> eventChannel;
@@ -128,11 +130,6 @@
TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
}
- @After
- public void tearDown() throws Exception {
- OnosMetrics.removeMatching(MetricFilter.ALL);
- }
-
/**
* Test the Switch discovered and Port discovered functions.
*/
diff --git a/src/test/java/net/onrc/onos/core/util/UnitTest.java b/src/test/java/net/onrc/onos/core/util/UnitTest.java
new file mode 100644
index 0000000..3660aff
--- /dev/null
+++ b/src/test/java/net/onrc/onos/core/util/UnitTest.java
@@ -0,0 +1,28 @@
+package net.onrc.onos.core.util;
+
+import com.codahale.metrics.MetricFilter;
+import net.onrc.onos.core.metrics.OnosMetrics;
+import org.junit.After;
+import org.junit.Before;
+
+/**
+ * Base class that Unit Tests should inherit from.
+ */
+public class UnitTest {
+
+ /**
+ * Performs initialization operations requred by all unit tests.
+ */
+ @Before
+ public void setUpBase() {}
+
+ /**
+ * Performs clean up operations required by all unit tests.
+ */
+ @After
+ public void tearDownBase() {
+ // Destroy any metrics created during the execution of the test.
+ OnosMetrics.removeMatching(MetricFilter.ALL);
+ }
+
+}