package io.irontest.core.runner;

import io.irontest.models.endpoint.Endpoint;
import io.irontest.models.endpoint.FTPEndpointProperties;
import io.irontest.models.teststep.APIRequest;
import io.irontest.models.teststep.FtpPutRequest;
import io.irontest.models.teststep.FtpPutRequestFileFromText;
import io.irontest.models.teststep.Teststep;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;

public class FTPTeststepRunner extends TeststepRunner {
    protected BasicTeststepRun run(Teststep teststep) throws Exception {
        BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
        Endpoint endpoint = teststep.getEndpoint();

        APIRequest apiRequest = teststep.getApiRequest();
        if (apiRequest instanceof FtpPutRequest) {
            put(endpoint, (FtpPutRequest) apiRequest);
        }

        return basicTeststepRun;
    }

    private void put(Endpoint endpoint, FtpPutRequest ftpPutRequest) throws IOException {
        String targetFilePath = StringUtils.trimToEmpty(ftpPutRequest.getTargetFilePath());
        byte[] fileBytes = null;

        //  validate arguments
        if ("".equals(targetFilePath)) {
            throw new IllegalArgumentException("Target File Path not specified.");
        }
        if (ftpPutRequest instanceof FtpPutRequestFileFromText) {
            FtpPutRequestFileFromText ftpPutRequestFileFromText = (FtpPutRequestFileFromText) ftpPutRequest;
            String fileContent = ftpPutRequestFileFromText.getFileContent();

            //  validate arguments
            if ("".equals(StringUtils.trimToEmpty(fileContent))) {
                throw new IllegalArgumentException("No file content.");
            }

            fileBytes = fileContent.getBytes();
        }

        FTPEndpointProperties endpointProperties = (FTPEndpointProperties) endpoint.getOtherProperties();
        String username = endpoint.getUsername() == null ? "anonymous" : endpoint.getUsername();
        String password = endpoint.getUsername() == null ?
                System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName() :
                endpoint.getPassword();
        FTPClient ftpClient = new FTPClient();
        ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        try {
            ftpClient.connect(endpointProperties.getHost(), endpointProperties.getPort());
            ftpClient.login(username, password);

            ftpClient.addProtocolCommandListener(new ProtocolCommandListener() {
                @Override
                public void protocolCommandSent(ProtocolCommandEvent event) {}

                @Override
                public void protocolReplyReceived(ProtocolCommandEvent event) {
                    if (FTPReply.isNegativePermanent(event.getReplyCode())) {
                        throw new RuntimeException("Failed to put the file. " + event.getMessage());
                    }
                }
            });
            ftpClient.storeFile(targetFilePath, new ByteArrayInputStream(fileBytes));
        } finally {
            ftpClient.disconnect();
        }
    }
}

@@ -2234,55 +2234,32 @@
 e = 
-endpoint.getUsername() == null ? %22anonymous%22 : 
+StringUtils.trimToEmpty(
 endp
@@ -2276,16 +2276,17 @@
 ername()
+)
 ;%0A      
@@ -2321,28 +2321,90 @@
 .get
-U
+Password();%0A        if (%22%22.equals(u
 sername
-() == null ?
+)) %7B%0A            username = %22anonymous%22;
 %0A   
@@ -2412,19 +2412,26 @@
         
-   
+password =
  System.
@@ -2507,50 +2507,19 @@
 me()
- :%0A                endpoint.getPassword();
+;%0A        %7D
 %0A   
@@ -2667,167 +2667,8 @@
 ));%0A
-        try %7B%0A            ftpClient.connect(endpointProperties.getHost(), endpointProperties.getPort());%0A            ftpClient.login(username, password);%0A%0A    
     
@@ -2744,36 +2744,32 @@
 ) %7B%0A            
-    
 @Override%0A      
@@ -2766,36 +2766,32 @@
 ide%0A            
-    
 public void prot
@@ -2842,36 +2842,32 @@
 %7B%7D%0A%0A            
-    
 @Override%0A      
@@ -2864,36 +2864,32 @@
 ide%0A            
-    
 public void prot
@@ -2948,28 +2948,24 @@
             
-    
 if (FTPReply
@@ -3022,36 +3022,32 @@
                 
-    
 throw new Runtim
@@ -3112,39 +3112,49 @@
 );%0A             
+   %7D%0A
        
+     
 %7D%0A              
@@ -3143,32 +3143,36 @@
   %7D%0A        
+%7D);%0A
         
 %7D%0A          
@@ -3151,33 +3151,37 @@
     %7D);%0A        
-%7D
+try %7B
 %0A            %7D);
@@ -3169,33 +3169,157 @@
 y %7B%0A            
-%7D
+ftpClient.connect(endpointProperties.getHost(), endpointProperties.getPort());%0A            ftpClient.login(username, password
 );%0A            f

add FTP test step