Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test containers #160

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<additionalOptions>-Xdoclint:none</additionalOptions>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<protobuf.version>3.11.1</protobuf.version>
<testcontainers.version>1.14.3</testcontainers.version>
</properties>

<scm>
Expand Down Expand Up @@ -177,6 +178,34 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- Spring test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/kafdrop/Kafdrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ public class Kafdrop {
private final static Logger LOG = LoggerFactory.getLogger(Kafdrop.class);

public static void main(String[] args) {
new SpringApplicationBuilder(Kafdrop.class)
.bannerMode(Mode.OFF)
.listeners(new EnvironmentSetupListener(),
new LoggingConfigurationListener())
.run(args);
createApplicationBuilder()
.run(args);
}


public static SpringApplicationBuilder createApplicationBuilder() {
return new SpringApplicationBuilder(Kafdrop.class)
.bannerMode(Mode.OFF)
.listeners(new EnvironmentSetupListener(),
new LoggingConfigurationListener());
}

@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> deploymentCustomizer() {
return factory -> {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/kafdrop/AbstractIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kafdrop;

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.core.env.MapPropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.lifecycle.Startables;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class)
abstract class AbstractIntegrationTest {
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
static KafkaContainer kafka = new KafkaContainer();

public static Map<String, String> getProperties() {
Startables.deepStart(List.of(kafka)).join();
return Map.of("kafka.brokerConnect", kafka.getBootstrapServers());
}

@Override
@SuppressWarnings("unchecked, rawtypes")
public void initialize(ConfigurableApplicationContext context) {
var env = context.getEnvironment();
env.getPropertySources().addFirst(new MapPropertySource(
"testcontainers", (Map) getProperties()
));
}
}
}
8 changes: 8 additions & 0 deletions src/test/java/kafdrop/KafdropTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kafdrop;

import org.junit.Test;

public class KafdropTest extends AbstractIntegrationTest {
@Test
public void contextTest(){}
}
13 changes: 13 additions & 0 deletions src/test/java/kafdrop/LocalRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kafdrop;

/**
* Use this class for local development.
* It will run local kafka in docker with test containers.
*/
public class LocalRunner {
public static void main(String[] args) {
Kafdrop.createApplicationBuilder()
.initializers(new AbstractIntegrationTest.Initializer())
.run(args);
}
}