Skip to content

Commit

Permalink
Added example for Quarkus DevServices test (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jan 19, 2022
1 parent 0b5ee2b commit 6ff5196
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
6 changes: 6 additions & 0 deletions examples/quarkus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-datasource-deployment</artifactId>
</dependency>


<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
Expand Down
7 changes: 4 additions & 3 deletions examples/quarkus/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
quarkus.datasource.db-kind=pg
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/database
quarkus.datasource.username=database
quarkus.datasource.password=password

%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/database
%prod.quarkus.datasource.username=database
%prod.quarkus.datasource.password=password

%test.quarkus.datasource.db-kind=h2
%test.quarkus.datasource.jdbc.url=jdbc:h2:./target/db/data;AUTO_SERVER=TRUE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.radcortez.flyway.quarkus;

import com.radcortez.flyway.test.annotation.DataSource;
import com.radcortez.flyway.test.annotation.FlywayTest;
import com.radcortez.flyway.test.junit.DataSourceInfo;
import com.radcortez.flyway.test.junit.DataSourceProvider;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtensionContext;

import static io.restassured.RestAssured.given;
import static javax.ws.rs.core.Response.Status.OK;

@DisabledOnOs(OS.WINDOWS)
@QuarkusTest
@TestProfile(UserRepositoryDevServicesTest.DevServicesTestProfile.class)
@FlywayTest(@DataSource(UserRepositoryDevServicesTest.QuarkusDataSourceProvider.class))
public class UserRepositoryDevServicesTest {
@Test
void find() {
given().get("/users/{id}", "3df5eeff-f93d-4036-b1aa-9e96a7a8820d")
.then()
.statusCode(OK.getStatusCode());
}

public static class DevServicesTestProfile implements QuarkusTestProfile {
@Override
public String getConfigProfile() {
return "dev";
}
}

public static class QuarkusDataSourceProvider implements DataSourceProvider {
@Override
public DataSourceInfo getDatasourceInfo(final ExtensionContext extensionContext) {
// Quarkus Dev Services rewrite the Dev Service datasource in the configuration
Config config = ConfigProvider.getConfig();
String url = config.getValue("quarkus.datasource.jdbc.url", String.class);
String username = config.getValue("quarkus.datasource.username", String.class);
String password = config.getValue("quarkus.datasource.password", String.class);
return DataSourceInfo.config(url, username, password);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down Expand Up @@ -71,21 +72,7 @@ void findByFirstName() {
public static class QuarkusDataSourceProvider implements DataSourceProvider {
@Override
public DataSourceInfo getDatasourceInfo(final ExtensionContext extensionContext) {
// We don't have access to the Quarkus CL here, so we cannot use ConfigProvider.getConfig() to retrieve the same configuration.

URL properties = Thread.currentThread().getContextClassLoader().getResource("application.properties");
assert properties != null;

try {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(new PropertiesConfigSource(properties))
.withProfile("test")
.build();

return DataSourceInfo.config(config.getRawValue("quarkus.datasource.jdbc.url"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
return DataSourceInfo.config(ConfigProvider.getConfig().getValue("quarkus.datasource.jdbc.url", String.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static DataSourceProvider newDatasourceProvider(final Class<? extends Da
try {
return klass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(e);
}
}

Expand Down

0 comments on commit 6ff5196

Please sign in to comment.