Search in sources :

Example 11 with RestClient

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

the class RestHelper method initializeRestClient.

/**
 * Create RestClient instance
 *
 * @param atsAgentIp
 *            the IP of the agent, on which we want to perform some
 *            monitoring operation
 * @param relativeRestUri
 *            the relative URI for the desired monitoring operation
 */
public void initializeRestClient(String atsAgentIp, String baseRestUri, String relativeRestUri) {
    // create RestClient instance
    this.restClient = new RestClient("http://" + atsAgentIp + baseRestUri + relativeRestUri);
    if (AtsSystemProperties.getPropertyAsBoolean(AtsSystemProperties.SYSTEM_MONITOR_VERBOSE_MODE, false)) {
        // enable all logging (both REST headers and body)
        this.restClient.setVerboseMode(RESTDebugLevel.ALL);
    } else {
        // disable any logging (both REST headers and body)
        this.restClient.setVerboseMode(RESTDebugLevel.NONE);
    }
    this.restClient.setRequestMediaType(RestMediaType.APPLICATION_JSON);
    this.restClient.setResponseMediaType(RestMediaType.APPLICATION_JSON);
    // set ATS_UID header
    this.uid = ExecutorUtils.getUUID();
    this.restClient.addRequestHeader(ApplicationContext.ATS_UID_SESSION_TOKEN, this.uid);
}
Also used : RestClient(com.axway.ats.action.rest.RestClient)

Example 12 with RestClient

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

the class RestOperationTests method get_usingRequestParams.

/**
 * A GET using some resource request(query) parameters.
 *
 * The application we work with returns a body with the request parameters we have sent
 */
@Test
public void get_usingRequestParams() {
    final String requestParam1Name = "from";
    final String requestParam1Value = "10";
    final String requestParam2Name = "to";
    final String requestParam2Value = "20";
    RestClient client = new RestClient(BASE_URI + "simplepage/query/");
    // add request parameters by specifying their name and value
    client.addRequestParameter(requestParam1Name, requestParam1Value);
    client.addRequestParameter(requestParam2Name, requestParam2Value);
    RestResponse response = client.get();
    assertEquals(response.getBodyAsString(), "You passed " + requestParam1Name + " " + requestParam1Value + " " + requestParam2Name + " " + requestParam2Value + " request params");
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) Test(org.testng.annotations.Test)

Example 13 with RestClient

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

the class RestOperationTests method get_downloadFileAsStream.

/**
 * A GET which returns a stream with data.
 *
 * This particular body is small, so we can save the content into a String,
 * but usual case would be to stream it on the file system
 */
@Test
public void get_downloadFileAsStream() throws IOException {
    RestClient client = new RestClient(BASE_URI + "simplepage/download");
    client.setResponseMediaType(RestMediaType.MULTIPART_FORM_DATA);
    RestResponse response = client.get();
    response.verifyStatusCode(200);
    // check the value of the content disposition header
    RestHeader contentDispositionHeader = response.getHeader("Content-Disposition");
    assertNotNull(contentDispositionHeader);
    assertTrue(contentDispositionHeader.getValue().startsWith("attachment"));
    // Read the returned body as an stream
    // In the usual case this could be a significant amount of data
    InputStream responseStream = response.getBodyAsInputStream();
    // The following method reads into a String and closes the stream
    // Note: this method is not a public one. It can be change at any moment without notice
    String fileContent = IoUtils.streamToString(responseStream);
    assertTrue(fileContent.contains("file line 1"));
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) InputStream(java.io.InputStream) RestClient(com.axway.ats.action.rest.RestClient) RestHeader(com.axway.ats.action.rest.RestHeader) Test(org.testng.annotations.Test)

Example 14 with RestClient

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

the class RestOperationTests method delete_usingRequestParam.

/**
 * A DELETE using request parameters.
 *
 * Our server expects the the last name of the person to be deleted.
 */
@Test
public void delete_usingRequestParam() {
    createPersonToManipulateInTheTest();
    final String lastNameParam = "lastName";
    final String lastNameParamValue = "Norris";
    RestClient client = new RestClient(BASE_URI + "peoplepage/delete");
    // add the request parameter
    client.addRequestParameter(lastNameParam, lastNameParamValue);
    RestResponse response = client.delete();
    assertEquals(response.getStatusCode(), 200);
    assertTrue(response.getBodyAsString().startsWith("Deleted person with last name " + lastNameParamValue));
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) Test(org.testng.annotations.Test)

Example 15 with RestClient

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

the class RestOperationTests method put_usingFormParameters.

/**
 * A PUT which sends form parameters.
 *
 * The PUT is usually used to modify an existing resource
 * Thus this test first issues a POST to create the resource which will
 * be modified(updated) by the following PUT
 */
@Test
public void put_usingFormParameters() {
    createPersonToManipulateInTheTest();
    // update the already existing person using PUT
    RestClient client = new RestClient(BASE_URI + "peoplepage/put");
    RestResponse response = client.putForm(new RestForm().addParameter("firstName", "Chuck").addParameter("lastName", "Norris").addParameter("age", "71"));
    assertEquals(response.getStatusCode(), 200);
    // verify Chuck Norris grows up as expected
    assertTrue(response.getBodyAsString().startsWith("Chuck Norris was 70 years old, but now is 71 years old"));
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) RestForm(com.axway.ats.action.rest.RestForm) 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