ATS : Quick test example

Please read this example carefully. There are a lot of comments which will help you get a good idea on what it is doing.

Note that some important topics are following this test code.

    import com.axway.ats.action.filesystem.FileSystemOperations;
    import com.axway.ats.action.filetransfer.FileTransferClient;
    import com.axway.ats.action.rest.RestClient;
    import com.axway.ats.action.rest.RestMediaType;
    import com.axway.ats.action.rest.RestResponse;
    import com.axway.ats.common.filetransfer.TransferProtocol;

    /**
     * 1. Configuration - Get the remote home folder of a user using a REST call
     * 
     * 2. Action - Send a file using this user's credentials
     * 
     * 3. Verification - Make sure the file was received in the user's home folder
     */
    @Test
    public void getUserHomeFolderAndSendFileOverFtp() {

        // 1. Configuration
        // Instantiate the Rest client by providing the remote URL
        RestClient restClient = new RestClient( SERVER_BASE_REST_URL + "demo/gethome" );

        // Issue a GET command, provide user first and last name.
        // If user exists, the server will respond back with status 200 and body containing the user's home folder in JSON content
        RestResponse restResponse = restClient.addRequestParameter( "firstName", "Jackie" )
                                              .addRequestParameter( "lastName", "Chan" )
                                              .setResponseMediaType( RestMediaType.APPLICATION_JSON )
                                              .get();
        // verify we received the expected status code
        assertEquals( 200, restResponse.getStatusCode() );

        // extract the user's home folder
        String userHome = restResponse.getBodyAsJson().getString( "home" );
        assertNotNull( userHome );

        // 2. Action
        // Instantiate the File Transfer client by providing protocol, remote IP, user name and password
        FileTransferClient transferClient = new FileTransferClient( TransferProtocol.FTP );
        transferClient.connect( SERVER_IP, "jchan", "password" );

        // upload a local file to the remote root folder of our user
        transferClient.uploadFile( LOCAL_FILE_LOCATION + FILE_NAME, "/" );

        // disconnect the File Transfer client
        transferClient.disconnect();

        // 3. Verification
        // Instantiate the File System Operations client by providing the remote ATS Agent address
        FileSystemOperations fileSystemClient = new FileSystemOperations( ATS_AGENT_ON_SERVER );

        // Check the existence of the expected remote file
        boolean fileExists = fileSystemClient.doesFileExist( userHome + FILE_NAME );
        assertTrue( fileExists, "File " + FILE_NAME + " was not found in the user's home folder" );
    }



What is this test doing?

As you can see this test contains three parts:

  1. Issue a REST GET call to our server(also called Tested Application).
    For the provided first and last user names, the server responds back with some JSON body which contains the home of the user.
  2. We establish a FTP connection to our server and upload a local file to it.
  3. We do not believe our server(otherwise we wouldn't test it), so we want to verify it has saved the uploaded file into the user's home folder.



What are the used ATS classes?

RestClient is the class capable of running REST calls. Beside the mandatory URL, you can specify headers, query parameters, content type etc.

After running the HTTP method(in our example we do a GET) you always get a RestResponse which allows you to inspect the response. You can check the status code or response headers. In our case we also need to extract data from the response body and since this is a JSON body, we specify the path (the "home" key) to the needed data. This data is used at the end of the scenario.

The FileTransferClient is a simple client which supports uploads and downloads over variety of transfer protocols.

FileSystemOperations allows doing different tasks on the file system. In our case we simply check whether some file(the uploaded one) is present in some folder on the server side. Since the server side is a remote host, we need to provide in the constructor the address of the remote ATS agent(TODO: make it a link). The ATS agent is a stand alone ATS application which allows our test example to do file operations on a remote host.

Test constants

To make the test running, you will need to assign valid values to all used constants



Why is the example broken into three parts?

We think that most test scenarios can be broken into three significant parts or phases:

  1. Configuration .
    These are the preparation steps to make the environment appropriate for having the test run as expected.
  2. Action .
    These are the actual test steps, they materialize the meaning of the test. This is the core of the test code.
  3. Verification .
    This is a very important part which is often underestimated. It is used to assure the action phase passed as expected or not. Here you will define whether we got a success or failure.
    Imagine in our example we are able to send the file over FTP without errors and then we conclude the upload was successful, so the test pass. What if the server consumed the file and did not save it in the user's home folder?
    For such cases we can make a separate check without relying on the server opinion. We do it in the verification phase.



What if something go wrong?

This test will fail if some error happen during the step executions. For example if provide wrong username, the file transfer step will throw a java exception containing the failure reason.

Another failure option is if some of the asserts in the code are not satisfied, in such case again a java exception will be thrown explaining difference between the expected and actually received values.



Back to parent page

Go to Table of Contents