public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); template.execute((SessionCallback<LsEntry, Void>) session -> { try { session.mkdir("si.sftp.sample"); } catch (Exception e) { assertThat(e.getMessage(), containsString("failed to create")); } for (int i = 0; i < fileNames.length; i++) { stream.reset(); session.write(stream, "si.sftp.sample/" + fileNames[i]); } return null; }); } }
@BeforeClass public static void setup() { FtpRemoteFileTemplate rft = new FtpRemoteFileTemplate(sessionFactory()); rft.execute(s -> { s.mkdir("foo"); s.mkdir("bar"); s.mkdir("baz"); s.mkdir("qux"); s.mkdir("fiz"); s.mkdir("buz"); ByteArrayInputStream bais = new ByteArrayInputStream("foo".getBytes()); s.write(bais, "foo/f1"); s.write(bais, "baz/f2"); s.write(bais, "fiz/f3"); return null; }); }
@Test //INT-2954 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)); }
@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)); }
session.mkdir(remoteDirectory);