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;
}
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;
}
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());
}
}
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;
}
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());
}
Aggregations