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());
}
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");
}
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"));
}
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));
}
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();
}
Aggregations