Search in sources :

Example 11 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class BindingServiceTests method checkDynamicBinding.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void checkDynamicBinding() {
    BindingServiceProperties properties = new BindingServiceProperties();
    BindingProperties bindingProperties = new BindingProperties();
    bindingProperties.setProducer(new ProducerProperties());
    properties.setBindings(Collections.singletonMap("foo", bindingProperties));
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    final ExtendedPropertiesBinder binder = mock(ExtendedPropertiesBinder.class);
    Properties extendedProps = new Properties();
    when(binder.getExtendedProducerProperties(anyString())).thenReturn(extendedProps);
    Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
    final AtomicReference<MessageChannel> dynamic = new AtomicReference<>();
    when(binder.bindProducer(matches("foo"), any(DirectChannel.class), any(ProducerProperties.class))).thenReturn(mockBinding);
    BindingService bindingService = new BindingService(properties, binderFactory) {

        @Override
        protected <T> Binder<T, ?, ?> getBinder(String channelName, Class<T> bindableType) {
            return binder;
        }
    };
    SubscribableChannelBindingTargetFactory bindableSubscribableChannelFactory = new SubscribableChannelBindingTargetFactory(new MessageConverterConfigurer(properties, new CompositeMessageConverterFactory()));
    final AtomicBoolean callbackInvoked = new AtomicBoolean();
    BinderAwareChannelResolver resolver = new BinderAwareChannelResolver(bindingService, bindableSubscribableChannelFactory, new DynamicDestinationsBindable(), (name, channel, props, extended) -> {
        callbackInvoked.set(true);
        assertThat(name).isEqualTo("foo");
        assertThat(channel).isNotNull();
        assertThat(props).isNotNull();
        assertThat(extended).isSameAs(extendedProps);
        props.setUseNativeEncoding(true);
        extendedProps.setProperty("bar", "baz");
    });
    ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
    when(beanFactory.getBean("foo", MessageChannel.class)).thenThrow(new NoSuchBeanDefinitionException(MessageChannel.class));
    when(beanFactory.getBean("bar", MessageChannel.class)).thenThrow(new NoSuchBeanDefinitionException(MessageChannel.class));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            dynamic.set(invocation.getArgument(1));
            return null;
        }
    }).when(beanFactory).registerSingleton(eq("foo"), any(MessageChannel.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return dynamic.get();
        }
    }).when(beanFactory).initializeBean(any(MessageChannel.class), eq("foo"));
    resolver.setBeanFactory(beanFactory);
    MessageChannel resolved = resolver.resolveDestination("foo");
    assertThat(resolved).isSameAs(dynamic.get());
    ArgumentCaptor<ProducerProperties> captor = ArgumentCaptor.forClass(ProducerProperties.class);
    verify(binder).bindProducer(eq("foo"), eq(dynamic.get()), captor.capture());
    assertThat(captor.getValue().isUseNativeEncoding()).isTrue();
    assertThat(captor.getValue()).isInstanceOf(ExtendedProducerProperties.class);
    assertThat(((ExtendedProducerProperties) captor.getValue()).getExtension()).isSameAs(extendedProps);
    doReturn(dynamic.get()).when(beanFactory).getBean("foo", MessageChannel.class);
    properties.setDynamicDestinations(new String[] { "foo" });
    resolved = resolver.resolveDestination("foo");
    assertThat(resolved).isSameAs(dynamic.get());
    properties.setDynamicDestinations(new String[] { "test" });
    try {
        resolved = resolver.resolveDestination("bar");
        fail();
    } catch (DestinationResolutionException e) {
        assertThat(e).hasMessageContaining("Failed to find MessageChannel bean with name 'bar'");
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) Properties(java.util.Properties) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageChannel(org.springframework.messaging.MessageChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) Test(org.junit.Test)

