|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + * http://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.core; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.kafka.streams.StreamsConfig; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.beans.factory.annotation.Value; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.kafka.KafkaException; |
| 33 | +import org.springframework.kafka.annotation.EnableKafka; |
| 34 | +import org.springframework.kafka.annotation.EnableKafkaStreams; |
| 35 | +import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration; |
| 36 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 37 | +import org.springframework.kafka.test.rule.KafkaEmbedded; |
| 38 | +import org.springframework.test.annotation.DirtiesContext; |
| 39 | +import org.springframework.test.context.junit4.SpringRunner; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Soby Chacko |
| 43 | + */ |
| 44 | +@RunWith(SpringRunner.class) |
| 45 | +@DirtiesContext |
| 46 | +@EmbeddedKafka |
| 47 | +public class StreamsBuilderFactoryLateConfigTests { |
| 48 | + |
| 49 | + private static final String APPLICATION_ID = "streamsBuilderFactoryLateConfigTests"; |
| 50 | + |
| 51 | + @Value("${" + KafkaEmbedded.SPRING_EMBEDDED_KAFKA_BROKERS + "}") |
| 52 | + private String brokerAddresses; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private StreamsBuilderFactoryBean streamsBuilderFactoryBean; |
| 56 | + |
| 57 | + @Test(expected = KafkaException.class) |
| 58 | + public void testStreamBuilderFactoryCannotBeStartedWithoutStreamconfig() { |
| 59 | + StreamsBuilderFactoryBean streamsBuilderFactoryBean = new StreamsBuilderFactoryBean(); |
| 60 | + streamsBuilderFactoryBean.start(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test(expected = IllegalArgumentException.class) |
| 64 | + public void testStreamBuilderFactoryCannotBeInstantiatedWhenAutoStart() throws Exception { |
| 65 | + StreamsBuilderFactoryBean streamsBuilderFactoryBean = new StreamsBuilderFactoryBean(); |
| 66 | + streamsBuilderFactoryBean.setAutoStartup(true); |
| 67 | + streamsBuilderFactoryBean.createInstance(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testStreamsBuilderFactoryWithConfigProvidedLater() { |
| 72 | + Map<String, Object> props = new HashMap<>(); |
| 73 | + props.put(StreamsConfig.APPLICATION_ID_CONFIG, APPLICATION_ID); |
| 74 | + props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddresses); |
| 75 | + StreamsConfig streamsConfig = new StreamsConfig(props); |
| 76 | + streamsBuilderFactoryBean.setStreamsConfig(streamsConfig); |
| 77 | + |
| 78 | + assertThat(streamsBuilderFactoryBean.isRunning()).isFalse(); |
| 79 | + streamsBuilderFactoryBean.start(); |
| 80 | + assertThat(streamsBuilderFactoryBean.isRunning()).isTrue(); |
| 81 | + } |
| 82 | + |
| 83 | + @Configuration |
| 84 | + @EnableKafka |
| 85 | + @EnableKafkaStreams |
| 86 | + public static class KafkaStreamsConfiguration { |
| 87 | + |
| 88 | + @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME) |
| 89 | + public StreamsBuilderFactoryBean defaultKafkaStreamsBuilder() { |
| 90 | + StreamsBuilderFactoryBean streamsBuilderFactoryBean = new StreamsBuilderFactoryBean(); |
| 91 | + streamsBuilderFactoryBean.setAutoStartup(false); |
| 92 | + return streamsBuilderFactoryBean; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments