Search in sources :

Example 11 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class AbstractRemoteFileSynchronizerTests method testRollback.

@Test
public void testRollback() throws Exception {
    final AtomicBoolean failWhenCopyingBar = new AtomicBoolean(true);
    final AtomicInteger count = new AtomicInteger();
    SessionFactory<String> sf = new StringSessionFactory();
    AbstractInboundFileSynchronizer<String> sync = new AbstractInboundFileSynchronizer<String>(sf) {

        @Override
        protected boolean isFile(String file) {
            return true;
        }

        @Override
        protected String getFilename(String file) {
            return file;
        }

        @Override
        protected long getModified(String file) {
            return 0;
        }

        @Override
        protected boolean copyFileToLocalDirectory(String remoteDirectoryPath, String remoteFile, File localDirectory, Session<String> session) throws IOException {
            if ("bar".equals(remoteFile) && failWhenCopyingBar.getAndSet(false)) {
                throw new IOException("fail");
            }
            count.incrementAndGet();
            return true;
        }
    };
    sync.setFilter(new AcceptOnceFileListFilter<String>());
    sync.setRemoteDirectory("foo");
    try {
        sync.synchronizeToLocalDirectory(mock(File.class));
        assertEquals(1, count.get());
        fail("Expected exception");
    } catch (MessagingException e) {
        assertThat(e.getCause(), instanceOf(MessagingException.class));
        assertThat(e.getCause().getCause(), instanceOf(IOException.class));
        assertEquals("fail", e.getCause().getCause().getMessage());
    }
    sync.synchronizeToLocalDirectory(mock(File.class));
    assertEquals(3, count.get());
    sync.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) File(java.io.File) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 12 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class SftpServerOutboundTests method testInt3088MPutNotRecursive.

@Test
public void testInt3088MPutNotRecursive() throws Exception {
    Session<?> session = sessionFactory.getSession();
    session.close();
    session = TestUtils.getPropertyValue(session, "targetSession", Session.class);
    ChannelSftp channel = spy(TestUtils.getPropertyValue(session, "channel", ChannelSftp.class));
    new DirectFieldAccessor(session).setPropertyValue("channel", channel);
    String dir = "sftpSource/";
    this.inboundMGetRecursive.send(new GenericMessage<Object>(dir + "*"));
    while (output.receive(0) != null) {
    // drain
    }
    this.inboundMPut.send(new GenericMessage<File>(getSourceLocalDirectory()));
    @SuppressWarnings("unchecked") Message<List<String>> out = (Message<List<String>>) this.output.receive(1000);
    assertNotNull(out);
    assertEquals(2, out.getPayload().size());
    assertThat(out.getPayload().get(0), not(equalTo(out.getPayload().get(1))));
    assertThat(out.getPayload().get(0), anyOf(equalTo("sftpTarget/localSource1.txt"), equalTo("sftpTarget/localSource2.txt")));
    assertThat(out.getPayload().get(1), anyOf(equalTo("sftpTarget/localSource1.txt"), equalTo("sftpTarget/localSource2.txt")));
    // 384 = 600 octal
    verify(channel).chmod(384, "sftpTarget/localSource1.txt");
    verify(channel).chmod(384, "sftpTarget/localSource2.txt");
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) ChannelSftp(com.jcraft.jsch.ChannelSftp) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) List(java.util.List) File(java.io.File) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 13 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_1_a_f_dirs_links_filtered.

@Test
public void testLs_1_a_f_dirs_links_filtered() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-1 -a -f -dirs -links");
    gw.setFilter(new TestPatternFilter("*4"));
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    TestLsEntry[] files = fileList();
    when(session.list("testremote/")).thenReturn(files);
    @SuppressWarnings("unchecked") MessageBuilder<List<String>> out = (MessageBuilder<List<String>>) gw.handleRequestMessage(new GenericMessage<>("testremote"));
    assertEquals(1, out.getPayload().size());
    assertEquals("f4", out.getPayload().get(0));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 14 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_1_dirs.

@Test
public void testLs_1_dirs() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-1 -dirs");
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    TestLsEntry[] files = fileList();
    when(session.list("testremote/")).thenReturn(files);
    @SuppressWarnings("unchecked") MessageBuilder<List<String>> out = (MessageBuilder<List<String>>) gw.handleRequestMessage(new GenericMessage<>("testremote"));
    assertEquals(3, out.getPayload().size());
    assertEquals("f1", out.getPayload().get(0));
    assertEquals("f2", out.getPayload().get(1));
    assertEquals("f3", out.getPayload().get(2));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 15 with Session

use of org.springframework.integration.file.remote.session.Session in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_f_R_dirs.

@Test
public void testLs_f_R_dirs() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-f -R -dirs");
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    TestLsEntry[] level1 = level1List();
    TestLsEntry[] level2 = level2List();
    TestLsEntry[] level3 = level3List();
    when(session.list("testremote/x/")).thenReturn(level1);
    when(session.list("testremote/x/d1/")).thenReturn(level2);
    when(session.list("testremote/x/d1/d2/")).thenReturn(level3);
    @SuppressWarnings("unchecked") MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw.handleRequestMessage(new GenericMessage<>("testremote/x"));
    assertEquals(6, out.getPayload().size());
    assertEquals("f1", out.getPayload().get(0).getFilename());
    assertEquals("d1", out.getPayload().get(1).getFilename());
    assertEquals("d1/d2", out.getPayload().get(2).getFilename());
    assertEquals("d1/d2/f4", out.getPayload().get(3).getFilename());
    assertEquals("d1/f3", out.getPayload().get(4).getFilename());
    assertEquals("f2", out.getPayload().get(5).getFilename());
    assertEquals("testremote/x/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Aggregations

Session (org.springframework.integration.file.remote.session.Session)23 Test (org.junit.Test)22 SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)16 List (java.util.List)13 GenericMessage (org.springframework.messaging.support.GenericMessage)13 ArrayList (java.util.ArrayList)12 MessageBuilder (org.springframework.integration.support.MessageBuilder)12 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)5 ChannelSftp (com.jcraft.jsch.ChannelSftp)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)3 JSch (com.jcraft.jsch.JSch)3 File (java.io.File)3 IOException (java.io.IOException)2 DefaultSftpSessionFactory (org.springframework.integration.sftp.session.DefaultSftpSessionFactory)2 SftpSession (org.springframework.integration.sftp.session.SftpSession)2 MessagingException (org.springframework.messaging.MessagingException)2