Search in sources :

Example 1 with IntegrationFlowRegistration

use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.

the class ManualFlowTests method testRoleControl.

@Test
public void testRoleControl() {
    String testRole = "bridge";
    PollableChannel resultChannel = new QueueChannel();
    IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow -> flow.bridge(e -> e.role(testRole)).channel(resultChannel)).register();
    MessagingTemplate messagingTemplate = this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId());
    messagingTemplate.send(new GenericMessage<>("test"));
    Message<?> receive = resultChannel.receive(1000);
    assertNotNull(receive);
    assertEquals("test", receive.getPayload());
    this.roleController.stopLifecyclesInRole(testRole);
    try {
        messagingTemplate.send(new GenericMessage<>("test2"));
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for channel"));
    }
    this.roleController.startLifecyclesInRole(testRole);
    messagingTemplate.send(new GenericMessage<>("test2"));
    receive = resultChannel.receive(1000);
    assertNotNull(receive);
    assertEquals("test2", receive.getPayload());
    flowRegistration.destroy();
    assertTrue(this.roleController.getEndpointsRunningStatus(testRole).isEmpty());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Date(java.util.Date) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowAdapter(org.springframework.integration.dsl.IntegrationFlowAdapter) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) Assert.assertThat(org.junit.Assert.assertThat) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) SpringRunner(org.springframework.test.context.junit4.SpringRunner) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) MessageProducerSpec(org.springframework.integration.dsl.MessageProducerSpec) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Objects(java.util.Objects) Configuration(org.springframework.context.annotation.Configuration) IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) MessageHandler(org.springframework.messaging.MessageHandler) Assert.assertFalse(org.junit.Assert.assertFalse) DisposableBean(org.springframework.beans.factory.DisposableBean) Matchers.containsString(org.hamcrest.Matchers.containsString) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) MessageChannels(org.springframework.integration.dsl.MessageChannels) Scope(org.springframework.context.annotation.Scope) Assert.assertSame(org.junit.Assert.assertSame) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Flux(reactor.core.publisher.Flux) Assert.assertNull(org.junit.Assert.assertNull) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) Matchers.containsString(org.hamcrest.Matchers.containsString) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 2 with IntegrationFlowRegistration

use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.

the class MockMessageSourceTests method testMockMessageSourceDynamicFlow.

@Test
public void testMockMessageSourceDynamicFlow() {
    QueueChannel out = new QueueChannel();
    StandardIntegrationFlow flow = IntegrationFlows.from(MockIntegration.mockMessageSource("foo", "bar", "baz")).<String, String>transform(String::toUpperCase).channel(out).get();
    IntegrationFlowRegistration registration = this.integrationFlowContext.registration(flow).register();
    Message<?> receive = out.receive(10_000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());
    receive = out.receive(10_000);
    assertNotNull(receive);
    assertEquals("BAR", receive.getPayload());
    for (int i = 0; i < 10; i++) {
        receive = out.receive(10_000);
        assertNotNull(receive);
        assertEquals("BAZ", receive.getPayload());
    }
    registration.destroy();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) Matchers.containsString(org.hamcrest.Matchers.containsString) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test) SpringIntegrationTest(org.springframework.integration.test.context.SpringIntegrationTest)

Example 3 with IntegrationFlowRegistration

use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.

the class IpIntegrationTests method testTcpGateways.

@Test
public void testTcpGateways() {
    TestingUtilities.waitListening(this.server1, null);
    IntegrationFlow flow = f -> f.handle(Tcp.outboundGateway(Tcp.netClient("localhost", this.server1.getPort()).serializer(TcpCodecs.crlf()).deserializer(TcpCodecs.lengthHeader1()).id("client1")).remoteTimeout(m -> 5000)).transform(Transformers.objectToString());
    IntegrationFlowRegistration theFlow = this.flowContext.registration(flow).register();
    assertThat(theFlow.getMessagingTemplate().convertSendAndReceive("foo", String.class), equalTo("FOO"));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) Assert.assertThat(org.junit.Assert.assertThat) TcpReceivingChannelAdapter(org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter) MulticastSendingMessageHandler(org.springframework.integration.ip.udp.MulticastSendingMessageHandler) MessageBuilder(org.springframework.integration.support.MessageBuilder) TestingUtilities(org.springframework.integration.ip.util.TestingUtilities) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ApplicationListener(org.springframework.context.ApplicationListener) EnableIntegration(org.springframework.integration.config.EnableIntegration) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) MessageChannel(org.springframework.messaging.MessageChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) Configuration(org.springframework.context.annotation.Configuration) CountDownLatch(java.util.concurrent.CountDownLatch) UdpServerListeningEvent(org.springframework.integration.ip.udp.UdpServerListeningEvent) Matchers.equalTo(org.hamcrest.Matchers.equalTo) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) TcpCodecs(org.springframework.integration.ip.tcp.serializer.TcpCodecs) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) Bean(org.springframework.context.annotation.Bean) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Transformers(org.springframework.integration.dsl.Transformers) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Test(org.junit.Test)

Example 4 with IntegrationFlowRegistration

use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.

the class FtpTests method testFtpOutboundFlow.

