use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testMputRecursive.
@Test
public void testMputRecursive() 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);
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "mput", null);
gw.setOptions("-R");
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
final AtomicReference<String> written = new AtomicReference<String>();
doAnswer(invocation -> {
written.set(invocation.getArgument(1));
return null;
}).when(session).write(any(InputStream.class), anyString());
tempFolder.newFile("baz.txt");
tempFolder.newFile("qux.txt");
File dir1 = tempFolder.newFolder();
File file3 = File.createTempFile("foo", ".txt", dir1);
Message<File> requestMessage = MessageBuilder.withPayload(tempFolder.getRoot()).build();
@SuppressWarnings("unchecked") List<String> out = (List<String>) gw.handleRequestMessage(requestMessage);
assertEquals(3, out.size());
assertThat(out.get(0), not(equalTo(out.get(1))));
assertThat(out.get(0), anyOf(equalTo("foo/baz.txt"), equalTo("foo/qux.txt"), equalTo("foo/" + dir1.getName() + "/" + file3.getName())));
assertThat(out.get(1), anyOf(equalTo("foo/baz.txt"), equalTo("foo/qux.txt"), equalTo("foo/" + dir1.getName() + "/" + file3.getName())));
assertThat(out.get(2), anyOf(equalTo("foo/baz.txt"), equalTo("foo/qux.txt"), equalTo("foo/" + dir1.getName() + "/" + file3.getName())));
}
use of org.springframework.integration.file.remote.RemoteFileTemplate 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());
}
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testMput.
@Test
public void testMput() 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);
template.setRemoteDirectoryExpression(new LiteralExpression("foo/"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(template, "mput", "payload");
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
final AtomicReference<String> written = new AtomicReference<String>();
doAnswer(invocation -> {
written.set(invocation.getArgument(1));
return null;
}).when(session).write(any(InputStream.class), anyString());
tempFolder.newFile("baz.txt");
tempFolder.newFile("qux.txt");
Message<File> requestMessage = MessageBuilder.withPayload(tempFolder.getRoot()).build();
@SuppressWarnings("unchecked") List<String> out = (List<String>) gw.handleRequestMessage(requestMessage);
assertEquals(2, out.size());
assertThat(out.get(0), not(equalTo(out.get(1))));
assertThat(out.get(0), anyOf(equalTo("foo/baz.txt"), equalTo("foo/qux.txt")));
assertThat(out.get(1), anyOf(equalTo("foo/baz.txt"), equalTo("foo/qux.txt")));
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class CachingSessionFactoryTests method testDirtySession.
@Test
public void testDirtySession() throws Exception {
@SuppressWarnings("unchecked") SessionFactory<Object> factory = mock(SessionFactory.class);
@SuppressWarnings("unchecked") Session<Object> session = mock(Session.class);
when(factory.getSession()).thenReturn(session);
when(session.readRaw("foo")).thenReturn(new ByteArrayInputStream("".getBytes()));
when(session.finalizeRaw()).thenReturn(true);
CachingSessionFactory<Object> ccf = new CachingSessionFactory<Object>(factory);
RemoteFileTemplate<Object> template = new RemoteFileTemplate<Object>(ccf);
template.setFileNameExpression(new LiteralExpression("foo"));
template.setBeanFactory(mock(BeanFactory.class));
template.afterPropertiesSet();
try {
template.get(new GenericMessage<String>("foo"), (InputStreamCallback) stream -> {
throw new RuntimeException("bar");
});
fail("Expected exception");
} catch (Exception e) {
assertThat(e.getCause(), instanceOf(RuntimeException.class));
assertThat(e.getCause().getMessage(), equalTo("bar"));
}
verify(session).close();
}
use of org.springframework.integration.file.remote.RemoteFileTemplate in project spring-integration by spring-projects.
the class SftpTests method testSftpOutboundFlow.
@Test
public void testSftpOutboundFlow() {
IntegrationFlow flow = f -> f.handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL).useTemporaryFileName(false).fileNameExpression("headers['" + FileHeaders.FILENAME + "']").remoteDirectory("sftpTarget"));
IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
String fileName = "foo.file";
registration.getInputChannel().send(MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, fileName).build());
RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(sessionFactory());
ChannelSftp.LsEntry[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName));
assertEquals(1, files.length);
assertEquals(3, files[0].getAttrs().getSize());
registration.destroy();
}
Aggregations