|
| 1 | +/* |
| 2 | + * Copyright 2019 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.streams; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Properties; |
| 24 | + |
| 25 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 26 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 27 | +import org.apache.kafka.streams.StreamsBuilder; |
| 28 | +import org.apache.kafka.streams.StreamsConfig; |
| 29 | +import org.apache.kafka.streams.TopologyTestDriver; |
| 30 | +import org.apache.kafka.streams.kstream.KStream; |
| 31 | +import org.apache.kafka.streams.test.ConsumerRecordFactory; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import org.springframework.expression.Expression; |
| 35 | +import org.springframework.expression.common.LiteralExpression; |
| 36 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Gary Russell |
| 40 | + * @since 2.3 |
| 41 | + * |
| 42 | + */ |
| 43 | +public class HeaderEnricherTests { |
| 44 | + |
| 45 | + private static final String INPUT = "input"; |
| 46 | + |
| 47 | + private static final String OUTPUT = "output"; |
| 48 | + |
| 49 | + @Test |
| 50 | + void testWithDriver() { |
| 51 | + StreamsBuilder builder = new StreamsBuilder(); |
| 52 | + Map<String, Expression> headers = new HashMap<>(); |
| 53 | + headers.put("foo", new LiteralExpression("bar")); |
| 54 | + SpelExpressionParser parser = new SpelExpressionParser(); |
| 55 | + headers.put("spel", parser.parseExpression("context.timestamp() + new String(key) + new String(value)")); |
| 56 | + HeaderEnricher<String, String> enricher = new HeaderEnricher<>(headers); |
| 57 | + KStream<String, String> stream = builder.stream(INPUT); |
| 58 | + stream |
| 59 | + .transform(() -> enricher) |
| 60 | + .to(OUTPUT); |
| 61 | + |
| 62 | + Properties config = new Properties(); |
| 63 | + config.put(StreamsConfig.APPLICATION_ID_CONFIG, "test"); |
| 64 | + config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999"); |
| 65 | + TopologyTestDriver driver = new TopologyTestDriver(builder.build(), config); |
| 66 | + |
| 67 | + ConsumerRecordFactory<String, String> recordFactory = new ConsumerRecordFactory<>(new StringSerializer(), |
| 68 | + new StringSerializer()); |
| 69 | + driver.pipeInput(recordFactory.create(INPUT, "key", "value")); |
| 70 | + ProducerRecord<byte[], byte[]> result = driver.readOutput(OUTPUT); |
| 71 | + assertThat(result.headers().lastHeader("foo")).isNotNull(); |
| 72 | + assertThat(result.headers().lastHeader("foo").value()).isEqualTo("bar".getBytes()); |
| 73 | + assertThat(result.headers().lastHeader("spel")).isNotNull(); |
| 74 | + driver.close(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments