package ua.pp.bizon.cripto.acceptance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ua.pp.bizon.cripto.authorization.Credentials;
import ua.pp.bizon.cripto.configuration.Configuration;
import ua.pp.bizon.cripto.file.FileFactory;
import ua.pp.bizon.cripto.file.IPath;
import ua.pp.bizon.cripto.keystore.CryptoUtil;
import ua.pp.bizon.cripto.keystore.CryptoUtil.Direction;
import ua.pp.bizon.cripto.utils.FtpConfig;
public class LocalToFtpCodeAcceptanceTest {
private final Log LOG = LogFactory.getLog(getClass());
private static final String HOST = "germany";
private static final String prefix = "src/test/resources/";
private Credentials credentials;
@Before
public void setUp() throws Exception {
credentials = new Credentials();
credentials.setUsername("");
credentials.setHashedPassword(CryptoUtil.cryptPassword("123".toCharArray()));
}
@After
public void tearDown() throws Exception {
}
@Test
public void testFile() throws Exception {
LOG.info("*****test file started******");
IPath from = FileFactory.getLocalFile(prefix + "1.pdf");
FtpConfig readConfig = FtpConfig.readConfig(HOST);
IPath to = FileFactory.getRemoteFile(readConfig.getUrl(), readConfig.getLogin(), readConfig.getPass(), "backup/1/1.pdf");
CryptoUtil cryptoUtil = new CryptoUtil(credentials);
Configuration c = new Configuration();
c.setDirection(Direction.ENCODE);
c.setFrom(from);
c.setTo(to);
cryptoUtil.process(c);
LOG.info("*****write to server done******");
IPath response = FileFactory.getLocalFile(prefix + "2.pdf");
c.setDirection(Direction.DECODE);
c.setFrom(to);
c.setTo(response);
cryptoUtil.process(c);
LOG.info("*****load from server done******");
assertTrue(FileUtils.contentEquals(new File(from.getPath()), new File(response.getPath())));
LOG.info("*****assert files done******");
new File(prefix + "2.pdf").delete();
FTPClient client = new FTPClient();
client.connect(readConfig.getUrl());
client.login(readConfig.getLogin(), readConfig.getPass());
client.deleteFile("~/backup/1/1.pdf");
client.disconnect();
LOG.info("*****clean directories done******\n");
}
@Test
public void testDirectory() throws Exception {
LOG.info("*****test directory started******");
IPath from = FileFactory.getLocalFile(prefix + "directory");
FtpConfig readConfig = FtpConfig.readConfig(HOST);
IPath to = FileFactory.getRemoteFile(readConfig.getUrl(), readConfig.getLogin(), readConfig.getPass(), "backup/2/");
CryptoUtil cryptoUtil = new CryptoUtil(credentials);
Configuration c = new Configuration();
c.setDirection(Direction.ENCODE);
c.setFrom(from);
c.setTo(to);
cryptoUtil.process(c);
LOG.info("*****write to server done******");
IPath response = FileFactory.getLocalFile(prefix + "responce");
c.setDirection(Direction.DECODE);
c.setFrom(to);
c.setTo(response);
cryptoUtil.process(c);
LOG.info("*****load from server done******");
assertDirectories(new File(from.getPath()), new File(response.getPath()));
LOG.info("*****assert directories done******");
FileUtils.deleteDirectory(new File(response.getPath()));
assertTrue(to.delete());
LOG.info("*****clean directories done******\n");
}
private boolean assertDirectories(File file, File file2) throws IOException {
assertTrue(file.isDirectory());
assertTrue(file2.isDirectory());
File[] ch1 = file.listFiles();
File[] ch2 = file.listFiles();
assertEquals(ch1.length, ch2.length);
for (int i = 0; i < ch1.length; i++) {
if (ch1[i].isFile()) {
assertEquals(ch1[i].getName(), ch2[i].getName());
assertTrue(FileUtils.contentEquals(ch1[i], ch2[i]));
} else {
assertTrue(assertDirectories(ch1[i], ch2[i]));
}
}
return true;
}
}