|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 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.cloud.task.batch.autoconfigure.kafka; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.kafka.clients.consumer.Consumer; |
| 25 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 26 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.batch.core.Job; |
| 31 | +import org.springframework.batch.core.JobExecution; |
| 32 | +import org.springframework.batch.core.JobParameters; |
| 33 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 34 | +import org.springframework.batch.core.explore.JobExplorer; |
| 35 | +import org.springframework.batch.core.launch.JobLauncher; |
| 36 | +import org.springframework.batch.item.support.ListItemReader; |
| 37 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 38 | +import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; |
| 39 | +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
| 40 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 41 | +import org.springframework.cloud.task.batch.autoconfigure.SingleStepJobAutoConfiguration; |
| 42 | +import org.springframework.context.ApplicationContext; |
| 43 | +import org.springframework.context.annotation.Bean; |
| 44 | +import org.springframework.context.annotation.Configuration; |
| 45 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 46 | +import org.springframework.kafka.support.serializer.JsonDeserializer; |
| 47 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 48 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 49 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 50 | + |
| 51 | +import static java.util.Collections.singleton; |
| 52 | +import static org.assertj.core.api.Assertions.assertThat; |
| 53 | + |
| 54 | +@EmbeddedKafka(partitions = 1, topics = { "topic1" }) |
| 55 | +public class KafkaItemWriterTests { |
| 56 | + |
| 57 | + private static EmbeddedKafkaBroker embeddedKafkaBroker; |
| 58 | + |
| 59 | + @BeforeAll |
| 60 | + public static void setupTest(EmbeddedKafkaBroker embeddedKafka) { |
| 61 | + embeddedKafkaBroker = embeddedKafka; |
| 62 | + embeddedKafka.addTopics("topic2"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testBaseKafkaItemWriter() { |
| 67 | + final String topicName = "topic1"; |
| 68 | + ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner() |
| 69 | + .withUserConfiguration(CustomMappingConfiguration.class) |
| 70 | + .withConfiguration( |
| 71 | + AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class, |
| 72 | + BatchAutoConfiguration.class, |
| 73 | + SingleStepJobAutoConfiguration.class, |
| 74 | + KafkaItemWriterAutoConfiguration.class)) |
| 75 | + .withPropertyValues("spring.batch.job.jobName=job", |
| 76 | + "spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5", |
| 77 | + "spring.kafka.producer.bootstrap-servers=" |
| 78 | + + embeddedKafkaBroker.getBrokersAsString(), |
| 79 | + "spring.kafka.producer.keySerializer=org.springframework.kafka.support.serializer.JsonSerializer", |
| 80 | + "spring.batch.job.kafkaitemwriter.topic=" + topicName); |
| 81 | + |
| 82 | + applicationContextRunner.run((context) -> { |
| 83 | + waitForTopicPopulation(context); |
| 84 | + validateResults(topicName); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + private void validateResults(String topicName) { |
| 89 | + Map<String, Object> configs = new HashMap<>( |
| 90 | + KafkaTestUtils.consumerProps("1", "false", embeddedKafkaBroker)); |
| 91 | + Consumer<String, Object> consumer = new DefaultKafkaConsumerFactory<>(configs, |
| 92 | + new StringDeserializer(), new JsonDeserializer<>()).createConsumer(); |
| 93 | + consumer.subscribe(singleton(topicName)); |
| 94 | + |
| 95 | + ConsumerRecords<String, Object> consumerRecords = KafkaTestUtils |
| 96 | + .getRecords(consumer); |
| 97 | + assertThat(consumerRecords.count()).isEqualTo(5); |
| 98 | + List<Map<Object, Object>> result = new ArrayList<>(); |
| 99 | + consumerRecords.forEach(cs -> { |
| 100 | + result.add((Map<Object, Object>) cs.value()); |
| 101 | + }); |
| 102 | + List<String> firstNames = new ArrayList<>(); |
| 103 | + result.forEach(s -> firstNames.add((String) s.get("first_name"))); |
| 104 | + assertThat(firstNames.size()).isEqualTo(5); |
| 105 | + assertThat(firstNames).contains("Jane"); |
| 106 | + assertThat(firstNames).contains("John"); |
| 107 | + assertThat(firstNames).contains("Liz"); |
| 108 | + assertThat(firstNames).contains("Cameron"); |
| 109 | + assertThat(firstNames).contains("Judy"); |
| 110 | + } |
| 111 | + |
| 112 | + private void waitForTopicPopulation(ApplicationContext context) throws Exception { |
| 113 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 114 | + Job job = context.getBean(Job.class); |
| 115 | + JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); |
| 116 | + JobExplorer jobExplorer = context.getBean(JobExplorer.class); |
| 117 | + |
| 118 | + while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) { |
| 119 | + Thread.sleep(1000); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @EnableBatchProcessing |
| 124 | + @Configuration |
| 125 | + public static class CustomMappingConfiguration { |
| 126 | + |
| 127 | + @Bean |
| 128 | + public ListItemReader<Map<Object, Object>> itemWriter() { |
| 129 | + List<Map<Object, Object>> list = new ArrayList<>(5); |
| 130 | + addNameToReaderList(list, "Jane"); |
| 131 | + addNameToReaderList(list, "John"); |
| 132 | + addNameToReaderList(list, "Liz"); |
| 133 | + addNameToReaderList(list, "Cameron"); |
| 134 | + addNameToReaderList(list, "Judy"); |
| 135 | + return new ListItemReader<>(list); |
| 136 | + } |
| 137 | + |
| 138 | + private void addNameToReaderList(List<Map<Object, Object>> itemReaderList, |
| 139 | + String value) { |
| 140 | + Map<Object, Object> prepMap = new HashMap<>(); |
| 141 | + prepMap.put("first_name", value); |
| 142 | + itemReaderList.add(prepMap); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments