This is an automated email from the ASF dual-hosted git repository. hqtran pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit c6dd5b3d8b6e799d6dfd0f2d2b470b8af475aed9 Author: Quan Tran <hqt...@linagora.com> AuthorDate: Mon Mar 31 11:57:01 2025 +0700 JAMES-4124 Introduce KvrocksExtension --- .../apache/james/backends/redis/DockerKvrocks.java | 106 +++++++++++++++++++++ .../james/backends/redis/KvrocksExtension.java | 75 +++++++++++++++ .../james/backends/redis/KvrocksExtensionTest.java | 44 +++++++++ 3 files changed, 225 insertions(+) diff --git a/backends-common/redis/src/test/java/org/apache/james/backends/redis/DockerKvrocks.java b/backends-common/redis/src/test/java/org/apache/james/backends/redis/DockerKvrocks.java new file mode 100644 index 0000000000..7b6e3dc974 --- /dev/null +++ b/backends-common/redis/src/test/java/org/apache/james/backends/redis/DockerKvrocks.java @@ -0,0 +1,106 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + + +package org.apache.james.backends.redis; + +import static java.lang.Boolean.TRUE; + +import java.net.URI; +import java.time.Duration; +import java.util.UUID; + +import org.apache.http.client.utils.URIBuilder; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.wait.strategy.DockerHealthcheckWaitStrategy; +import org.testcontainers.utility.DockerImageName; + +import com.github.fge.lambdas.Throwing; + +import io.lettuce.core.RedisClient; +import io.lettuce.core.api.sync.RedisCommands; + +public class DockerKvrocks { + public static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("apache/kvrocks") + .withTag("2.11.1"); + public static final int DEFAULT_PORT = 6666; + + private final GenericContainer<?> container; + + public DockerKvrocks() { + this.container = getContainer(); + } + + public DockerKvrocks(Network network) { + this.container = getContainer() + .withNetwork(network); + } + + private GenericContainer<?> getContainer() { + return new GenericContainer<>(DEFAULT_IMAGE_NAME) + .withExposedPorts(DEFAULT_PORT) + .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withName("james-kvrocks-test-" + UUID.randomUUID())) + .withNetworkAliases("kvrocks") + .waitingFor(new DockerHealthcheckWaitStrategy() + .withStartupTimeout(Duration.ofMinutes(2))); + } + + public URI redisURI() { + return Throwing.supplier(() -> new URIBuilder() + .setScheme("redis") + .setHost(container.getHost()) + .setPort(container.getMappedPort(DEFAULT_PORT)) + .build()).get(); + } + + public void start() { + if (!container.isRunning()) { + container.start(); + } + } + + public void stop() { + container.stop(); + } + + public void pause() { + container.getDockerClient().pauseContainerCmd(container.getContainerId()).exec(); + } + + public void unPause() { + container.getDockerClient().unpauseContainerCmd(container.getContainerId()).exec(); + } + + public boolean isPaused() { + return TRUE.equals(container.getDockerClient().inspectContainerCmd(container.getContainerId()) + .exec() + .getState() + .getPaused()); + } + + public RedisCommands<String, String> createClient() { + return RedisClient.create(redisURI().toString()) + .connect().sync(); + } + + public void flushAll() { + createClient().flushall(); + } +} \ No newline at end of file diff --git a/backends-common/redis/src/test/java/org/apache/james/backends/redis/KvrocksExtension.java b/backends-common/redis/src/test/java/org/apache/james/backends/redis/KvrocksExtension.java new file mode 100644 index 0000000000..b887b7276d --- /dev/null +++ b/backends-common/redis/src/test/java/org/apache/james/backends/redis/KvrocksExtension.java @@ -0,0 +1,75 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.redis; + +import jakarta.inject.Singleton; + +import org.apache.james.GuiceModuleTestExtension; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; + +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.Provides; + +public class KvrocksExtension implements GuiceModuleTestExtension { + private static final DockerKvrocks DOCKER_KVROCKS_SINGLETON = new DockerKvrocks(); + + @Override + public void beforeAll(ExtensionContext extensionContext) { + DOCKER_KVROCKS_SINGLETON.start(); + } + + @Override + public void afterAll(ExtensionContext extensionContext) { + DOCKER_KVROCKS_SINGLETON.stop(); + } + + @Override + public void beforeEach(ExtensionContext extensionContext) throws Exception { + DOCKER_KVROCKS_SINGLETON.flushAll(); + } + + @Override + public Module getModule() { + return new AbstractModule() { + @Provides + @Singleton + public RedisConfiguration provideConfig() { + return StandaloneRedisConfiguration.from(dockerKvrocks().redisURI().toString()); + } + }; + } + + public DockerKvrocks dockerKvrocks() { + return DOCKER_KVROCKS_SINGLETON; + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return parameterContext.getParameter().getType() == DockerKvrocks.class; + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return dockerKvrocks(); + } +} diff --git a/backends-common/redis/src/test/java/org/apache/james/backends/redis/KvrocksExtensionTest.java b/backends-common/redis/src/test/java/org/apache/james/backends/redis/KvrocksExtensionTest.java new file mode 100644 index 0000000000..2c8408fced --- /dev/null +++ b/backends-common/redis/src/test/java/org/apache/james/backends/redis/KvrocksExtensionTest.java @@ -0,0 +1,44 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.backends.redis; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.lettuce.core.api.sync.RedisCommands; + +class KvrocksExtensionTest { + + @RegisterExtension + static KvrocksExtension kvrocksExtension = new KvrocksExtension(); + + @Test + void redisExtensionShouldWork(DockerKvrocks kvrocks) { + RedisCommands<String, String> client = kvrocks.createClient(); + String key = "KEY1"; + String keyValue = "Value1"; + client.set(key, keyValue); + + assertThat(client.get(key)).isEqualTo(keyValue); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org