Search in sources :

Example 1 with PropertyExtractorRunner

use of io.irontest.core.propertyextractor.PropertyExtractorRunner 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 2 with PropertyExtractorRunner

use of io.irontest.core.propertyextractor.PropertyExtractorRunner 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

PropertyExtractorRunner (io.irontest.core.propertyextractor.PropertyExtractorRunner)2 PropertyExtractor (io.irontest.models.propertyextractor.PropertyExtractor)2 IOException (java.io.IOException)2 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 IronTestConstants (io.irontest.IronTestConstants)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 Endpoint (io.irontest.models.endpoint.Endpoint)1 PropertyExtractionResult (io.irontest.models.propertyextractor.PropertyExtractionResult)1 TestcaseRun (io.irontest.models.testrun.TestcaseRun)1