package org.openslx.imagemaster; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; import org.openslx.imagemaster.thrift.iface.ImageServer.Client; import org.openslx.imagemaster.thrift.iface.SessionData; import org.openslx.imagemaster.thrift.iface.UserInfo; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } /** * Test the authentication */ public void testAuthentication() { TTransport transport = new TSocket("localhost", 9090); try { transport.open(); } catch (TTransportException e) { assertTrue("Could not connect", false); } TProtocol protocol = new TBinaryProtocol(transport); Client client = new Client(protocol); try { assertTrue("Could not ping server", client.ping()); } catch (TException e) { assertTrue("Could not ping server", false); } try { SessionData sessionData = client.authenticate("ns202", "xxxxxxxxxxxx"); UserInfo userInfo = client.getUserFromToken(sessionData.getAuthToken()); System.out.println("User info: " + userInfo); System.out.println("Server address from MySQL: " + sessionData.serverAddress); } catch (TException e) { e.printStackTrace(); assertTrue("Could not login", false); } } /** * Test FTP connection */ public void testFtpConnection() { FTPClient client = new FTPClient(); String host = "localhost"; int port = 2221; String user = "admin"; String password = "SI*HoZCC!]V)p>B2"; String fileName = "/home/nils/file_to_upload.bin"; try { client.connect(host, port); System.out.println("Connected to " + host + ":" + port + ". Reply code: " + client.getReplyCode()); if ( !FTPReply.isPositiveCompletion( client.getReplyCode() ) ) { ConnectException ce = new ConnectException("No positive reply code."); throw ce; } if ( !client.login(user, password) ) { ConnectException ce = new ConnectException("Could not login."); throw ce; } System.out.println("Logged in with user: " + user); client.setFileType(FTP.BINARY_FILE_TYPE); client.enterLocalPassiveMode(); System.out.println("Entered PASSIVE MODE"); InputStream input = new FileInputStream(fileName); client.makeDirectory("myFolder"); System.out.println("Made directory 'myFolder'"); System.out.print("Starting file upload ... "); client.storeFile("myFolder/myFile.txt", input); System.out.println("done."); client.noop(); } catch (IOException e) { e.printStackTrace(); } finally { if (client.isConnected()) { try { client.logout(); client.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } }