Skip to content

Instantly share code, notes, and snippets.

@oeddyo

oeddyo/abc.java Secret

Last active May 8, 2020
Embed
What would you like to do?
package com.kalasearch.restful.integration;
import com.opentable.db.postgres.junit5.EmbeddedPostgresExtension;
import com.opentable.db.postgres.junit5.SingleInstancePostgresExtension;
import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import lombok.Getter;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.extension.*;
import java.nio.file.Files;
import java.nio.file.Path;
import com.kalasearch.restful.HttpServer;
import com.kalasearch.restful.config.HttpServerConfiguration;
public class IntegrationExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback, AfterEachCallback {
private SingleInstancePostgresExtension singleInstancePostgresExtension =
EmbeddedPostgresExtension.singleInstance();
private Flyway flyway;
private Path tempDir;
@Getter
private DropwizardAppExtension<HttpServerConfiguration> dropwizardApp;
public RequestHandler requestHandler;
public IntegrationExtension() {
}
@Override
public void beforeAll(ExtensionContext context) throws Exception {
tempDir = Files.createTempDirectory("junit-test");
singleInstancePostgresExtension.beforeTestExecution(context);
flyway = Flyway.configure().dataSource(
singleInstancePostgresExtension.getEmbeddedPostgres().getDatabase("postgres", "postgres")
).load();
dropwizardApp =
new DropwizardAppExtension<>(
HttpServer.class,
"src/test/resources/dropwizard-test-config.yml",
ConfigOverride.config(
"database.url",
singleInstancePostgresExtension.getEmbeddedPostgres().getJdbcUrl(
"postgres", "postgres")
),
ConfigOverride.config(
"server.applicationConnectors[0].port", String.valueOf(0)
),
ConfigOverride.config(
"server.adminConnectors[0].port", String.valueOf(0)
),
ConfigOverride.config(
"engine.indexDataPrefix",
tempDir.toAbsolutePath().toString()
)
);
dropwizardApp.before();
requestHandler = new RequestHandler(dropwizardApp.getLocalPort());
}
@Override
public void afterAll(ExtensionContext context) throws Exception {
Files.deleteIfExists(tempDir.toAbsolutePath());
singleInstancePostgresExtension.getEmbeddedPostgres().close();
dropwizardApp.after();
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
flyway.clean();
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
flyway.clean();
flyway.migrate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment