This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Writing a Test Case for FTP Server

This page explains how to write a test case for an FTP server using the Test Automation Framework.

Objective:

Our objective in this test case is to illustrate how we can use the FTP server as a VFS transport provider to the listener proxy service. We will use VFS transport using FTP server and use the ESB VFS transport listener proxy to verify the functionalities related to the FTP server.

The main steps in the test scenario are as follows:

  1. Start-up the WSO2 ESB server and change the VFS transport configurations in the axis2.xml (enable VFS transport).
  2. Restart WSO2 ESB server gracefully.

  3. Start-up the FTP server programmatically.

  4. Copy a file that contains a SOAP message to the FTP server folder.

  5. Add VFS transport listener proxy to WSO2 ESB.

  6. Check the existence of the message file in the FTP server.

Prerequisites:

Start-up the axis2 server and deploy the service to accept messages sent by the ESB proxy service. More details on starting the axis2 server can be found at Writing a Test Case for Axis2 Server.

package org.wso2.carbon.esb.FTPTest;

 import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.api.clients.proxy.admin.ProxyServiceAdminClient;
import org.wso2.carbon.automation.engine.context.AutomationContext;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.automation.engine.frameworkutils.FrameworkPathUtil;
import org.wso2.carbon.automation.extensions.servers.ftpserver.FTPServerManager;
import org.wso2.carbon.automation.test.utils.server.ServerConfigurationManager;
import org.wso2.carbon.proxyadmin.stub.ProxyServiceAdminProxyAdminException;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

 public class FTPTestCase {
 private FTPServerManager ftpServerManager;
 private ProxyServiceAdminClient proxyServiceAdminClient;
 private String FTPUsername;
 private String FTPPassword;
 private File inputFolder;
 private File outputFolder;
 private File FTPFolder;

  @BeforeClass(alwaysRun = true)
 public void runFTPServer() throws Exception {

      //username password for the FTP server to be started
     FTPUsername = "abc";
     FTPPassword = "123";
     String inputFolderName = "in";
     String outputFolderName = "out";
              int FTPPort=8085; 
       
              //create atuomationContext instance with ESB product and super tenant admin user
     AutomationContext automationContext = new AutomationContext("ESB",    TestUserMode.SUPER_TENANT_ADMIN);

      //local folder of the FTP server root
     FTPFolder = new File(System.getProperty("basedir") + File.separator + "src" + File.separator + "test" +
             File.separator + "resources" + File.separator + "artifacts" + File.separator + "ESB" + File.separator +
             "synapseconfig" + File.separator + "vfsTransport" + File.separator + "FTP_Location");

               //create FTP server root folder and subfolders
     if (!FTPFolder.exists()) {
         FTPFolder.mkdir();
     }
     inputFolder = new File(FTPFolder.getAbsolutePath() + File.separator + inputFolderName);
     outputFolder = new File(FTPFolder.getAbsolutePath() + File.separator + outputFolderName);
     if (inputFolder.exists()) {
         inputFolder.delete();
     }
     if (outputFolder.exists()) {
         outputFolder.delete();
     }
     inputFolder.mkdir();
     outputFolder.mkdir();

 
               //start-up FTP server
     ftpServerManager = new FTPServerManager(FTPPort, FTPFolder.getAbsolutePath(), FTPUsername, FTPPassword);
     ftpServerManager.startFtpServer();

 
      //replace the axis2.xml enabled vfs transfer and restart the ESB server gracefully
     ServerConfigurationManager serverConfigurationManager = new ServerConfigurationManager
             (automationContext.getContextUrls().getBackEndUrl());
     serverConfigurationManager.applyConfiguration(new File(FrameworkPathUtil.getSystemResourceLocation() +
             "artifacts" + File.separator + "ESB" + File.separator
             + "synapseconfig" + File.separator + "vfsTransport" + File.separator + "axis2.xml"));
     }

  @Test(groups = "wso2.esb", description = "Test")
 public void test() throws XMLStreamException, ProxyServiceAdminProxyAdminException, IOException, InterruptedException {

                //copy SOAP message  into the FTP server
     String sentMessageFile = "test.xml";
     File sourceMessage = new File(FTPFolder.getParent() + File.separator + sentMessageFile);
     File destinationMessage = new File(inputFolder + File.separator + sentMessageFile);
         copyFile(sourceMessage, destinationMessage);
           
              //create VFS transport listener proxy
     proxyServiceAdminClient = new ProxyServiceAdminClient
             (automationContext.getContextUrls().getBackEndUrl(), automationContext.login());


              String proxy = [ Source code of the proxy service has been provided below ]
         OMElement proxyOM = AXIOMUtil.stringToOM(proxy);

      //add the listener  proxy to ESB server
     proxyServiceAdminClient.addProxyService(proxyOM);
     Thread.sleep(30000);

      //check whether the added message file is deleted successfully
     Assert.assertEquals(destinationMessage.exists(), false);
 }

  @AfterClass(alwaysRun = true)
 public void stopFTPServer() throws InterruptedException {
     ftpServerManager.stop();
     log.info("FTP Server stopped successfully");
 }

 
  /**
  * Copy the given source file to the given destination
  * @param sourceFile source file
  * @param destFile   destination file
  * @throws IOException
  */
 public static void copyFile(File sourceFile, File destFile) throws IOException {
     if (!destFile.exists()) {
         destFile.createNewFile();
     }
     FileChannel source = null;
     FileChannel destination = null;
     try {
               source = new FileInputStream(sourceFile).getChannel();
               destination = new FileOutputStream(destFile).getChannel();
               destination.transferFrom(source, 0, source.size());
     } finally {
               if (source != null) {
                        source.close();
               }
               if (destination != null) {
                          destination.close();
                }
               }
         }
} 

