Skip to content

Commit

Permalink
Merge pull request #1346 from folio-org/OKAPI-1184-vertx-4.5.3
Browse files Browse the repository at this point in the history
OKAPI-1184: Quesnelia dependencies: Vert.x 4.5.3, Micrometer 1.12.2, …
  • Loading branch information
julianladisch committed Feb 21, 2024
2 parents 7101d6f + d7c10f3 commit 7575fcd
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.folio.okapi.common;

import io.vertx.core.AsyncResult;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import org.assertj.core.api.WithAssertions;
import org.junit.Test;

Expand Down Expand Up @@ -44,70 +41,6 @@ public void generics() {
assertThat(join.succeeded()).isTrue();
}

class MyFuture<T> implements Future<T> {
@Override
public boolean isComplete() {
return false;
}

@Override
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
return null;
}

@Override
public T result() {
return null;
}

@Override
public Throwable cause() {
return null;
}

@Override
public boolean succeeded() {
return false;
}

@Override
public boolean failed() {
return false;
}

@Override
public <U> Future<U> compose(Function<T, Future<U>> successMapper, Function<Throwable, Future<U>> failureMapper) {
return null;
}

@Override
public <U> Future<U> transform(Function<AsyncResult<T>, Future<U>> mapper) {
return null;
}

@Override
public <U> Future<T> eventually(Function<Void, Future<U>> mapper) {
return null;
}

@Override
public <U> Future<U> map(Function<T, U> mapper) {
return null;
}

@Override
public <V> Future<V> map(V value) {
return null;
}

@Override
public Future<T> otherwise(Function<Throwable, T> mapper) {
return null;
}

@Override
public Future<T> otherwise(T value) {
return null;
}
};
abstract class MyFuture<T> implements Future<T> {
}
}
12 changes: 6 additions & 6 deletions okapi-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
Expand All @@ -66,11 +71,6 @@
<groupId>io.vertx</groupId>
<artifactId>vertx-pg-client</artifactId>
</dependency>
<dependency> <!-- needed because OpenJDK 8 doesn't support TLSv1.3 -->
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
Expand Down Expand Up @@ -200,7 +200,7 @@

<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
Expand Down
17 changes: 7 additions & 10 deletions okapi-core/src/main/java/org/folio/okapi/MainDeploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ private void deployClustered(Handler<AsyncResult<Vertx>> fut) {
hazelcastConfig.setProperty("hazelcast.logging.type", "log4j2");

HazelcastClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);
vopt.setClusterManager(mgr);
EventBusOptions eventBusOptions = vopt.getEventBusOptions();
if (clusterHost != null) {
logger.info("clusterHost={}", clusterHost);
Expand All @@ -211,15 +210,13 @@ private void deployClustered(Handler<AsyncResult<Vertx>> fut) {
logger.warn("clusterPort not set");
}

Vertx.clusteredVertx(vopt, res -> {
if (res.succeeded()) {
MainVerticle v = new MainVerticle();
v.setClusterManager(mgr);
deployVerticle(v, res.result(), fut);
} else {
fut.handle(Future.failedFuture(res.cause()));
}
});
Vertx.builder().with(vopt).withClusterManager(mgr).buildClustered()
.onSuccess(vertx -> {
MainVerticle v = new MainVerticle();
v.setClusterManager(mgr);
deployVerticle(v, vertx, fut);
})
.onFailure(e -> fut.handle(Future.failedFuture(e)));
}

