Search in sources :

Example 1 with MessageBuilder

use of org.springframework.integration.support.MessageBuilder in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testGet_P.

@Test
public void testGet_P() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "get", "payload");
    gw.setLocalDirectory(new File(this.tmpDir));
    gw.setOptions("-P");
    gw.afterPropertiesSet();
    new File(this.tmpDir + "/f1").delete();
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MONTH, -1);
    final Date modified = new Date(cal.getTime().getTime() / 1000 * 1000);
    when(sessionFactory.getSession()).thenReturn(new TestSession() {

        @Override
        public TestLsEntry[] list(String path) throws IOException {
            return new TestLsEntry[] { new TestLsEntry("f1", 1234, false, false, modified.getTime(), "-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<>("x/f1"));
    File outFile = new File(this.tmpDir + "/f1");
    assertEquals(outFile, out.getPayload());
    assertTrue(outFile.exists());
    assertEquals(modified.getTime(), outFile.lastModified());
    outFile.delete();
    assertEquals("x/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("f1", out.getHeaders().get(FileHeaders.REMOTE_FILE));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) Calendar(java.util.Calendar) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) Date(java.util.Date) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) File(java.io.File) Test(org.junit.Test)

Example 2 with MessageBuilder

use of org.springframework.integration.support.MessageBuilder in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_None.

@Test
public void testLs_None() 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 = new TestLsEntry[0];
    when(session.list("testremote/")).thenReturn(files);
    @SuppressWarnings("unchecked") MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw.handleRequestMessage(new GenericMessage<>("testremote"));
    assertEquals(0, out.getPayload().size());
}
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 3 with MessageBuilder

use of org.springframework.integration.support.MessageBuilder in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testLs_f.

@Test
public void testLs_f() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
    gw.setOptions("-f");
    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());
    assertSame(files[0], out.getPayload().get(0));
    assertSame(files[1], 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 4 with MessageBuilder

use of org.springframework.integration.support.MessageBuilder in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testMGetSingle.

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

        @Override
        public void read(String source, OutputStream outputStream) throws IOException {
            outputStream.write("testData".getBytes());
        }

        @Override
        public TestLsEntry[] list(String path) throws IOException {
            return new TestLsEntry[] { new TestLsEntry("f1", 123, false, false, 1234, "-r--r--r--") };
        }
    });
    @SuppressWarnings("unchecked") MessageBuilder<List<File>> out = (MessageBuilder<List<File>>) gw.handleRequestMessage(new GenericMessage<>("testremote/f1"));
    assertEquals(1, out.getPayload().size());
    assertEquals("f1", out.getPayload().get(0).getName());
    assertEquals("testremote/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
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) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 5 with MessageBuilder

use of org.springframework.integration.support.MessageBuilder in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testRm.

@Test
public void testRm() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "rm", "payload");
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    when(session.remove("testremote/x/f1")).thenReturn(Boolean.TRUE);
    @SuppressWarnings("unchecked") MessageBuilder<Boolean> out = (MessageBuilder<Boolean>) gw.handleRequestMessage(new GenericMessage<>("testremote/x/f1"));
    assertEquals(Boolean.TRUE, out.getPayload());
    verify(session).remove("testremote/x/f1");
    assertEquals("testremote/x/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("f1", out.getHeaders().get(FileHeaders.REMOTE_FILE));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Aggregations

SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)19 MessageBuilder (org.springframework.integration.support.MessageBuilder)19 Test (org.junit.Test)18 GenericMessage (org.springframework.messaging.support.GenericMessage)17 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 Session (org.springframework.integration.file.remote.session.Session)12 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Calendar (java.util.Calendar)1 Date (java.util.Date)1