Skip to content

Commit

Permalink
Dev services: MSSQL reactive client does not support JDBC url format
Browse files Browse the repository at this point in the history
Fixes #25233

The "encrypt" property is not needed anyway, it is the default of both the JDBC and reactive clients.

https://docs.microsoft.com/en-us/sql/connect/jdbc/understanding-ssl-support?view=sql-server-ver15#remarks
(cherry picked from commit c4a4640)
  • Loading branch information
tsegismont authored and gsmet committed May 24, 2022
1 parent 0657f6c commit a57562b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import io.quarkus.builder.item.MultiBuildItem;
Expand Down Expand Up @@ -85,17 +86,31 @@ private static String datasourceURLPropName(String dsName) {
}

public static DevServicesDatasourceConfigurationHandlerBuildItem reactive(String dbKind) {
return reactive(dbKind, new Function<String, String>() {
@Override
public String apply(String url) {
return url.replaceFirst("jdbc:", "vertx-reactive:");
}
});
}

public static DevServicesDatasourceConfigurationHandlerBuildItem reactive(String dbKind,
Function<String, String> jdbcUrlTransformer) {
return new DevServicesDatasourceConfigurationHandlerBuildItem(dbKind,
new BiFunction<String, DevServicesDatasourceProvider.RunningDevServicesDatasource, Map<String, String>>() {
@Override
public Map<String, String> apply(String dsName,
DevServicesDatasourceProvider.RunningDevServicesDatasource runningDevDb) {
String url = jdbcUrlTransformer.apply(runningDevDb.getUrl());
if (dsName == null) {
return Collections.singletonMap("quarkus.datasource.reactive.url",
runningDevDb.getUrl().replaceFirst("jdbc:", "vertx-reactive:"));
return Collections.singletonMap("quarkus.datasource.reactive.url", url);
} else {
return Collections.singletonMap("quarkus.datasource.\"" + dsName + "\".reactive.url",
runningDevDb.getUrl().replaceFirst("jdbc:", "vertx-reactive:"));
// we use quoted and unquoted versions because depending on whether a user configured other JDBC properties
// one of the URLs may be ignored
// see https://github.com/quarkusio/quarkus/issues/21387
return Map.of(
datasourceReactiveURLPropName(dsName, false), url,
datasourceReactiveURLPropName(dsName, true), url);
}
}
}, new Predicate<String>() {
Expand All @@ -104,10 +119,24 @@ public boolean test(String dsName) {
if (dsName == null) {
return ConfigUtils.isPropertyPresent("quarkus.datasource.reactive.url");
} else {
return ConfigUtils.isPropertyPresent("quarkus.datasource.\"" + dsName + "\".reactive.url") ||
ConfigUtils.isPropertyPresent("quarkus.datasource." + dsName + ".reactive.url");
return ConfigUtils.isPropertyPresent(datasourceReactiveURLPropName(dsName, false)) ||
ConfigUtils.isPropertyPresent(datasourceReactiveURLPropName(dsName, true));
}
}
});
}

private static String datasourceReactiveURLPropName(String dsName, boolean quotedName) {
StringBuilder key = new StringBuilder("quarkus.datasource");
key.append('.');
if (quotedName) {
key.append('"');
}
key.append(dsName);
if (quotedName) {
key.append('"');
}
key.append(".reactive.url");
return key.toString();
}
}
5 changes: 5 additions & 0 deletions extensions/reactive-mssql-client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
<artifactId>quarkus-smallrye-health-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import javax.enterprise.context.ApplicationScoped;

Expand Down Expand Up @@ -83,7 +84,16 @@ ServiceStartBuildItem build(BuildProducer<FeatureBuildItem> feature,

@BuildStep
DevServicesDatasourceConfigurationHandlerBuildItem devDbHandler() {
return DevServicesDatasourceConfigurationHandlerBuildItem.reactive(DatabaseKind.MSSQL);
return DevServicesDatasourceConfigurationHandlerBuildItem.reactive(DatabaseKind.MSSQL, new Function<String, String>() {
@Override
public String apply(String url) {
url = url.replaceFirst("jdbc:", "vertx-reactive:");
if (url.endsWith(";encrypt=false")) {
return url.substring(0, url.length() - ";encrypt=false".length());
}
return url;
}
});
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.reactive.mssql.client;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Duration;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import javax.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.vertx.mutiny.mssqlclient.MSSQLPool;

public class DevServicesMsSQLDatasourceTestCase {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addAsResource("container-license-acceptance.txt"))
// Expect no warnings from reactive
.setLogRecordPredicate(record -> record.getLevel().intValue() >= Level.WARNING.intValue()
&& record.getMessage().toLowerCase(Locale.ENGLISH).contains("reactive"))
.assertLogRecords(records -> assertThat(records)
// This is just to get meaningful error messages, as LogRecord doesn't have a toString()
.extracting(LogRecord::getMessage)
.isEmpty());

@Inject
MSSQLPool pool;

@Test
public void testDatasource() throws Exception {
pool.withConnection(conn -> conn.query("SELECT 1").execute().replaceWithVoid())
.await().atMost(Duration.ofMinutes(2));
}
}

0 comments on commit a57562b

Please sign in to comment.