|
|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2018-2019 the original author or authors.
|
|
|
|
|
* Copyright 2018-2022 the original author or authors.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
@@ -18,12 +18,8 @@ package org.springframework.cloud.stream.binder.kafka;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
|
|
import kafka.server.KafkaConfig;
|
|
|
|
|
import org.apache.kafka.common.errors.UnknownTopicOrPartitionException;
|
|
|
|
|
import org.junit.ClassRule;
|
|
|
|
|
import org.junit.Rule;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.junit.rules.ExpectedException;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
|
|
|
|
import org.springframework.cloud.stream.binder.BinderException;
|
|
|
|
|
@@ -34,60 +30,56 @@ import org.springframework.cloud.stream.binder.kafka.properties.KafkaConsumerPro
|
|
|
|
|
import org.springframework.cloud.stream.binder.kafka.properties.KafkaProducerProperties;
|
|
|
|
|
import org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner;
|
|
|
|
|
import org.springframework.integration.channel.DirectChannel;
|
|
|
|
|
import org.springframework.kafka.test.rule.EmbeddedKafkaRule;
|
|
|
|
|
import org.springframework.kafka.test.EmbeddedKafkaBroker;
|
|
|
|
|
import org.springframework.kafka.test.condition.EmbeddedKafkaCondition;
|
|
|
|
|
import org.springframework.kafka.test.context.EmbeddedKafka;
|
|
|
|
|
import org.springframework.retry.policy.SimpleRetryPolicy;
|
|
|
|
|
import org.springframework.retry.support.RetryTemplate;
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.isA;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Soby Chacko
|
|
|
|
|
*/
|
|
|
|
|
@EmbeddedKafka(brokerProperties = {"auto.create.topics.enable=false"})
|
|
|
|
|
public class AutoCreateTopicDisabledTests {
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
|
public ExpectedException expectedException = ExpectedException.none();
|
|
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, 1)
|
|
|
|
|
.brokerProperty(KafkaConfig.AutoCreateTopicsEnableProp(), "false");
|
|
|
|
|
private static final EmbeddedKafkaBroker embeddedKafka = EmbeddedKafkaCondition.getBroker();
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testAutoCreateTopicDisabledFailsOnConsumerIfTopicNonExistentOnBroker()
|
|
|
|
|
throws Throwable {
|
|
|
|
|
public void testAutoCreateTopicDisabledFailsOnConsumerIfTopicNonExistentOnBroker() {
|
|
|
|
|
|
|
|
|
|
KafkaProperties kafkaProperties = new TestKafkaProperties();
|
|
|
|
|
kafkaProperties.setBootstrapServers(Collections
|
|
|
|
|
.singletonList(embeddedKafka.getEmbeddedKafka().getBrokersAsString()));
|
|
|
|
|
.singletonList(embeddedKafka.getBrokersAsString()));
|
|
|
|
|
KafkaBinderConfigurationProperties configurationProperties = new KafkaBinderConfigurationProperties(
|
|
|
|
|
kafkaProperties);
|
|
|
|
|
kafkaProperties);
|
|
|
|
|
// disable auto create topic on the binder.
|
|
|
|
|
configurationProperties.setAutoCreateTopics(false);
|
|
|
|
|
|
|
|
|
|
KafkaTopicProvisioner provisioningProvider = new KafkaTopicProvisioner(
|
|
|
|
|
configurationProperties, kafkaProperties, null);
|
|
|
|
|
configurationProperties, kafkaProperties, null);
|
|
|
|
|
provisioningProvider.setMetadataRetryOperations(new RetryTemplate());
|
|
|
|
|
|
|
|
|
|
KafkaMessageChannelBinder binder = new KafkaMessageChannelBinder(
|
|
|
|
|
configurationProperties, provisioningProvider);
|
|
|
|
|
configurationProperties, provisioningProvider);
|
|
|
|
|
|
|
|
|
|
final String testTopicName = "nonExistent" + System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
ExtendedConsumerProperties<KafkaConsumerProperties> properties = new ExtendedConsumerProperties<>(
|
|
|
|
|
new KafkaConsumerProperties());
|
|
|
|
|
new KafkaConsumerProperties());
|
|
|
|
|
|
|
|
|
|
expectedException.expect(BinderException.class);
|
|
|
|
|
expectedException.expectCause(isA(UnknownTopicOrPartitionException.class));
|
|
|
|
|
binder.createConsumerEndpoint(() -> testTopicName, "group", properties);
|
|
|
|
|
assertThatExceptionOfType(BinderException.class)
|
|
|
|
|
.isThrownBy(() -> binder.createConsumerEndpoint(() -> testTopicName, "group", properties))
|
|
|
|
|
.withCauseExactlyInstanceOf(UnknownTopicOrPartitionException.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testAutoCreateTopicDisabledFailsOnProducerIfTopicNonExistentOnBroker()
|
|
|
|
|
throws Throwable {
|
|
|
|
|
public void testAutoCreateTopicDisabledFailsOnProducerIfTopicNonExistentOnBroker() {
|
|
|
|
|
|
|
|
|
|
KafkaProperties kafkaProperties = new TestKafkaProperties();
|
|
|
|
|
kafkaProperties.setBootstrapServers(Collections
|
|
|
|
|
.singletonList(embeddedKafka.getEmbeddedKafka().getBrokersAsString()));
|
|
|
|
|
.singletonList(embeddedKafka.getBrokersAsString()));
|
|
|
|
|
|
|
|
|
|
KafkaBinderConfigurationProperties configurationProperties = new KafkaBinderConfigurationProperties(
|
|
|
|
|
kafkaProperties);
|
|
|
|
|
@@ -111,11 +103,8 @@ public class AutoCreateTopicDisabledTests {
|
|
|
|
|
ExtendedProducerProperties<KafkaProducerProperties> properties = new ExtendedProducerProperties<>(
|
|
|
|
|
new KafkaProducerProperties());
|
|
|
|
|
|
|
|
|
|
expectedException.expect(BinderException.class);
|
|
|
|
|
expectedException.expectCause(isA(UnknownTopicOrPartitionException.class));
|
|
|
|
|
|
|
|
|
|
binder.bindProducer(testTopicName, new DirectChannel(), properties);
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BinderException.class)
|
|
|
|
|
.isThrownBy(() -> binder.bindProducer(testTopicName, new DirectChannel(), properties))
|
|
|
|
|
.withCauseExactlyInstanceOf(UnknownTopicOrPartitionException.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|