private MethodInvokingSplitter getSplitter(String methodName) throws Exception { Class<?> paramType = methodName.startsWith("message") ? Message.class : String.class; Method splittingMethod = this.testBean.getClass().getMethod(methodName, paramType); MethodInvokingSplitter methodInvokingSplitter = new MethodInvokingSplitter(this.testBean, splittingMethod); methodInvokingSplitter.setBeanFactory(mock(BeanFactory.class)); methodInvokingSplitter.afterPropertiesSet(); return methodInvokingSplitter; }
@Test public void splitWithMassiveReplyMessages_allMessagesSent() { final int messageQuantity = 100000; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); DirectChannel replyChannel = new DirectChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); final AtomicInteger receivedMessageCounter = new AtomicInteger(0); new EventDrivenConsumer(replyChannel, message -> { assertThat("Failure with msg: " + message, message.getPayload(), is(notNullValue())); receivedMessageCounter.incrementAndGet(); }).start(); splitter.handleMessage(this.message); assertThat(receivedMessageCounter.get(), is(messageQuantity)); }
@Test public void splitToIterator_sequenceSizeInLastMessageHeader() { int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); splitter.handleMessage(this.message); List<Message<?>> receivedMessages = replyChannel.clear(); receivedMessages.sort(Comparator.comparing(o -> o.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Integer.class))); assertThat(receivedMessages.get(4) .getHeaders() .get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Integer.class), is(messageQuantity)); }
@Test public void splitToIterator_allMessagesContainSequenceNumber() { final int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); DirectChannel replyChannel = new DirectChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); new EventDrivenConsumer(replyChannel, message -> assertThat("Failure with msg: " + message, message.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Integer.class), is(Integer.valueOf((String) message.getPayload())))).start(); splitter.handleMessage(this.message); }
splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear();
@Test public void splitMessageToStringArrayConfiguredByMethodName() { GenericMessage<String> message = new GenericMessage<>("foo.bar"); MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "messageToStringArray"); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("foo", reply1.getPayload()); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("bar", reply2.getPayload()); }
@Test public void splitStringToMessageListConfiguredByMethodName() { GenericMessage<String> message = new GenericMessage<>("foo.bar"); MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "stringToMessageList"); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("foo", reply1.getPayload()); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("bar", reply2.getPayload()); }
@Test public void splitListPayload() { class ListSplitter { @SuppressWarnings("unused") public List<String> split(List<String> list) { return list; } } GenericMessage<List<?>> message = new GenericMessage<>(Arrays.asList("foo", "bar")); MethodInvokingSplitter splitter = new MethodInvokingSplitter(new ListSplitter(), "split"); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("foo", reply1.getPayload()); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("bar", reply2.getPayload()); }
@Test public void splitToIterator_allMessagesSent() { int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); splitter.handleMessage(this.message); assertThat(replyChannel.getQueueSize(), is(messageQuantity)); }
@Test public void splitPayloadAndHeader() throws Exception { Message<String> message = MessageBuilder.withPayload("a.b").setHeader("testHeader", "c.d").build(); Method splittingMethod = this.testBean.getClass() .getMethod("splitPayloadAndHeader", String.class, String.class); MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, splittingMethod); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("a", reply1.getPayload()); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("b", reply2.getPayload()); Message<?> reply3 = replies.get(2); assertNotNull(reply3); assertEquals("c", reply3.getPayload()); Message<?> reply4 = replies.get(3); assertNotNull(reply4); assertEquals("d", reply4.getPayload()); }
@Test public void singlePublicMethod() { GenericMessage<String> message = new GenericMessage<>("foo.bar"); SinglePublicMethodTestBean testBean = new SinglePublicMethodTestBean(); MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("foo", reply1.getPayload()); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("bar", reply2.getPayload()); }
@Test public void splitToIterable_allMessagesSent() { int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IterableTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); splitter.handleMessage(this.message); assertThat(replyChannel.getQueueSize(), is(messageQuantity)); }
@Test public void singleAnnotation() { GenericMessage<String> message = new GenericMessage<>("foo.bar"); SingleAnnotationTestBean annotatedBean = new SingleAnnotationTestBean(); MethodInvokingSplitter splitter = new MethodInvokingSplitter(annotatedBean); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("foo", reply1.getPayload()); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("bar", reply2.getPayload()); }
@Test public void splitMessageToMessageBuilderList() { Message<String> message = MessageBuilder.withPayload("foo.bar") .setHeader("myHeader", "myValue") .build(); MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "messageToMessageBuilderList"); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); List<Message<?>> replies = replyChannel.clear(); Message<?> reply1 = replies.get(0); assertNotNull(reply1); assertEquals("foo", reply1.getPayload()); assertEquals("myValue", reply1.getHeaders().get("myHeader")); assertEquals("bar", reply1.getHeaders().get("foo")); Message<?> reply2 = replies.get(1); assertNotNull(reply2); assertEquals("bar", reply2.getPayload()); assertEquals("myValue", reply2.getHeaders().get("myHeader")); assertEquals("bar", reply2.getHeaders().get("foo")); }
@Test public void splitToIterator_sourceMessageHeadersIncluded() { String anyHeaderKey = "anyProperty1"; String anyHeaderValue = "anyValue1"; this.message = MessageBuilder.fromMessage(this.message) .setHeader(anyHeaderKey, anyHeaderValue) .build(); int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); QueueChannel replyChannel = new QueueChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); splitter.handleMessage(this.message); List<Message<?>> receivedMessages = replyChannel.clear(); assertThat(receivedMessages.size(), is(messageQuantity)); for (Message<?> receivedMessage : receivedMessages) { MessageHeaders headers = receivedMessage.getHeaders(); assertTrue("Unexpected result with: " + headers, headers.containsKey(anyHeaderKey)); assertThat("Unexpected result with: " + headers, headers.get(anyHeaderKey, String.class), is(anyHeaderValue)); assertThat("Unexpected result with: " + headers, headers.get(IntegrationMessageHeaderAccessor.CORRELATION_ID, UUID.class), is(message.getHeaders().getId())); } }
@Test public void testCorrelationIdWithSplitterWhenNotValueSetOnIncomingMessage() throws Exception { Message<?> message = new GenericMessage<String>("test1,test2"); QueueChannel testChannel = new QueueChannel(); MethodInvokingSplitter splitter = new MethodInvokingSplitter( new TestBean(), TestBean.class.getMethod("split", String.class)); splitter.setOutputChannel(testChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); Message<?> reply1 = testChannel.receive(100); Message<?> reply2 = testChannel.receive(100); assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply1).getCorrelationId()); assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply2).getCorrelationId()); }
@Test public void testCorrelationIdWithSplitterWhenValueSetOnIncomingMessage() throws Exception { final String correlationIdForTest = "#FOR_TEST#"; Message<?> message = MessageBuilder.withPayload("test1,test2").setCorrelationId(correlationIdForTest).build(); QueueChannel testChannel = new QueueChannel(); MethodInvokingSplitter splitter = new MethodInvokingSplitter( new TestBean(), TestBean.class.getMethod("split", String.class)); splitter.setOutputChannel(testChannel); splitter.setBeanFactory(mock(BeanFactory.class)); splitter.afterPropertiesSet(); splitter.handleMessage(message); Message<?> reply1 = testChannel.receive(100); Message<?> reply2 = testChannel.receive(100); assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply1).getCorrelationId()); assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply2).getCorrelationId()); assertTrue("Sequence details missing", reply1.getHeaders().containsKey(IntegrationMessageHeaderAccessor.SEQUENCE_DETAILS)); assertTrue("Sequence details missing", reply2.getHeaders().containsKey(IntegrationMessageHeaderAccessor.SEQUENCE_DETAILS)); }