|
73 | 73 | import org.springframework.integration.expression.FunctionExpression;
|
74 | 74 | import org.springframework.integration.expression.ValueExpression;
|
75 | 75 | import org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter;
|
| 76 | +import org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler.ProducerRecordCreator; |
76 | 77 | import org.springframework.integration.kafka.support.KafkaIntegrationHeaders;
|
77 | 78 | import org.springframework.integration.kafka.support.KafkaSendFailureException;
|
78 | 79 | import org.springframework.integration.support.MessageBuilder;
|
|
90 | 91 | import org.springframework.kafka.support.KafkaNull;
|
91 | 92 | import org.springframework.kafka.support.SendResult;
|
92 | 93 | import org.springframework.kafka.support.TransactionSupport;
|
| 94 | +import org.springframework.kafka.support.converter.RecordMessageConverter; |
93 | 95 | import org.springframework.kafka.test.EmbeddedKafkaBroker;
|
94 | 96 | import org.springframework.kafka.test.utils.KafkaTestUtils;
|
95 | 97 | import org.springframework.kafka.transaction.KafkaTransactionManager;
|
@@ -783,6 +785,39 @@ void testNoFlush() {
|
783 | 785 | handler.stop();
|
784 | 786 | }
|
785 | 787 |
|
| 788 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 789 | + @Test |
| 790 | + void conversion() { |
| 791 | + ProducerFactory pf = mock(ProducerFactory.class); |
| 792 | + Producer producer = mock(Producer.class); |
| 793 | + given(pf.createProducer()).willReturn(producer); |
| 794 | + ListenableFuture future = mock(ListenableFuture.class); |
| 795 | + willReturn(future).given(producer).send(any(ProducerRecord.class), any(Callback.class)); |
| 796 | + KafkaTemplate template = new KafkaTemplate(pf); |
| 797 | + RecordMessageConverter converter = mock(RecordMessageConverter.class); |
| 798 | + ProducerRecord recordFromConverter = mock(ProducerRecord.class); |
| 799 | + given(converter.fromMessage(any(), any())).willReturn(recordFromConverter); |
| 800 | + template.setMessageConverter(converter); |
| 801 | + KafkaProducerMessageHandler handler = new KafkaProducerMessageHandler(template); |
| 802 | + handler.setTopicExpression(new LiteralExpression("bar")); |
| 803 | + handler.setBeanFactory(mock(BeanFactory.class)); |
| 804 | + ProducerRecordCreator creator = mock(ProducerRecordCreator.class); |
| 805 | + ProducerRecord recordFromCreator = mock(ProducerRecord.class); |
| 806 | + given(creator.create(any(), any(), any(), any(), any(), any(), any())).willReturn(recordFromCreator); |
| 807 | + handler.setProducerRecordCreator(creator); |
| 808 | + handler.afterPropertiesSet(); |
| 809 | + handler.start(); |
| 810 | + handler.handleMessage(new GenericMessage<>("foo")); |
| 811 | + ArgumentCaptor<ProducerRecord> captor = ArgumentCaptor.forClass(ProducerRecord.class); |
| 812 | + verify(producer).send(captor.capture(), any(Callback.class)); |
| 813 | + assertThat(captor.getValue()).isSameAs(recordFromCreator); |
| 814 | + handler.setUseTemplateConverter(true); |
| 815 | + handler.handleMessage(new GenericMessage<>("foo")); |
| 816 | + verify(producer, times(2)).send(captor.capture(), any(Callback.class)); |
| 817 | + assertThat(captor.getValue()).isSameAs(recordFromConverter); |
| 818 | + handler.stop(); |
| 819 | + } |
| 820 | + |
786 | 821 | @SuppressWarnings("serial")
|
787 | 822 | static class SomeOtherTransactionManager extends AbstractPlatformTransactionManager {
|
788 | 823 |
|
|
0 commit comments