private ServiceActivatingHandler getHandler(String methodName, Class<?>... types) throws Exception { ServiceActivatingHandler serviceActivatingHandler = new ServiceActivatingHandler(bean, TestBean.class.getMethod(methodName, types)); serviceActivatingHandler.setBeanFactory(applicationContext); serviceActivatingHandler.afterPropertiesSet(); return serviceActivatingHandler; }
private static ServiceActivatingHandler createConsumer(Object object) { ServiceActivatingHandler handler = new ServiceActivatingHandler(object); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); return handler; }
private ServiceActivatingHandler createEndpoint() { ServiceActivatingHandler handler = new ServiceActivatingHandler(new TestBean(), "handle"); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); return handler; }
@Test public void testRequestReplyExchanger() { RequestReplyExchanger testBean = request -> request; final Message<?> test = new GenericMessage<Object>("foo"); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(testBean) { @Override protected Object handleRequestMessage(Message<?> message) { Object o = super.handleRequestMessage(message); assertSame(test, o); return null; } }; serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); serviceActivator.handleMessage(test); }
@Test public void testBeanFactoryPopulation() { ServiceActivatingHandler endpoint = this.createEndpoint(); BeanFactory mock = mock(BeanFactory.class); endpoint.setBeanFactory(mock); endpoint.afterPropertiesSet(); Object beanFactory = TestUtils.getPropertyValue(endpoint, "processor.beanFactory"); assertNotNull(beanFactory); assertSame(mock, beanFactory); }
@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 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 public void correlationIdNotSetIfMessageIsReturnedUnaltered() { QueueChannel replyChannel = new QueueChannel(1); ServiceActivatingHandler endpoint = new ServiceActivatingHandler(new Object() { @SuppressWarnings("unused") public Message<?> handle(Message<?> message) { return message; } }, "handle"); endpoint.setBeanFactory(mock(BeanFactory.class)); endpoint.afterPropertiesSet(); Message<String> message = MessageBuilder.withPayload("test") .setReplyChannel(replyChannel).build(); endpoint.handleMessage(message); Message<?> reply = replyChannel.receive(500); assertNull(new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@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 public void correlationIdSetByHandlerTakesPrecedence() { QueueChannel replyChannel = new QueueChannel(1); ServiceActivatingHandler endpoint = new ServiceActivatingHandler(new Object() { @SuppressWarnings("unused") public Message<?> handle(Message<?> message) { return MessageBuilder.fromMessage(message) .setCorrelationId("ABC-123").build(); } }, "handle"); endpoint.setBeanFactory(mock(BeanFactory.class)); endpoint.afterPropertiesSet(); Message<String> message = MessageBuilder.withPayload("test") .setReplyChannel(replyChannel).build(); endpoint.handleMessage(message); Message<?> reply = replyChannel.receive(500); Object correlationId = new IntegrationMessageHeaderAccessor(reply).getCorrelationId(); assertNotEquals(message.getHeaders().getId(), correlationId); assertEquals("ABC-123", correlationId); }
@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); }
@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 validateErrorChannelWithSuccessfulReply() throws InterruptedException { DirectChannel reqChannel = new DirectChannel(); reqChannel.subscribe(message -> { throw new RuntimeException("ooops"); }); PublishSubscribeChannel errorChannel = new PublishSubscribeChannel(); MyOneWayErrorService myOneWayErrorService = new MyOneWayErrorService(); ServiceActivatingHandler handler = new ServiceActivatingHandler(myOneWayErrorService); handler.setBeanFactory(this.applicationContext); handler.afterPropertiesSet(); errorChannel.subscribe(handler); this.messagingGateway = new MessagingGatewaySupport() { }; this.messagingGateway.setRequestChannel(reqChannel); this.messagingGateway.setErrorChannel(errorChannel); this.messagingGateway.setReplyChannel(null); this.messagingGateway.setBeanFactory(mock(BeanFactory.class)); this.messagingGateway.afterPropertiesSet(); this.messagingGateway.start(); this.messagingGateway.send("hello"); assertTrue(myOneWayErrorService.errorReceived.await(10, TimeUnit.SECONDS)); }
@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(expected = MessagingException.class) public void validateErrorMessageCanNotBeReplyMessage() { DirectChannel reqChannel = new DirectChannel(); reqChannel.subscribe(message -> { throw new RuntimeException("ooops"); }); PublishSubscribeChannel errorChannel = new PublishSubscribeChannel(); ServiceActivatingHandler handler = new ServiceActivatingHandler(new MyErrorService()); handler.setBeanFactory(this.applicationContext); handler.afterPropertiesSet(); errorChannel.subscribe(handler); this.messagingGateway = new MessagingGatewaySupport() { }; this.messagingGateway.setRequestChannel(reqChannel); this.messagingGateway.setErrorChannel(errorChannel); this.messagingGateway.setReplyChannel(null); this.messagingGateway.setBeanFactory(this.applicationContext); this.messagingGateway.afterPropertiesSet(); this.messagingGateway.start(); this.messagingGateway.sendAndReceiveMessage("hello"); }
@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 returnAddressHeaderWithChannelName() { TestUtils.TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext(); testApplicationContext.refresh(); QueueChannel channel = new QueueChannel(1); channel.setBeanName("testChannel"); TestChannelResolver channelResolver = new TestChannelResolver(); channelResolver.addChannel("testChannel", channel); ServiceActivatingHandler endpoint = this.createEndpoint(); endpoint.setChannelResolver(channelResolver); endpoint.setBeanFactory(testApplicationContext); endpoint.afterPropertiesSet(); Message<?> message = MessageBuilder.withPayload("foo") .setReplyChannelName("testChannel").build(); endpoint.handleMessage(message); Message<?> reply = channel.receive(0); assertNotNull(reply); assertEquals("FOO", reply.getPayload()); testApplicationContext.close(); }
@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()); }