Search in sources :

Example 16 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.

the class CustomPartitionedProducerTest method testCustomPartitionedProducerAsSingletons.

@Test
public void testCustomPartitionedProducerAsSingletons() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 17 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.

the class GlobalChannelInterceptorTests method testWildCardPatternMatch.

@Test
public void testWildCardPatternMatch() {
    List<ChannelInterceptor> channelInterceptors = this.inputCChannel.getChannelInterceptors();
    List<String> interceptorNames = new ArrayList<String>();
    for (ChannelInterceptor interceptor : channelInterceptors) {
        interceptorNames.add(interceptor.toString());
    }
    assertTrue(interceptorNames.contains("interceptor-ten"));
    assertTrue(interceptorNames.contains("interceptor-eleven"));
}
Also used : ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 18 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.

the class GlobalChannelInterceptorTests method testDynamicMessageChannelBeanWithAutoGlobalChannelInterceptor.

@Test
public void testDynamicMessageChannelBeanWithAutoGlobalChannelInterceptor() {
    DirectChannel testChannel = new DirectChannel();
    ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory();
    beanFactory.initializeBean(testChannel, "testChannel");
    List<ChannelInterceptor> channelInterceptors = testChannel.getChannelInterceptors();
    assertEquals(2, channelInterceptors.size());
    assertThat(channelInterceptors.get(0), instanceOf(SampleInterceptor.class));
    assertThat(channelInterceptors.get(0), instanceOf(SampleInterceptor.class));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Example 19 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorMatchingPattern.

@Test
public void testProcessorWithInterceptorMatchingPattern() {
    Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
    Map<String, ChannelInterceptorAware> channels = new HashMap<>();
    ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
    GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper = new GlobalChannelInterceptorWrapper(channelInterceptor);
    ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
    globalChannelInterceptorWrapper.setPatterns(new String[] { "Te*" });
    interceptors.put("Test-1", globalChannelInterceptorWrapper);
    channels.put("Test-1", channel);
    when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class)).thenReturn(interceptors);
    when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class)).thenReturn(channels);
    this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
    verify(channel).addInterceptor(channelInterceptor);
}
Also used : HashMap(java.util.HashMap) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 20 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorMatchingNegativePattern.

@Test
public void testProcessorWithInterceptorMatchingNegativePattern() {
    Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
    Map<String, ChannelInterceptorAware> channels = new HashMap<>();
    ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
    GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper = new GlobalChannelInterceptorWrapper(channelInterceptor);
    ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
    globalChannelInterceptorWrapper.setPatterns(new String[] { "!te*", "!Te*" });
    interceptors.put("Test-1", globalChannelInterceptorWrapper);
    channels.put("Test-1", channel);
    when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class)).thenReturn(interceptors);
    when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class)).thenReturn(channels);
    this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
    verify(channel, Mockito.never()).addInterceptor(channelInterceptor);
}
Also used : HashMap(java.util.HashMap) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Aggregations

ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)30 Test (org.junit.Test)18 ChannelInterceptorAware (org.springframework.integration.channel.ChannelInterceptorAware)8 ApplicationContext (org.springframework.context.ApplicationContext)7 ArrayList (java.util.ArrayList)6 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)6 Test (org.junit.jupiter.api.Test)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 GlobalChannelInterceptorWrapper (org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)5 Message (org.springframework.messaging.Message)5 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 PartitionHandler (org.springframework.cloud.stream.binder.PartitionHandler)4 PartitionKeyExtractorStrategy (org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy)4 PartitionSelectorStrategy (org.springframework.cloud.stream.binder.PartitionSelectorStrategy)4 Source (org.springframework.cloud.stream.messaging.Source)4 CustomPartitionKeyExtractorClass (org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass)4 CustomPartitionSelectorClass (org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass)4 PropertySource (org.springframework.context.annotation.PropertySource)4