|
| 1 | +package de.muenchen.oss.dbs.ticketing.eventing.service; |
| 2 | + |
| 3 | +import static de.muenchen.oss.dbs.ticketing.eventing.service.TestConstants.SPRING_TEST_PROFILE; |
| 4 | +import static org.awaitility.Awaitility.await; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import de.muenchen.oss.dbs.ticketing.eventing.service.adapter.in.rest.EventDTO; |
| 9 | +import de.muenchen.oss.dbs.ticketing.eventing.service.adapter.in.rest.EventMapperImpl; |
| 10 | +import de.muenchen.oss.dbs.ticketing.eventing.service.domain.model.Event; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.Base64; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import org.apache.kafka.clients.consumer.Consumer; |
| 16 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.context.SpringBootTest; |
| 23 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 24 | +import org.springframework.http.HttpEntity; |
| 25 | +import org.springframework.http.HttpHeaders; |
| 26 | +import org.springframework.http.HttpStatusCode; |
| 27 | +import org.springframework.http.MediaType; |
| 28 | +import org.springframework.http.ResponseEntity; |
| 29 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 30 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 31 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 32 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 33 | +import org.springframework.test.context.ActiveProfiles; |
| 34 | +import org.springframework.test.context.ContextConfiguration; |
| 35 | +import org.springframework.test.context.junit4.SpringRunner; |
| 36 | + |
| 37 | +@SpringBootTest(classes = DbsTicketingEventingService.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 38 | +@RunWith(SpringRunner.class) |
| 39 | +@ContextConfiguration(classes = { EventMapperImpl.class, ObjectMapper.class }) |
| 40 | +@ActiveProfiles(SPRING_TEST_PROFILE) |
| 41 | +@EmbeddedKafka(partitions = 1, topics = { "event-out" }) |
| 42 | +@ExtendWith(MockitoExtension.class) |
| 43 | +class E2ETest { |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private TestRestTemplate restTemplate; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private EmbeddedKafkaBroker embeddedKafkaBroker; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private ObjectMapper objectMapper; |
| 53 | + |
| 54 | + @Test |
| 55 | + void testEventUnauthorized() { |
| 56 | + final ResponseEntity<Void> response = restTemplate.postForEntity("/api/event", Map.of(), Void.class); |
| 57 | + assertEquals(HttpStatusCode.valueOf(401), response.getStatusCode()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testEventSuccessful() throws Exception { |
| 62 | + // setup Kafka consumer |
| 63 | + final Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", embeddedKafkaBroker); |
| 64 | + final DefaultKafkaConsumerFactory<String, String> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerProps); |
| 65 | + @SuppressWarnings("PMD.CloseResource") |
| 66 | + final Consumer<String, String> consumer = consumerFactory.createConsumer(); |
| 67 | + embeddedKafkaBroker.consumeFromEmbeddedTopics(consumer, "event-out"); |
| 68 | + |
| 69 | + // setup call |
| 70 | + final EventDTO eventDTO = new EventDTO("123", "open", "1", "test-anliegen", "test-lhm-1"); |
| 71 | + final String basicAuth = "Basic %s".formatted(Base64.getEncoder().encodeToString("test-user:test-password".getBytes(StandardCharsets.UTF_8))); |
| 72 | + @SuppressWarnings("PMD.LooseCoupling") |
| 73 | + final HttpHeaders headers = new HttpHeaders(); |
| 74 | + headers.set("Authorization", basicAuth); |
| 75 | + headers.set("X-Zammad-Trigger", "test-trigger"); |
| 76 | + headers.set("X-Zammad-Delivery", "test-delivery-id"); |
| 77 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 78 | + final HttpEntity<String> entity = new HttpEntity<>(objectMapper.writeValueAsString(eventDTO), headers); |
| 79 | + // call |
| 80 | + final ResponseEntity<Void> response = restTemplate.postForEntity("/api/event", entity, Void.class); |
| 81 | + |
| 82 | + // test |
| 83 | + assertEquals(HttpStatusCode.valueOf(200), response.getStatusCode()); |
| 84 | + // await and verify the event sent to Kafka |
| 85 | + await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { |
| 86 | + final ConsumerRecord<String, String> singleRecord = KafkaTestUtils.getSingleRecord(consumer, "event-out"); |
| 87 | + final Event capturedEvent = objectMapper.readValue(singleRecord.value(), Event.class); |
| 88 | + // test |
| 89 | + assertEquals("test-action", capturedEvent.action()); |
| 90 | + assertEquals("123", capturedEvent.ticket()); |
| 91 | + assertEquals("open", capturedEvent.status()); |
| 92 | + assertEquals("1", capturedEvent.statusId()); |
| 93 | + assertEquals("test-anliegen", capturedEvent.anliegenart()); |
| 94 | + assertEquals("test-lhm-1", capturedEvent.lhmExtId()); |
| 95 | + }); |
| 96 | + } |
| 97 | +} |
0 commit comments