INT-1614 refactored SftpInboundRemoteFileSystemSynchronizerTests, removed unnecessary test
This commit is contained in:
@@ -1,113 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.sftp.impl;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
||||
import com.jcraft.jsch.SftpATTRS;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SftpInboundRemoteFileSystemSynchronizerTests {
|
||||
@Before
|
||||
public void prepare() throws Exception{
|
||||
File foo = new File("target/foo.txt");
|
||||
if (foo.exists()){
|
||||
foo.delete();
|
||||
}
|
||||
File bar = new File("target/bar.txt");
|
||||
if (bar.exists()){
|
||||
bar.delete();
|
||||
}
|
||||
if (!foo.createNewFile()){
|
||||
throw new IOException("Can not create test file 'foo.txt' in the 'temp' directory");
|
||||
}
|
||||
foo.deleteOnExit();
|
||||
bar.deleteOnExit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that if local file with the name provided exists, then this method
|
||||
* will return 'true', although no copy will be performed. Just a pass through.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void testCopyAndRenameWhenLocalFileExists() throws Exception {
|
||||
SftpInboundFileSynchronizer synchronizer = new SftpInboundFileSynchronizer(mock(SessionFactory.class));
|
||||
Method method = ReflectionUtils.findMethod(synchronizer.getClass(),
|
||||
"copyFileToLocalDirectory", String.class, LsEntry.class, File.class, Session.class);
|
||||
method.setAccessible(true);
|
||||
Session session = mock(Session.class);
|
||||
LsEntry entry = mock(LsEntry.class);
|
||||
SftpATTRS attrs = mock(SftpATTRS.class);
|
||||
when(attrs.isDir()).thenReturn(false);
|
||||
when(attrs.isLink()).thenReturn(false);
|
||||
when(entry.getAttrs()).thenReturn(attrs);
|
||||
when(entry.getFilename()).thenReturn("foo.txt");
|
||||
File localDir = new File("target");
|
||||
Boolean success = (Boolean) method.invoke(synchronizer, "remoteDir", entry, localDir, session);
|
||||
assertTrue(success);
|
||||
}
|
||||
/**
|
||||
* Asserts that if local file with the name provided doesn't exists, then this method
|
||||
* will perfrom copy file and cleanup of the open resource INT-1433, meaning
|
||||
* it should not result in exception.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@org.junit.Ignore
|
||||
@Test
|
||||
public void testCopyAndRenameWhenLocalFileDoesntExist() throws Exception {
|
||||
SftpInboundFileSynchronizer synchronizer = new SftpInboundFileSynchronizer(mock(SessionFactory.class));
|
||||
Method method =
|
||||
ReflectionUtils.findMethod(synchronizer.getClass(), "copyFromRemoteToLocalDirectory", Session.class, LsEntry.class, Resource.class);
|
||||
method.setAccessible(true);
|
||||
Session session = mock(Session.class);
|
||||
ChannelSftp channelSftp = mock(ChannelSftp.class);
|
||||
File originalFile = new File("pom.xml");
|
||||
when(channelSftp.get("null/bar.txt")).thenReturn(new FileInputStream(originalFile));
|
||||
LsEntry entry = mock(LsEntry.class);
|
||||
when(entry.getFilename()).thenReturn("bar.txt");
|
||||
Resource localDir = new FileSystemResource(new File("target"));
|
||||
boolean success = (Boolean) method.invoke(synchronizer, session, entry, localDir);
|
||||
assertTrue(success);
|
||||
File file = new File("target/bar.txt");
|
||||
assertTrue(file.exists());
|
||||
assertTrue(file.length() == originalFile.length());
|
||||
}
|
||||
}
|
||||
@@ -16,26 +16,29 @@
|
||||
|
||||
package org.springframework.integration.sftp.inbound;
|
||||
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.integration.sftp.filters.SftpPatternMatchingFileListFilter;
|
||||
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
|
||||
import org.springframework.integration.sftp.session.SftpTestSessionFactory;
|
||||
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
||||
@@ -47,56 +50,83 @@ import com.jcraft.jsch.SftpATTRS;
|
||||
*/
|
||||
public class SftpInboundRemoteFileSystemSynchronizerTests {
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Test
|
||||
@Ignore
|
||||
public void testCopyFileToLocalDir() throws Exception {
|
||||
File file = new File(System.getProperty("java.io.tmpdir") + "/foo.txt");
|
||||
private static com.jcraft.jsch.Session jschSession = mock(com.jcraft.jsch.Session.class);
|
||||
|
||||
@After
|
||||
public void cleanup(){
|
||||
File file = new File("test");
|
||||
if (file.exists()){
|
||||
String[] files = file.list();
|
||||
for (String fileName : files) {
|
||||
new File(file, fileName).delete();
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
SessionFactory sessionFactory = mock(SessionFactory.class);
|
||||
SftpInboundFileSynchronizer syncronizer = new SftpInboundFileSynchronizer(sessionFactory);
|
||||
syncronizer.setRemoteDirectory("foo/bar");
|
||||
|
||||
FileListFilter filter = mock(FileListFilter.class);
|
||||
|
||||
syncronizer.setFilter(filter);
|
||||
|
||||
|
||||
Session sftpSession = mock(Session.class);
|
||||
|
||||
when(sessionFactory.getSession()).thenReturn(sftpSession);
|
||||
final ChannelSftp channel = mock(ChannelSftp.class);
|
||||
when(channel.get((String) Mockito.any())).thenReturn(new FileInputStream(new File("template.mf")));
|
||||
Vector<LsEntry> entries = new Vector<ChannelSftp.LsEntry>();
|
||||
LsEntry entry = mock(LsEntry.class);
|
||||
SftpATTRS attr = mock(SftpATTRS.class);
|
||||
when(attr.isDir()).thenReturn(false);
|
||||
when(attr.isLink()).thenReturn(false);
|
||||
when(entry.getFilename()).thenReturn("foo.txt");
|
||||
when(entry.getAttrs()).thenReturn(attr);
|
||||
entries.add(entry);
|
||||
when(channel.ls("foo/bar")).thenReturn(entries);
|
||||
when(filter.filterFiles((Object[]) Mockito.any())).thenReturn(entries);
|
||||
|
||||
when(sftpSession.get(Mockito.anyString())).thenAnswer(new Answer<InputStream>() {
|
||||
public InputStream answer(InvocationOnMock invocation)
|
||||
throws Throwable {
|
||||
String filePath = (String) invocation.getArguments()[0];
|
||||
return channel.get(filePath);
|
||||
}
|
||||
});
|
||||
|
||||
syncronizer.setDeleteRemoteFiles(true);
|
||||
syncronizer.afterPropertiesSet();
|
||||
|
||||
File localDirectory = new File(System.getProperty("java.io.tmpdir"));
|
||||
syncronizer.synchronizeToLocalDirectory(localDirectory);
|
||||
|
||||
verify(sessionFactory, times(1)).getSession();
|
||||
verify(attr, atLeast(1)).isDir();
|
||||
// will add more validation, but for now this test is mainly to get the test coverage up
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyFileToLocalDir() throws Exception {
|
||||
this.cleanup();
|
||||
File localDirectoy = new File("test");
|
||||
assertFalse(localDirectoy.exists());
|
||||
|
||||
TestSftpSessionFactory ftpSessionFactory = new TestSftpSessionFactory();
|
||||
ftpSessionFactory.setUser("kermit");
|
||||
ftpSessionFactory.setPassword("frog");
|
||||
ftpSessionFactory.setHost("foo.com");
|
||||
|
||||
SftpInboundFileSynchronizingMessageSource ms =
|
||||
new SftpInboundFileSynchronizingMessageSource();
|
||||
|
||||
SftpInboundFileSynchronizer synchronizer = spy(new SftpInboundFileSynchronizer(ftpSessionFactory));
|
||||
synchronizer.setDeleteRemoteFiles(true);
|
||||
synchronizer.setRemoteDirectory("remote-test-dir");
|
||||
synchronizer.setFilter(new SftpPatternMatchingFileListFilter(".*\\.test$"));
|
||||
|
||||
ms.setSynchronizer(synchronizer);
|
||||
ms.setAutoCreateDirectories(true);
|
||||
|
||||
ms.setLocalDirectory(localDirectoy);
|
||||
ms.afterPropertiesSet();
|
||||
Message<File> atestFile = ms.receive();
|
||||
assertNotNull(atestFile);
|
||||
assertEquals("a.test", atestFile.getPayload().getName());
|
||||
Message<File> btestFile = ms.receive();
|
||||
assertNotNull(btestFile);
|
||||
assertEquals("b.test", btestFile.getPayload().getName());
|
||||
Message<File> nothing = ms.receive();
|
||||
assertNull(nothing);
|
||||
|
||||
// two times becouse on teh third receive (above) the internal queue will be empty, so it will attempt
|
||||
verify(synchronizer, times(2)).synchronizeToLocalDirectory(localDirectoy);
|
||||
|
||||
assertTrue(new File("test/a.test").exists());
|
||||
assertTrue(new File("test/b.test").exists());
|
||||
}
|
||||
|
||||
public static class TestSftpSessionFactory extends DefaultSftpSessionFactory {
|
||||
|
||||
public Session getSession() {
|
||||
try {
|
||||
ChannelSftp channel = mock(ChannelSftp.class);
|
||||
|
||||
String[] files = new File("remote-test-dir").list();
|
||||
Vector<LsEntry> sftpEntries = new Vector<LsEntry>();
|
||||
for (String fileName : files) {
|
||||
LsEntry lsEntry = mock(LsEntry.class);
|
||||
SftpATTRS attributes = mock(SftpATTRS.class);
|
||||
when(lsEntry.getAttrs()).thenReturn(attributes);
|
||||
when(lsEntry.getFilename()).thenReturn(fileName);
|
||||
sftpEntries.add(lsEntry);
|
||||
when(channel.get("remote-test-dir/"+fileName)).thenReturn(new FileInputStream("remote-test-dir/" + fileName));
|
||||
}
|
||||
when(channel.ls("remote-test-dir")).thenReturn(sftpEntries);
|
||||
|
||||
when(jschSession.openChannel("sftp")).thenReturn(channel);
|
||||
return SftpTestSessionFactory.createSftpSession(jschSession);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to create mock sftp session", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.sftp.session;
|
||||
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class SftpTestSessionFactory {
|
||||
|
||||
public static Session createSftpSession(com.jcraft.jsch.Session jschSession){
|
||||
SftpSession sftpSession = new SftpSession(jschSession);
|
||||
sftpSession.connect();
|
||||
return sftpSession;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user