Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tstavinoha committed Jun 17, 2021
1 parent e275a10 commit 1e95f2b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.infobip.testcontainers.spring.kafka;

import static org.assertj.core.api.BDDAssertions.then;
import static org.awaitility.Awaitility.await;

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import lombok.AllArgsConstructor;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestConstructor;

import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.*;

import static org.assertj.core.api.BDDAssertions.then;

@AllArgsConstructor
@ActiveProfiles("test")
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
Expand All @@ -28,26 +29,18 @@ class KafkaContainerInitializerTest {

@Test
void shouldCreateContainer() throws InterruptedException, ExecutionException, TimeoutException {

// given
String givenData = "givenData";
String givenValue = this.getClass().getName();

// when
SendResult<?, ?> actual = kafkaTemplate.send(TOPIC_NAME, "key", givenData)
SendResult<?, ?> actual = kafkaTemplate.send(TOPIC_NAME, "key", givenValue)
.completable()
.get(30, TimeUnit.SECONDS);

// then
then(actual).isNotNull();
Awaitility.await().atMost(Duration.ofSeconds(30)).until(() -> {
String value = listener.getValue();

if(Objects.isNull(value)) {
return false;
}

then(value).isEqualTo(givenData);
return true;
});
await().atMost(Duration.ofSeconds(10))
.untilAsserted(() -> then(listener.getValue()).isEqualTo(givenValue));
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.infobip.testcontainers.spring.kafka;

import static org.assertj.core.api.BDDAssertions.then;
import static org.awaitility.Awaitility.await;

import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import lombok.AllArgsConstructor;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -33,31 +32,23 @@ class KafkaContainerInitializerWithStaticPortTest {
@Test
void shouldCreateContainer() throws InterruptedException, ExecutionException, TimeoutException {
// given
String givenData = "givenData";
String givenValue = this.getClass().getName();

// when
SendResult<?, ?> actual = kafkaTemplate.send(TOPIC_NAME, "key", givenData)
SendResult<?, ?> actual = kafkaTemplate.send(TOPIC_NAME, "key", givenValue)
.completable()
.get(30, TimeUnit.SECONDS);

// then
then(actual).isNotNull();
Awaitility.await().atMost(Duration.ofSeconds(30)).until(() -> {
String value = listener.getValue();

if(Objects.isNull(value)) {
return false;
}

then(value).isEqualTo(givenData);
return true;
});
await().atMost(Duration.ofSeconds(10))
.untilAsserted(() -> then(listener.getValue()).isEqualTo(givenValue));
}

@Test
void shouldResolveHostInUrl() {
// then
then(kafkaProperties.getBootstrapServers()).containsExactly("localhost:5000");
then(kafkaProperties.getBootstrapServers()).containsExactly("localhost:5001");
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.infobip.testcontainers.spring.kafka;

import java.util.concurrent.atomic.AtomicReference;

import lombok.AllArgsConstructor;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicReference;

@AllArgsConstructor
@Component
class Listener {
Expand All @@ -23,4 +21,5 @@ public void handle(@Payload String value) {
String getValue() {
return value.get();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
spring:
kafka:
consumer:
groupId: test
bootstrap-servers: <host>:5000
groupId: static
autoOffsetReset: earliest
bootstrap-servers: <host>:5001

testcontainers.kafka.topics: test-topic:1:1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ spring:
kafka:
consumer:
groupId: test
autoOffsetReset: earliest
bootstrap-servers: <host>:<port>

testcontainers.kafka.topics: test-topic:1:1
testcontainers.kafka.topics: test-topic:1:1

0 comments on commit 1e95f2b

Please sign in to comment.