Search in sources :

Example 1 with RestClient

use of com.axway.ats.action.rest.RestClient in project ats-framework by Axway.

the class BaseTestClass method beforeSuite.

@BeforeSuite
public void beforeSuite() {
    boolean hasServiceException = false;
    // quick sanity check if all servers are working properly
    log.info("Running SANITY check for Example servers...");
    // check agents
    log.info("ATS Agents sanity check START");
    try {
        String agent1AtsVersion = new SystemOperations(configuration.getAgent1Address()).getAtsVersion();
        log.info("Agent 1 is working and its version is: " + agent1AtsVersion);
        String agent2AtsVersion = new SystemOperations(configuration.getAgent1Address()).getAtsVersion();
        log.info("Agent 2 is working and its version is: " + agent2AtsVersion);
    } catch (Exception e) {
        hasServiceException = true;
        log.error(e);
    }
    log.info("ATS Agents sanity check END");
    log.info("HTTP Server sanity check START");
    // check HTTP server
    RestClient restClient = null;
    try {
        restClient = new RestClient("http://" + configuration.getServerIp() + ":" + configuration.getHttpServerPort());
        RestResponse rr = restClient.get();
        if (rr.getStatusCode() != 200) {
            throw new RuntimeException("HTTP Server server maybe in trouble or not started properly. Status of HTTP sanity check response is :\n\tStatus Message: " + rr.getStatusMessage() + "\n\tBody: " + rr.getBodyAsString());
        } else {
            log.info("HTTP server working");
        }
    } catch (Exception e) {
        hasServiceException = true;
        log.error(e);
    } finally {
        if (restClient != null) {
            restClient.disconnect();
            restClient = null;
        }
    }
    log.info("HTTP Server sanity check END");
    log.info("FTP/FTPS Server sanity check START");
    // check FTP/FTPS server (TODO)
    FtpClient ftpClient = null;
    try {
        ftpClient = new FtpClient();
        ftpClient.setCustomPort(configuration.getFtpServerPort());
        ftpClient.connect(configuration.getServerIp(), configuration.getUserName(), configuration.getUserPassword());
    } catch (Exception e) {
        hasServiceException = true;
        log.error(e);
    } finally {
        if (ftpClient != null) {
            ftpClient.disconnect();
            ftpClient = null;
        }
    }
    log.info("FTP/FTPS Server sanity check END");
    log.info("DONE running SANITY check for Examples servers");
    if (hasServiceException) {
        throw new RuntimeException("Not all servers started or working properly. Did you run 'Start servers' from the Desktop directory?");
    }
}
Also used : SystemOperations(com.axway.ats.action.system.SystemOperations) RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) FtpClient(com.axway.ats.core.filetransfer.FtpClient) IOException(java.io.IOException) BeforeSuite(org.testng.annotations.BeforeSuite)

Example 2 with RestClient

use of com.axway.ats.action.rest.RestClient in project ats-framework by Axway.

the class HttpBaseClass method beforeMethod.

// servlet name
/**
 * Do some clean up, so the tested application is in the same good
 * initial state to guarantee each test starts on clean.
 *
 * It is true that not all tests here need this cleanup, but it will not hurt anyway.
 */
@BeforeMethod
public void beforeMethod() {
    // some of the tests in this class add users on the remote server
    // so we clean them up here
    RestClient client = new RestClient(BASE_URI + "peoplepage/deleteAllPeople");
    RestResponse response = client.delete();
    assertEquals(response.getStatusCode(), 200);
    assertEquals(response.getBodyAsString(), "Nobody is left in the community");
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with RestClient

use of com.axway.ats.action.rest.RestClient in project ats-framework by Axway.

the class HttpBaseClass method createPersonToManipulateInTheTest.

protected void createPersonToManipulateInTheTest() {
    // create a new person using POST
    RestClient client = new RestClient(BASE_URI + "peoplepage/post");
    RestResponse response = client.postForm(new RestForm().addParameter("firstName", "Chuck").addParameter("lastName", "Norris").addParameter("age", "70"));
    assertEquals(response.getStatusCode(), 201);
    assertTrue(response.getBodyAsString().startsWith("Saved Person with name Chuck Norris at 70 years"));
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) RestForm(com.axway.ats.action.rest.RestForm)

Example 4 with RestClient

use of com.axway.ats.action.rest.RestClient in project ats-framework by Axway.

the class RestOperationTests method get.

/**
 * A simple GET which returns some textual body
 */
@Test
public void get() {
    // create an instance of the RestClient by providing the URI to work with
    RestClient client = new RestClient(BASE_URI + "simplepage/");
    // Optionally set the expected media type of the response.
    // If receive response with different media type, the returned status code will be "406 NOT ACCEPTABLE".
    // You can try this by specifying a different type
    client.setResponseMediaType(RestMediaType.TEXT_HTML);
    // execute the HTTP method - in this case a GET
    // each HTTP method returns a response object
    RestResponse response = client.get();
    // verify the response status in any of the following ways
    assertEquals(response.getStatusCode(), 200);
    response.verifyStatusCode(200);
    assertEquals(response.getStatusMessage(), "OK");
    response.verifyStatusMessage("OK");
    // this is how you can read the response headers
    log.info("We received the following response headers:");
    for (RestHeader header : response.getHeaders()) {
        log.info(header.getKey() + ":" + header.getValue());
    }
    // there are different ways to get the body, here we get it as a simple text
    String responseBody = response.getBodyAsString();
    assertEquals(responseBody, "Some simple page response");
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) RestHeader(com.axway.ats.action.rest.RestHeader) Test(org.testng.annotations.Test)

Example 5 with RestClient

use of com.axway.ats.action.rest.RestClient in project ats-framework by Axway.

the class RestOperationTests method get_usingHeader.

/**
 * A GET using some request header.
 *
 * The application we work with returns a body with the header we have sent
 */
@Test
public void get_usingHeader() {
    final String header1 = "value1";
    RestClient client = new RestClient(BASE_URI + "simplepage/header");
    // add some important header(s)
    client.addRequestHeader("header1", header1);
    RestResponse response = client.get();
    assertEquals(response.getBodyAsString(), "You passed header with name header1 and value " + header1);
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) Test(org.testng.annotations.Test)

Aggregations

RestClient (com.axway.ats.action.rest.RestClient)17 RestResponse (com.axway.ats.action.rest.RestResponse)16 Test (org.testng.annotations.Test)13 RestForm (com.axway.ats.action.rest.RestForm)3 RestHeader (com.axway.ats.action.rest.RestHeader)2 JsonText (com.axway.ats.action.json.JsonText)1 SystemOperations (com.axway.ats.action.system.SystemOperations)1 XmlText (com.axway.ats.action.xml.XmlText)1 FtpClient (com.axway.ats.core.filetransfer.FtpClient)1 PersonPojo (com.axway.ats.examples.basic.http.testbeans.PersonPojo)1 PersonXmlBean (com.axway.ats.examples.basic.http.testbeans.PersonXmlBean)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 BeforeSuite (org.testng.annotations.BeforeSuite)1