Skip to content

Commit

Permalink
DropwizardAppExtension support for RegisterExtension (#3649)
Browse files Browse the repository at this point in the history
Closes #3549

(cherry picked from commit 639f030)
  • Loading branch information
christopher-cudennec authored and joschi committed Jan 11, 2021
1 parent d96497c commit ab2d5d7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

//@formatter:off
/**
Expand All @@ -33,7 +36,8 @@
* @param <C> the configuration type
*/
//@formatter:on
public class DropwizardAppExtension<C extends Configuration> implements DropwizardExtension {
public class DropwizardAppExtension<C extends Configuration> implements DropwizardExtension,
BeforeAllCallback, AfterAllCallback {

private static final int DEFAULT_CONNECT_TIMEOUT_MS = 1000;
private static final int DEFAULT_READ_TIMEOUT_MS = 5000;
Expand Down Expand Up @@ -175,6 +179,16 @@ public void onRun(C configuration, Environment environment, DropwizardAppExtensi
});
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
this.before();
}

@Override
public void afterAll(ExtensionContext extensionContext) {
this.after();
}

@Override
public void before() throws Exception {
if (recursiveCallCount.getAndIncrement() == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.dropwizard.testing.junit5;

import static org.assertj.core.api.Assertions.assertThat;

import io.dropwizard.setup.Environment;
import io.dropwizard.testing.app.DropwizardTestApplication;
import io.dropwizard.testing.app.TestConfiguration;
import java.util.Optional;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

abstract class AbstractDropwizardAppExtensionTest {

@Test
void canGetExpectedResourceOverHttp() {
final String content = ClientBuilder.newClient().target(
"http://localhost:" + getExtension().getLocalPort() + "/test").request().get(String.class);

assertThat(content).isEqualTo("Yes, it's here");
}

@Test
void returnsConfiguration() {
final TestConfiguration config = getExtension().getConfiguration();
assertThat(config.getMessage()).isEqualTo("Yes, it's here");
}

@Test
void returnsApplication() {
final DropwizardTestApplication application = getExtension().getApplication();
Assertions.assertNotNull(application);
}

@Test
void returnsEnvironment() {
final Environment environment = getExtension().getEnvironment();
assertThat(environment.getName()).isEqualTo("DropwizardTestApplication");
}

@Test
void canPerformAdminTask() {
final String response
= getExtension().client().target("http://localhost:"
+ getExtension().getAdminPort() + "/tasks/hello?name=test_user")
.request()
.post(Entity.entity("", MediaType.TEXT_PLAIN), String.class);

assertThat(response).isEqualTo("Hello has been said to test_user");
}

@Test
void canPerformAdminTaskWithPostBody() {
final String response = getExtension().client()
.target("http://localhost:" + getExtension().getAdminPort() + "/tasks/echo")
.request()
.post(Entity.entity("Custom message", MediaType.TEXT_PLAIN), String.class);

assertThat(response).isEqualTo("Custom message");
}

@Test
void clientUsesJacksonMapperFromEnvironment() {
final Optional<String> message = getExtension().client()
.target("http://localhost:" + getExtension().getLocalPort() + "/message")
.request()
.get(DropwizardTestApplication.MessageView.class)
.getMessage();
assertThat(message)
.hasValue("Yes, it's here");
}

@Test
void clientSupportsPatchMethod() {
final String method = getExtension().client()
.target("http://localhost:" + getExtension().getLocalPort() + "/echoPatch")
.request()
.method("PATCH", Entity.text("Patch is working"), String.class);
assertThat(method).isEqualTo("Patch is working");
}

abstract DropwizardAppExtension<TestConfiguration> getExtension();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.dropwizard.testing.junit5;

import static io.dropwizard.testing.ResourceHelpers.resourceFilePath;

import io.dropwizard.testing.app.DropwizardTestApplication;
import io.dropwizard.testing.app.TestConfiguration;
import org.junit.jupiter.api.extension.RegisterExtension;

class DropwizardAppExtensionRegisterExtensionTest extends AbstractDropwizardAppExtensionTest {

@RegisterExtension
public static final DropwizardAppExtension<TestConfiguration> EXTENSION =
new DropwizardAppExtension<>(DropwizardTestApplication.class, resourceFilePath("test-config.yaml"));

@Override
DropwizardAppExtension<TestConfiguration> getExtension() {
return EXTENSION;
}
}
Original file line number Diff line number Diff line change
@@ -1,89 +1,19 @@
package io.dropwizard.testing.junit5;

import io.dropwizard.setup.Environment;
import io.dropwizard.testing.app.DropwizardTestApplication;
import io.dropwizard.testing.app.TestConfiguration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import java.util.Optional;

import static io.dropwizard.testing.ResourceHelpers.resourceFilePath;
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(DropwizardExtensionsSupport.class)
class DropwizardAppExtensionTest {
class DropwizardAppExtensionTest extends AbstractDropwizardAppExtensionTest {

private static final DropwizardAppExtension<TestConfiguration> EXTENSION =
new DropwizardAppExtension<>(DropwizardTestApplication.class, resourceFilePath("test-config.yaml"));

@Test
void canGetExpectedResourceOverHttp() {
final String content = ClientBuilder.newClient().target(
"http://localhost:" + EXTENSION.getLocalPort() + "/test").request().get(String.class);

assertThat(content).isEqualTo("Yes, it's here");
}

@Test
void returnsConfiguration() {
final TestConfiguration config = EXTENSION.getConfiguration();
assertThat(config.getMessage()).isEqualTo("Yes, it's here");
}

@Test
void returnsApplication() {
final DropwizardTestApplication application = EXTENSION.getApplication();
Assertions.assertNotNull(application);
}

@Test
void returnsEnvironment() {
final Environment environment = EXTENSION.getEnvironment();
assertThat(environment.getName()).isEqualTo("DropwizardTestApplication");
}

@Test
void canPerformAdminTask() {
final String response
= EXTENSION.client().target("http://localhost:"
+ EXTENSION.getAdminPort() + "/tasks/hello?name=test_user")
.request()
.post(Entity.entity("", MediaType.TEXT_PLAIN), String.class);

assertThat(response).isEqualTo("Hello has been said to test_user");
}

@Test
void canPerformAdminTaskWithPostBody() {
final String response = EXTENSION.client()
.target("http://localhost:" + EXTENSION.getAdminPort() + "/tasks/echo")
.request()
.post(Entity.entity("Custom message", MediaType.TEXT_PLAIN), String.class);

assertThat(response).isEqualTo("Custom message");
}

@Test
void clientUsesJacksonMapperFromEnvironment() {
final Optional<String> message = EXTENSION.client()
.target("http://localhost:" + EXTENSION.getLocalPort() + "/message")
.request()
.get(DropwizardTestApplication.MessageView.class)
.getMessage();
assertThat(message)
.hasValue("Yes, it's here");
}

@Test
void clientSupportsPatchMethod() {
final String method = EXTENSION.client()
.target("http://localhost:" + EXTENSION.getLocalPort() + "/echoPatch")
.request()
.method("PATCH", Entity.text("Patch is working"), String.class);
assertThat(method).isEqualTo("Patch is working");
@Override
DropwizardAppExtension<TestConfiguration> getExtension() {
return EXTENSION;
}
}

0 comments on commit ab2d5d7

Please sign in to comment.