use of io.irontest.models.propertyextractor.PropertyExtractor in project irontest by zheng-wang.
the class PropertyExtractorRunnerFactory method create.
/**
* This method modifies content of the propertyExtractor object.
* @param propertyExtractor
* @param referenceableStringProperties
* @return
* @throws IOException
*/
public PropertyExtractorRunner create(PropertyExtractor propertyExtractor, Map<String, String> referenceableStringProperties) throws IOException {
PropertyExtractorRunner result;
String propertyExtractorType = propertyExtractor.getType();
switch(propertyExtractorType) {
case PropertyExtractor.TYPE_JSONPATH:
result = new JSONPathPropertyExtractorRunner();
break;
case PropertyExtractor.TYPE_COOKIE:
result = new CookiePropertyExtractorRunner();
break;
default:
throw new RuntimeException("Unrecognized property extractor type " + propertyExtractorType);
}
MapValueLookup stringPropertyReferenceResolver = new MapValueLookup(referenceableStringProperties, true);
// resolve string property references in propertyExtractor.otherProperties
ObjectMapper objectMapper = new ObjectMapper();
String otherPropertiesJSON = objectMapper.writeValueAsString(propertyExtractor.getOtherProperties());
String resolvedOtherPropertiesJSON = new StrSubstitutor(stringPropertyReferenceResolver).replace(otherPropertiesJSON);
Set<String> undefinedStringProperties = stringPropertyReferenceResolver.getUnfoundKeys();
String tempPropertyExtractorJSON = "{\"type\":\"" + propertyExtractor.getType() + "\",\"otherProperties\":" + resolvedOtherPropertiesJSON + "}";
PropertyExtractor tempPropertyExtractor = objectMapper.readValue(tempPropertyExtractorJSON, PropertyExtractor.class);
propertyExtractor.setOtherProperties(tempPropertyExtractor.getOtherProperties());
if (!undefinedStringProperties.isEmpty()) {
throw new RuntimeException("String properties " + undefinedStringProperties + " not defined.");
}
result.setPropertyExtractor(propertyExtractor);
return result;
}
use of io.irontest.models.propertyextractor.PropertyExtractor in project irontest by zheng-wang.
the class PropertyExtractorResource method extract.
/**
* This is a stateless operation, i.e. not persisting anything in database.
* @param propertyExtractionRequest
* @return
*/
@POST
@Path("propertyExtractors/{propertyExtractorId}/extract")
@PermitAll
public PropertyExtractionResult extract(PropertyExtractionRequest propertyExtractionRequest) throws IOException {
PropertyExtractor propertyExtractor = propertyExtractionRequest.getPropertyExtractor();
// gather referenceable string properties
long testcaseId = propertyExtractorDAO.findTestcaseIdById(propertyExtractor.getId());
List<UserDefinedProperty> testcaseUDPs = udpDAO.findByTestcaseId(testcaseId);
Map<String, String> referenceableStringProperties = IronTestUtils.udpListToMap(testcaseUDPs);
Set<String> udpNames = referenceableStringProperties.keySet();
DataTable dataTable = dataTableDAO.getTestcaseDataTable(testcaseId, true);
if (dataTable.getRows().size() > 0) {
IronTestUtils.checkDuplicatePropertyNameBetweenDataTableAndUPDs(udpNames, dataTable);
referenceableStringProperties.putAll(dataTable.getStringPropertiesInRow(0));
}
PropertyExtractorRunner propertyExtractorRunner = PropertyExtractorRunnerFactory.getInstance().create(propertyExtractor, referenceableStringProperties);
String propertyExtractionInput = propertyExtractionRequest.getInput();
PropertyExtractionResult result = new PropertyExtractionResult();
try {
result.setPropertyValue(propertyExtractorRunner.extract(propertyExtractionInput));
} catch (Exception e) {
LOGGER.error("Failed to extract property", e);
result.setError(e.getMessage());
}
return result;
}
use of io.irontest.models.propertyextractor.PropertyExtractor in project irontest by zheng-wang.
the class TeststepDAO method insertByImport.
@Transaction
default void insertByImport(Teststep teststep) throws JsonProcessingException {
Long endpointId = null;
if (teststep.getEndpoint() != null) {
endpointId = endpointDAO().insertUnmanagedEndpoint(teststep.getEndpoint());
}
String requestString = (String) teststep.getRequest();
byte[] request = null;
if (requestString != null) {
request = teststep.getRequestType() == TeststepRequestType.FILE ? Base64.getDecoder().decode(requestString) : requestString.getBytes();
}
String apiRequestJSONString = new ObjectMapper().writeValueAsString(teststep.getApiRequest());
long teststepId = _insertWithName(teststep, request, teststep.getRequestType().toString(), apiRequestJSONString, endpointId);
for (Assertion assertion : teststep.getAssertions()) {
assertion.setTeststepId(teststepId);
assertionDAO().insert(assertion);
}
for (PropertyExtractor propertyExtractor : teststep.getPropertyExtractors()) {
propertyExtractorDAO().insert(teststepId, propertyExtractor);
}
}
use of io.irontest.models.propertyextractor.PropertyExtractor in project irontest by zheng-wang.
the class PropertyExtractorMapper method map.
public PropertyExtractor map(ResultSet rs, StatementContext ctx) throws SQLException {
PropertyExtractor propertyExtractor;
String type = rs.getString("type");
if (rs.getString("other_properties") != null) {
String tempPropertyExtractorJSON = "{\"type\":\"" + type + "\",\"otherProperties\":" + rs.getString("other_properties") + "}";
try {
propertyExtractor = new ObjectMapper().readValue(tempPropertyExtractorJSON, PropertyExtractor.class);
} catch (IOException e) {
throw new SQLException("Failed to deserialize other_properties JSON.", e);
}
} else {
propertyExtractor = new PropertyExtractor();
}
propertyExtractor.setId(rs.getLong("id"));
propertyExtractor.setPropertyName(rs.getString("property_name"));
propertyExtractor.setType(type);
return propertyExtractor;
}
use of io.irontest.models.propertyextractor.PropertyExtractor in project irontest by zheng-wang.
the class TestcaseRunner method extractPropertiesOutOfAPIResponse.
/**
* Extract properties out of API response, and make the properties visible to the next test step run.
*/
private Map<String, String> extractPropertiesOutOfAPIResponse(String teststepType, List<PropertyExtractor> propertyExtractors, Object apiResponse, Map<String, String> referenceableStringProperties) throws Exception {
Map<String, String> extractedProperties = new HashMap<>();
for (PropertyExtractor propertyExtractor : propertyExtractors) {
String propertyExtractionInput = null;
if (Teststep.TYPE_HTTP.equals(teststepType)) {
HTTPAPIResponse httpApiResponse = (HTTPAPIResponse) apiResponse;
if (PropertyExtractor.TYPE_COOKIE.equals(propertyExtractor.getType())) {
Optional<HTTPHeader> setCookieHeader = httpApiResponse.getHttpHeaders().stream().filter(httpHeader -> HttpHeader.SET_COOKIE.asString().equals(httpHeader.getName())).findFirst();
propertyExtractionInput = setCookieHeader.isPresent() ? setCookieHeader.get().getValue() : null;
} else {
propertyExtractionInput = httpApiResponse.getHttpBody();
}
}
PropertyExtractorRunner propertyExtractorRunner = PropertyExtractorRunnerFactory.getInstance().create(propertyExtractor, referenceableStringProperties);
String propertyValue = propertyExtractorRunner.extract(propertyExtractionInput);
extractedProperties.put(propertyExtractor.getPropertyName(), propertyValue);
}
return extractedProperties;
}
Aggregations