private void deployVerticle(Verticle v, Vertx vertx, Handler<AsyncResult<Vertx>> fut) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PullManager {
*/
public PullManager(Vertx vertx, ModuleManager moduleManager) {
HttpClientOptions options = new HttpClientOptions()
.setTryUseCompression(true)
.setDecompressionSupported(true)
.setMaxPoolSize(2); // suffice as pull is normally not performed concurrently
this.httpClient = new FuturisedHttpClient(vertx, options);
this.moduleManager = moduleManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public Future<Void> stop() {
.compose(
x -> deleteContainer(),
// if stopContainer fails with e run deleteContainer but return original failure e
e -> deleteContainer().eventually(x -> Future.failedFuture(e)))
e -> deleteContainer().eventually(() -> Future.failedFuture(e)))
.onComplete(x -> ports.free(hostPort));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.OpenSSLEngineOptions;
import io.vertx.core.net.JdkSSLEngineOptions;
import io.vertx.core.net.PemTrustOptions;
import io.vertx.pgclient.PgBuilder;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.pgclient.SslMode;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnection;
import java.util.Collections;
Expand Down Expand Up @@ -42,7 +43,7 @@
class PostgresHandle {

private final PgConnectOptions connectOptions;
private final PgPool pool;
private final Pool pool;

PostgresHandle(Vertx vertx, JsonObject conf) {
String val;
Expand Down Expand Up @@ -76,13 +77,13 @@ class PostgresHandle {
connectOptions.setPemTrustOptions(
new PemTrustOptions().addCertValue(Buffer.buffer(serverPem)));
connectOptions.setEnabledSecureTransportProtocols(Collections.singleton("TLSv1.3"));
connectOptions.setOpenSslEngineOptions(new OpenSSLEngineOptions());
connectOptions.setJdkSslEngineOptions(new JdkSSLEngineOptions());
}

PoolOptions poolOptions = new PoolOptions();
poolOptions.setMaxSize(5);

pool = PgPool.pool(vertx, connectOptions, poolOptions);
pool = PgBuilder.pool().using(vertx).connectingTo(connectOptions).with(poolOptions).build();
logger.debug("created");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public static void addCorsHandler(Router router, final TenantManager tenantManag
if (ctx.data().containsKey(DELEGATE_CORS)) {
ctx.next();
} else {
CorsHandler.create("*")
CorsHandler.create()
.addRelativeOrigin(".*")
.allowedMethod(HttpMethod.PUT)
.allowedMethod(HttpMethod.PATCH)
.allowedMethod(HttpMethod.DELETE)
Expand Down
12 changes: 6 additions & 6 deletions okapi-core/src/main/java/org/folio/okapi/util/JsonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
*/
public final class JsonDecoder {
private static final Pattern pattern = Pattern.compile(
"^Failed to decode:Cannot deserialize value of type `\\[L([^;`]+);` from Object value "
+ "\\(token `JsonToken\\.START_OBJECT`\\)(.*)$", Pattern.DOTALL);
"^Failed to decode:Cannot deserialize value of type `\\[L(?<listElement>[^;`]+);` "
+ "from Object value \\(token `JsonToken\\.START_OBJECT`\\).*; "
+ "(?<at>line: \\d+, column: \\d+)\\]$", Pattern.DOTALL);

private JsonDecoder() {
}
Expand All @@ -19,7 +20,6 @@ private JsonDecoder() {
* Same as {@link Json#decodeValue(String, Class)} but with better error message
* when `{` is found where an array is expected.
*/
@SuppressWarnings("java:S109") // suppress "Assign this magic number 2 to a well-named constant"
public static <T> T decode(String str, Class<T> clazz) throws DecodeException {
try {
return Json.decodeValue(str, clazz);
Expand All @@ -28,10 +28,10 @@ public static <T> T decode(String str, Class<T> clazz) throws DecodeException {
if (! matcher.find()) {
throw e;
}
var listElement = matcher.group(1);
var at = matcher.group(2);
var listElement = matcher.group("listElement");
var at = matcher.group("at");
throw new DecodeException("Expected `[` but found `{` when trying to "
+ "deserialize an array of " + listElement + at, e);
+ "deserialize an array of " + listElement + " at " + at, e);
}
}
}
15 changes: 11 additions & 4 deletions okapi-core/src/test/java/org/folio/okapi/ModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public static Iterable<String> data() {
private static final String LS = System.lineSeparator();
private final int port = 9230;
private static RamlDefinition api;
/**
* Testcontainers startup config workaround for podman:
* https://github.com/testcontainers/testcontainers-java/issues/6640#issuecomment-1431636203
*/
private static final int STARTUP_ATTEMPTS = 3;
private static PostgreSQLContainer<?> postgresSQLContainer;
private static MongoDBContainer mongoDBContainer;

Expand Down Expand Up @@ -130,7 +135,8 @@ public ModuleTest(String value) throws Exception {
switch (value) {
case "postgres":
if (postgresSQLContainer == null) {
postgresSQLContainer = new PostgreSQLContainer<>("postgres:12-alpine");
postgresSQLContainer = new PostgreSQLContainer<>("postgres:12-alpine")
.withStartupAttempts(STARTUP_ATTEMPTS);
postgresSQLContainer.start();
}
conf.put("postgres_username", postgresSQLContainer.getUsername());
Expand All @@ -141,7 +147,8 @@ public ModuleTest(String value) throws Exception {
break;
case "mongo":
if (mongoDBContainer == null) {
mongoDBContainer = new MongoDBContainer("mongo:5.0.8");
mongoDBContainer = new MongoDBContainer("mongo:5.0.8")
.withStartupAttempts(STARTUP_ATTEMPTS);
mongoDBContainer.start();
}
conf.put("mongo_port", mongoDBContainer.getFirstMappedPort().toString());
Expand Down Expand Up @@ -3062,7 +3069,7 @@ public void testPullModules(TestContext context) {
+ " {\"id\":\"bar-4.5.6\", \"name\":\"bar\"} ]");
})
.listen(0)
.compose(registry -> vertx.executeBlocking(promise -> {
.compose(registry -> vertx.executeBlocking(() -> {
given()
.header("Content-Type", "application/json")
.body("{ \"urls\" : [ \"http://localhost:" + registry.actualPort() + "\" ] }")
Expand All @@ -3079,7 +3086,7 @@ public void testPullModules(TestContext context) {
.statusCode(200)
.body("$", hasSize(0));

promise.complete();
return null;
}))
.onComplete(context.asyncAssertSuccess());
}
Expand Down
4 changes: 1 addition & 3 deletions okapi-core/src/test/java/org/folio/okapi/ProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.streams.Pump;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
Expand Down Expand Up @@ -236,8 +235,7 @@ private void myTimerHandle(RoutingContext ctx) {
response.setStatusCode(200);
response.putHeader("Content-Type", request.getHeader("Content-Type"));
response.setChunked(true);
Pump pump = Pump.pump(request, response);
pump.start();
request.pipeTo(response);
request.endHandler(e -> response.end());
request.pause();
vertx.setTimer(100, x -> request.resume()); // pause to provoke writeQueueFull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.folio.okapi.service.impl;

import static org.folio.okapi.service.impl.DockerModuleHandle.DOCKER_REGISTRIES_EMPTY_LIST;
import static org.mockito.Mockito.*;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
Expand All @@ -30,7 +32,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.AdditionalAnswers;
import static org.mockito.Mockito.*;

@RunWith(VertxUnitRunner.class)
public class DockerModuleHandleTest implements WithAssertions {
Expand All @@ -41,7 +42,7 @@ public class DockerModuleHandleTest implements WithAssertions {
public void testRequestException(TestContext testContext) {
Vertx vertx = mock(Vertx.class);
HttpClient httpClient = mock(HttpClient.class);
when(vertx.createHttpClient(any())).thenReturn(httpClient);
when(vertx.createHttpClient((HttpClientOptions) any())).thenReturn(httpClient);
when(httpClient.request(any())).thenThrow(new RuntimeException("foo"));
JsonObject conf = new JsonObject();
new DockerModuleHandle(vertx, new LaunchDescriptor(), null, null, null, 0, conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void permissionsPost(RoutingContext ctx) {
void tenantPost(RoutingContext ctx) {
String path = ctx.request().path();
if (path.equals("/_/tenant")) {
JsonObject tenantSchema = ctx.getBodyAsJson();
JsonObject tenantSchema = ctx.body().asJsonObject();
operations.add(tenantSchema);
if (tenantSchema.getBoolean("purge", false))
if (purgeFail) {
Expand All @@ -99,7 +99,7 @@ void tenantPost(RoutingContext ctx) {
ctx.response().end();
return;
}
JsonObject obj = ctx.getBodyAsJson();
JsonObject obj = ctx.body().asJsonObject();
String module = obj.getString("module_to", obj.getString("module_from"));
if (module != null) {
logger.info("Tenant begin for module {}", module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ void decode() {
DecodeException e = assertThrowsExactly(DecodeException.class,
() -> JsonDecoder.decode("{}", String[].class));
assertThat(e.getMessage(), is("Expected `[` but found `{` when trying to deserialize "
+ "an array of java.lang.String\n at [Source: (String)\"{}\"; line: 1, column: 1]"));
+ "an array of java.lang.String at line: 1, column: 1"));
assertThat(e.getCause().getMessage(), startsWith("Failed to decode:Cannot deserialize"));
}

@Test
void decodeWithLocation() {
DecodeException e = assertThrowsExactly(DecodeException.class,
() -> JsonDecoder.decode("\n\n\n\n\n\n\n\n\n {}", String[].class));
assertThat(e.getMessage(), is("Expected `[` but found `{` when trying to deserialize "
+ "an array of java.lang.String at line: 10, column: 10"));
assertThat(e.getCause().getMessage(), startsWith("Failed to decode:Cannot deserialize"));
}
}
Loading

0 comments on commit 7575fcd

Please sign in to comment.