Search in sources :

Example 1 with PropertyExtractor

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;
}
Also used : StrSubstitutor(org.apache.commons.text.StrSubstitutor) PropertyExtractor(io.irontest.models.propertyextractor.PropertyExtractor) MapValueLookup(io.irontest.core.MapValueLookup) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with PropertyExtractor

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;
}
Also used : DataTable(io.irontest.models.DataTable) UserDefinedProperty(io.irontest.models.UserDefinedProperty) PropertyExtractor(io.irontest.models.propertyextractor.PropertyExtractor) PropertyExtractorRunner(io.irontest.core.propertyextractor.PropertyExtractorRunner) IOException(java.io.IOException) PropertyExtractionResult(io.irontest.models.propertyextractor.PropertyExtractionResult) PermitAll(javax.annotation.security.PermitAll)

Example 3 with PropertyExtractor

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);
    }
}
Also used : PropertyExtractor(io.irontest.models.propertyextractor.PropertyExtractor) Assertion(io.irontest.models.assertion.Assertion) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction)

Example 4 with 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;
}
Also used : PropertyExtractor(io.irontest.models.propertyextractor.PropertyExtractor) SQLException(java.sql.SQLException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with 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;
}
Also used : PropertyExtractorRunner(io.irontest.core.propertyextractor.PropertyExtractorRunner) java.util(java.util) TestResult(io.irontest.models.TestResult) HTTPStubMapping(io.irontest.models.HTTPStubMapping) io.irontest.models.assertion(io.irontest.models.assertion) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) PropertyExtractor(io.irontest.models.propertyextractor.PropertyExtractor) HttpHeader(org.eclipse.jetty.http.HttpHeader) IronTestConstants(io.irontest.IronTestConstants) TestcaseRun(io.irontest.models.testrun.TestcaseRun) Testcase(io.irontest.models.Testcase) TeststepRun(io.irontest.models.testrun.TeststepRun) HTTPStubsSetupTeststepProperties(io.irontest.models.teststep.HTTPStubsSetupTeststepProperties) Logger(org.slf4j.Logger) Endpoint(io.irontest.models.endpoint.Endpoint) AssertionVerifier(io.irontest.core.assertion.AssertionVerifier) IOException(java.io.IOException) UtilsDAO(io.irontest.db.UtilsDAO) AssertionVerifierFactory(io.irontest.core.assertion.AssertionVerifierFactory) io.irontest.core.teststep(io.irontest.core.teststep) PropertyExtractorRunnerFactory(io.irontest.core.propertyextractor.PropertyExtractorRunnerFactory) HTTPHeader(io.irontest.models.teststep.HTTPHeader) IronTestUtils(io.irontest.utils.IronTestUtils) Teststep(io.irontest.models.teststep.Teststep) TestcaseRunDAO(io.irontest.db.TestcaseRunDAO) PropertyExtractor(io.irontest.models.propertyextractor.PropertyExtractor) PropertyExtractorRunner(io.irontest.core.propertyextractor.PropertyExtractorRunner) HTTPHeader(io.irontest.models.teststep.HTTPHeader)

Aggregations

PropertyExtractor (io.irontest.models.propertyextractor.PropertyExtractor)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3 PropertyExtractorRunner (io.irontest.core.propertyextractor.PropertyExtractorRunner)2 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 IronTestConstants (io.irontest.IronTestConstants)1 MapValueLookup (io.irontest.core.MapValueLookup)1 AssertionVerifier (io.irontest.core.assertion.AssertionVerifier)1 AssertionVerifierFactory (io.irontest.core.assertion.AssertionVerifierFactory)1 PropertyExtractorRunnerFactory (io.irontest.core.propertyextractor.PropertyExtractorRunnerFactory)1 io.irontest.core.teststep (io.irontest.core.teststep)1 TestcaseRunDAO (io.irontest.db.TestcaseRunDAO)1 UtilsDAO (io.irontest.db.UtilsDAO)1 DataTable (io.irontest.models.DataTable)1 HTTPStubMapping (io.irontest.models.HTTPStubMapping)1 TestResult (io.irontest.models.TestResult)1 Testcase (io.irontest.models.Testcase)1 UserDefinedProperty (io.irontest.models.UserDefinedProperty)1 io.irontest.models.assertion (io.irontest.models.assertion)1 Assertion (io.irontest.models.assertion.Assertion)1