Search in sources :

Example 11 with IntegrationFlow

use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.

the class ManualFlowTests method testWrongLifecycle.

@Test
public void testWrongLifecycle() {
    class MyIntegrationFlow implements IntegrationFlow {

        @Override
        public void configure(IntegrationFlowDefinition<?> flow) {
            flow.bridge();
        }
    }
    IntegrationFlow testFlow = new MyIntegrationFlow();
    // This is fine because we are not going to start it automatically.
    assertNotNull(this.integrationFlowContext.registration(testFlow).autoStartup(false).register());
    try {
        this.integrationFlowContext.registration(testFlow).register();
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("Consider to implement it for [" + testFlow + "]."));
    }
    try {
        this.integrationFlowContext.remove("foo");
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("But [" + "foo" + "] ins't one of them."));
    }
}
Also used : IntegrationFlowDefinition(org.springframework.integration.dsl.IntegrationFlowDefinition) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) MessagingException(org.springframework.messaging.MessagingException) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 12 with IntegrationFlow

use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.

the class ManualFlowTests method testWithAnonymousMessageProducerStart.

@Test
public void testWithAnonymousMessageProducerStart() {
    final AtomicBoolean started = new AtomicBoolean();
    MessageProducerSupport producer = new MessageProducerSupport() {

        @Override
        protected void doStart() {
            started.set(true);
            super.doStart();
        }
    };
    QueueChannel channel = new QueueChannel();
    IntegrationFlow flow = IntegrationFlows.from(producer).channel(channel).get();
    this.integrationFlowContext.registration(flow).register();
    assertTrue(started.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageProducerSupport(org.springframework.integration.endpoint.MessageProducerSupport) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test)

Example 13 with IntegrationFlow

use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.

the class ManualFlowTests method testDynaSubFlowCreation.

@Test
public void testDynaSubFlowCreation() {
    Flux<Message<?>> messageFlux = Flux.just("1,2,3,4").map(v -> v.split(",")).flatMapIterable(Arrays::asList).map(Integer::parseInt).map(GenericMessage::new);
    QueueChannel resultChannel = new QueueChannel();
    IntegrationFlow integrationFlow = IntegrationFlows.from(messageFlux).<Integer, Boolean>route(p -> p % 2 == 0, m -> m.subFlowMapping(true, sf -> sf.<Integer, String>transform(em -> "even:" + em)).subFlowMapping(false, sf -> sf.<Integer, String>transform(em -> "odd:" + em)).defaultOutputToParentFlow()).channel(resultChannel).get();
    this.integrationFlowContext.registration(integrationFlow).register();
    for (int i = 0; i < 4; i++) {
        Message<?> receive = resultChannel.receive(10_000);
        assertNotNull(receive);
    }
    assertNull(resultChannel.receive(0));
}
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) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 14 with IntegrationFlow

use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.

the class SftpTests method testSftpMgetFlow.

@Test
@SuppressWarnings("unchecked")
public void testSftpMgetFlow() {
    QueueChannel out = new QueueChannel();
    IntegrationFlow flow = f -> f.handle(Sftp.outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET, "payload").options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE).regexFileNameFilter("(subSftpSource|.*1.txt)").localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory").localFilenameExpression("#remoteFileName.replaceFirst('sftpSource', 'localTarget')")).channel(out);
    String dir = "sftpSource/";
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    registration.getInputChannel().send(new GenericMessage<>(dir + "*"));
    Message<?> result = out.receive(10_000);
    assertNotNull(result);
    List<File> localFiles = (List<File>) result.getPayload();
    // should have filtered sftpSource2.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 + "subSftpSource"));
    registration.destroy();
}
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) FileHeaders(org.springframework.integration.file.FileHeaders) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Matcher(java.util.regex.Matcher) Pollers(org.springframework.integration.dsl.Pollers) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) ChannelSftp(com.jcraft.jsch.ChannelSftp) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) Test(org.junit.Test) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) File(java.io.File) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SftpTestSupport(org.springframework.integration.sftp.SftpTestSupport) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) 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) File(java.io.File) Test(org.junit.Test)

Example 15 with IntegrationFlow

use of org.springframework.integration.dsl.IntegrationFlow in project spring-integration by spring-projects.

the class SftpTests method testSftpSessionCallback.

@Test
public void testSftpSessionCallback() {
    QueueChannel out = new QueueChannel();
    IntegrationFlow flow = f -> f.<String>handle((p, h) -> new SftpRemoteFileTemplate(sessionFactory()).execute(s -> s.list(p))).channel(out);
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    registration.getInputChannel().send(new GenericMessage<>("sftpSource"));
    Message<?> receive = out.receive(10_000);
    assertNotNull(receive);
    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(ChannelSftp.LsEntry[].class));
    assertTrue(((ChannelSftp.LsEntry[]) payload).length > 0);
    registration.destroy();
}
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) FileHeaders(org.springframework.integration.file.FileHeaders) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Matcher(java.util.regex.Matcher) Pollers(org.springframework.integration.dsl.Pollers) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) ChannelSftp(com.jcraft.jsch.ChannelSftp) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) Test(org.junit.Test) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) File(java.io.File) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SftpTestSupport(org.springframework.integration.sftp.SftpTestSupport) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) ChannelSftp(com.jcraft.jsch.ChannelSftp) QueueChannel(org.springframework.integration.channel.QueueChannel) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test)

Aggregations

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