|
| 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.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import org.apache.kafka.streams.StreamsConfig; |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | + |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.beans.factory.annotation.Value; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.kafka.annotation.EnableKafka; |
| 38 | +import org.springframework.kafka.annotation.EnableKafkaStreams; |
| 39 | +import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration; |
| 40 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 41 | +import org.springframework.kafka.test.rule.KafkaEmbedded; |
| 42 | +import org.springframework.test.annotation.DirtiesContext; |
| 43 | +import org.springframework.test.context.junit4.SpringRunner; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Pawel Szymczyk |
| 47 | + */ |
| 48 | +@RunWith(SpringRunner.class) |
| 49 | +@DirtiesContext |
| 50 | +@EmbeddedKafka |
| 51 | +public class StreamsBuilderFactoryBeanTest { |
| 52 | + |
| 53 | + private static final String APPLICATION_ID = "testCleanupStreams"; |
| 54 | + |
| 55 | + private static Path stateStoreDir; |
| 56 | + |
| 57 | + @BeforeClass |
| 58 | + public static void setup() throws IOException { |
| 59 | + stateStoreDir = Files.createTempDirectory("test-state-dir"); |
| 60 | + } |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private StreamsBuilderFactoryBean streamsBuilderFactoryBean; |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testCleanupStreams() throws IOException { |
| 67 | + Path stateStore = Files.createDirectory(Paths.get(stateStoreDir.toString(), APPLICATION_ID, "0_0")); |
| 68 | + assertThat(stateStore).exists(); |
| 69 | + streamsBuilderFactoryBean.stop(); |
| 70 | + assertThat(stateStore).doesNotExist(); |
| 71 | + |
| 72 | + stateStore = Files.createDirectory(Paths.get(stateStoreDir.toString(), APPLICATION_ID, "0_0")); |
| 73 | + assertThat(stateStore).exists(); |
| 74 | + streamsBuilderFactoryBean.start(); |
| 75 | + assertThat(stateStore).doesNotExist(); |
| 76 | + } |
| 77 | + |
| 78 | + @Configuration |
| 79 | + @EnableKafka |
| 80 | + @EnableKafkaStreams |
| 81 | + public static class KafkaStreamsConfiguration { |
| 82 | + |
| 83 | + @Value("${" + KafkaEmbedded.SPRING_EMBEDDED_KAFKA_BROKERS + "}") |
| 84 | + private String brokerAddresses; |
| 85 | + |
| 86 | + @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME) |
| 87 | + public StreamsBuilderFactoryBean defaultKafkaStreamsBuilder() throws IOException { |
| 88 | + return new StreamsBuilderFactoryBean(kStreamsConfigs(), new CleanupConfig(true, true)); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME) |
| 92 | + public StreamsConfig kStreamsConfigs() throws IOException { |
| 93 | + Map<String, Object> props = new HashMap<>(); |
| 94 | + props.put(StreamsConfig.APPLICATION_ID_CONFIG, APPLICATION_ID); |
| 95 | + props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddresses); |
| 96 | + props.put(StreamsConfig.STATE_DIR_CONFIG, stateStoreDir.toString()); |
| 97 | + return new StreamsConfig(props); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments