refactror riak test config

This commit is contained in:
Anatoly Karlov 2021-09-30 20:17:23 +07:00
parent a474d130a6
commit e83383ed92
8 changed files with 104 additions and 61 deletions

12
pom.xml
View File

@ -114,18 +114,6 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>1.15.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.15.3</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.rbkmoney</groupId> <groupId>com.rbkmoney</groupId>
<artifactId>testcontainers-annotations</artifactId> <artifactId>testcontainers-annotations</artifactId>

View File

@ -20,6 +20,5 @@ public class RiakInitializer {
public void onStartup() { public void onStartup() {
log.info("Starting RiakCluster"); log.info("Starting RiakCluster");
riakCluster.start(); riakCluster.start();
} }
} }

View File

@ -5,7 +5,7 @@ import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.core.query.Location; import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace; import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.query.RiakObject; import com.basho.riak.client.core.query.RiakObject;
import com.rbkmoney.wb.list.manager.extension.RiakContainerExtension; import com.rbkmoney.wb.list.manager.extension.RiakTestcontainerExtension;
import com.rbkmoney.wb.list.manager.model.Row; import com.rbkmoney.wb.list.manager.model.Row;
import com.rbkmoney.wb.list.manager.repository.ListRepository; import com.rbkmoney.wb.list.manager.repository.ListRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -13,7 +13,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -21,8 +20,8 @@ import java.util.concurrent.ExecutionException;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(RiakTestcontainerExtension.class)
@SpringBootTest @SpringBootTest
@ExtendWith({SpringExtension.class, RiakContainerExtension.class})
public class RiakTest { public class RiakTest {
private static final String VALUE = "value"; private static final String VALUE = "value";
@ -38,7 +37,7 @@ public class RiakTest {
private RiakClient client; private RiakClient client;
@Test @Test
void riakTest() throws ExecutionException, InterruptedException { public void riakTest() throws ExecutionException, InterruptedException {
sleep(10000); sleep(10000);
Row row = new Row(); Row row = new Row();
@ -64,5 +63,4 @@ public class RiakTest {
obj = response.getValue(RiakObject.class); obj = response.getValue(RiakObject.class);
assertNull(obj); assertNull(obj);
} }
} }

View File

@ -1,9 +1,12 @@
package com.rbkmoney.wb.list.manager; package com.rbkmoney.wb.list.manager;
import com.rbkmoney.damsel.wb_list.*; import com.rbkmoney.damsel.wb_list.*;
import com.rbkmoney.testcontainers.annotations.KafkaSpringBootTest;
import com.rbkmoney.testcontainers.annotations.kafka.KafkaTestcontainer; import com.rbkmoney.testcontainers.annotations.kafka.KafkaTestcontainer;
import com.rbkmoney.wb.list.manager.extension.RiakContainerExtension; import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaConsumer;
import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaConsumerConfig;
import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaProducer;
import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaProducerConfig;
import com.rbkmoney.wb.list.manager.extension.RiakTestcontainerExtension;
import com.rbkmoney.wb.list.manager.utils.ChangeCommandWrapper; import com.rbkmoney.wb.list.manager.utils.ChangeCommandWrapper;
import com.rbkmoney.woody.thrift.impl.http.THClientBuilder; import com.rbkmoney.woody.thrift.impl.http.THClientBuilder;
import org.apache.thrift.TBase; import org.apache.thrift.TBase;
@ -16,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -28,10 +32,14 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.testcontainers.shaded.com.trilead.ssh2.ChannelCondition.TIMEOUT; import static org.testcontainers.shaded.com.trilead.ssh2.ChannelCondition.TIMEOUT;
@ExtendWith({RiakContainerExtension.class}) @ExtendWith({RiakTestcontainerExtension.class})
@SpringBootTest(webEnvironment = RANDOM_PORT)
@KafkaSpringBootTest
@KafkaTestcontainer(topicsKeys = {"kafka.wblist.topic.command", "kafka.wblist.topic.event.sink"}) @KafkaTestcontainer(topicsKeys = {"kafka.wblist.topic.command", "kafka.wblist.topic.event.sink"})
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ContextConfiguration(
classes = {
WbListManagerApplication.class,
KafkaProducerConfig.class,
KafkaConsumerConfig.class})
public class WbListManagerApplicationTest { public class WbListManagerApplicationTest {
public static final String IDENTITY_ID = "identityId"; public static final String IDENTITY_ID = "identityId";
@ -52,11 +60,10 @@ public class WbListManagerApplicationTest {
int serverPort; int serverPort;
@Autowired @Autowired
private com.rbkmoney.testcontainers.annotations.kafka.config.KafkaProducer<TBase<?, ?>> testThriftKafkaProducer; private KafkaProducer<TBase<?, ?>> testThriftKafkaProducer;
@Autowired @Autowired
private com.rbkmoney.testcontainers.annotations.kafka.config.KafkaConsumer<Event> private KafkaConsumer<Event> testCommandKafkaConsumer;
testCommandKafkaConsumer;
private WbListServiceSrv.Iface handler; private WbListServiceSrv.Iface handler;

View File

@ -1,31 +0,0 @@
package com.rbkmoney.wb.list.manager.extension;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import java.time.Duration;
public class RiakContainerExtension implements BeforeAllCallback, AfterAllCallback {
private GenericContainer riak;
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
riak = new GenericContainer("basho/riak-kv")
.withExposedPorts(8098, 8087)
.withPrivilegedMode(true)
.waitingFor(new WaitAllStrategy()
.withStartupTimeout(Duration.ofMinutes(2)));
riak.start();
System.setProperty("riak.port", String.valueOf(riak.getMappedPort(8087)));
}
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
riak.stop();
}
}

View File

@ -0,0 +1,70 @@
package com.rbkmoney.wb.list.manager.extension;
import com.rbkmoney.testcontainers.annotations.util.GenericContainerUtil;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.utility.DockerImageName;
import java.time.Duration;
import java.util.List;
import java.util.UUID;
public class RiakTestcontainerExtension implements BeforeAllCallback, AfterAllCallback {
private static final ThreadLocal<GenericContainer<?>> THREAD_CONTAINER = new ThreadLocal<>();
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
GenericContainer<?> container = createRiakTestcontainer();
GenericContainerUtil.startContainer(container);
THREAD_CONTAINER.set(container);
}
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
var container = THREAD_CONTAINER.get();
if (container != null && container.isRunning()) {
container.stop();
}
THREAD_CONTAINER.remove();
}
private GenericContainer<?> createRiakTestcontainer() {
try (GenericContainer<?> container = new GenericContainer<>(
DockerImageName
.parse("basho/riak-kv"))
.withExposedPorts(8087)
.withNetworkAliases("riak-kv-" + UUID.randomUUID())
.withEnv("CLUSTER_NAME", "riakkv")
.withLabel("com.basho.riak.cluster.name", "riak-kv")
.waitingFor(new WaitAllStrategy()
.withStartupTimeout(Duration.ofMinutes(2)))) {
return container;
}
}
public static class RiakTestcontainerContextCustomizerFactory implements ContextCustomizerFactory {
@Override
public ContextCustomizer createContextCustomizer(
Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
return (context, mergedConfig) -> {
var container = THREAD_CONTAINER.get();
if (container != null) {
TestPropertyValues.of(
"riak.address=" + container.getContainerIpAddress(),
"riak.port=" + container.getMappedPort(8087))
.applyTo(context);
}
};
}
}
}

View File

@ -0,0 +1,3 @@
# Spring Test ContextCustomizerFactories
org.springframework.test.context.ContextCustomizerFactory=\
com.rbkmoney.wb.list.manager.extension.RiakTestcontainerExtension.RiakTestcontainerContextCustomizerFactory

View File

@ -2,10 +2,19 @@
<configuration> <configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="info">
<root level="INFO">
<appender-ref ref="CONSOLE"/> <appender-ref ref="CONSOLE"/>
</root> </root>
<logger name="com.rbkmoney.woody" level="ALL"/> <logger name="com.rbkmoney.wb.list.manager">
<logger name="com.rbkmoney.wb" level="ALL"/> <level value="info"/>
</logger>
<logger name="org.apache.kafka">
<level value="warn"/>
</logger>
<logger name="org.springframework.boot.test.context">
<level value="warn"/>
</logger>
<logger name="org.springframework.test.context">
<level value="warn"/>
</logger>
</configuration> </configuration>