Last active
May 26, 2022 09:51
-
-
Save krmahadevan/7acce7c97535c575260108bec935fbba to your computer and use it in GitHub Desktop.
A soft assertion implementation backed by org.skyscreamer.jsonassert.JSONAssert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import org.skyscreamer.jsonassert.JSONCompareMode; | |
import org.skyscreamer.jsonassert.JSONParser; | |
import org.testng.asserts.IAssert; | |
import org.testng.asserts.SoftAssert; | |
public class JSONAwareSoftAssertion extends SoftAssert { | |
private final boolean strict; | |
public JSONAwareSoftAssertion(boolean strict) { | |
this.strict = strict; | |
} | |
protected void doAssert(IAssert<?> a) { | |
super.doAssert(new JSONAssert<>(a, strict)); | |
} | |
public static class JSONAssert<T> implements IAssert<T> { | |
private final IAssert<T> iAssert; | |
private final JSONCompareMode compareMode; | |
public JSONAssert(IAssert<T> iAssert, boolean strict) { | |
this.iAssert = iAssert; | |
compareMode = strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT; | |
} | |
@Override | |
public String getMessage() { | |
return iAssert.getMessage(); | |
} | |
@Override | |
public void doAssert() { | |
Object rawExpected = transform(getExpected()); | |
Object rawActual = transform(getActual()); | |
if (rawExpected instanceof JSONObject) { | |
boolean isTrue = rawActual instanceof JSONObject; | |
if (!isTrue) { | |
String msg = getMessage() + " : " + " was expected to be a JSON object, but it was not."; | |
throw new AssertionError(msg); | |
} | |
try { | |
org.skyscreamer.jsonassert.JSONAssert.assertEquals(getMessage(), (JSONObject) rawExpected, | |
(JSONObject) rawActual, compareMode); | |
} catch (JSONException e) { | |
throw new RuntimeException(e.getMessage(), e); | |
} | |
} else if (rawExpected instanceof JSONArray) { | |
boolean isTrue = rawActual instanceof JSONArray; | |
if (!isTrue) { | |
String msg = getMessage() + " : " + getActual() + " was expected to be a JSON Array, but it was not."; | |
throw new AssertionError(msg); | |
} | |
try { | |
org.skyscreamer.jsonassert.JSONAssert.assertEquals(getMessage(), | |
(JSONArray) rawExpected, (JSONArray) rawActual, compareMode); | |
} catch (JSONException e) { | |
throw new RuntimeException(e.getMessage(), e); | |
} | |
} | |
String msg = getMessage() + " : " + getExpected() + " was neither a JSONArray nor a JSONObject."; | |
throw new AssertionError(msg); | |
} | |
private static Object transform(Object input) { | |
if (input instanceof String) { | |
try { | |
return JSONParser.parseJSON(String.valueOf(input)); | |
} catch (JSONException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
return input; | |
} | |
@Override | |
public T getActual() { | |
return iAssert.getActual(); | |
} | |
@Override | |
public T getExpected() { | |
return iAssert.getExpected(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here's a test class that consumes this assertion