Search in sources :

Example 1 with SftpRemoteFileTemplate

use of org.springframework.integration.sftp.session.SftpRemoteFileTemplate 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 2 with SftpRemoteFileTemplate

use of org.springframework.integration.sftp.session.SftpRemoteFileTemplate 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 3 with SftpRemoteFileTemplate

use of org.springframework.integration.sftp.session.SftpRemoteFileTemplate in project spring-integration by spring-projects.

the class SftpServerOutboundTests method testInt3412FileMode.

@Test
public void testInt3412FileMode() {
    Message<String> m = MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, "appending.txt").build();
    appending.send(m);
    appending.send(m);
    SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sessionFactory);
    assertLength6(template);
    ignoring.send(m);
    assertLength6(template);
    try {
        failing.send(m);
        fail("Expected exception");
    } catch (MessagingException e) {
        assertThat(e.getCause().getCause().getMessage(), containsString("The destination file already exists"));
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 SftpRemoteFileTemplate (org.springframework.integration.sftp.session.SftpRemoteFileTemplate)3 InputStream (java.io.InputStream)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 StandardIntegrationFlow (org.springframework.integration.dsl.StandardIntegrationFlow)2 IntegrationFlowRegistration (org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)1 File (java.io.File)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Matchers (org.hamcrest.Matchers)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.isOneOf (org.hamcrest.Matchers.isOneOf)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 RunWith (org.junit.runner.RunWith)1