@Test public void singleAnnotationMatches() { SingleAnnotationTestBean testBean = new SingleAnnotationTestBean(); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); QueueChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); serviceActivator.handleMessage(new GenericMessage<>("foo")); Message<?> result = outputChannel.receive(0); assertEquals("FOO", result.getPayload()); }
@Test public void singlePublicMethodMatches() { SinglePublicMethodTestBean testBean = new SinglePublicMethodTestBean(); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); QueueChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); serviceActivator.handleMessage(new GenericMessage<>("foo")); Message<?> result = outputChannel.receive(0); assertEquals("FOO", result.getPayload()); }
@Test public void outputChannel() { QueueChannel channel = new QueueChannel(1); ServiceActivatingHandler endpoint = createEndpoint(); endpoint.setOutputChannel(channel); Message<?> message = MessageBuilder.withPayload("foo").build(); endpoint.handleMessage(message); Message<?> reply = channel.receive(0); assertNotNull(reply); assertEquals("FOO", reply.getPayload()); }
@Test public void nullOk() { NullOkTestBean testBean = new NullOkTestBean(); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); QueueChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); serviceActivator.handleMessage(new GenericMessage<>(new KafkaNull())); Message<?> result = outputChannel.receive(0); assertEquals("gotNull", result.getPayload()); }
@Test /* * A handler and message handler fallback (RRE); don't force RRE */ public void testRequestReplyExchangerSeveralMethods() { RequestReplyExchanger testBean = new RequestReplyExchanger() { @Override public Message<?> exchange(Message<?> request) { return request; } @SuppressWarnings("unused") public String foo(String request) { return request.toUpperCase(); } }; ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); PollableChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); Message<?> test = new GenericMessage<Object>(new Date()); serviceActivator.handleMessage(test); assertEquals(test, outputChannel.receive(10)); test = new GenericMessage<Object>("foo"); serviceActivator.handleMessage(test); assertEquals("FOO", outputChannel.receive(10).getPayload()); }
@Test /* * No handler fallback methods; don't force RRE */ public void testRequestReplyExchangerWithGenericMessageMethod() { RequestReplyExchanger testBean = new RequestReplyExchanger() { @Override public Message<?> exchange(Message<?> request) { return request; } @SuppressWarnings("unused") public String foo(Message<String> request) { return request.getPayload().toUpperCase(); } }; ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); PollableChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); Message<?> test = new GenericMessage<Object>(new Date()); serviceActivator.handleMessage(test); assertEquals(test, outputChannel.receive(10)); test = new GenericMessage<Object>("foo"); serviceActivator.handleMessage(test); assertEquals("FOO", outputChannel.receive(10).getPayload()); }
@Test(expected = ReplyRequiredException.class) public void noReplyMessageWithRequiresReply() { QueueChannel channel = new QueueChannel(1); ServiceActivatingHandler endpoint = new ServiceActivatingHandler(new TestNullReplyBean(), "handle"); endpoint.setRequiresReply(true); endpoint.setOutputChannel(channel); endpoint.setBeanFactory(mock(BeanFactory.class)); endpoint.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("foo").build(); endpoint.handleMessage(message); }
ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); PollableChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet();
ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean); PollableChannel outputChannel = new QueueChannel(); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet();
ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); QueueChannel replyChannel = new QueueChannel(); handler.setOutputChannel(replyChannel); handler.handleMessage(new GenericMessage<>(foos)); Message<?> message = replyChannel.receive(0);
@Test public void sendAndReceiveForRegisteredEndpoint() { ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "handle"); serviceActivator.setOutputChannel(this.targetChannel); context.registerBean("testServiceActivator", serviceActivator); EventDrivenConsumer endpoint = new EventDrivenConsumer(this.sourceChannel, serviceActivator); context.registerEndpoint("testEndpoint", endpoint); context.refresh(); this.sourceChannel.send(new GenericMessage<>("foo")); Message<?> response = this.targetChannel.receive(); assertEquals("foo!", response.getPayload()); }
@Test public void noReplyMessage() { QueueChannel channel = new QueueChannel(1); ServiceActivatingHandler endpoint = new ServiceActivatingHandler(new TestNullReplyBean(), "handle"); endpoint.setOutputChannel(channel); endpoint.setBeanFactory(mock(BeanFactory.class)); endpoint.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("foo").build(); endpoint.handleMessage(message); assertNull(channel.receive(0)); }
@Test public void outputChannelTakesPrecedence() { QueueChannel channel1 = new QueueChannel(1); QueueChannel channel2 = new QueueChannel(1); ServiceActivatingHandler endpoint = createEndpoint(); endpoint.setOutputChannel(channel1); Message<?> message = MessageBuilder.withPayload("foo").setReplyChannel(channel2).build(); endpoint.handleMessage(message); Message<?> reply1 = channel1.receive(0); assertNotNull(reply1); assertEquals("FOO", reply1.getPayload()); Message<?> reply2 = channel2.receive(0); assertNull(reply2); }
@Test public void testCorrelationNotCopiedFromRequestMessgeIdIfAlreadySetByHandler() throws Exception { Message<?> message = new GenericMessage<String>("test"); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "createMessage"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals("456-XYZ", new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationIdPassedIfAvailable() { Object correlationId = "123-ABC"; Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId(correlationId).build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "upperCase"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(correlationId, new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationNotPassedFromRequestHeaderIfAlreadySetByHandler() { Object correlationId = "123-ABC"; Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId(correlationId).build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "createMessage"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals("456-XYZ", new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCollectionIsConverted() { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new Converter<Foo, Bar>() { // Must be explicit type with generics @Override public Bar convert(Foo source) { return new Bar(); } }); BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter(conversionService); beanFactory.setConversionService(conversionService); typeConverter.setBeanFactory(beanFactory); Service service = new Service(); MethodInvokingMessageProcessor<Service> processor = new MethodInvokingMessageProcessor<>(service, "handle"); processor.setConversionService(conversionService); processor.setUseSpelInvoker(true); processor.setBeanFactory(beanFactory); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); QueueChannel replyChannel = new QueueChannel(); handler.setOutputChannel(replyChannel); handler.handleMessage(new GenericMessage<Collection<Foo>>(Collections.singletonList(new Foo()))); Message<?> message = replyChannel.receive(10000); assertNotNull(message); assertEquals("baz", message.getPayload()); }
@Test public void testCorrelationIdCopiedFromMessageCorrelationIdIfAvailable() { Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId("correlationId").build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "upperCase"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(new IntegrationMessageHeaderAccessor(message).getCorrelationId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Override protected MessageHandler createHandler(Object bean, Method method, ServiceActivator annotation) { ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(bean, method); String outputChannelName = annotation.outputChannel(); if (StringUtils.hasText(outputChannelName)) { serviceActivator.setOutputChannel(this.channelResolver.resolveChannelName(outputChannelName)); } return serviceActivator; }