Search in sources :

Example 1 with JSONEqualAssertionProperties

use of io.irontest.models.assertion.JSONEqualAssertionProperties in project irontest by zheng-wang.

the class JSONEqualAssertionVerifier method _verify.

/**
 * @param assertion
 * @param input the JSON string that the assertion is verified against
 * @return
 */
@Override
public AssertionVerificationResult _verify(Assertion assertion, Object input) {
    JSONEqualAssertionProperties assertionProperties = (JSONEqualAssertionProperties) assertion.getOtherProperties();
    // validate arguments
    if (assertionProperties.getExpectedJSON() == null) {
        throw new IllegalArgumentException("Expected JSON is null.");
    } else if (input == null) {
        throw new IllegalArgumentException("Actual JSON is null.");
    }
    String expectedJSON = assertionProperties.getExpectedJSON().replaceAll(JSON_UNIT_PLACEHOLDER_REGEX, JSON_UNIT_PLACEHOLDER_DELIMITER_REPLACEMENT);
    MessageEqualAssertionVerificationResult result = new MessageEqualAssertionVerificationResult();
    try {
        assertJsonEquals(expectedJSON, input);
    } catch (IllegalArgumentException e) {
        Throwable c = e.getCause();
        if (c instanceof JsonParseException) {
            throw new IllegalArgumentException(e.getMessage() + " " + c.getMessage());
        } else {
            throw e;
        }
    } catch (AssertionError ae) {
        result.setDifferences(ae.getMessage());
    }
    if (result.getDifferences() == null) {
        result.setResult(TestResult.PASSED);
    } else {
        result.setResult(TestResult.FAILED);
    }
    return result;
}
Also used : JSONEqualAssertionProperties(io.irontest.models.assertion.JSONEqualAssertionProperties) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MessageEqualAssertionVerificationResult(io.irontest.models.assertion.MessageEqualAssertionVerificationResult)

Example 2 with JSONEqualAssertionProperties

use of io.irontest.models.assertion.JSONEqualAssertionProperties in project irontest by zheng-wang.

the class JSONEqualAssertionVerifier method verify.

/**
 * @param inputs contains only one argument: the JSON string that the assertion is verified against
 * @return
 */
@Override
public AssertionVerificationResult verify(Object... inputs) {
    JSONEqualAssertionProperties assertionProperties = (JSONEqualAssertionProperties) getAssertion().getOtherProperties();
    String expectedJSON = assertionProperties.getExpectedJSON();
    // validate arguments
    if (expectedJSON == null) {
        throw new IllegalArgumentException("Expected JSON is null.");
    } else if (inputs[0] == null) {
        throw new IllegalArgumentException("Actual JSON is null.");
    } else if (inputs[0].equals("")) {
        throw new IllegalArgumentException("Actual JSON is empty.");
    }
    MessageEqualAssertionVerificationResult result = new MessageEqualAssertionVerificationResult();
    try {
        assertJsonEquals(expectedJSON, inputs[0]);
    } catch (IllegalArgumentException e) {
        Throwable c = e.getCause();
        if (c instanceof JsonParseException) {
            throw new IllegalArgumentException(e.getMessage() + " " + c.getMessage());
        } else {
            throw e;
        }
    } catch (AssertionError ae) {
        result.setDifferences(ae.getMessage());
    }
    if (result.getDifferences() == null) {
        result.setResult(TestResult.PASSED);
    } else {
        result.setResult(TestResult.FAILED);
    }
    return result;
}
Also used : JSONEqualAssertionProperties(io.irontest.models.assertion.JSONEqualAssertionProperties) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MessageEqualAssertionVerificationResult(io.irontest.models.assertion.MessageEqualAssertionVerificationResult)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JSONEqualAssertionProperties (io.irontest.models.assertion.JSONEqualAssertionProperties)2 MessageEqualAssertionVerificationResult (io.irontest.models.assertion.MessageEqualAssertionVerificationResult)2