Search in sources :

Example 1 with FileTransferringMessageHandler

use of org.springframework.integration.file.remote.handler.FileTransferringMessageHandler in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testPutExists.

@Test
public void testPutExists() throws Exception {
    @SuppressWarnings("unchecked") SessionFactory<TestLsEntry> sessionFactory = mock(SessionFactory.class);
    @SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class);
    RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory) {

        @Override
        public boolean exists(String path) {
            return true;
        }
    };
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setBeanFactory(mock(BeanFactory.class));
    template.afterPropertiesSet();
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", "payload");
    FileTransferringMessageHandler<TestLsEntry> handler = new FileTransferringMessageHandler<TestLsEntry>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    Message<String> requestMessage = MessageBuilder.withPayload("hello").setHeader(FileHeaders.FILENAME, "bar.txt").build();
    // default (null) == REPLACE
    String path = (String) gw.handleRequestMessage(requestMessage);
    assertEquals("foo/bar.txt", path);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(session).write(any(InputStream.class), captor.capture());
    assertEquals("foo/bar.txt.writing", captor.getValue());
    verify(session).rename("foo/bar.txt.writing", "foo/bar.txt");
    gw.setFileExistsMode(FileExistsMode.FAIL);
    try {
        path = (String) gw.handleRequestMessage(requestMessage);
        fail("Exception expected");
    } catch (Exception e) {
        assertThat(e.getMessage(), containsString("The destination file already exists"));
    }
    gw.setFileExistsMode(FileExistsMode.REPLACE);
    path = (String) gw.handleRequestMessage(requestMessage);
    assertEquals("foo/bar.txt", path);
    captor = ArgumentCaptor.forClass(String.class);
    verify(session, times(2)).write(any(InputStream.class), captor.capture());
    assertEquals("foo/bar.txt.writing", captor.getValue());
    verify(session, times(2)).rename("foo/bar.txt.writing", "foo/bar.txt");
    gw.setFileExistsMode(FileExistsMode.APPEND);
    path = (String) gw.handleRequestMessage(requestMessage);
    assertEquals("foo/bar.txt", path);
    captor = ArgumentCaptor.forClass(String.class);
    verify(session).append(any(InputStream.class), captor.capture());
    assertEquals("foo/bar.txt", captor.getValue());
    gw.setFileExistsMode(FileExistsMode.IGNORE);
    path = (String) gw.handleRequestMessage(requestMessage);
    assertEquals("foo/bar.txt", path);
    // no more writes/appends
    verify(session, times(2)).write(any(InputStream.class), anyString());
    verify(session, times(1)).append(any(InputStream.class), anyString());
}
Also used : RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) InputStream(java.io.InputStream) LiteralExpression(org.springframework.expression.common.LiteralExpression) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 2 with FileTransferringMessageHandler

use of org.springframework.integration.file.remote.handler.FileTransferringMessageHandler in project spring-integration by spring-projects.

the class RemoteFileOutboundGatewayTests method testPut.

@Test
public void testPut() throws Exception {
    @SuppressWarnings("unchecked") SessionFactory<TestLsEntry> sessionFactory = mock(SessionFactory.class);
    @SuppressWarnings("unchecked") Session<TestLsEntry> session = mock(Session.class);
    RemoteFileTemplate<TestLsEntry> template = new RemoteFileTemplate<TestLsEntry>(sessionFactory) {

        @Override
        public boolean exists(String path) {
            return false;
        }
    };
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
    template.setBeanFactory(mock(BeanFactory.class));
    template.afterPropertiesSet();
    TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "put", "payload");
    FileTransferringMessageHandler<TestLsEntry> handler = new FileTransferringMessageHandler<TestLsEntry>(sessionFactory);
    handler.setRemoteDirectoryExpressionString("'foo/'");
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    gw.afterPropertiesSet();
    when(sessionFactory.getSession()).thenReturn(session);
    Message<String> requestMessage = MessageBuilder.withPayload("hello").setHeader(FileHeaders.FILENAME, "bar.txt").build();
    String path = (String) gw.handleRequestMessage(requestMessage);
    assertEquals("foo/bar.txt", path);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(session).write(any(InputStream.class), captor.capture());
    assertEquals("foo/bar.txt.writing", captor.getValue());
    verify(session).rename("foo/bar.txt.writing", "foo/bar.txt");
}
Also used : RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) InputStream(java.io.InputStream) LiteralExpression(org.springframework.expression.common.LiteralExpression) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 3 with FileTransferringMessageHandler

use of org.springframework.integration.file.remote.handler.FileTransferringMessageHandler in project spring-integration by spring-projects.

the class FtpOutboundChannelAdapterParserTests method testFtpOutboundChannelAdapterComplete.

