Search in sources :

Example 6 with Binder

use of org.springframework.cloud.stream.binder.Binder 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 7 with Binder

use of org.springframework.cloud.stream.binder.Binder 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 8 with Binder

use of org.springframework.cloud.stream.binder.Binder in project spring-cloud-stream by spring-cloud.

the class PartitionedConsumerTest method testBindingPartitionedConsumer.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testBindingPartitionedConsumer() {
    Binder binder = this.binderFactory.getBinder(null, MessageChannel.class);
    ArgumentCaptor<ConsumerProperties> argumentCaptor = ArgumentCaptor.forClass(ConsumerProperties.class);
    verify(binder).bindConsumer(eq("partIn"), isNull(), eq(this.testSink.input()), argumentCaptor.capture());
    Assert.assertThat(argumentCaptor.getValue().getInstanceIndex(), equalTo(0));
    Assert.assertThat(argumentCaptor.getValue().getInstanceCount(), equalTo(2));
    verifyNoMoreInteractions(binder);
}
Also used : Binder(org.springframework.cloud.stream.binder.Binder) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Binder (org.springframework.cloud.stream.binder.Binder)8 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)7 Test (org.junit.Test)6 Binding (org.springframework.cloud.stream.binder.Binding)6 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)6 HashMap (java.util.HashMap)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)5 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)5 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 MessageChannel (org.springframework.messaging.MessageChannel)5 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)2 PollableConsumerBinder (org.springframework.cloud.stream.binder.PollableConsumerBinder)2 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)2 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 DataBinder (org.springframework.validation.DataBinder)2 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1