@Test
public void testFtpOutboundFlow() {
    IntegrationFlow flow = f -> f.handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL).useTemporaryFileName(false).fileNameExpression("headers['" + FileHeaders.FILENAME + "']").remoteDirectory("ftpTarget"));
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    String fileName = "foo.file";
    Message<ByteArrayInputStream> message = MessageBuilder.withPayload(new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8))).setHeader(FileHeaders.FILENAME, fileName).build();
    registration.getInputChannel().send(message);
    RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<>(sessionFactory());
    FTPFile[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
    assertEquals(1, files.length);
    assertEquals(3, files[0].getSize());
    registration.destroy();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) Assert.assertThat(org.junit.Assert.assertThat) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) FtpInboundFileSynchronizingMessageSource(org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizingMessageSource) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) FtpStreamingMessageSource(org.springframework.integration.ftp.inbound.FtpStreamingMessageSource) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FtpTestSupport(org.springframework.integration.ftp.FtpTestSupport) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) FileCopyUtils(org.springframework.util.FileCopyUtils) QueueChannel(org.springframework.integration.channel.QueueChannel) RunWith(org.junit.runner.RunWith) FileHeaders(org.springframework.integration.file.FileHeaders) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) TestUtils(org.springframework.integration.test.util.TestUtils) MessageSource(org.springframework.integration.core.MessageSource) MessageBuilder(org.springframework.integration.support.MessageBuilder) Pollers(org.springframework.integration.dsl.Pollers) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileOutputStream(java.io.FileOutputStream) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) IOException(java.io.IOException) Test(org.junit.Test) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) Assert.assertNull(org.junit.Assert.assertNull) FileReader(java.io.FileReader) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) ByteArrayInputStream(java.io.ByteArrayInputStream) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FTPFile(org.apache.commons.net.ftp.FTPFile) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with IntegrationFlowRegistration

use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration in project spring-integration by spring-projects.

the class FtpTests method testFtpMgetFlow.

@Test
@SuppressWarnings("unchecked")
public void testFtpMgetFlow() {
    QueueChannel out = new QueueChannel();
    IntegrationFlow flow = f -> f.handle(Ftp.outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET, "payload").options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE).fileExistsMode(FileExistsMode.IGNORE).filterExpression("name matches 'subFtpSource|.*1.txt'").localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory").localFilenameExpression("#remoteFileName.replaceFirst('ftpSource', 'localTarget')")).channel(out);
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    String dir = "ftpSource/";
    registration.getInputChannel().send(new GenericMessage<>(dir + "*"));
    Message<?> result = out.receive(10_000);
    assertNotNull(result);
    List<File> localFiles = (List<File>) result.getPayload();
    // should have filtered ftpSource2.txt
    assertEquals(2, localFiles.size());
    for (File file : localFiles) {
        assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir));
    }
    assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), Matchers.containsString(dir + "subFtpSource"));
    registration.destroy();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) Autowired(org.springframework.beans.factory.annotation.Autowired) Assert.assertThat(org.junit.Assert.assertThat) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) FtpInboundFileSynchronizingMessageSource(org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizingMessageSource) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) FtpStreamingMessageSource(org.springframework.integration.ftp.inbound.FtpStreamingMessageSource) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) FtpTestSupport(org.springframework.integration.ftp.FtpTestSupport) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) FTPFile(org.apache.commons.net.ftp.FTPFile) Matchers.containsString(org.hamcrest.Matchers.containsString) FileCopyUtils(org.springframework.util.FileCopyUtils) QueueChannel(org.springframework.integration.channel.QueueChannel) RunWith(org.junit.runner.RunWith) FileHeaders(org.springframework.integration.file.FileHeaders) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) TestUtils(org.springframework.integration.test.util.TestUtils) MessageSource(org.springframework.integration.core.MessageSource) MessageBuilder(org.springframework.integration.support.MessageBuilder) Pollers(org.springframework.integration.dsl.Pollers) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) FtpRemoteFileTemplate(org.springframework.integration.ftp.session.FtpRemoteFileTemplate) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileOutputStream(java.io.FileOutputStream) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) IOException(java.io.IOException) Test(org.junit.Test) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) Assert.assertNull(org.junit.Assert.assertNull) FileReader(java.io.FileReader) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) List(java.util.List) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 QueueChannel (org.springframework.integration.channel.QueueChannel)14 IntegrationFlowRegistration (org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration)14 StandardIntegrationFlow (org.springframework.integration.dsl.StandardIntegrationFlow)13 IntegrationFlow (org.springframework.integration.dsl.IntegrationFlow)11 Matchers.containsString (org.hamcrest.Matchers.containsString)9 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 Assert.assertNotNull (org.junit.Assert.assertNotNull)9 Assert.assertThat (org.junit.Assert.assertThat)9 RunWith (org.junit.runner.RunWith)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 Configuration (org.springframework.context.annotation.Configuration)9 EnableIntegration (org.springframework.integration.config.EnableIntegration)9 IntegrationFlows (org.springframework.integration.dsl.IntegrationFlows)9 IntegrationFlowContext (org.springframework.integration.dsl.context.IntegrationFlowContext)9 Message (org.springframework.messaging.Message)9 GenericMessage (org.springframework.messaging.support.GenericMessage)9 DirtiesContext (org.springframework.test.annotation.DirtiesContext)9 SpringRunner (org.springframework.test.context.junit4.SpringRunner)9