VFS transport listener proxy:

<proxy xmlns="http://ws.apache.org/ns/synapse"
    name="StockQuoteProxy"
    transports="vfs"
    statistics="disable"
    trace="disable"
    startOnLoad="true">
   <target>
   <outSequence>
      <property name="transport.vfs.ReplyFileName"
                expression="fn:concat(fn:substring-after(get-property('MessageID'), 'urn:uuid:'), '.xml')"
                scope="transport"/>
      <property name="OUT_ONLY" value="true"/>
      <send>
         <endpoint>
            <address uri="vfs:file://[ output folder ]"/>
         </endpoint>
      </send>
   </outSequence>
   <endpoint>
      <address uri="[ endpoint address of the axis2 service of which the message to be sent ]"
               format="soap12"/>
   </endpoint>
   </target>
   <publishWSDL uri="file:repository/samples/resources/proxy/sample_proxy_1.wsdl"/>
   <parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
   <parameter name="transport.PollInterval">15</parameter>
   <parameter name="transport.vfs.MoveAfterProcess">file://[ output folder ]</parameter>
   <parameter name="transport.vfs.FileURI">vfs:ftp://[ FTP username ]:[ FTP password ]@localhost:8085/[ input folder ]/?vfs.passive=true</parameter>
   <parameter name="transport.vfs.MoveAfterFailure">file://[ output folder ]</parameter>
   <parameter name="transport.vfs.FileNamePattern">.*\.xml</parameter>
   <parameter name="transport.vfs.ContentType">text/xml</parameter>
   <parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
   <description/>
</proxy> 

Replace the variables in the proxy service with the following with reference to the variables in the code of the FTPTestCase class:

  1. Output folder - outputFolder.getAbsolutePath()

  2. Input folder - inputFolderName

  3. FTP username - FTPUsername

  4. FTP password - FTPPassword

  5. Endpoint address of the axis2 service of which the message is to be sent - http://localhost:9000/services/SimpleStockQuoteService