From ee5acdc94adfe9f0d873a0d38f047683d4e301d9 Mon Sep 17 00:00:00 2001 From: Hannu Varjoranta Date: Wed, 4 Feb 2015 13:52:52 +0100 Subject: [PATCH] some refactoring on acceptance tests --- pom.xml | 2 +- src/main/db/reaper_db.sql | 2 +- .../com/spotify/reaper/ReaperApplication.java | 6 ++++ .../spotify/reaper/acceptance/BasicSteps.java | 30 ++++++++++------- .../acceptance/ReaperTestJettyRunner.java | 32 +++++++++---------- .../reaper/acceptance/TestContext.java | 2 +- 6 files changed, 43 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index b5cff04ad..e90cebb29 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ UTF-8 0.7.1 2.0.12 - 1.1.3 + 1.1.5 diff --git a/src/main/db/reaper_db.sql b/src/main/db/reaper_db.sql index cd111da4a..b14770bda 100644 --- a/src/main/db/reaper_db.sql +++ b/src/main/db/reaper_db.sql @@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS "cluster" ( "partitioner" TEXT NOT NULL, "seed_hosts" TEXT[] NOT NULL ); -ยง + -- Repair unit is basically a keyspace with a set of column families. -- Cassandra supports repairing multiple column families in one go. -- diff --git a/src/main/java/com/spotify/reaper/ReaperApplication.java b/src/main/java/com/spotify/reaper/ReaperApplication.java index d069fb361..9fd14cdae 100644 --- a/src/main/java/com/spotify/reaper/ReaperApplication.java +++ b/src/main/java/com/spotify/reaper/ReaperApplication.java @@ -15,6 +15,7 @@ import com.google.common.annotations.VisibleForTesting; +import com.spotify.reaper.cassandra.JmxConnectionFactory; import com.spotify.reaper.resources.ClusterResource; import com.spotify.reaper.resources.PingResource; import com.spotify.reaper.resources.ReaperHealthCheck; @@ -98,6 +99,11 @@ public void run(ReaperApplicationConfiguration config, LOG.info("storage already given in context, not initializing a new one"); } + if (context.jmxConnectionFactory == null) { + LOG.info("no JMX connection factory given in context, creating default"); + context.jmxConnectionFactory = new JmxConnectionFactory(); + } + LOG.info("creating and registering health checks"); // Notice that health checks are registered under the admin application on /healthcheck final ReaperHealthCheck healthCheck = new ReaperHealthCheck(context); diff --git a/src/test/java/com/spotify/reaper/acceptance/BasicSteps.java b/src/test/java/com/spotify/reaper/acceptance/BasicSteps.java index 006854add..bcf556006 100644 --- a/src/test/java/com/spotify/reaper/acceptance/BasicSteps.java +++ b/src/test/java/com/spotify/reaper/acceptance/BasicSteps.java @@ -3,6 +3,9 @@ import com.google.common.base.Optional; import com.google.common.collect.Maps; +import com.spotify.reaper.AppContext; +import com.sun.jersey.api.client.ClientResponse; + import java.util.Map; import javax.ws.rs.core.Response; @@ -13,6 +16,8 @@ import cucumber.api.java.en.Then; import cucumber.api.java.en.When; +import static org.junit.Assert.assertEquals; + /** * Basic acceptance test (Cucumber) steps. */ @@ -20,13 +25,19 @@ public class BasicSteps { @Before public static void setup() throws Exception { - ReaperTestJettyRunner.setup(); + AppContext context = new AppContext(); + ReaperTestJettyRunner.setup(context); + } + + public void callAndExpect(String httpMethod, String callPath, + Optional> params, Response.Status expectedStatus) { + ClientResponse response = ReaperTestJettyRunner.callReaper(httpMethod, callPath, params); + assertEquals(expectedStatus.getStatusCode(), response.getStatus()); } @Given("^a reaper service is running$") public void a_reaper_service_is_running() throws Throwable { - ReaperTestJettyRunner.callAndExpect( - "GET", "/ping", Optional.>absent(), Response.Status.OK); + callAndExpect("GET", "/ping", Optional.>absent(), Response.Status.OK); } @Given("^that we are going to use \"([^\"]*)\" as cluster seed host$") @@ -36,24 +47,21 @@ public void that_we_are_going_to_use_as_cluster_seed_host(String seedHost) throw @And("^reaper has no cluster with name \"([^\"]*)\" in storage$") public void reaper_has_no_cluster_with_name_in_storage(String clusterName) throws Throwable { - ReaperTestJettyRunner.callAndExpect( - "GET", "/cluster/" + clusterName, - Optional.>absent(), Response.Status.NOT_FOUND); + callAndExpect("GET", "/cluster/" + clusterName, + Optional.>absent(), Response.Status.NOT_FOUND); } @When("^an add-cluster request is made to reaper$") public void an_add_cluster_request_is_made_to_reaper() throws Throwable { Map params = Maps.newHashMap(); params.put("seedHost", TestContext.SEED_HOST); - ReaperTestJettyRunner.callAndExpect( - "POST", "/cluster", Optional.of(params), Response.Status.CREATED); + callAndExpect("POST", "/cluster", Optional.of(params), Response.Status.CREATED); } @Then("^reaper has a cluster called \"([^\"]*)\" in storage$") public void reaper_has_a_cluster_called_in_storage(String clusterName) throws Throwable { - ReaperTestJettyRunner.callAndExpect( - "GET", "/cluster/" + clusterName, - Optional.>absent(), Response.Status.OK.getStatusCode()); + callAndExpect("GET", "/cluster/" + clusterName, + Optional.>absent(), Response.Status.OK); } } diff --git a/src/test/java/com/spotify/reaper/acceptance/ReaperTestJettyRunner.java b/src/test/java/com/spotify/reaper/acceptance/ReaperTestJettyRunner.java index acc1a28da..e0f670ff0 100644 --- a/src/test/java/com/spotify/reaper/acceptance/ReaperTestJettyRunner.java +++ b/src/test/java/com/spotify/reaper/acceptance/ReaperTestJettyRunner.java @@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.io.Resources; +import com.spotify.reaper.AppContext; import com.spotify.reaper.ReaperApplication; import com.spotify.reaper.ReaperApplicationConfiguration; import com.sun.jersey.api.client.Client; @@ -22,15 +23,11 @@ import java.net.URL; import java.util.Map; -import javax.ws.rs.core.Response; - import io.dropwizard.cli.ServerCommand; import io.dropwizard.lifecycle.ServerLifecycleListener; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; -import static org.junit.Assert.assertEquals; - /** * Simple Reaper application runner for testing purposes. * Starts a Jetty server that wraps Reaper application, @@ -42,11 +39,11 @@ public class ReaperTestJettyRunner { private static ReaperTestJettyRunner runnerInstance; - public static void setup() throws Exception { + public static void setup(AppContext testContext) throws Exception { if (runnerInstance == null) { String testConfigPath = Resources.getResource("cassandra-reaper-at.yaml").getPath(); LOG.info("initializing ReaperTestJettyRunner with config in path: " + testConfigPath); - runnerInstance = new ReaperTestJettyRunner(testConfigPath); + runnerInstance = new ReaperTestJettyRunner(testConfigPath, testContext); runnerInstance.start(); // Stop the testing Reaper after tests are finished. Runtime.getRuntime().addShutdownHook(new Thread() { @@ -58,8 +55,8 @@ public void run() { } } - public static void callAndExpect(String httpMethod, String urlPath, - Optional> params, int statusCode) { + public static ClientResponse callReaper(String httpMethod, String urlPath, + Optional> params) { assert runnerInstance != null : "service not initialized, call setup() first"; String reaperBase = "http://localhost:" + runnerInstance.getLocalPort() + "/"; URI uri; @@ -84,21 +81,17 @@ public static void callAndExpect(String httpMethod, String urlPath, } else { throw new RuntimeException("Invalid HTTP method: " + httpMethod); } - - assertEquals(statusCode, response.getStatus()); - } - - public static void callAndExpect(String httpMethod, String url, - Optional> params, Response.Status status) { - callAndExpect(httpMethod, url, params, status.getStatusCode()); + return response; } private final String configPath; private Server jettyServer; + private AppContext context; - public ReaperTestJettyRunner(String configPath) { + public ReaperTestJettyRunner(String configPath, AppContext context) { this.configPath = configPath; + this.context = context; } public void start() { @@ -106,7 +99,12 @@ public void start() { return; } try { - ReaperApplication reaper = new ReaperApplication(); + ReaperApplication reaper; + if (context != null) { + reaper = new ReaperApplication(context); + } else { + reaper = new ReaperApplication(); + } final Bootstrap bootstrap = new Bootstrap(reaper) { diff --git a/src/test/java/com/spotify/reaper/acceptance/TestContext.java b/src/test/java/com/spotify/reaper/acceptance/TestContext.java index 50bcb9977..6d61e0b93 100644 --- a/src/test/java/com/spotify/reaper/acceptance/TestContext.java +++ b/src/test/java/com/spotify/reaper/acceptance/TestContext.java @@ -1,7 +1,7 @@ package com.spotify.reaper.acceptance; public class TestContext { - public static String SEED_HOST; + public static String SEED_HOST; }