Set up configuration for testing

This commit is contained in:
Ildar Almakaev 2021-02-02 12:48:45 +03:00
parent 5e0de32439
commit f3e30f3c12
4 changed files with 73 additions and 13 deletions

View file

@ -0,0 +1,35 @@
package com.provectus.kafka.ui;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
@Testcontainers
public abstract class AbstractBaseTest {
@Container
public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.2.1"))
.withNetwork(Network.SHARED);
@Container
public static SchemaRegistryContainer schemaRegistry = new SchemaRegistryContainer("5.2.1")
.withKafka(kafka)
.dependsOn(kafka);
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext context) {
System.setProperty("kafka.clusters.0.name", "local");
System.setProperty("kafka.clusters.0.schemaRegistry", schemaRegistry.getTarget());
}
}
}

View file

@ -1,12 +1,24 @@
package com.provectus.kafka.ui;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.provectus.kafka.ui.rest.MetricsRestController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.reactive.server.WebTestClient;
@SpringBootTest
class KafkaMetricsApplicationTests {
@ContextConfiguration(initializers = {AbstractBaseTest.Initializer.class})
class KafkaMetricsApplicationTests extends AbstractBaseTest {
@Autowired
MetricsRestController metricsRestController;
@Test
public void contextLoads() {
}
@Test
public void shouldReturnEmptyRespWhenGetAllSchemas() {
WebTestClient testClient = WebTestClient.bindToController(metricsRestController)
.build();
testClient.get()
.uri("http://localhost:8080/api/clusters/local/schemas")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody().isEmpty();
}
}

View file

@ -7,17 +7,14 @@ import org.testcontainers.containers.Network;
public class SchemaRegistryContainer extends GenericContainer<SchemaRegistryContainer> {
private static final int SCHEMA_PORT = 8081;
public SchemaRegistryContainer() {
this("5.2.1");
}
public SchemaRegistryContainer(String version) {
super("confluentinc/cp-schema-registry:" + version);
withExposedPorts(8081);
}
public SchemaRegistryContainer withKafka(KafkaContainer kafka) {
return withKafka(kafka.getNetwork(), kafka.getNetworkAliases().get(0) + ":9092");
String bootstrapServers = kafka.getNetworkAliases().get(0) + ":9092";
return withKafka(kafka.getNetwork(), bootstrapServers);
}
public SchemaRegistryContainer withKafka(Network network, String bootstrapServers) {
@ -29,6 +26,6 @@ public class SchemaRegistryContainer extends GenericContainer<SchemaRegistryCont
}
public String getTarget() {
return "http://" + getContainerIpAddress() + ":" + getMappedPort(8081);
return "http://" + getContainerIpAddress() + ":" + getMappedPort(SCHEMA_PORT);
}
}

View file

@ -0,0 +1,16 @@
kafka:
clusters:
-
name: local
bootstrapServers: localhost:9093
zookeeper: localhost:2181
schemaRegistry: http://localhost:8081
jmxPort: 9997
admin-client-timeout: 5000
zookeeper:
connection-timeout: 1000
spring:
jmx:
enabled: true
auth:
enabled: false