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'");
}
}
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();
}
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");
}
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");
}
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'");
}
}
Aggregations