Test changes for extended default properties
Polishing
This commit is contained in:
@@ -32,7 +32,7 @@ import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.integration.support.utils.IntegrationUtils;
|
||||
|
||||
/**
|
||||
* Vase implementation of {@link ExtendedBindingProperties}
|
||||
* Base implementation of {@link ExtendedBindingProperties}
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.binder;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.stream.utils.FooConsumerProperties;
|
||||
import org.springframework.cloud.stream.utils.FooProducerProperties;
|
||||
import org.springframework.cloud.stream.utils.MockExtendedBinderConfiguration;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
public class ExtendedPropertiesDefaultTests {
|
||||
|
||||
@Test
|
||||
public void testExtendedDefaultProducerProperties() {
|
||||
DefaultBinderFactory binderFactory = createMockExtendedBinderFactory();
|
||||
Binder<MessageChannel, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
|
||||
FooProducerProperties fooProducerProperties =
|
||||
(FooProducerProperties) ((ExtendedPropertiesBinder<?, ?, ?>) binder)
|
||||
.getExtendedProducerProperties("output");
|
||||
// Expectations are set in the mock configuration for the binder factory
|
||||
assertThat(fooProducerProperties.getExtendedProperty()).isEqualTo("someFancyExtension");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedDefaultConsumerProperties() {
|
||||
DefaultBinderFactory binderFactory = createMockExtendedBinderFactory();
|
||||
Binder<MessageChannel, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
|
||||
FooConsumerProperties fooConsumerProperties =
|
||||
(FooConsumerProperties)((ExtendedPropertiesBinder<?,?,?>)binder)
|
||||
.getExtendedConsumerProperties("input");
|
||||
// Expectations are set in the mock configuration for the binder factory
|
||||
assertThat(fooConsumerProperties.getExtendedProperty()).isEqualTo("someFancyExtension");
|
||||
}
|
||||
|
||||
private DefaultBinderFactory createMockExtendedBinderFactory() {
|
||||
BinderTypeRegistry binderTypeRegistry = createMockExtendedBinderTypeRegistry();
|
||||
return new DefaultBinderFactory(
|
||||
Collections.singletonMap("mock", new BinderConfiguration("mock", new HashMap<>(), true, true)),
|
||||
binderTypeRegistry);
|
||||
}
|
||||
|
||||
private DefaultBinderTypeRegistry createMockExtendedBinderTypeRegistry() {
|
||||
return new DefaultBinderTypeRegistry(Collections.singletonMap("mock",
|
||||
new BinderType("mock", new Class[] { MockExtendedBinderConfiguration.class })));
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
@@ -59,15 +58,9 @@ import org.springframework.cloud.stream.config.BindingServiceConfiguration;
|
||||
import org.springframework.cloud.stream.config.BindingServiceProperties;
|
||||
import org.springframework.cloud.stream.converter.CompositeMessageConverterFactory;
|
||||
import org.springframework.cloud.stream.reflection.GenericsUtils;
|
||||
import org.springframework.cloud.stream.utils.FooExtendedConsumerProperties;
|
||||
import org.springframework.cloud.stream.utils.FooExtendedProducerProperties;
|
||||
import org.springframework.cloud.stream.utils.MockBinderConfiguration;
|
||||
import org.springframework.cloud.stream.utils.MockExtendedBinderConfiguration;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
@@ -365,72 +358,6 @@ public class BindingServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testExtendedDefaultProducerProperties() {
|
||||
BindingServiceProperties serviceProperties = new BindingServiceProperties();
|
||||
Map<String, BindingProperties> bindingProperties = new HashMap<>();
|
||||
BindingProperties props = new BindingProperties();
|
||||
ProducerProperties producerProperties = new ProducerProperties();
|
||||
props.setDestination("dest");
|
||||
props.setProducer(producerProperties);
|
||||
final String outputChannelName = "output";
|
||||
bindingProperties.put(outputChannelName, props);
|
||||
serviceProperties.setBindings(bindingProperties);
|
||||
|
||||
DefaultBinderFactory binderFactory = createMockExtendedBinderFactory();
|
||||
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
Map<String, Object> propertiesToAdd = new HashMap<>();
|
||||
propertiesToAdd.put("spring.cloud.stream.foo.default.producer.extendedProperty", "someFancyExtension");
|
||||
environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd));
|
||||
|
||||
BindingService service = new BindingService(serviceProperties, binderFactory, null);
|
||||
MessageChannel outputChannel = new DirectChannel();
|
||||
|
||||
Binder<MessageChannel, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
|
||||
FooExtendedProducerProperties fooExtendedProducerProperties =
|
||||
(FooExtendedProducerProperties)((ExtendedPropertiesBinder<?,?,?>)binder).getExtendedProducerProperties("output");
|
||||
assertThat(fooExtendedProducerProperties.getExtendedProperty()).isNull();
|
||||
|
||||
service.bindProducer(outputChannel, outputChannelName);
|
||||
|
||||
assertThat(fooExtendedProducerProperties.getExtendedProperty()).isEqualTo("someFancyExtension");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testExtendedDefaultConsumerProperties() {
|
||||
BindingServiceProperties serviceProperties = new BindingServiceProperties();
|
||||
Map<String, BindingProperties> bindingProperties = new HashMap<>();
|
||||
BindingProperties props = new BindingProperties();
|
||||
ConsumerProperties consumerProperties = new ConsumerProperties();
|
||||
props.setDestination("dest");
|
||||
props.setConsumer(consumerProperties);
|
||||
final String inputChannelName = "input";
|
||||
bindingProperties.put(inputChannelName, props);
|
||||
serviceProperties.setBindings(bindingProperties);
|
||||
|
||||
DefaultBinderFactory binderFactory = createMockExtendedBinderFactory();
|
||||
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
Map<String, Object> propertiesToAdd = new HashMap<>();
|
||||
propertiesToAdd.put("spring.cloud.stream.foo.default.consumer.extendedProperty", "someFancyExtension");
|
||||
environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd));
|
||||
|
||||
BindingService service = new BindingService(serviceProperties, binderFactory, null);
|
||||
MessageChannel inputChannel = new DirectChannel();
|
||||
|
||||
Binder<MessageChannel, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
|
||||
FooExtendedConsumerProperties fooExtendedConsumerProperties =
|
||||
(FooExtendedConsumerProperties)((ExtendedPropertiesBinder<?,?,?>)binder).getExtendedConsumerProperties("input");
|
||||
assertThat(fooExtendedConsumerProperties.getExtendedProperty()).isNull();
|
||||
|
||||
service.bindConsumer(inputChannel, inputChannelName);
|
||||
|
||||
assertThat(fooExtendedConsumerProperties.getExtendedProperty()).isEqualTo("someFancyExtension");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPropertyBehavior() {
|
||||
ConfigurableApplicationContext run = SpringApplication.run(DefaultConsumerPropertiesTestSink.class,
|
||||
@@ -689,24 +616,11 @@ public class BindingServiceTests {
|
||||
binderTypeRegistry);
|
||||
}
|
||||
|
||||
private DefaultBinderFactory createMockExtendedBinderFactory() {
|
||||
BinderTypeRegistry binderTypeRegistry = createMockExtendedBinderTypeRegistry();
|
||||
return new DefaultBinderFactory(
|
||||
Collections.singletonMap("mock", new BinderConfiguration("mock", new HashMap<>(), true, true)),
|
||||
binderTypeRegistry);
|
||||
}
|
||||
|
||||
|
||||
private DefaultBinderTypeRegistry createMockBinderTypeRegistry() {
|
||||
return new DefaultBinderTypeRegistry(Collections.singletonMap("mock",
|
||||
new BinderType("mock", new Class[] { MockBinderConfiguration.class })));
|
||||
}
|
||||
|
||||
private DefaultBinderTypeRegistry createMockExtendedBinderTypeRegistry() {
|
||||
return new DefaultBinderTypeRegistry(Collections.singletonMap("mock",
|
||||
new BinderType("mock", new Class[] { MockExtendedBinderConfiguration.class })));
|
||||
}
|
||||
|
||||
private BindingServiceProperties createBindingServiceProperties(HashMap<String, String> properties) {
|
||||
BindingServiceProperties bindingServiceProperties = new BindingServiceProperties();
|
||||
org.springframework.boot.context.properties.bind.Binder propertiesBinder = new org.springframework.boot.context.properties.bind.Binder(new MapConfigurationPropertySource(properties));
|
||||
|
||||
@@ -23,23 +23,23 @@ import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider;
|
||||
*/
|
||||
public class FooBindingProperties implements BinderSpecificPropertiesProvider {
|
||||
|
||||
private FooExtendedProducerProperties producer = new FooExtendedProducerProperties();
|
||||
private FooProducerProperties producer = new FooProducerProperties();
|
||||
|
||||
private FooExtendedConsumerProperties consumer = new FooExtendedConsumerProperties();
|
||||
private FooConsumerProperties consumer = new FooConsumerProperties();
|
||||
|
||||
public FooExtendedProducerProperties getProducer() {
|
||||
public FooProducerProperties getProducer() {
|
||||
return producer;
|
||||
}
|
||||
|
||||
public void setProducer(FooExtendedProducerProperties producer) {
|
||||
public void setProducer(FooProducerProperties producer) {
|
||||
this.producer = producer;
|
||||
}
|
||||
|
||||
public FooExtendedConsumerProperties getConsumer() {
|
||||
public FooConsumerProperties getConsumer() {
|
||||
return consumer;
|
||||
}
|
||||
|
||||
public void setConsumer(FooExtendedConsumerProperties consumer) {
|
||||
public void setConsumer(FooConsumerProperties consumer) {
|
||||
this.consumer = consumer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.stream.utils;
|
||||
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
public class FooExtendedConsumerProperties {
|
||||
public class FooConsumerProperties {
|
||||
|
||||
String extendedProperty;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.stream.utils;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.stream.binder.AbstractExtendedBindingProperties;
|
||||
import org.springframework.cloud.stream.binder.BinderSpecificPropertiesProvider;
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.stream.foo")
|
||||
public class FooExtendedBindingProperties extends AbstractExtendedBindingProperties<FooConsumerProperties, FooProducerProperties, FooBindingProperties> {
|
||||
|
||||
private static final String DEFAULTS_PREFIX = "spring.cloud.stream.foo.default";
|
||||
|
||||
@Override
|
||||
public String getDefaultsPrefix() {
|
||||
return DEFAULTS_PREFIX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends BinderSpecificPropertiesProvider> getExtendedPropertiesEntryClass() {
|
||||
return FooBindingProperties.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.stream.utils;
|
||||
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
public class FooExtendedProducerProperties {
|
||||
public class FooProducerProperties {
|
||||
|
||||
String extendedProperty;
|
||||
|
||||
@@ -16,12 +16,20 @@
|
||||
|
||||
package org.springframework.cloud.stream.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.cloud.stream.binder.Binder;
|
||||
import org.springframework.cloud.stream.binder.ExtendedPropertiesBinder;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -36,14 +44,27 @@ public class MockExtendedBinderConfiguration {
|
||||
public Binder<?, ?, ?> extendedPropertiesBinder() {
|
||||
Binder mock = Mockito.mock(Binder.class, Mockito.withSettings().defaultAnswer(Mockito.RETURNS_MOCKS)
|
||||
.extraInterfaces(ExtendedPropertiesBinder.class));
|
||||
when (((ExtendedPropertiesBinder)mock).getExtendedProducerProperties("output"))
|
||||
.thenReturn(new FooExtendedProducerProperties());
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
Map<String, Object> propertiesToAdd = new HashMap<>();
|
||||
propertiesToAdd.put("spring.cloud.stream.foo.default.consumer.extendedProperty", "someFancyExtension");
|
||||
propertiesToAdd.put("spring.cloud.stream.foo.default.producer.extendedProperty", "someFancyExtension");
|
||||
environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd));
|
||||
|
||||
ConfigurableApplicationContext applicationContext = new GenericApplicationContext();
|
||||
applicationContext.setEnvironment(environment);
|
||||
|
||||
FooExtendedBindingProperties fooExtendedBindingProperties = new FooExtendedBindingProperties();
|
||||
fooExtendedBindingProperties.setApplicationContext(applicationContext);
|
||||
|
||||
final FooConsumerProperties fooConsumerProperties = fooExtendedBindingProperties.getExtendedConsumerProperties("input");
|
||||
final FooProducerProperties fooProducerProperties = fooExtendedBindingProperties.getExtendedProducerProperties("output");
|
||||
|
||||
when (((ExtendedPropertiesBinder)mock).getExtendedConsumerProperties("input"))
|
||||
.thenReturn(new FooExtendedConsumerProperties());
|
||||
when (((ExtendedPropertiesBinder)mock).getDefaultsPrefix())
|
||||
.thenReturn("spring.cloud.stream.foo.default");
|
||||
when (((ExtendedPropertiesBinder)mock).getExtendedPropertiesEntryClass())
|
||||
.thenReturn(FooBindingProperties.class);
|
||||
.thenReturn(fooConsumerProperties);
|
||||
|
||||
when (((ExtendedPropertiesBinder)mock).getExtendedProducerProperties("output"))
|
||||
.thenReturn(fooProducerProperties);
|
||||
|
||||
return mock;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user