From 5cbdf3dc05ec8751326607e79964878f587fce1b Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 15 Sep 2023 10:57:59 +0200 Subject: [PATCH] WIP:ALIGN --- .../kafka/KafkaChatHomeServiceTest.java | 83 +++---------------- .../implementation/kafka/KafkaTestUtils.java | 78 ++++++----------- 2 files changed, 35 insertions(+), 126 deletions(-) diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java index 79cd8f1b..129ab2b3 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaChatHomeServiceTest.java @@ -1,27 +1,16 @@ package de.juplo.kafka.chat.backend.implementation.kafka; -import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest; import lombok.extern.slf4j.Slf4j; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.apache.kafka.common.TopicPartition; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Import; import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.kafka.support.SendResult; import org.springframework.kafka.test.context.EmbeddedKafka; -import java.time.Clock; -import java.util.List; - import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS; import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.DATA_TOPIC; import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.INFO_TOPIC; @@ -29,19 +18,14 @@ import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServ @SpringBootTest( classes = { - KafkaChatHomeServiceTest.KafkaChatHomeTestConfiguration.class, + KafkaTestConfiguration.class, KafkaAutoConfiguration.class, TaskExecutionAutoConfiguration.class, }, properties = { - "spring.main.allow-bean-definition-overriding=true", - "chat.backend.services=kafka", - "chat.backend.kafka.client-id-PREFIX=TEST", - "chat.backend.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", - "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", - "chat.backend.kafka.info-channel-topic=" + INFO_TOPIC, - "chat.backend.kafka.data-channel-topic=" + DATA_TOPIC, - "chat.backend.kafka.num-partitions=" + NUM_SHARDS, + "chat.backend.kafka.info-channel-topic=" + INFO_TOPIC, + "chat.backend.kafka.data-channel-topic=" + DATA_TOPIC, + "chat.backend.kafka.num-partitions=" + NUM_SHARDS, }) @EmbeddedKafka( topics = { INFO_TOPIC, DATA_TOPIC }, @@ -52,66 +36,19 @@ public class KafkaChatHomeServiceTest extends ChatHomeServiceWithShardsTest final static String INFO_TOPIC = "KAFKA_CHAT_HOME_TEST_INFO"; final static String DATA_TOPIC = "KAFKA_CHAT_HOME_TEST_DATA"; - - @TestConfiguration - @EnableConfigurationProperties(ChatBackendProperties.class) - @Import(KafkaServicesConfiguration.class) - static class KafkaChatHomeTestConfiguration - { - @Bean - WorkAssignor dataChannelWorkAssignor(DataChannel dataChannel) - { - return consumer -> - { - List assignedPartitions = - List.of(new TopicPartition(DATA_TOPIC, 2)); - consumer.assign(assignedPartitions); - dataChannel.onPartitionsAssigned(assignedPartitions); - }; - } - - @Bean - Clock clock() - { - return Clock.systemDefaultZone(); - } - } - - @BeforeAll - public static void sendAndLoadStoredData( + static void sendAndLoadStoredData( @Autowired ConsumerTaskRunner consumerTaskRunner, - @Autowired KafkaTemplate messageTemplate) throws InterruptedException - { - send(messageTemplate, INFO_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\": \"5c73531c-6fc4-426c-adcb-afc5c140a0f7\", \"shard\": 2, \"name\": \"FOO\" }", "event_chatroom_created"); - send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"peter\", \"text\" : \"Hallo, ich heiße Peter!\" }", "event_chatmessage_received"); - send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"ute\", \"text\" : \"Ich bin Ute...\" }", "event_chatmessage_received"); - send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 2, \"user\" : \"peter\", \"text\" : \"Willst du mit mir gehen?\" }", "event_chatmessage_received"); - send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"klaus\", \"text\" : \"Ja? Nein? Vielleicht??\" }", "event_chatmessage_received"); - - consumerTaskRunner.run(); - } - - static void send( - KafkaTemplate kafkaTemplate, - String topic, - String key, - String value, - String typeId) + @Autowired KafkaTemplate messageTemplate) { - ProducerRecord record = new ProducerRecord<>(topic, key, value); - record.headers().add("__TypeId__", typeId.getBytes()); - SendResult result = kafkaTemplate.send(record).join(); - log.info( - "Sent {}={} to {}", - key, - value, - new TopicPartition(result.getRecordMetadata().topic(), result.getRecordMetadata().partition())); + KafkaTestConfiguration.sendAndLoadStoredData( + consumerTaskRunner, + messageTemplate); } @AfterAll static void joinConsumerTasks(@Autowired ConsumerTaskRunner consumerTaskRunner) { - consumerTaskRunner.joinConsumerTasks(); + KafkaTestConfiguration.joinConsumerTasks(consumerTaskRunner); } } diff --git a/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaTestUtils.java b/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaTestUtils.java index dba219b0..f0844c9a 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaTestUtils.java +++ b/src/test/java/de/juplo/kafka/chat/backend/implementation/kafka/KafkaTestUtils.java @@ -1,87 +1,60 @@ package de.juplo.kafka.chat.backend.implementation.kafka; import de.juplo.kafka.chat.backend.ChatBackendProperties; -import de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.TopicPartition; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; -import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.SendResult; -import org.springframework.kafka.test.context.EmbeddedKafka; +import org.springframework.test.context.TestPropertySource; import java.time.Clock; import java.util.List; -import static de.juplo.kafka.chat.backend.domain.ChatHomeServiceWithShardsTest.NUM_SHARDS; import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.DATA_TOPIC; import static de.juplo.kafka.chat.backend.implementation.kafka.KafkaChatHomeServiceTest.INFO_TOPIC; -@SpringBootTest( - classes = { - KafkaTestConfiguration.KafkaChatHomeTestConfiguration.class, - KafkaAutoConfiguration.class, - TaskExecutionAutoConfiguration.class, - }, - properties = { +@TestConfiguration +@EnableConfigurationProperties(ChatBackendProperties.class) +@Import(KafkaServicesConfiguration.class) +@TestPropertySource(properties = { "spring.main.allow-bean-definition-overriding=true", "chat.backend.services=kafka", "chat.backend.kafka.client-id-PREFIX=TEST", "chat.backend.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", - "chat.backend.kafka.info-channel-topic=" + INFO_TOPIC, - "chat.backend.kafka.data-channel-topic=" + DATA_TOPIC, - "chat.backend.kafka.num-partitions=" + NUM_SHARDS, }) -@EmbeddedKafka( - topics = { INFO_TOPIC, DATA_TOPIC }, - partitions = 10) @Slf4j -public class KafkaTestConfiguration extends ChatHomeServiceWithShardsTest +public abstract class KafkaTestConfiguration { - final static String INFO_TOPIC = "KAFKA_CHAT_HOME_TEST_INFO"; - final static String DATA_TOPIC = "KAFKA_CHAT_HOME_TEST_DATA"; - - - @TestConfiguration - @EnableConfigurationProperties(ChatBackendProperties.class) - @Import(KafkaServicesConfiguration.class) - static class KafkaChatHomeTestConfiguration + @Bean + WorkAssignor dataChannelWorkAssignor(DataChannel dataChannel) { - @Bean - WorkAssignor dataChannelWorkAssignor(DataChannel dataChannel) + return consumer -> { - return consumer -> - { - List assignedPartitions = - List.of(new TopicPartition(DATA_TOPIC, 2)); - consumer.assign(assignedPartitions); - dataChannel.onPartitionsAssigned(assignedPartitions); - }; - } + List assignedPartitions = + List.of(new TopicPartition(DATA_TOPIC, 2)); + consumer.assign(assignedPartitions); + dataChannel.onPartitionsAssigned(assignedPartitions); + }; + } - @Bean - Clock clock() - { - return Clock.systemDefaultZone(); - } + @Bean + Clock clock() + { + return Clock.systemDefaultZone(); } - @BeforeAll - public static void sendAndLoadStoredData( - @Autowired ConsumerTaskRunner consumerTaskRunner, - @Autowired KafkaTemplate messageTemplate) throws InterruptedException + static void sendAndLoadStoredData( + ConsumerTaskRunner consumerTaskRunner, + KafkaTemplate messageTemplate) { send(messageTemplate, INFO_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\": \"5c73531c-6fc4-426c-adcb-afc5c140a0f7\", \"shard\": 2, \"name\": \"FOO\" }", "event_chatroom_created"); send(messageTemplate, DATA_TOPIC, "5c73531c-6fc4-426c-adcb-afc5c140a0f7","{ \"id\" : 1, \"user\" : \"peter\", \"text\" : \"Hallo, ich heiße Peter!\" }", "event_chatmessage_received"); @@ -92,7 +65,7 @@ public class KafkaTestConfiguration extends ChatHomeServiceWithShardsTest consumerTaskRunner.run(); } - static void send( + private static void send( KafkaTemplate kafkaTemplate, String topic, String key, @@ -109,8 +82,7 @@ public class KafkaTestConfiguration extends ChatHomeServiceWithShardsTest new TopicPartition(result.getRecordMetadata().topic(), result.getRecordMetadata().partition())); } - @AfterAll - static void joinConsumerTasks(@Autowired ConsumerTaskRunner consumerTaskRunner) + static void joinConsumerTasks(ConsumerTaskRunner consumerTaskRunner) { consumerTaskRunner.joinConsumerTasks(); } -- 2.20.1