Search in sources :

Example 11 with IntegrationFlowRegistration

use of org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration 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)

Example 12 with IntegrationFlowRegistration

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

the class SftpTests method testSftpOutboundFlow.

@Test
public void testSftpOutboundFlow() {
    IntegrationFlow flow = f -> f.handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL).useTemporaryFileName(false).fileNameExpression("headers['" + FileHeaders.FILENAME + "']").remoteDirectory("sftpTarget"));
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    String fileName = "foo.file";
    registration.getInputChannel().send(MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, fileName).build());
    RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(sessionFactory());
    ChannelSftp.LsEntry[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
    assertEquals(1, files.length);
    assertEquals(3, files[0].getAttrs().getSize());
    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) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) 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) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 13 with IntegrationFlowRegistration

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

the class SftpTests method testSftpInboundStreamFlow.

@Test
public void testSftpInboundStreamFlow() throws Exception {
    QueueChannel out = new QueueChannel();
    StandardIntegrationFlow flow = IntegrationFlows.from(Sftp.inboundStreamingAdapter(new SftpRemoteFileTemplate(sessionFactory())).remoteDirectory("sftpSource").regexFilter(".*\\.txt$"), e -> e.id("sftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get();
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    Message<?> message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" sftpSource1.txt", "sftpSource2.txt"));
    ((InputStream) message.getPayload()).close();
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
    message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" sftpSource1.txt", "sftpSource2.txt"));
    ((InputStream) message.getPayload()).close();
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();
    registration.destroy();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) InputStream(java.io.InputStream) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Test(org.junit.Test)

Example 14 with IntegrationFlowRegistration

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

the class SftpTests method testSftpInboundFlow.

@Test
public void testSftpInboundFlow() {
    QueueChannel out = new QueueChannel();
    IntegrationFlow flow = IntegrationFlows.from(Sftp.inboundAdapter(sessionFactory()).preserveTimestamp(true).remoteDirectory("sftpSource").regexFilter(".*\\.txt$").localFilenameExpression("#this.toUpperCase() + '.a'").localDirectory(getTargetLocalDirectory()), e -> e.id("sftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get();
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    Message<?> message = out.receive(10_000);
    assertNotNull(message);
    Object payload = message.getPayload();
    assertThat(payload, instanceOf(File.class));
    File file = (File) payload;
    assertThat(file.getName(), isOneOf(" SFTPSOURCE1.TXT.a", "SFTPSOURCE2.TXT.a"));
    assertThat(file.getAbsolutePath(), containsString("localTarget"));
    message = out.receive(10_000);
    assertNotNull(message);
    file = (File) message.getPayload();
    assertThat(file.getName(), isOneOf(" SFTPSOURCE1.TXT.a", "SFTPSOURCE2.TXT.a"));
    assertThat(file.getAbsolutePath(), containsString("localTarget"));
    registration.destroy();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) 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