Created
January 25, 2017 17:47
-
-
Save eduardomoroni/c2e3ae1b71b7788955d25a606b80752a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile("commons-net:commons-net:3.5") | |
// testCompile("org.mockftpserver:MockFtpServer:2.7.1") | |
import org.apache.commons.net.ftp.FTPClient; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
import java.io.IOException; | |
import static org.mockito.Matchers.any; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.verify; | |
import static org.mockito.Mockito.when; | |
import static org.mockito.internal.verification.VerificationModeFactory.times; | |
public class VibesClientTest { | |
@Rule | |
public TemporaryFolder folder = new TemporaryFolder(); | |
private VibesClient vibesClient; | |
private VibesCredential vibesCredential; | |
private FTPClient ftpClient; | |
@Before | |
public void setUp() throws Exception { | |
vibesCredential = mock(VibesCredential.class); | |
ftpClient = mock(FTPClient.class); | |
vibesClient = new VibesClient(vibesCredential, ftpClient); | |
} | |
@Test(expected = VibesException.class) | |
public void shouldThrowExceptionWhenFailedToStore() throws Exception { | |
when(ftpClient.login(any(), any())).thenReturn(true); | |
when(ftpClient.storeFileStream(any())).thenThrow(new IOException()); | |
vibesClient.sendFile(folder.newFile("testFile")); | |
verify(ftpClient, times(1)).storeFile(any(), any()); | |
verify(ftpClient, times(1)).login(any(), any()); | |
verify(ftpClient, times(1)).logout(); | |
} | |
} | |
import org.apache.commons.net.ftp.FTP; | |
import org.apache.commons.net.ftp.FTPClient; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.time.Duration; | |
import java.time.LocalDateTime; | |
import static java.lang.String.format; | |
public class VibesClient { | |
private static final Logger LOGGER = LoggerFactory.getLogger(VibesClient.class); | |
private static final int ONE_KILOBYTE = 1024; | |
private static final int ONE_MEGABYTE = 1024*ONE_KILOBYTE; | |
private LocalDateTime lastLogTime = LocalDateTime.now(); | |
private String hostname; | |
private String username; | |
private String password; | |
private int port; | |
private FTPClient client = new FTPClient(); | |
public VibesClient (VibesCredential credential){ | |
this.hostname = credential.getHostname(); | |
this.username = credential.getUsername(); | |
this.password = credential.getPassword(); | |
this.port = credential.getPort(); | |
} | |
public VibesClient(VibesCredential credential, FTPClient client){ | |
this(credential); | |
this.client = client; | |
} | |
public void sendFile(File file) throws IOException { | |
LOGGER.info(format("Sending file %s.", file.getName())); | |
try { | |
login(); | |
storeFile(file); | |
} catch (IOException e) { | |
String errorMessage = format("Error happened on uploading for %s: %s.", username, client.getReplyString()); | |
LOGGER.error(errorMessage); | |
throw new VibesException(errorMessage); | |
} finally { | |
client.logout(); | |
} | |
} | |
private void storeFile(File file) throws IOException { | |
LOGGER.info(String.format("Started sending file %.4f MB.", (file.length() / (float) ONE_MEGABYTE))); | |
LocalDateTime startTime = LocalDateTime.now(); | |
byte[] bytesIn = new byte[ONE_KILOBYTE]; | |
client.setBufferSize(ONE_MEGABYTE); | |
client.setFileType(FTP.ASCII_FILE_TYPE); | |
int read; | |
InputStream inputStream = new FileInputStream(file); | |
OutputStream outputStream = client.storeFileStream(file.getName()); | |
while((read = inputStream.read(bytesIn)) != -1) { | |
outputStream.write(bytesIn, 0, read); | |
timeTrackingLog(LocalDateTime.now()); | |
} | |
inputStream.close(); | |
outputStream.close(); | |
LOGGER.info(String.format("File sent, time elapsed %d seconds.", | |
Duration.between(startTime, LocalDateTime.now()).getSeconds())); | |
LOGGER.info(String.format("Server reply: %s.", client.getReplyString())); | |
} | |
private void timeTrackingLog(LocalDateTime currentTime) { | |
long seconds = Duration.between(lastLogTime, currentTime).getSeconds(); | |
if(seconds > 30){ | |
LOGGER.info("Still sending file..."); | |
lastLogTime = currentTime; | |
} | |
} | |
private void login() throws IOException { | |
client.enterLocalPassiveMode(); | |
client.enterRemotePassiveMode(); | |
boolean loggedIn = client.login(username, password); | |
String serverReply = client.getReplyString(); | |
if (!loggedIn){ | |
LOGGER.error(format("Error happened while logging in to %s: %s.", username, serverReply)); | |
throw new VibesException(format("%s: %s.", username, serverReply)); | |
} else { | |
LOGGER.info(format("Logged in successfully: %s.", serverReply)); | |
} | |
} | |
public void connect() throws IOException { | |
LOGGER.info(format("Connecting to %s with %s as username.", hostname, username)); | |
if (port == 0){ | |
client.connect(hostname); | |
} else { | |
client.connect(hostname,port); | |
} | |
LOGGER.info(format("Server reply: %s.", client.getReplyString())); | |
} | |
public void disconnect() throws IOException { | |
client.disconnect(); | |
LOGGER.info(format("Closing connection with %s.", hostname)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment