Search in sources :

Example 26 with BindingProperties

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

the class BindingServiceTests method testLateBindingConsumer.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
// Inconsistent, needs fixing
@Ignore
public void testLateBindingConsumer() throws Exception {
    BindingServiceProperties properties = new BindingServiceProperties();
    properties.setBindingRetryInterval(1);
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    props.setDestination("foo");
    final String inputChannelName = "input";
    bindingProperties.put(inputChannelName, props);
    properties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    BindingService service = new BindingService(properties, binderFactory, scheduler);
    MessageChannel inputChannel = new DirectChannel();
    final Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
    final CountDownLatch fail = new CountDownLatch(2);
    doAnswer(i -> {
        fail.countDown();
        if (fail.getCount() == 1) {
            throw new RuntimeException("fail");
        }
        return mockBinding;
    }).when(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
    Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, inputChannelName);
    assertThat(fail.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(bindings).hasSize(1);
    Binding<MessageChannel> delegate = TestUtils.getPropertyValue(bindings.iterator().next(), "delegate", Binding.class);
    int n = 0;
    while (n++ < 300 && delegate == null) {
        Thread.sleep(100);
    }
    assertThat(delegate).isSameAs(mockBinding);
    service.unbindConsumers(inputChannelName);
    verify(binder, times(2)).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
    verify(delegate).unbind();
    binderFactory.destroy();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with BindingProperties

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

the class BindingServiceTests method testExplicitGroup.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testExplicitGroup() throws Exception {
    BindingServiceProperties properties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    props.setDestination("foo");
    props.setGroup("fooGroup");
    final String inputChannelName = "input";
    bindingProperties.put(inputChannelName, props);
    properties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
    BindingService service = new BindingService(properties, binderFactory);
    MessageChannel inputChannel = new DirectChannel();
    Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
    when(binder.bindConsumer(eq("foo"), eq("fooGroup"), same(inputChannel), any(ConsumerProperties.class))).thenReturn(mockBinding);
    Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, inputChannelName);
    assertThat(bindings).hasSize(1);
    Binding<MessageChannel> binding = bindings.iterator().next();
    assertThat(binding).isSameAs(mockBinding);
    service.unbindConsumers(inputChannelName);
    verify(binder).bindConsumer(eq("foo"), eq(props.getGroup()), same(inputChannel), any(ConsumerProperties.class));
    verify(binding).unbind();
    binderFactory.destroy();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Example 28 with BindingProperties

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

the class BindingServiceTests method testMultipleConsumerBindings.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testMultipleConsumerBindings() throws Exception {
    BindingServiceProperties properties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    props.setDestination("foo,bar");
    final String inputChannelName = "input";
    bindingProperties.put(inputChannelName, props);
    properties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
    BindingService service = new BindingService(properties, binderFactory);
    MessageChannel inputChannel = new DirectChannel();
    Binding<MessageChannel> mockBinding1 = Mockito.mock(Binding.class);
    Binding<MessageChannel> mockBinding2 = Mockito.mock(Binding.class);
    when(binder.bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class))).thenReturn(mockBinding1);
    when(binder.bindConsumer(eq("bar"), isNull(), same(inputChannel), any(ConsumerProperties.class))).thenReturn(mockBinding2);
    Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, "input");
    assertThat(bindings).hasSize(2);
    Iterator<Binding<MessageChannel>> iterator = bindings.iterator();
    Binding<MessageChannel> binding1 = iterator.next();
    Binding<MessageChannel> binding2 = iterator.next();
    assertThat(binding1).isSameAs(mockBinding1);
    assertThat(binding2).isSameAs(mockBinding2);
    service.unbindConsumers("input");
    verify(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
    verify(binder).bindConsumer(eq("bar"), isNull(), same(inputChannel), any(ConsumerProperties.class));
    verify(binding1).unbind();
    verify(binding2).unbind();
    binderFactory.destroy();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Example 29 with BindingProperties

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

the class BindingServiceTests method testProducerPropertiesValidation.

@Test
public void testProducerPropertiesValidation() {
    BindingServiceProperties serviceProperties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    ProducerProperties producerProperties = new ProducerProperties();
    producerProperties.setPartitionCount(0);
    props.setDestination("foo");
    props.setProducer(producerProperties);
    final String outputChannelName = "output";
    bindingProperties.put(outputChannelName, props);
    serviceProperties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    BindingService service = new BindingService(serviceProperties, binderFactory);
    MessageChannel outputChannel = new DirectChannel();
    try {
        service.bindProducer(outputChannel, outputChannelName);
        fail("Producer properties should be validated.");
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageContaining("Partition count should be greater than zero.");
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) MessageChannel(org.springframework.messaging.MessageChannel) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) Test(org.junit.Test)

Example 30 with BindingProperties

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

the class MessageConverterConfigurerTests method testConfigureInputChannelWithLegacyContentType.

@Test
public void testConfigureInputChannelWithLegacyContentType() {
    BindingServiceProperties props = new BindingServiceProperties();
    BindingProperties bindingProps = new BindingProperties();
    bindingProps.setContentType("foo/bar");
    props.setBindings(Collections.singletonMap("foo", bindingProps));
    CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(Collections.<MessageConverter>emptyList(), null);
    MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory);
    QueueChannel in = new QueueChannel();
    configurer.configureInputChannel(in, "foo");
    Foo foo = new Foo();
    in.send(MessageBuilder.withPayload(foo).setHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, "application/json").setHeader(BinderHeaders.SCST_VERSION, "1.x").build());
    Message<?> received = in.receive(0);
    assertThat(received).isNotNull();
    assertThat(received.getPayload()).isEqualTo(foo);
    assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).isEqualTo("application/json");
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) 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