Search in sources :

Example 16 with RestResponse

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

the class RestOperationTests method post_usingFormParameters.

/**
 * A POST which sends form parameters.
 *
 * In the particular case on the server side a new person will be added to the list
 * of known persons.
 * It returns info about the newly added person.
 */
@Test
public void post_usingFormParameters() {
    RestClient client = new RestClient(BASE_URI + "peoplepage/post");
    // create a RestForm and add parameters' info
    RestForm formParameters = new RestForm().addParameter("firstName", "Chuck").addParameter("lastName", "Norris").addParameter("age", "70");
    // send the form data
    RestResponse response = client.postForm(formParameters);
    // check the status code is "201 CREATED"
    response.verifyStatusCode(201);
    // check the returned result to make sure the server accepted the data we sent
    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) Test(org.testng.annotations.Test)

Example 17 with RestResponse

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

the class RestHelper method post.

public RestResponse post(String atsAgentIp, String baseRestUri, String relativeRestUri, Object[] values) {
    // check if ActiveDbAppender is attached
    if (!ActiveDbAppender.isAttached) {
        throw new IllegalStateException("Unable to execute monitoring operation.ATS DB Appender is not presented in log4j2.xml");
    }
    RestResponse response = null;
    initializeRestClient(atsAgentIp, baseRestUri, relativeRestUri);
    String jsonBody = null;
    if (relativeRestUri.endsWith(INITIALIZE_DB_CONNECTION_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructInitializeDbConnectionJson(values);
    } else if (relativeRestUri.endsWith(JOIN_TESTCASE_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructJoinTestcaseJson(values);
    } else if (relativeRestUri.endsWith(INITIALIZE_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructInitializeMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_SYSTEM_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleSystemMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_PROCESS_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleProcessMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_CHILD_PROCESS_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleProcessMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_JVM_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleJvmProcessMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_CUSTOM_JVM_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleCustomJvmProcessMonitoringJson(values);
    } else if (relativeRestUri.endsWith(SCHEDULE_USER_ACTIVITY_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructScheduleUserActivityJson(values);
    } else if (relativeRestUri.endsWith(START_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructStartMonitoringJson(values);
    } else if (relativeRestUri.endsWith(STOP_MONITORING_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructStopMonitoringJson(values);
    } else if (relativeRestUri.endsWith(LEAVE_TESTCASE_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructLeaveTestcaseJson(values);
    } else if (relativeRestUri.endsWith(DEINITIALIZE_DB_CONNECTION_RELATIVE_URI)) {
        jsonBody = JsonMonitoringUtils.constructDeinitializeDbConnectionJson(values);
    } else {
        throw new IllegalArgumentException("relativeRestUri does not lead to existing REST method. Please consult the documentation.");
    }
    response = this.restClient.postObject(jsonBody);
    return response;
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse)

Example 18 with RestResponse

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

the class SystemMonitor method performMonitoringOperation.

/**
 * Executes specific monitoring service REST call
 * If an error is returned/occurred, the error message is returned,
 * else null is returned, which means no errors occurred
 */
private String performMonitoringOperation(String monitoredHost, String baseUri, String relativeUri, String errorMessage, Object[] values) {
    RestHelper helper = null;
    RestResponse response = null;
    try {
        helper = this.restHelpers.get(monitoredHost);
        response = helper.post(monitoredHost, baseUri, relativeUri, values);
        if (response.getStatusCode() >= 400) {
            log.error(errorMessage + " on '" + monitoredHost + "'");
            return response.getBodyAsJson().getString("error");
        }
    } catch (Exception e) {
        log.error("Could not perform monitoring operation!", e);
    } finally {
        helper.disconnect();
    }
    return null;
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) MonitoringException(com.axway.ats.core.monitoring.MonitoringException)

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