|
16 | 16 |
|
17 | 17 | package com.google.cloud.spring.stream.binder.pubsub;
|
18 | 18 |
|
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import com.google.api.gax.core.CredentialsProvider; |
| 23 | +import com.google.auth.Credentials; |
| 24 | +import com.google.cloud.spring.core.GcpProjectIdProvider; |
19 | 25 | import com.google.cloud.spring.pubsub.PubSubAdmin;
|
20 | 26 | import com.google.cloud.spring.pubsub.core.PubSubTemplate;
|
| 27 | +import com.google.cloud.spring.pubsub.integration.inbound.PubSubInboundChannelAdapter; |
21 | 28 | import com.google.cloud.spring.pubsub.integration.inbound.PubSubMessageSource;
|
22 | 29 | import com.google.cloud.spring.pubsub.integration.outbound.PubSubMessageHandler;
|
23 | 30 | import com.google.cloud.spring.stream.binder.pubsub.config.PubSubBinderConfiguration;
|
24 | 31 | import com.google.cloud.spring.stream.binder.pubsub.properties.PubSubConsumerProperties;
|
25 | 32 | import com.google.cloud.spring.stream.binder.pubsub.properties.PubSubExtendedBindingProperties;
|
26 | 33 | import com.google.cloud.spring.stream.binder.pubsub.provisioning.PubSubChannelProvisioner;
|
| 34 | +import org.apache.commons.logging.Log; |
| 35 | +import org.apache.commons.logging.LogFactory; |
27 | 36 | import org.junit.Before;
|
28 | 37 | import org.junit.Test;
|
29 | 38 | import org.junit.runner.RunWith;
|
30 | 39 | import org.mockito.Mock;
|
31 | 40 | import org.mockito.junit.MockitoJUnitRunner;
|
32 | 41 |
|
| 42 | +import org.springframework.beans.DirectFieldAccessor; |
33 | 43 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
| 44 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
34 | 45 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
| 46 | +import org.springframework.cloud.stream.annotation.EnableBinding; |
| 47 | +import org.springframework.cloud.stream.annotation.Input; |
| 48 | +import org.springframework.cloud.stream.annotation.StreamListener; |
| 49 | +import org.springframework.cloud.stream.binder.Binding; |
35 | 50 | import org.springframework.cloud.stream.binder.ExtendedConsumerProperties;
|
36 | 51 | import org.springframework.cloud.stream.binder.ExtendedProducerProperties;
|
| 52 | +import org.springframework.cloud.stream.binder.PollableMessageSource; |
| 53 | +import org.springframework.cloud.stream.binding.BindingService; |
| 54 | +import org.springframework.cloud.stream.config.ConsumerEndpointCustomizer; |
| 55 | +import org.springframework.cloud.stream.config.ProducerMessageHandlerCustomizer; |
| 56 | +import org.springframework.cloud.stream.messaging.Processor; |
| 57 | +import org.springframework.cloud.stream.messaging.Sink; |
37 | 58 | import org.springframework.cloud.stream.provisioning.ConsumerDestination;
|
38 | 59 | import org.springframework.cloud.stream.provisioning.ProducerDestination;
|
| 60 | +import org.springframework.context.annotation.Bean; |
39 | 61 | import org.springframework.messaging.MessageChannel;
|
40 | 62 |
|
41 | 63 | import static org.assertj.core.api.Assertions.assertThat;
|
| 64 | +import static org.mockito.Mockito.mock; |
42 | 65 | import static org.mockito.Mockito.verify;
|
43 | 66 | import static org.mockito.Mockito.when;
|
44 | 67 |
|
|
52 | 75 | */
|
53 | 76 | @RunWith(MockitoJUnitRunner.class)
|
54 | 77 | public class PubSubMessageChannelBinderTests {
|
| 78 | + private static final Log LOGGER = LogFactory.getLog(PubSubMessageChannelBinderTests.class); |
55 | 79 |
|
56 | 80 | PubSubMessageChannelBinder binder;
|
57 | 81 |
|
@@ -148,4 +172,67 @@ public void consumerMaxFetchPropertyPropagatesToMessageSource() {
|
148 | 172 | });
|
149 | 173 | }
|
150 | 174 |
|
| 175 | + @Test |
| 176 | + public void testProducerAndConsumerCustomizers() { |
| 177 | + baseContext.withUserConfiguration(PubSubBinderTestConfig.class) |
| 178 | + .withPropertyValues("spring.cloud.stream.bindings.input.group=testGroup") |
| 179 | + .run(context -> { |
| 180 | + |
| 181 | + DirectFieldAccessor channelBindingServiceAccessor = new DirectFieldAccessor( |
| 182 | + context.getBean(BindingService.class)); |
| 183 | + @SuppressWarnings("unchecked") |
| 184 | + Map<String, List<Binding<MessageChannel>>> consumerBindings = |
| 185 | + (Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor |
| 186 | + .getPropertyValue("consumerBindings"); |
| 187 | + assertThat(new DirectFieldAccessor( |
| 188 | + consumerBindings.get("input").get(0)).getPropertyValue( |
| 189 | + "lifecycle.beanName")) |
| 190 | + .isEqualTo("setByCustomizer:input"); |
| 191 | + |
| 192 | + @SuppressWarnings("unchecked") |
| 193 | + Map<String, Binding<MessageChannel>> producerBindings = |
| 194 | + (Map<String, Binding<MessageChannel>>) channelBindingServiceAccessor |
| 195 | + .getPropertyValue("producerBindings"); |
| 196 | + assertThat(new DirectFieldAccessor( |
| 197 | + producerBindings.get("output")).getPropertyValue( |
| 198 | + "val$producerMessageHandler.beanName")) |
| 199 | + .isEqualTo("setByCustomizer:output"); |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + public interface PMS { |
| 204 | + @Input |
| 205 | + PollableMessageSource source(); |
| 206 | + } |
| 207 | + |
| 208 | + @EnableBinding({ Processor.class, PMS.class }) |
| 209 | + @EnableAutoConfiguration |
| 210 | + public static class PubSubBinderTestConfig { |
| 211 | + |
| 212 | + @Bean |
| 213 | + public ConsumerEndpointCustomizer<PubSubInboundChannelAdapter> consumerCustomizer() { |
| 214 | + return (p, q, g) -> p.setBeanName("setByCustomizer:" + q); |
| 215 | + } |
| 216 | + |
| 217 | + @Bean |
| 218 | + public ProducerMessageHandlerCustomizer<PubSubMessageHandler> handlerCustomizer() { |
| 219 | + return (handler, destinationName) -> handler.setBeanName("setByCustomizer:" + destinationName); |
| 220 | + } |
| 221 | + |
| 222 | + @StreamListener(Sink.INPUT) |
| 223 | + public void process(String payload) throws InterruptedException { |
| 224 | + LOGGER.info("received: " + payload); |
| 225 | + } |
| 226 | + |
| 227 | + @Bean |
| 228 | + public GcpProjectIdProvider projectIdProvider() { |
| 229 | + return () -> "fake project"; |
| 230 | + } |
| 231 | + |
| 232 | + @Bean |
| 233 | + public CredentialsProvider googleCredentials() { |
| 234 | + return () -> mock(Credentials.class); |
| 235 | + } |
| 236 | + |
| 237 | + } |
151 | 238 | }
|
0 commit comments