Search in sources :

Example 1 with Endpoint

use of io.irontest.models.endpoint.Endpoint in project irontest by zheng-wang.

the class TeststepResource method run.

/**
 * Run a test step individually (not as part of test case running).
 * This is a stateless operation, i.e. not persisting anything in database.
 * @param teststep
 * @return
 */
@POST
@Path("{teststepId}/run")
@PermitAll
public BasicTeststepRun run(Teststep teststep) throws Exception {
    // workaround for Chrome's 'Failed to load response data' problem (still exist in Chrome 65)
    Thread.sleep(100);
    // gather referenceable string properties and endpoint properties
    List<UserDefinedProperty> testcaseUDPs = udpDAO.findByTestcaseId(teststep.getTestcaseId());
    Map<String, String> referenceableStringProperties = IronTestUtils.udpListToMap(testcaseUDPs);
    referenceableStringProperties.put(IMPLICIT_PROPERTY_NAME_TEST_STEP_START_TIME, IMPLICIT_PROPERTY_DATE_TIME_FORMAT.format(new Date()));
    Map<String, Endpoint> referenceableEndpointProperties = new HashMap<>();
    DataTable dataTable = utilsDAO.getTestcaseDataTable(teststep.getTestcaseId(), true);
    if (dataTable.getRows().size() == 1) {
        referenceableStringProperties.putAll(dataTable.getStringPropertiesInRow(0));
        referenceableEndpointProperties.putAll(dataTable.getEndpointPropertiesInRow(0));
    }
    // run the test step
    TeststepRunner teststepRunner = TeststepRunnerFactory.getInstance().newTeststepRunner(teststep, teststepDAO, utilsDAO, referenceableStringProperties, referenceableEndpointProperties, null);
    BasicTeststepRun basicTeststepRun = teststepRunner.run();
    // for better display in browser, transform XML response to be pretty-printed
    if (Teststep.TYPE_SOAP.equals(teststep.getType())) {
        SOAPAPIResponse soapAPIResponse = (SOAPAPIResponse) basicTeststepRun.getResponse();
        soapAPIResponse.setHttpBody(XMLUtils.prettyPrintXML(soapAPIResponse.getHttpBody()));
    } else if (Teststep.TYPE_MQ.equals(teststep.getType()) && Teststep.ACTION_DEQUEUE.equals(teststep.getAction())) {
        MQAPIResponse mqAPIResponse = (MQAPIResponse) basicTeststepRun.getResponse();
        mqAPIResponse.setValue(XMLUtils.prettyPrintXML((String) mqAPIResponse.getValue()));
    }
    return basicTeststepRun;
}
Also used : DataTable(io.irontest.models.DataTable) UserDefinedProperty(io.irontest.models.UserDefinedProperty) HashMap(java.util.HashMap) Date(java.util.Date) Endpoint(io.irontest.models.endpoint.Endpoint) PermitAll(javax.annotation.security.PermitAll)

Example 2 with Endpoint

use of io.irontest.models.endpoint.Endpoint in project irontest by zheng-wang.

the class SOAPTeststepRunner method run.

protected BasicTeststepRun run(Teststep teststep) throws Exception {
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
    Endpoint endpoint = teststep.getEndpoint();
    // set request HTTP headers
    HttpPost httpPost = new HttpPost(endpoint.getUrl());
    SOAPTeststepProperties otherProperties = (SOAPTeststepProperties) teststep.getOtherProperties();
    if (otherProperties != null) {
        for (HTTPHeader httpHeader : otherProperties.getHttpHeaders()) {
            httpPost.setHeader(httpHeader.getName(), httpHeader.getValue());
        }
    }
    // set HTTP basic auth
    if (!"".equals(StringUtils.trimToEmpty(endpoint.getUsername()))) {
        String auth = endpoint.getUsername() + ":" + getDecryptedEndpointPassword();
        String encodedAuth = Base64.encodeBase64String(auth.getBytes());
        String authHeader = "Basic " + encodedAuth;
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
    }
    // set request HTTP body
    httpPost.setEntity(new StringEntity((String) teststep.getRequest(), "UTF-8"));
    final SOAPAPIResponse apiResponse = new SOAPAPIResponse();
    ResponseHandler<Void> responseHandler = new ResponseHandler<Void>() {

        public Void handleResponse(final HttpResponse httpResponse) throws IOException {
            LOGGER.info(httpResponse.toString());
            apiResponse.getHttpHeaders().add(new HTTPHeader("*Status-Line*", httpResponse.getStatusLine().toString()));
            Header[] headers = httpResponse.getAllHeaders();
            for (Header header : headers) {
                apiResponse.getHttpHeaders().add(new HTTPHeader(header.getName(), header.getValue()));
            }
            HttpEntity entity = httpResponse.getEntity();
            apiResponse.setHttpBody(entity != null ? EntityUtils.toString(entity) : null);
            return null;
        }
    };
    // build HTTP Client instance
    // trust all SSL certificates
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {

        public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            return true;
        }
    }).build();
    HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
    // invoke the web service
    httpClient.execute(httpPost, responseHandler);
    basicTeststepRun.setResponse(apiResponse);
    return basicTeststepRun;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) SOAPTeststepProperties(io.irontest.models.teststep.SOAPTeststepProperties) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) StringEntity(org.apache.http.entity.StringEntity) Endpoint(io.irontest.models.endpoint.Endpoint) Header(org.apache.http.Header) HTTPHeader(io.irontest.models.teststep.HTTPHeader) HttpClient(org.apache.http.client.HttpClient) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HTTPHeader(io.irontest.models.teststep.HTTPHeader)

