use of org.springframework.integration.file.remote.session.SessionFactory in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testMoveWithMkDirs.
@Test
public void testMoveWithMkDirs() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mv", "payload");
gw.setRenameExpression(PARSER.parseExpression("'foo/bar/baz'"));
gw.afterPropertiesSet();
Session<?> session = mock(Session.class);
final AtomicReference<String> args = new AtomicReference<String>();
doAnswer(invocation -> {
Object[] arguments = invocation.getArguments();
args.set((String) arguments[0] + (String) arguments[1]);
return null;
}).when(session).rename(anyString(), anyString());
final List<String> madeDirs = new ArrayList<String>();
doAnswer(invocation -> {
madeDirs.add(invocation.getArgument(0));
return null;
}).when(session).mkdir(anyString());
when(sessionFactory.getSession()).thenReturn(session);
Message<String> requestMessage = MessageBuilder.withPayload("foo").setHeader(FileHeaders.RENAME_TO, "bar").build();
MessageBuilder<?> out = (MessageBuilder<?>) gw.handleRequestMessage(requestMessage);
assertEquals("foo", out.getHeaders().get(FileHeaders.REMOTE_FILE));
assertEquals("foofoo/bar/baz", args.get());
assertEquals(Boolean.TRUE, out.getPayload());
assertEquals(2, madeDirs.size());
assertEquals("foo", madeDirs.get(0));
assertEquals("foo/bar", madeDirs.get(1));
}
use of org.springframework.integration.file.remote.session.SessionFactory in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testMGetWildGuts.
private void testMGetWildGuts(final String path1, final String path2) {
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();
new File(this.tmpDir + "/f2").delete();
when(sessionFactory.getSession()).thenReturn(new TestSession() {
int n;
@Override
public void read(String source, OutputStream outputStream) throws IOException {
if (n++ == 0) {
assertEquals("testremote/f1", source);
} else {
assertEquals("testremote/f2", source);
}
outputStream.write("testData".getBytes());
}
@Override
public TestLsEntry[] list(String path) throws IOException {
return new TestLsEntry[] { new TestLsEntry(path1.replaceFirst("testremote/", ""), 123, false, false, 1234, "-r--r--r--"), new TestLsEntry(path2.replaceFirst("testremote/", ""), 123, false, false, 1234, "-r--r--r--") };
}
});
@SuppressWarnings("unchecked") MessageBuilder<List<File>> out = (MessageBuilder<List<File>>) gw.handleRequestMessage(new GenericMessage<>("testremote/*"));
assertEquals(2, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0).getName());
assertEquals("f2", out.getPayload().get(1).getName());
assertEquals("testremote/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
use of org.springframework.integration.file.remote.session.SessionFactory in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testMoveWithExpression.
@Test
public void testMoveWithExpression() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mv", "payload");
gw.setRenameExpression(PARSER.parseExpression("payload.substring(1)"));
gw.afterPropertiesSet();
Session<?> session = mock(Session.class);
final AtomicReference<String> args = new AtomicReference<String>();
doAnswer(invocation -> {
Object[] arguments = invocation.getArguments();
args.set((String) arguments[0] + arguments[1]);
return null;
}).when(session).rename(anyString(), anyString());
when(sessionFactory.getSession()).thenReturn(session);
MessageBuilder<?> out = (MessageBuilder<?>) gw.handleRequestMessage(new GenericMessage<>("foo"));
assertEquals("oo", out.getHeaders().get(FileHeaders.RENAME_TO));
assertEquals("foo", out.getHeaders().get(FileHeaders.REMOTE_FILE));
assertEquals("foooo", args.get());
assertEquals(Boolean.TRUE, out.getPayload());
}
use of org.springframework.integration.file.remote.session.SessionFactory in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testLs_f_R.
@Test
public void testLs_f_R() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "ls", "payload");
gw.setOptions("-f -R");
gw.afterPropertiesSet();
when(sessionFactory.getSession()).thenReturn(session);
TestLsEntry[] level1 = level1List();
TestLsEntry[] level2 = level2List();
TestLsEntry[] level3 = level3List();
when(session.list("testremote/x/")).thenReturn(level1);
when(session.list("testremote/x/d1/")).thenReturn(level2);
when(session.list("testremote/x/d1/d2/")).thenReturn(level3);
@SuppressWarnings("unchecked") MessageBuilder<List<TestLsEntry>> out = (MessageBuilder<List<TestLsEntry>>) gw.handleRequestMessage(new GenericMessage<>("testremote/x"));
assertEquals(4, out.getPayload().size());
assertEquals("f1", out.getPayload().get(0).getFilename());
assertEquals("d1/d2/f4", out.getPayload().get(1).getFilename());
assertEquals("d1/f3", out.getPayload().get(2).getFilename());
assertEquals("f2", out.getPayload().get(3).getFilename());
assertEquals("testremote/x/", out.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
}
use of org.springframework.integration.file.remote.session.SessionFactory in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testMGetEmpty.
@Test(expected = MessagingException.class)
public void testMGetEmpty() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "mget", "payload");
gw.setLocalDirectory(new File(this.tmpDir));
gw.setOptions(" -x ");
gw.afterPropertiesSet();
new File(this.tmpDir + "/f1").delete();
new File(this.tmpDir + "/f2").delete();
when(sessionFactory.getSession()).thenReturn(new TestSession() {
@Override
public void read(String source, OutputStream outputStream) throws IOException {
outputStream.write("testData".getBytes());
}
});
gw.handleRequestMessage(new GenericMessage<String>("testremote/*"));
}
Aggregations