Merge pull request #29 from rbkmoney/sahgdkjasd

refactror riak test config
This commit is contained in:
Gregory 2021-09-30 19:04:26 +03:00 committed by GitHub
commit 4d50d7ccb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 116 additions and 62 deletions

12
pom.xml
View File

@ -114,18 +114,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<groupId>com.rbkmoney</groupId>
<artifactId>testcontainers-annotations</artifactId>

View File

@ -20,6 +20,5 @@ public class RiakInitializer {
public void onStartup() {
log.info("Starting RiakCluster");
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.Namespace;
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.repository.ListRepository;
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.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
@ -21,8 +20,8 @@ import java.util.concurrent.ExecutionException;
import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(RiakTestcontainerExtension.class)
@SpringBootTest
@ExtendWith({SpringExtension.class, RiakContainerExtension.class})
public class RiakTest {
private static final String VALUE = "value";
@ -64,5 +63,4 @@ public class RiakTest {
obj = response.getValue(RiakObject.class);
assertNull(obj);
}
}

View File

@ -1,11 +1,12 @@
package com.rbkmoney.wb.list.manager;
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.config.KafkaConsumer;
import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaConsumerConfig;
import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaProducer;
import com.rbkmoney.wb.list.manager.extension.RiakContainerExtension;
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.woody.thrift.impl.http.THClientBuilder;
import org.apache.thrift.TBase;
@ -18,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import java.net.URI;
import java.net.URISyntaxException;
@ -30,10 +32,14 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.testcontainers.shaded.com.trilead.ssh2.ChannelCondition.TIMEOUT;
@ExtendWith({RiakContainerExtension.class})
@SpringBootTest(webEnvironment = RANDOM_PORT)
@KafkaSpringBootTest
@ExtendWith({RiakTestcontainerExtension.class})
@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 static final String IDENTITY_ID = "identityId";

View File

@ -5,6 +5,7 @@ import com.rbkmoney.damsel.wb_list.Command;
import com.rbkmoney.testcontainers.annotations.KafkaSpringBootTest;
import com.rbkmoney.testcontainers.annotations.kafka.KafkaTestcontainer;
import com.rbkmoney.testcontainers.annotations.kafka.config.KafkaProducer;
import com.rbkmoney.wb.list.manager.config.MockedStartupInitializers;
import com.rbkmoney.wb.list.manager.exception.RiakExecutionException;
import com.rbkmoney.wb.list.manager.repository.ListRepository;
import org.apache.thrift.TBase;
@ -12,6 +13,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;
import static org.mockito.ArgumentMatchers.any;
@ -20,6 +22,7 @@ import static org.mockito.Mockito.*;
@KafkaSpringBootTest
@TestPropertySource(properties = {"retry.timeout=100"})
@KafkaTestcontainer(topicsKeys = {"kafka.wblist.topic.command", "kafka.wblist.topic.event.sink"})
@Import(MockedStartupInitializers.class)
public class WbListSafetyApplicationTest {
@Value("${kafka.wblist.topic.command}")

View File

@ -0,0 +1,12 @@
package com.rbkmoney.wb.list.manager.config;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
@TestConfiguration
public class MockedStartupInitializers {
@MockBean
private RiakInitializer riakInitializer;
}

View File

@ -1,36 +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;
import java.util.UUID;
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)
.withNetworkAliases("riak-kv-" + UUID.randomUUID())
.withEnv("CLUSTER_NAME", "riakts")
.withEnv("WAIT_FOR_ERLANG", "1000")
.withLabel("com.basho.riak.cluster.name", "riakts")
.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,72 @@
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(8098, 8087)
.withPrivilegedMode(true)
.withNetworkAliases("riak-kv-" + UUID.randomUUID())
.withEnv("CLUSTER_NAME", "riakts")
.withEnv("WAIT_FOR_ERLANG", "1000")
.withLabel("com.basho.riak.cluster.name", "riakts")
.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>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<root level="info">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.rbkmoney.woody" level="ALL"/>
<logger name="com.rbkmoney.wb" level="ALL"/>
<logger name="com.rbkmoney.wb.list.manager">
<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>