Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Last active May 26, 2022 09:51
Show Gist options
  • Save krmahadevan/7acce7c97535c575260108bec935fbba to your computer and use it in GitHub Desktop.
Save krmahadevan/7acce7c97535c575260108bec935fbba to your computer and use it in GitHub Desktop.
A soft assertion implementation backed by org.skyscreamer.jsonassert.JSONAssert
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();
}
}
}
@krmahadevan
Copy link
Author

krmahadevan commented May 26, 2022

And here's a test class that consumes this assertion

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.annotations.Test;

public class JSONAssertDemo {

  @Test
  public void softAssertionsUsingJSONAssert() throws JSONException {
    JSONObject expected = new JSONObject();
    expected.put("name", "Kung-fu Panda");
    JSONObject actual = new JSONObject();
    actual.put("name", "Dragon Warrior");
    JSONAwareSoftAssertion assertion = new JSONAwareSoftAssertion(true);
    assertion.assertEquals(actual.toString(), expected.toString(), "Equality check");
    System.err.println("Hello World");
    JSONArray expectedArray = new JSONArray();
    expectedArray.put(0, 10);
    assertion.assertEquals(new JSONObject().toString(), expectedArray.toString(), "Another check");
    assertion.assertAll();
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment