|
1 | 1 | package com.example.integrationtestspringkafka.service; |
2 | 2 |
|
| 3 | +import com.example.integrationtestspringkafka.dto.ExampleDTO; |
| 4 | +import com.example.integrationtestspringkafka.repository.ExampleRepository; |
| 5 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 6 | +import org.apache.kafka.clients.producer.Producer; |
| 7 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 8 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 9 | +import org.awaitility.Durations; |
3 | 10 | import org.junit.jupiter.api.Test; |
4 | 11 | import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
5 | 13 | import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 15 | +import org.springframework.kafka.core.ProducerFactory; |
| 16 | +import org.springframework.kafka.support.serializer.JsonSerializer; |
| 17 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
6 | 18 | import org.springframework.kafka.test.context.EmbeddedKafka; |
| 19 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 20 | +import org.springframework.test.annotation.DirtiesContext; |
7 | 21 | import org.springframework.test.context.junit.jupiter.SpringExtension; |
8 | 22 |
|
9 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import static org.awaitility.Awaitility.await; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
10 | 27 |
|
11 | 28 | @ExtendWith(SpringExtension.class) |
12 | 29 | @SpringBootTest |
13 | | -@EmbeddedKafka(topics = { "TOPIC_EXAMPLE" }) |
| 30 | +@DirtiesContext |
| 31 | +@EmbeddedKafka(topics = {"TOPIC_EXAMPLE", "TOPIC_EXAMPLE_EXTERNE"}) |
14 | 32 | public class ConsumerServiceIntegrationTest { |
15 | 33 |
|
| 34 | + private static final String TOPIC_EXAMPLE = "TOPIC_EXAMPLE"; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private EmbeddedKafkaBroker embeddedKafkaBroker; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private ExampleRepository exampleRepository; |
| 41 | + |
| 42 | + public ExampleDTO mockExampleDTO(String name, String description) { |
| 43 | + ExampleDTO exampleDTO = new ExampleDTO(); |
| 44 | + exampleDTO.setDescription(description); |
| 45 | + exampleDTO.setName(name); |
| 46 | + return exampleDTO; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * We verify the output in the topic. But aslo in the database. |
| 51 | + */ |
16 | 52 | @Test |
17 | | - public void itShouldConsumeExampleDTO() { |
18 | | - // TODO |
| 53 | + public void itShould_ConsumeCorrectExampleDTO_to_TOPIC_EXAMPLE_and_should_saveCorrectExampleEntity() { |
19 | 54 | // GIVEN |
| 55 | + ExampleDTO exampleDTO = mockExampleDTO("Un nom 2", "Une description 2"); |
| 56 | + // simulation consumer |
| 57 | + Map<String, Object> producerProps = KafkaTestUtils.producerProps(embeddedKafkaBroker); |
| 58 | + Producer<String, ExampleDTO> producerTest = new KafkaProducer(producerProps, new StringSerializer(), new JsonSerializer<ExampleDTO>()); |
| 59 | + |
| 60 | + ProducerFactory producerFactory = new DefaultKafkaProducerFactory<String, ExampleDTO>(producerProps, new StringSerializer(), new JsonSerializer<ExampleDTO>()); |
| 61 | + //Producer<String, ExampleDTO> producerTest = producerFactory.createProducer(); |
| 62 | + ProducerRecord<String, ExampleDTO> producerRecord = new ProducerRecord<String, ExampleDTO>(TOPIC_EXAMPLE,"key", exampleDTO); |
| 63 | + // KafkaTemplate<String, ExampleDTO> template = new KafkaTemplate<>(producerFactory); |
| 64 | + /// template.setDefaultTopic(TOPIC_EXAMPLE); |
| 65 | + // template.send(producerRecord); |
20 | 66 | // WHEN |
| 67 | + producerTest.send( new ProducerRecord<String, ExampleDTO>(TOPIC_EXAMPLE,"", exampleDTO)); |
| 68 | + producerTest.close(); |
21 | 69 | // THEN |
22 | | - assertFalse(false); |
| 70 | + // we must have 1 entity inserted |
| 71 | + // We cannot predict when the insertion into the database will occur. So we wait until the value is present. Thank to Awaitility. |
| 72 | + // List<ExampleEntity> exampleEntityList = exampleRepository.findAll(); |
| 73 | + await().atLeast(Durations.ONE_MINUTE).untilAsserted(() -> { |
| 74 | + assertEquals(1, exampleRepository.findAll().size()); |
| 75 | + |
| 76 | + /// ExampleEntity firstEntity = exampleEntityList.get(0); |
| 77 | + |
| 78 | + //// assertEquals(exampleDTO.getDescription(), firstEntity.getDescription()); |
| 79 | + // assertEquals(exampleDTO.getName(), firstEntity.getName()); |
| 80 | + }); |
23 | 81 | } |
24 | 82 | } |
0 commit comments