Example 12 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class BinderAwareChannelResolverTests method propertyPassthrough.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void propertyPassthrough() {
    Map<String, BindingProperties> bindings = new HashMap<>();
    BindingProperties genericProperties = new BindingProperties();
    genericProperties.setContentType("text/plain");
    bindings.put("foo", genericProperties);
    this.bindingServiceProperties.setBindings(bindings);
    Binder binder = mock(Binder.class);
    Binder binder2 = mock(Binder.class);
    BinderFactory mockBinderFactory = Mockito.mock(BinderFactory.class);
    Binding<MessageChannel> fooBinding = Mockito.mock(Binding.class);
    Binding<MessageChannel> barBinding = Mockito.mock(Binding.class);
    when(binder.bindProducer(matches("foo"), any(DirectChannel.class), any(ProducerProperties.class))).thenReturn(fooBinding);
    when(binder2.bindProducer(matches("bar"), any(DirectChannel.class), any(ProducerProperties.class))).thenReturn(barBinding);
    when(mockBinderFactory.getBinder(null, DirectChannel.class)).thenReturn(binder);
    when(mockBinderFactory.getBinder("someTransport", DirectChannel.class)).thenReturn(binder2);
    BindingService bindingService = new BindingService(bindingServiceProperties, mockBinderFactory);
    BinderAwareChannelResolver resolver = new BinderAwareChannelResolver(bindingService, this.bindingTargetFactory, new DynamicDestinationsBindable());
    resolver.setBeanFactory(context.getBeanFactory());
    SubscribableChannel resolved = (SubscribableChannel) resolver.resolveDestination("foo");
    verify(binder).bindProducer(eq("foo"), any(MessageChannel.class), any(ProducerProperties.class));
    assertThat(resolved).isSameAs(context.getBean("foo"));
    this.context.close();
}
Also used : BindingService(org.springframework.cloud.stream.binding.BindingService) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BinderAwareChannelResolver(org.springframework.cloud.stream.binding.BinderAwareChannelResolver) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) DynamicDestinationsBindable(org.springframework.cloud.stream.binding.DynamicDestinationsBindable) Test(org.junit.Test)

Example 13 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testConvertSimpler.

@Test
public void testConvertSimpler() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    BindingServiceProperties bsps = this.context.getBean(BindingServiceProperties.class);
    BindingProperties props = new BindingProperties();
    props.setContentType("text/plain");
    bsps.setBindings(Collections.singletonMap("foo", props));
    binder.setMessageSourceDelegate(() -> new GenericMessage<>("foo".getBytes()));
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicReference<Object> payload = new AtomicReference<>();
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<String>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(String.class);
    assertThat(payload.get()).isEqualTo("foo");
    // test the cache for coverage
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<String>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(String.class);
    assertThat(payload.get()).isEqualTo("foo");
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Example 14 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class SourceBindingWithGlobalPropertiesOnlyTest method testGlobalPropertiesSet.

@Test
public void testGlobalPropertiesSet() {
    BindingProperties bindingProperties = bindingServiceProperties.getBindingProperties(Source.OUTPUT);
    Assertions.assertThat(bindingProperties.getContentType()).isEqualTo("application/json");
    Assertions.assertThat(bindingProperties.getProducer()).isNotNull();
    Assertions.assertThat(bindingProperties.getProducer().getPartitionKeyExpression().getExpressionString()).isEqualTo("key");
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 15 with BindingProperties

use of org.springframework.cloud.stream.config.BindingProperties in project spring-cloud-stream by spring-cloud.

the class MessageConverterConfigurerTests method testConfigureOutputChannelCannotConvert.

@Test
@Ignore
public void testConfigureOutputChannelCannotConvert() {
    BindingServiceProperties props = new BindingServiceProperties();
    BindingProperties bindingProps = new BindingProperties();
    bindingProps.setContentType("foo/bar");
    props.setBindings(Collections.singletonMap("foo", bindingProps));
    MessageConverter converter = new AbstractMessageConverter(new MimeType("foo", "bar")) {

        @Override
        protected boolean supports(Class<?> clazz) {
            return true;
        }

        @Override
        protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
            return null;
        }
    };
    CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(Collections.<MessageConverter>singletonList(converter), null);
    MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory);
    QueueChannel out = new QueueChannel();
    configurer.configureOutputChannel(out, "foo");
    try {
        out.send(new GenericMessage<Foo>(new Foo(), Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE, "bad/ct")));
        fail("Expected MessageConversionException: " + out.receive(0));
    } catch (MessageConversionException e) {
        assertThat(e.getMessage()).endsWith("to the configured output type: 'foo/bar'");
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MimeType(org.springframework.util.MimeType) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) MessageHeaders(org.springframework.messaging.MessageHeaders) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

BindingProperties (org.springframework.cloud.stream.config.BindingProperties)34 Test (org.junit.Test)26 DirectChannel (org.springframework.integration.channel.DirectChannel)21 MessageChannel (org.springframework.messaging.MessageChannel)21 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)12 QueueChannel (org.springframework.integration.channel.QueueChannel)10 Message (org.springframework.messaging.Message)9 HashMap (java.util.HashMap)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)8 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)6 Binder (org.springframework.cloud.stream.binder.Binder)5 Binding (org.springframework.cloud.stream.binder.Binding)5 StreamListenerMessageHandler (org.springframework.cloud.stream.binding.StreamListenerMessageHandler)5 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)4 CompositeMessageConverterFactory (org.springframework.cloud.stream.converter.CompositeMessageConverterFactory)4 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)3