Search in sources :

Example 6 with FileTransferringMessageHandler

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

the class SftpOutboundTests method testHandleStringMessage.

@Test
public void testHandleStringMessage() 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<>(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<String>("String data"));
    assertTrue(new File("remote-target-dir", "foo.txt").exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("String 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)

Example 7 with FileTransferringMessageHandler

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

the class SftpOutboundTests method testHandleFileMessage.

@Test
public void testHandleFileMessage() throws Exception {
    File targetDir = new File("remote-target-dir");
    assertTrue("target directory does not exist: " + targetDir.getName(), targetDir.exists());
    SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
    FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
    DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
    fGenerator.setBeanFactory(mock(BeanFactory.class));
    fGenerator.setExpression("payload + '.test'");
    handler.setFileNameGenerator(fGenerator);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    File srcFile = File.createTempFile("testHandleFileMessage", ".tmp", new File("."));
    srcFile.deleteOnExit();
    File destFile = new File(targetDir, srcFile.getName() + ".test");
    destFile.deleteOnExit();
    handler.handleMessage(new GenericMessage<>(srcFile));
    assertTrue("destination file was not created", destFile.exists());
}
Also used : FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 8 with FileTransferringMessageHandler

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

the class OutboundChannelAdapterParserTests method testOutboundChannelAdapterWithId.

@Test
public void testOutboundChannelAdapterWithId() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("OutboundChannelAdapterParserTests-context.xml", this.getClass());
    Object consumer = context.getBean("sftpOutboundAdapter");
    assertTrue(consumer instanceof EventDrivenConsumer);
    PublishSubscribeChannel channel = context.getBean("inputChannel", PublishSubscribeChannel.class);
    assertEquals(channel, TestUtils.getPropertyValue(consumer, "inputChannel"));
    assertEquals("sftpOutboundAdapter", ((EventDrivenConsumer) consumer).getComponentName());
    FileTransferringMessageHandler<?> handler = TestUtils.getPropertyValue(consumer, "handler", FileTransferringMessageHandler.class);
    String remoteFileSeparator = (String) TestUtils.getPropertyValue(handler, "remoteFileTemplate.remoteFileSeparator");
    assertNotNull(remoteFileSeparator);
    assertEquals(".", remoteFileSeparator);
    assertEquals(".bar", TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryFileSuffix", String.class));
    Expression remoteDirectoryExpression = (Expression) TestUtils.getPropertyValue(handler, "remoteFileTemplate.directoryExpressionProcessor.expression");
    assertNotNull(remoteDirectoryExpression);
    assertTrue(remoteDirectoryExpression instanceof LiteralExpression);
    assertNotNull(TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryDirectoryExpressionProcessor"));
    assertEquals(context.getBean("fileNameGenerator"), TestUtils.getPropertyValue(handler, "remoteFileTemplate.fileNameGenerator"));
    assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "remoteFileTemplate.charset"));
    CachingSessionFactory<?> sessionFactory = TestUtils.getPropertyValue(handler, "remoteFileTemplate.sessionFactory", CachingSessionFactory.class);
    DefaultSftpSessionFactory clientFactory = TestUtils.getPropertyValue(sessionFactory, "sessionFactory", DefaultSftpSessionFactory.class);
    assertEquals("localhost", TestUtils.getPropertyValue(clientFactory, "host"));
    assertEquals(2222, TestUtils.getPropertyValue(clientFactory, "port"));
    assertEquals(23, TestUtils.getPropertyValue(handler, "order"));
    // verify subscription order
    @SuppressWarnings("unchecked") Set<MessageHandler> handlers = (Set<MessageHandler>) TestUtils.getPropertyValue(TestUtils.getPropertyValue(channel, "dispatcher"), "handlers");
    Iterator<MessageHandler> iterator = handlers.iterator();
    assertSame(TestUtils.getPropertyValue(context.getBean("sftpOutboundAdapterWithExpression"), "handler"), iterator.next());
    assertSame(handler, iterator.next());
    assertEquals(384, TestUtils.getPropertyValue(handler, "chmod"));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) Set(java.util.Set) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) MessageHandler(org.springframework.messaging.MessageHandler) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) DefaultSftpSessionFactory(org.springframework.integration.sftp.session.DefaultSftpSessionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) LiteralExpression(org.springframework.expression.common.LiteralExpression) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 9 with FileTransferringMessageHandler

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

the class FtpOutboundTests method testHandleFileContentMessage.

@Test
public void testHandleFileContentMessage() throws Exception {
    File file = new File("remote-target-dir/handlerContent.test");
    if (file.exists()) {
        file.delete();
    }
    assertFalse(file.exists());
    FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir"));
    handler.setFileNameGenerator(message -> "handlerContent.test");
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.handleMessage(new GenericMessage<String>("String data"));
    assertTrue(file.exists());
    byte[] inFile = FileCopyUtils.copyToByteArray(file);
    assertEquals("String 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) FTPFile(org.apache.commons.net.ftp.FTPFile) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) Test(org.junit.Test)

Example 10 with FileTransferringMessageHandler

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

the class FtpOutboundTests method testHandleMissingFileMessage.

@Test
public void testHandleMissingFileMessage() throws Exception {
    File targetDir = new File("remote-target-dir");
    assertTrue("target directory does not exist: " + targetDir.getName(), targetDir.exists());
    FileTransferringMessageHandler<FTPFile> handler = new FileTransferringMessageHandler<FTPFile>(sessionFactory);
    handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
    handler.setFileNameGenerator(message -> ((File) message.getPayload()).getName() + ".test");
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    File srcFile = new File(UUID.randomUUID() + ".txt");
    Log logger = spy(TestUtils.getPropertyValue(handler, "remoteFileTemplate.logger", Log.class));
    when(logger.isWarnEnabled()).thenReturn(true);
    final AtomicReference<String> logged = new AtomicReference<>();
    doAnswer(invocation -> {
        logged.set(invocation.getArgument(0));
        invocation.callRealMethod();
        return null;
    }).when(logger).warn(Mockito.anyString());
    RemoteFileTemplate<?> template = TestUtils.getPropertyValue(handler, "remoteFileTemplate", RemoteFileTemplate.class);
    new DirectFieldAccessor(template).setPropertyValue("logger", logger);
    handler.handleMessage(new GenericMessage<File>(srcFile));
    assertNotNull(logged.get());
    assertEquals("File " + srcFile.toString() + " does not exist", logged.get());
}
Also used : Log(org.apache.commons.logging.Log) FileTransferringMessageHandler(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) FTPFile(org.apache.commons.net.ftp.FTPFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) 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