Skip to content

Commit

Permalink
some refactoring on acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varjoranta committed Feb 4, 2015
1 parent 9d34227 commit ee5acdc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dropwizard.version>0.7.1</dropwizard.version>
<cassandra.version>2.0.12</cassandra.version>
<cucumber.version>1.1.3</cucumber.version>
<cucumber.version>1.1.5</cucumber.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/db/reaper_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
--
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/spotify/reaper/ReaperApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 19 additions & 11 deletions src/test/java/com/spotify/reaper/acceptance/BasicSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,20 +16,28 @@
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import static org.junit.Assert.assertEquals;

/**
* Basic acceptance test (Cucumber) steps.
*/
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<Map<String, String>> 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.<Map<String, String>>absent(), Response.Status.OK);
callAndExpect("GET", "/ping", Optional.<Map<String, String>>absent(), Response.Status.OK);
}

@Given("^that we are going to use \"([^\"]*)\" as cluster seed host$")
Expand All @@ -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.<Map<String, String>>absent(), Response.Status.NOT_FOUND);
callAndExpect("GET", "/cluster/" + clusterName,
Optional.<Map<String, String>>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<String, String> 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.<Map<String, String>>absent(), Response.Status.OK.getStatusCode());
callAndExpect("GET", "/cluster/" + clusterName,
Optional.<Map<String, String>>absent(), Response.Status.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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() {
Expand All @@ -58,8 +55,8 @@ public void run() {
}
}

public static void callAndExpect(String httpMethod, String urlPath,
Optional<Map<String, String>> params, int statusCode) {
public static ClientResponse callReaper(String httpMethod, String urlPath,
Optional<Map<String, String>> params) {
assert runnerInstance != null : "service not initialized, call setup() first";
String reaperBase = "http://localhost:" + runnerInstance.getLocalPort() + "/";
URI uri;
Expand All @@ -84,29 +81,30 @@ 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<Map<String, String>> 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() {
if (jettyServer != null) {
return;
}
try {
ReaperApplication reaper = new ReaperApplication();
ReaperApplication reaper;
if (context != null) {
reaper = new ReaperApplication(context);
} else {
reaper = new ReaperApplication();
}

final Bootstrap<ReaperApplicationConfiguration> bootstrap =
new Bootstrap<ReaperApplicationConfiguration>(reaper) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.spotify.reaper.acceptance;

public class TestContext {
public static String SEED_HOST;

public static String SEED_HOST;

}

0 comments on commit ee5acdc

Please sign in to comment.