Search in sources :

Example 21 with SessionFactory

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));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageBuilder(org.springframework.integration.support.MessageBuilder) Test(org.junit.Test)

Example 22 with SessionFactory

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));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 23 with SessionFactory

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());
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 24 with SessionFactory

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));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageBuilder(org.springframework.integration.support.MessageBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.springframework.integration.file.remote.session.Session) Test(org.junit.Test)

Example 25 with SessionFactory

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/*"));
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Aggregations

SessionFactory (org.springframework.integration.file.remote.session.SessionFactory)31 Test (org.junit.Test)30 MessageBuilder (org.springframework.integration.support.MessageBuilder)19 GenericMessage (org.springframework.messaging.support.GenericMessage)19 Matchers.containsString (org.hamcrest.Matchers.containsString)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)17 Session (org.springframework.integration.file.remote.session.Session)15 ArrayList (java.util.ArrayList)14 List (java.util.List)14 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)8 CachingSessionFactory (org.springframework.integration.file.remote.session.CachingSessionFactory)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 RunWith (org.junit.runner.RunWith)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2