Skip to content

Commit

Permalink
ReusableTestBase fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lpandzic committed Aug 9, 2023
1 parent db09313 commit bf136e4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,18 @@ public void initialize(ConfigurableApplicationContext applicationContext) {
private static void createTestKafkaTopics(String bootstrapServers, String[] topics) {

try (var client = AdminClient.create(Collections.singletonMap("bootstrap.servers", bootstrapServers))) {
deleteTopics(client, topics);
createTopics(client, topics);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static void deleteTopics(AdminClient client, String[] topics) throws InterruptedException,
ExecutionException, TimeoutException {
var existingTopics = client.listTopics().names().get(60, TimeUnit.SECONDS);
var deleteTopics = Stream.of(topics)
.map(topic -> topic.split(":"))
.map(topicParts -> topicParts[0])
.filter(existingTopics::contains)
.collect(Collectors.toList());
client.deleteTopics(deleteTopics)
.all()
.get(60, TimeUnit.SECONDS);
}

private static void createTopics(AdminClient client, String[] topics) throws InterruptedException,
ExecutionException, TimeoutException {
var existingTopics = client.listTopics().names().get(60, TimeUnit.SECONDS);
var newTopics = Stream.of(topics)
.map(topic -> topic.split(":"))
.filter(topic -> !existingTopics.contains(topic[0]))
.map(topicParts -> new NewTopic(topicParts[0], parseInt(topicParts[1]),
parseShort(topicParts[2])))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import org.springframework.test.context.TestConstructor;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;

@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@SpringBootTest(classes = Main.class)
Expand All @@ -30,6 +28,7 @@ static void createTestcontainersPropertiesFile() {

@AfterAll
static void cleanupTestcontainersPropertiesFile() {
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "false");
FILE.delete();

if (TEMP_FILE.exists()) {
Expand Down

0 comments on commit bf136e4

Please sign in to comment.