Example 3 with Endpoint

use of io.irontest.models.endpoint.Endpoint in project irontest by zheng-wang.

the class TeststepRunner method prepareTeststep.

/**
 * This method modifies the content of teststep object.
 * @throws IOException
 */
private void prepareTeststep() throws IOException {
    // fetch request binary if its type is file
    if (teststep.getRequestType() == TeststepRequestType.FILE) {
        teststep.setRequest(teststepDAO.getBinaryRequestById(teststep.getId()));
    }
    resolveReferenceableStringProperties();
    // resolve endpoint property if set on test step
    if (teststep.getEndpointProperty() != null) {
        teststep.setEndpoint(referenceableEndpointProperties.get(teststep.getEndpointProperty()));
        if (teststep.getEndpoint() == null) {
            throw new RuntimeException("Endpoint property " + teststep.getEndpointProperty() + " not defined or is null.");
        }
    }
    // decrypt password from endpoint
    // not modifying the endpoint object. Reasons
    // 1. Avoid the decrypted password leaking out of this runner (moving around with the Endpoint object)
    // 2. Avoid affecting other step runs when the endpoint object comes from a referenceable property (like from data table)
    Endpoint endpoint = teststep.getEndpoint();
    if (endpoint != null && endpoint.getPassword() != null) {
        decryptedEndpointPassword = utilsDAO.decryptEndpointPassword(endpoint.getPassword());
    }
}
Also used : Endpoint(io.irontest.models.endpoint.Endpoint)

Example 4 with Endpoint

use of io.irontest.models.endpoint.Endpoint in project irontest by zheng-wang.

the class DBTeststepRunner method run.

protected BasicTeststepRun run(Teststep teststep) throws Exception {
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
    DBAPIResponse response = new DBAPIResponse();
    String request = (String) teststep.getRequest();
    Endpoint endpoint = teststep.getEndpoint();
    DBI jdbi = new DBI(endpoint.getUrl(), endpoint.getUsername(), getDecryptedEndpointPassword());
    // get SQL statements (trimmed and without comments) and JDBI script object
    List<String> statements = IronTestUtils.getStatements(request);
    sanityCheckTheStatements(statements);
    Handle handle = jdbi.open();
    if (SQLStatementType.isSelectStatement(statements.get(0))) {
        // the request is a select statement
        RetainingColumnOrderResultSetMapper resultSetMapper = new RetainingColumnOrderResultSetMapper();
        // use statements.get(0) instead of the raw request, as Oracle does not support trailing semicolon in select statement
        Query<Map<String, Object>> query = handle.createQuery(statements.get(0)).map(resultSetMapper);
        // obtain columnNames in case the query returns no row
        final List<String> columnNames = new ArrayList<String>();
        query.addStatementCustomizer(new BaseStatementCustomizer() {

            public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException {
                ResultSetMetaData metaData = stmt.getMetaData();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    columnNames.add(metaData.getColumnLabel(i).toLowerCase());
                }
            }
        });
        // limit the number of returned rows
        List<Map<String, Object>> rows = query.list(5000);
        response.setColumnNames(columnNames);
        response.setRowsJSON(jacksonObjectMapper.writeValueAsString(rows));
    } else {
        // the request is one or more non-select statements
        Script script = handle.createScript(request);
        int[] returnValues = script.execute();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < returnValues.length; i++) {
            String statementType = SQLStatementType.getByStatement(statements.get(i)).toString();
            sb.append(returnValues[i]).append(" row(s) ").append(statementType.toLowerCase()).append(statementType.endsWith("E") ? "d" : "ed").append("\n");
            response.setStatementExecutionResults(sb.toString());
        }
    }
    handle.close();
    basicTeststepRun.setResponse(response);
    return basicTeststepRun;
}
Also used : BaseStatementCustomizer(org.skife.jdbi.v2.tweak.BaseStatementCustomizer) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Endpoint(io.irontest.models.endpoint.Endpoint) ResultSetMetaData(java.sql.ResultSetMetaData) Endpoint(io.irontest.models.endpoint.Endpoint) Map(java.util.Map)

Example 5 with Endpoint

use of io.irontest.models.endpoint.Endpoint in project irontest by zheng-wang.

the class TeststepDAO method useDirectEndpoint.

@Transaction
public void useDirectEndpoint(Teststep teststep, AppMode appMode) throws JsonProcessingException {
    Endpoint endpoint = endpointDAO().createUnmanagedEndpoint_NoTransaction(teststep.getType(), appMode);
    switchToDirectEndpoint(teststep.getId(), endpoint.getId());
}
Also used : Endpoint(io.irontest.models.endpoint.Endpoint)

Aggregations

Endpoint (io.irontest.models.endpoint.Endpoint)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Teststep (io.irontest.models.teststep.Teststep)3 SQLException (java.sql.SQLException)3 Environment (io.irontest.models.Environment)2 IOException (java.io.IOException)2 DataTable (io.irontest.models.DataTable)1 Testcase (io.irontest.models.Testcase)1 UserDefinedProperty (io.irontest.models.UserDefinedProperty)1 Assertion (io.irontest.models.assertion.Assertion)1 MQEndpointProperties (io.irontest.models.endpoint.MQEndpointProperties)1 SOAPEndpointProperties (io.irontest.models.endpoint.SOAPEndpointProperties)1 HTTPHeader (io.irontest.models.teststep.HTTPHeader)1 SOAPTeststepProperties (io.irontest.models.teststep.SOAPTeststepProperties)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1