|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.test.junit; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.io.BufferedOutputStream; |
| 22 | +import java.io.File; |
| 23 | +import java.io.FileOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Properties; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.concurrent.ExecutionException; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.concurrent.TimeoutException; |
| 32 | + |
| 33 | +import org.apache.kafka.clients.admin.AdminClient; |
| 34 | +import org.apache.kafka.clients.admin.AdminClientConfig; |
| 35 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 36 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 37 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 38 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 41 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 42 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 43 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 44 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 45 | + |
| 46 | +import org.springframework.util.DefaultPropertiesPersister; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Artem Bilan |
| 50 | + * |
| 51 | + * @since 3.0 |
| 52 | + */ |
| 53 | +public class GlobalEmbeddedKafkaTestExecutionListenerTests { |
| 54 | + |
| 55 | + @Test |
| 56 | + void testGlobalEmbeddedKafkaTestExecutionListener() throws IOException { |
| 57 | + System.setProperty(GlobalEmbeddedKafkaTestExecutionListener.LISTENER_ENABLED_PROPERTY_NAME, "true"); |
| 58 | + |
| 59 | + var brokerProperties = new Properties(); |
| 60 | + brokerProperties.setProperty("auto.create.topics.enable", "false"); |
| 61 | + |
| 62 | + var propertiesFile = File.createTempFile("kafka-broker", ".properties"); |
| 63 | + |
| 64 | + try (var outputStream = new BufferedOutputStream(new FileOutputStream(propertiesFile))) { |
| 65 | + new DefaultPropertiesPersister().store(brokerProperties, outputStream, "Last entry"); |
| 66 | + } |
| 67 | + |
| 68 | + System.setProperty(GlobalEmbeddedKafkaTestExecutionListener.BROKER_PROPERTIES_LOCATION_PROPERTY_NAME, |
| 69 | + "file:/" + propertiesFile.getAbsolutePath()); |
| 70 | + |
| 71 | + var discoveryRequest = |
| 72 | + LauncherDiscoveryRequestBuilder.request() |
| 73 | + .selectors(DiscoverySelectors.selectClass(TestClass1.class), |
| 74 | + DiscoverySelectors.selectClass(TestClass2.class)) |
| 75 | + .build(); |
| 76 | + |
| 77 | + var summaryGeneratingListener = new SummaryGeneratingListener(); |
| 78 | + LauncherFactory.create().execute(discoveryRequest, summaryGeneratingListener); |
| 79 | + |
| 80 | + var summary = summaryGeneratingListener.getSummary(); |
| 81 | + assertThat(summary.getTestsStartedCount()).isEqualTo(2); |
| 82 | + assertThat(summary.getTestsSucceededCount()).isEqualTo(1); |
| 83 | + assertThat(summary.getTestsFailedCount()).isEqualTo(1); |
| 84 | + |
| 85 | + System.clearProperty(GlobalEmbeddedKafkaTestExecutionListener.LISTENER_ENABLED_PROPERTY_NAME); |
| 86 | + System.clearProperty(GlobalEmbeddedKafkaTestExecutionListener.BROKER_PROPERTIES_LOCATION_PROPERTY_NAME); |
| 87 | + } |
| 88 | + |
| 89 | + @EnabledIfSystemProperty(named = GlobalEmbeddedKafkaTestExecutionListener.LISTENER_ENABLED_PROPERTY_NAME, |
| 90 | + matches = "true") |
| 91 | + static class TestClass1 { |
| 92 | + |
| 93 | + @Test |
| 94 | + void testDescribeTopic() throws ExecutionException, InterruptedException, TimeoutException { |
| 95 | + Map<String, Object> adminConfigs = |
| 96 | + Map.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, |
| 97 | + System.getProperty("spring.kafka.bootstrap-servers")); |
| 98 | + try (var admin = AdminClient.create(adminConfigs)) { |
| 99 | + var topicsMap = |
| 100 | + admin.describeTopics(Set.of("topic1", "topic2")) |
| 101 | + .allTopicNames() |
| 102 | + .get(10, TimeUnit.SECONDS); |
| 103 | + |
| 104 | + assertThat(topicsMap).containsOnlyKeys("topic1", "topic2"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @EnabledIfSystemProperty(named = GlobalEmbeddedKafkaTestExecutionListener.LISTENER_ENABLED_PROPERTY_NAME, |
| 111 | + matches = "true") |
| 112 | + static class TestClass2 { |
| 113 | + |
| 114 | + @Test |
| 115 | + void testCannotAutoCreateTopic() throws ExecutionException, InterruptedException, TimeoutException { |
| 116 | + Map<String, Object> producerConfigs = new HashMap<>(); |
| 117 | + producerConfigs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, |
| 118 | + System.getProperty("spring.kafka.bootstrap-servers")); |
| 119 | + producerConfigs.put(ProducerConfig.RETRIES_CONFIG, 1); |
| 120 | + producerConfigs.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 1); |
| 121 | + producerConfigs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10); |
| 122 | + |
| 123 | + StringSerializer serializer = new StringSerializer(); |
| 124 | + try (var kafkaProducer = new KafkaProducer<>(producerConfigs, serializer, serializer)) { |
| 125 | + var recordMetadata = |
| 126 | + kafkaProducer.send(new ProducerRecord<>("nonExistingTopic", "testValue")) |
| 127 | + .get(10, TimeUnit.SECONDS); |
| 128 | + |
| 129 | + assertThat(recordMetadata).isNotNull(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments