-
-
Save oeddyo/415d8d94f50cde90a1c706fe4011330d to your computer and use it in GitHub Desktop.
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
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