From ab2d5d774181b828c536b96d0ae3f7c5b59d367e Mon Sep 17 00:00:00 2001 From: Christopher Cudennec <48206222+christopher-cudennec@users.noreply.github.com> Date: Mon, 11 Jan 2021 22:29:14 +0100 Subject: [PATCH] DropwizardAppExtension support for RegisterExtension (#3649) Closes #3549 (cherry picked from commit 639f030f84e272b1a70134e1fc57bf9bd3cf204a) --- .../junit5/DropwizardAppExtension.java | 16 +++- .../AbstractDropwizardAppExtensionTest.java | 85 +++++++++++++++++++ ...zardAppExtensionRegisterExtensionTest.java | 19 +++++ .../junit5/DropwizardAppExtensionTest.java | 80 ++--------------- 4 files changed, 124 insertions(+), 76 deletions(-) create mode 100644 dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/AbstractDropwizardAppExtensionTest.java create mode 100644 dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionRegisterExtensionTest.java diff --git a/dropwizard-testing/src/main/java/io/dropwizard/testing/junit5/DropwizardAppExtension.java b/dropwizard-testing/src/main/java/io/dropwizard/testing/junit5/DropwizardAppExtension.java index e609125b893..c4484f9fa58 100644 --- a/dropwizard-testing/src/main/java/io/dropwizard/testing/junit5/DropwizardAppExtension.java +++ b/dropwizard-testing/src/main/java/io/dropwizard/testing/junit5/DropwizardAppExtension.java @@ -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 /** @@ -33,7 +36,8 @@ * @param the configuration type */ //@formatter:on -public class DropwizardAppExtension implements DropwizardExtension { +public class DropwizardAppExtension implements DropwizardExtension, + BeforeAllCallback, AfterAllCallback { private static final int DEFAULT_CONNECT_TIMEOUT_MS = 1000; private static final int DEFAULT_READ_TIMEOUT_MS = 5000; @@ -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) { diff --git a/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/AbstractDropwizardAppExtensionTest.java b/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/AbstractDropwizardAppExtensionTest.java new file mode 100644 index 00000000000..02a0ca2a91a --- /dev/null +++ b/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/AbstractDropwizardAppExtensionTest.java @@ -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 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 getExtension(); +} diff --git a/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionRegisterExtensionTest.java b/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionRegisterExtensionTest.java new file mode 100644 index 00000000000..c8a97b97e4d --- /dev/null +++ b/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionRegisterExtensionTest.java @@ -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 EXTENSION = + new DropwizardAppExtension<>(DropwizardTestApplication.class, resourceFilePath("test-config.yaml")); + + @Override + DropwizardAppExtension getExtension() { + return EXTENSION; + } +} diff --git a/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionTest.java b/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionTest.java index b9051fb9585..297ac8922dd 100644 --- a/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionTest.java +++ b/dropwizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionTest.java @@ -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 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 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 getExtension() { + return EXTENSION; } }