@Test
public void testFtpOutboundChannelAdapterComplete() throws Exception {
    assertEquals(ftpChannel, TestUtils.getPropertyValue(ftpOutbound, "inputChannel"));
    assertEquals("ftpOutbound", ftpOutbound.getComponentName());
    FileTransferringMessageHandler<?> handler = TestUtils.getPropertyValue(ftpOutbound, "handler", FileTransferringMessageHandler.class);
    String remoteFileSeparator = (String) TestUtils.getPropertyValue(handler, "remoteFileTemplate.remoteFileSeparator");
    assertNotNull(remoteFileSeparator);
    assertEquals(".foo", TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryFileSuffix", String.class));
    assertEquals("", remoteFileSeparator);
    assertEquals(this.fileNameGenerator, TestUtils.getPropertyValue(handler, "remoteFileTemplate.fileNameGenerator"));
    assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "remoteFileTemplate.charset"));
    assertNotNull(TestUtils.getPropertyValue(handler, "remoteFileTemplate.directoryExpressionProcessor"));
    assertNotNull(TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryDirectoryExpressionProcessor"));
    assertEquals(FtpRemoteFileTemplate.ExistsMode.NLST, TestUtils.getPropertyValue(handler, "remoteFileTemplate.existsMode"));
    Object sfProperty = TestUtils.getPropertyValue(handler, "remoteFileTemplate.sessionFactory");
    assertEquals(DefaultFtpSessionFactory.class, sfProperty.getClass());
    DefaultFtpSessionFactory sessionFactory = (DefaultFtpSessionFactory) sfProperty;
    assertEquals("localhost", TestUtils.getPropertyValue(sessionFactory, "host"));
    assertEquals(22, TestUtils.getPropertyValue(sessionFactory, "port"));
    assertEquals(23, TestUtils.getPropertyValue(handler, "order"));
    // verify subscription order
    Object dispatcher = TestUtils.getPropertyValue(ftpChannel, "dispatcher");
    @SuppressWarnings("unchecked") Set<MessageHandler> handlers = (Set<MessageHandler>) TestUtils.getPropertyValue(dispatcher, "handlers");
    Iterator<MessageHandler> iterator = handlers.iterator();
    assertSame(TestUtils.getPropertyValue(this.ftpOutbound2, "handler"), iterator.next());
    assertSame(handler, iterator.next());
    assertEquals(FileExistsMode.APPEND, TestUtils.getPropertyValue(ftpOutbound, "handler.mode"));
}
Also used : Set(java.util.Set) MessageHandler(org.springframework.messaging.MessageHandler) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) DefaultFtpSessionFactory(org.springframework.integration.ftp.session.DefaultFtpSessionFactory) Test(org.junit.Test)

Example 4 with FileTransferringMessageHandler

use of org.springframework.integration.file.remote.handler.FileTransferringMessageHandler in project spring-integration by spring-projects.

the class SftpOutboundTests method testMkDir.

// INT-2954
@Test
public void testMkDir() throws Exception {
    @SuppressWarnings("unchecked") Session<LsEntry> session = mock(Session.class);
    when(session.exists(anyString())).thenReturn(Boolean.FALSE);
    @SuppressWarnings("unchecked") SessionFactory<LsEntry> sessionFactory = mock(SessionFactory.class);
    when(sessionFactory.getSession()).thenReturn(session);
    FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
    handler.setAutoCreateDirectory(true);
    handler.setRemoteDirectoryExpression(new LiteralExpression("/foo/bar/baz"));
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    final List<String> madeDirs = new ArrayList<String>();
    doAnswer(invocation -> {
        madeDirs.add(invocation.getArgument(0));
        return null;
    }).when(session).mkdir(anyString());
    handler.handleMessage(new GenericMessage<String>("qux"));
    assertEquals(3, madeDirs.size());
    assertEquals("/foo", madeDirs.get(0));
    assertEquals("/foo/bar", madeDirs.get(1));
    assertEquals("/foo/bar/baz", madeDirs.get(2));
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) Test(org.junit.Test)

Example 5 with FileTransferringMessageHandler

use of org.springframework.integration.file.remote.handler.FileTransferringMessageHandler in project spring-integration by spring-projects.

the class SftpOutboundTests method testHandleBytesMessage.

@Test
public void testHandleBytesMessage() throws Exception {
    File file = new File("remote-target-dir", "foo.txt");
    if (file.exists()) {
        file.delete();
    }
    SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
    FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
    DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
    fGenerator.setBeanFactory(mock(BeanFactory.class));
    fGenerator.setExpression("'foo.txt'");
    handler.setFileNameGenerator(fGenerator);
    handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.handleMessage(new GenericMessage<byte[]>("byte[] data".getBytes()));
    assertTrue(new File("remote-target-dir", "foo.txt").exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("byte[] data", new String(inFile));
    file.delete();
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 FileTransferringMessageHandler (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler)12 LiteralExpression (org.springframework.expression.common.LiteralExpression)11 BeanFactory (org.springframework.beans.factory.BeanFactory)10 File (java.io.File)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)4 FTPFile (org.apache.commons.net.ftp.FTPFile)4 DefaultFileNameGenerator (org.springframework.integration.file.DefaultFileNameGenerator)3 InputStream (java.io.InputStream)2 Set (java.util.Set)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 RemoteFileTemplate (org.springframework.integration.file.remote.RemoteFileTemplate)2 MessageHandler (org.springframework.messaging.MessageHandler)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Log (org.apache.commons.logging.Log)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1