Search in sources :

Example 1 with MapValueLookup

use of io.irontest.core.MapValueLookup in project irontest by zheng-wang.

the class TeststepRunner method resolveReferenceableStringProperties.

/**
 * Resolve as many string property references as possible. For unresolved references, throw exception in the end.
 * @throws IOException
 */
private void resolveReferenceableStringProperties() throws IOException {
    List<String> undefinedStringProperties = new ArrayList<String>();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    // resolve property references in teststep.otherProperties
    String otherPropertiesJSON = objectMapper.writeValueAsString(teststep.getOtherProperties());
    MapValueLookup propertyReferenceResolver = new MapValueLookup(referenceableStringProperties, true);
    String resolvedOtherPropertiesJSON = new StrSubstitutor(propertyReferenceResolver).replace(otherPropertiesJSON);
    undefinedStringProperties.addAll(propertyReferenceResolver.getUnfoundKeys());
    String tempStepJSON = "{\"type\":\"" + teststep.getType() + "\",\"otherProperties\":" + resolvedOtherPropertiesJSON + "}";
    Teststep tempStep = objectMapper.readValue(tempStepJSON, Teststep.class);
    teststep.setOtherProperties(tempStep.getOtherProperties());
    // resolve property references in teststep.request (text type)
    if (teststep.getRequestType() == TeststepRequestType.TEXT) {
        propertyReferenceResolver = new MapValueLookup(referenceableStringProperties, false);
        teststep.setRequest(new StrSubstitutor(propertyReferenceResolver).replace((String) teststep.getRequest()));
        undefinedStringProperties.addAll(propertyReferenceResolver.getUnfoundKeys());
    }
    if (!undefinedStringProperties.isEmpty()) {
        throw new RuntimeException("String properties " + undefinedStringProperties + " not defined.");
    }
}
Also used : Teststep(io.irontest.models.teststep.Teststep) StrSubstitutor(org.apache.commons.text.StrSubstitutor) MapValueLookup(io.irontest.core.MapValueLookup) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with MapValueLookup

use of io.irontest.core.MapValueLookup in project irontest by zheng-wang.

the class AssertionVerifier method verify.

/**
 * This method modifies the content of assertion object.
 * @param assertion the assertion to be verified (against the input)
 * @param input the object that the assertion is verified against
 * @return
 */
public AssertionVerificationResult verify(Assertion assertion, Object input) throws Exception {
    MapValueLookup stringPropertyReferenceResolver = new MapValueLookup(referenceableStringProperties, true);
    // resolve string property references in assertion.name
    String resolvedAssertionName = new StrSubstitutor(stringPropertyReferenceResolver).replace(assertion.getName());
    assertion.setName(resolvedAssertionName);
    Set<String> undefinedStringProperties = stringPropertyReferenceResolver.getUnfoundKeys();
    // resolve string property references in assertion.otherProperties
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    String assertionOtherPropertiesJSON = objectMapper.writeValueAsString(assertion.getOtherProperties());
    String resolvedAssertionOtherPropertiesJSON = new StrSubstitutor(stringPropertyReferenceResolver).replace(assertionOtherPropertiesJSON);
    undefinedStringProperties.addAll(stringPropertyReferenceResolver.getUnfoundKeys());
    String tempAssertionJSON = "{\"type\":\"" + assertion.getType() + "\",\"otherProperties\":" + resolvedAssertionOtherPropertiesJSON + "}";
    Assertion tempAssertion = objectMapper.readValue(tempAssertionJSON, Assertion.class);
    assertion.setOtherProperties(tempAssertion.getOtherProperties());
    if (!undefinedStringProperties.isEmpty()) {
        throw new RuntimeException("String properties " + undefinedStringProperties + " not defined.");
    }
    return _verify(assertion, input);
}
Also used : StrSubstitutor(org.apache.commons.text.StrSubstitutor) MapValueLookup(io.irontest.core.MapValueLookup) Assertion(io.irontest.models.assertion.Assertion) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 MapValueLookup (io.irontest.core.MapValueLookup)2 StrSubstitutor (org.apache.commons.text.StrSubstitutor)2 Assertion (io.irontest.models.assertion.Assertion)1 Teststep (io.irontest.models.teststep.Teststep)1 ArrayList (java.util.ArrayList)1