Java Code Examples for org.springframework.cloud.stream.config.BindingServiceProperties#setBindings()

The following examples show how to use org.springframework.cloud.stream.config.BindingServiceProperties#setBindings() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageConverterConfigurerTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
public void testConfigureOutputChannelWithBadContentType() {
	BindingServiceProperties props = new BindingServiceProperties();
	BindingProperties bindingProps = new BindingProperties();
	bindingProps.setContentType("application/json");
	props.setBindings(Collections.singletonMap("foo", bindingProps));
	CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(
			Collections.<MessageConverter>emptyList(), null);
	MessageConverterConfigurer configurer = new MessageConverterConfigurer(props,
			converterFactory.getMessageConverterForAllRegistered());
	QueueChannel out = new QueueChannel();
	configurer.configureOutputChannel(out, "foo");
	out.send(new GenericMessage<Foo>(new Foo(), Collections
			.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE, "bad/ct")));
	Message<?> received = out.receive(0);
	assertThat(received).isNotNull();
	assertThat(received.getPayload()).isInstanceOf(Foo.class);
}
 
Example 2
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@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.");
	}
}
 
Example 3
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumerPropertiesValidation() {
	BindingServiceProperties serviceProperties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	ConsumerProperties consumerProperties = new ConsumerProperties();
	consumerProperties.setConcurrency(0);
	props.setDestination("foo");
	props.setConsumer(consumerProperties);
	final String inputChannelName = "input";
	bindingProperties.put(inputChannelName, props);
	serviceProperties.setBindings(bindingProperties);
	DefaultBinderFactory binderFactory = createMockBinderFactory();
	BindingService service = new BindingService(serviceProperties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	try {
		service.bindConsumer(inputChannel, inputChannelName);
		fail("Consumer properties should be validated.");
	}
	catch (IllegalStateException e) {
		assertThat(e)
				.hasMessageContaining("Concurrency should be greater than zero.");
	}
}
 
Example 4
Source File: MessageConverterConfigurerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@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.getMessageConverterForAllRegistered());
	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'");
	}
}
 
Example 5
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDefaultGroup() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	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);
	BindingService service = new BindingService(properties, binderFactory);
	MessageChannel inputChannel = new DirectChannel();
	Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
	when(binder.bindConsumer(eq("foo"), isNull(), 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"), isNull(), same(inputChannel),
			any(ConsumerProperties.class));
	verify(binding).unbind();
	binderFactory.destroy();
}
 
Example 6
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 7
Source File: PollableConsumerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertSimpler() {
	TestChannelBinder binder = createBinder();
	MessageConverterConfigurer configurer = this.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");
}
 
Example 8
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 9
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testMultipleConsumerBindingsFromIndexList() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo");

	ConsumerProperties consumer = properties.getConsumerProperties("input");
	consumer.setInstanceIndexList(Arrays.asList(0, 1));
	consumer.setInstanceCount(2);
	consumer.setPartitioned(true);
	props.setConsumer(consumer);

	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, "FirstBinding");
	Binding<MessageChannel> mockBinding2 = Mockito.mock(Binding.class, "SecondBinding");

	ArgumentCaptor<ConsumerProperties> captor = ArgumentCaptor.forClass(ConsumerProperties.class);

	when(binder.bindConsumer(eq("foo"), isNull(), same(inputChannel),
		any(ConsumerProperties.class))).thenReturn(mockBinding1).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, times(2)).bindConsumer(eq("foo"), isNull(), same(inputChannel),
		captor.capture());
	verify(binding1).unbind();
	verify(binding2).unbind();

	List<ConsumerProperties> allValues = captor.getAllValues();

	assertThat(allValues.size()).isEqualTo(2);

	assertThat(allValues.get(0).getInstanceIndex()).isEqualTo(0);
	assertThat(allValues.get(1).getInstanceIndex()).isEqualTo(1);

	binderFactory.destroy();
}
 
Example 10
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testConsumerBindingWhenMultiplexingIsEnabled() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo,bar");

	ConsumerProperties consumer = properties.getConsumerProperties("input");
	consumer.setMultiplex(true);
	props.setConsumer(consumer);

	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);

	when(binder.bindConsumer(eq("foo,bar"), isNull(), same(inputChannel),
			any(ConsumerProperties.class))).thenReturn(mockBinding1);

	Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel,
			"input");
	assertThat(bindings).hasSize(1);

	Iterator<Binding<MessageChannel>> iterator = bindings.iterator();
	Binding<MessageChannel> binding1 = iterator.next();

	assertThat(binding1).isSameAs(mockBinding1);

	service.unbindConsumers("input");

	verify(binder).bindConsumer(eq("foo,bar"), isNull(), same(inputChannel),
			any(ConsumerProperties.class));
	verify(binding1).unbind();

	binderFactory.destroy();
}
 
Example 11
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
@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(400);
	}
	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();
}
 
Example 12
Source File: BindingServiceTests.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testLateBindingProducer() throws Exception {
	BindingServiceProperties properties = new BindingServiceProperties();
	properties.setBindingRetryInterval(1);
	Map<String, BindingProperties> bindingProperties = new HashMap<>();
	BindingProperties props = new BindingProperties();
	props.setDestination("foo");
	final String outputChannelName = "output";
	bindingProperties.put(outputChannelName, 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 outputChannel = 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).bindProducer(eq("foo"), same(outputChannel),
			any(ProducerProperties.class));
	Binding<MessageChannel> binding = service.bindProducer(outputChannel,
			outputChannelName);
	assertThat(fail.await(10, TimeUnit.SECONDS)).isTrue();
	assertThat(binding).isNotNull();
	Binding delegate = TestUtils.getPropertyValue(binding, "delegate", Binding.class);
	int n = 0;
	while (n++ < 300 && delegate == null) {
		Thread.sleep(100);
		delegate = TestUtils.getPropertyValue(binding, "delegate", Binding.class);
	}
	assertThat(delegate).isSameAs(mockBinding);
	service.unbindProducers(outputChannelName);
	verify(binder, times(2)).bindProducer(eq("foo"), same(outputChannel),
			any(ProducerProperties.class));
	verify(delegate).unbind();
	binderFactory.destroy();
	scheduler.destroy();
}