Search in sources :

Example 6 with RemoteFileTemplate

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())));
}
Also used : RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) InputStream(java.io.InputStream) LiteralExpression(org.springframework.expression.common.LiteralExpression) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BeanFactory(org.springframework.beans.factory.BeanFactory) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 7 with RemoteFileTemplate

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());
    }
}
Also used : SessionFactory(org.springframework.integration.file.remote.session.SessionFactory) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) MessagingException(org.springframework.messaging.MessagingException) 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)

Example 8 with RemoteFileTemplate

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")));
}
Also used : RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) InputStream(java.io.InputStream) LiteralExpression(org.springframework.expression.common.LiteralExpression) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BeanFactory(org.springframework.beans.factory.BeanFactory) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 9 with RemoteFileTemplate

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();
}
Also used : OutputStream(java.io.OutputStream) LiteralExpression(org.springframework.expression.common.LiteralExpression) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TestUtils(org.springframework.integration.test.util.TestUtils) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamCallback(org.springframework.integration.file.remote.InputStreamCallback) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BeanFactory(org.springframework.beans.factory.BeanFactory) Assert.fail(org.junit.Assert.fail) GenericMessage(org.springframework.messaging.support.GenericMessage) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) InputStream(java.io.InputStream) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) LiteralExpression(org.springframework.expression.common.LiteralExpression) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 10 with RemoteFileTemplate

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();
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) QueueChannel(org.springframework.integration.channel.QueueChannel) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) RunWith(org.junit.runner.RunWith) FileHeaders(org.springframework.integration.file.FileHeaders) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationFlowContext(org.springframework.integration.dsl.context.IntegrationFlowContext) FileExistsMode(org.springframework.integration.file.support.FileExistsMode) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Matcher(java.util.regex.Matcher) Pollers(org.springframework.integration.dsl.Pollers) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) IntegrationFlows(org.springframework.integration.dsl.IntegrationFlows) Message(org.springframework.messaging.Message) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) ChannelSftp(com.jcraft.jsch.ChannelSftp) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) AbstractRemoteFileOutboundGateway(org.springframework.integration.file.remote.gateway.AbstractRemoteFileOutboundGateway) Test(org.junit.Test) EnableIntegration(org.springframework.integration.config.EnableIntegration) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) File(java.io.File) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SftpTestSupport(org.springframework.integration.sftp.SftpTestSupport) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) InputStream(java.io.InputStream) RemoteFileTemplate(org.springframework.integration.file.remote.RemoteFileTemplate) SftpRemoteFileTemplate(org.springframework.integration.sftp.session.SftpRemoteFileTemplate) IntegrationFlowRegistration(org.springframework.integration.dsl.context.IntegrationFlowContext.IntegrationFlowRegistration) IntegrationFlow(org.springframework.integration.dsl.IntegrationFlow) StandardIntegrationFlow(org.springframework.integration.dsl.StandardIntegrationFlow) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

RemoteFileTemplate (org.springframework.integration.file.remote.RemoteFileTemplate)11 Test (org.junit.Test)10 Matchers.containsString (org.hamcrest.Matchers.containsString)9 InputStream (java.io.InputStream)7 File (java.io.File)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 IOException (java.io.IOException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 List (java.util.List)4 Assert.assertThat (org.junit.Assert.assertThat)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2