Search in sources :

Example 11 with SessionFactory

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

the class RemoteFileOutboundGatewayTests method testLs.

@Test
public void testLs() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    TestLsEntry[] files = fileList();
    when(session.list("testremote/x/")).thenReturn(files);
    @SuppressWarnings("unchecked") MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw.handleRequestMessage(new GenericMessage<>("testremote/x"));
    assertEquals(2, out.getPayload().size());
    // sort by default
    assertSame(files[1], out.getPayload().get(0));
    assertSame(files[0], out.getPayload().get(1));
    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)

Example 12 with SessionFactory

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

the class RemoteFileOutboundGatewayTests method testLs_1_a_f_dirs_links.

@Test
public void testLs_1_a_f_dirs_links() 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.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(6, out.getPayload().size());
    assertEquals("f2", out.getPayload().get(0));
    assertEquals("f1", out.getPayload().get(1));
    assertEquals("f3", out.getPayload().get(2));
    assertEquals("f4", out.getPayload().get(3));
    assertEquals(".f5", out.getPayload().get(4));
    assertEquals(".f6", out.getPayload().get(5));
}
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 13 with SessionFactory

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

the class RemoteFileOutboundGatewayTests method testLs_1_f.

@Test
public void testLs_1_f() throws Exception {
    // no sort
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-1 -f");
    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(2, out.getPayload().size());
    assertEquals("f2", out.getPayload().get(0));
    assertEquals("f1", out.getPayload().get(1));
}
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 SessionFactory

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

the class RemoteFileOutboundGatewayTests method testGet.

@Test
public void testGet() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "get", "payload");
    gw.setLocalDirectory(new File(this.tmpDir));
    gw.afterPropertiesSet();
    new File(this.tmpDir + "/f1").delete();
    when(sessionFactory.getSession()).thenReturn(new TestSession() {

        @Override
        public TestLsEntry[] list(String path) throws IOException {
            return new TestLsEntry[] { new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--") };
        }

        @Override
        public void read(String source, OutputStream outputStream) throws IOException {
            outputStream.write("testfile".getBytes());
        }
    });
    @SuppressWarnings("unchecked") MessageBuilder<File> out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
    File outFile = new File(this.tmpDir + "/f1");
    assertEquals(outFile, out.getPayload());
    assertTrue(outFile.exists());
    outFile.delete();
    assertNull(out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("f1", out.getHeaders().get(FileHeaders.REMOTE_FILE));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) File(java.io.File) Test(org.junit.Test)

Example 15 with SessionFactory

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

the class RemoteFileOutboundGatewayTests method testGetTempFileDelete.

@Test
public void testGetTempFileDelete() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "get", "payload");
    gw.setLocalDirectory(new File(this.tmpDir));
    gw.afterPropertiesSet();
    new File(this.tmpDir + "/f1").delete();
    when(sessionFactory.getSession()).thenReturn(new TestSession() {

        @Override
        public TestLsEntry[] list(String path) throws IOException {
            return new TestLsEntry[] { new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--") };
        }

        @Override
        public void read(String source, OutputStream outputStream) {
            throw new RuntimeException("test remove .writing");
        }
    });
    try {
        gw.handleRequestMessage(new GenericMessage<String>("f1"));
        fail("Expected exception");
    } catch (MessagingException e) {
        assertThat(e.getCause(), instanceOf(RuntimeException.class));
        assertEquals("test remove .writing", e.getCause().getMessage());
        @SuppressWarnings("unchecked") RemoteFileTemplate template = new RemoteFileTemplate(sessionFactory);
        File outFile = new File(this.tmpDir + "/f1" + template.getTemporaryFileSuffix());
        assertFalse(outFile.exists());
    }
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) MessagingException(org.springframework.messaging.MessagingException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Aggregations

SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)31 Test (org.junit.Test)30 MessageBuilder (org.springframework.integration.support.MessageBuilder)19 GenericMessage (org.springframework.messaging.support.GenericMessage)19 Matchers.containsString (org.hamcrest.Matchers.containsString)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)17 Session (org.springframework.integration.file.remote.session.Session)15 ArrayList (java.util.ArrayList)14 List (java.util.List)14 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)8 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 RunWith (org.junit.runner.RunWith)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2