-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DropwizardAppExtension support for RegisterExtension (#3649)
- Loading branch information
1 parent
d96497c
commit ab2d5d7
Showing
4 changed files
with
124 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...esting/src/test/java/io/dropwizard/testing/junit5/AbstractDropwizardAppExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
19 changes: 19 additions & 0 deletions
19
...c/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionRegisterExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
80 changes: 5 additions & 75 deletions
80
...wizard-testing/src/test/java/io/dropwizard/testing/junit5/DropwizardAppExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |