Search in sources :

Example 11 with RestResponse

use of com.axway.ats.action.rest.RestResponse 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 12 with RestResponse

use of com.axway.ats.action.rest.RestResponse 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 13 with RestResponse

use of com.axway.ats.action.rest.RestResponse 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 14 with RestResponse

use of com.axway.ats.action.rest.RestResponse 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)

Example 15 with RestResponse

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

the class RestOperationTests method get_jsonToJavaObject.

/**
 * A GET which returns some JSON body.
 *
 * We serialize the response body to a java instance and use its particular class methods to retrieve the needed data
 */
@Test
public void get_jsonToJavaObject() {
    createPersonToManipulateInTheTest();
    RestClient client = new RestClient(BASE_URI + "peoplepage/get_jsonPojo");
    client.addResourcePath("Chuck");
    client.setResponseMediaType(RestMediaType.APPLICATION_JSON);
    RestResponse response = client.get();
    // get the body as a java object
    PersonPojo person = response.getBodyAsObject(PersonPojo.class);
    // now call the different java methods to retrieve the needed data
    assertEquals(person.getFirstName(), "Chuck");
    assertEquals(person.getLastName(), "Norris");
    assertEquals(person.getAge(), 70);
    log.info("Got back a java object which is: " + person.toString());
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) PersonPojo(com.axway.ats.examples.basic.http.testbeans.PersonPojo) Test(org.testng.annotations.Test)

Aggregations

RestResponse (com.axway.ats.action.rest.RestResponse)18 RestClient (com.axway.ats.action.rest.RestClient)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 MonitoringException (com.axway.ats.core.monitoring.MonitoringException)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