JSON validation using JSONAssert with regular expressions

5.8k Views Asked by At

I am working in a open-source project which uses REST interface. To validate (match actual response with expected) our rest interfaces in the JUnit, we would like to use the JSONAssert. (https://github.com/skyscreamer/JSONassert). But I have a problem with the usage.Kindly help to resolve it.

Expected JSON:
{
"objectId": "[^\\s]",
"protocol": "[^\\s]",
"hostName": "[^\\s]",
"port": "[^\\s]",
"commParams": "[^\\s]"
}

Remarks: objectId/protocol/hostName/port/commParams can be anything but should not be empty

Actual JSON:
{
"objectId": "controller2",
"protocol": "ftp",
"hostName": "sdnorchestrator",
"port": "21",
"commParams": "username:tomcat, password:tomdog"
}

Problem1: Which interface of JSON Assert, i need to use to solve the above issue:Below one?

JSONAssert.assertEquals("Expected JSON", "Actual JSON" new CustomComparator(
    JSONCompareMode.LENIENT_ORDER, new Customization(PREFIX, new        RegularExpressionValueMatcher<Object>())));

Problem 2: What should be the PREFIX here?(I tried with "", "., "." but had no success)

Any other recommendation (other than JSONAssert) for the above problem is also welcome.

3

There are 3 best solutions below

2
On

If you want to globally apply regular expression customizations with JSONAssert, you can construct a single Customization with path = "***", and use the RegularExpressionValueMatcher constructor with no arguments.

Example:

final String expectedJson = "{objectId: \"[^\\s]+\", protocol: \"[^\\s]+\"}";
final String actualJson = "{\"objectId\": \"controller2\", \"protocol\": \"ftp\"}";

JSONAssert.assertEquals(
    expectedJson,
    actualJson,
    new CustomComparator(
        JSONCompareMode.LENIENT,
        new Customization("***", new RegularExpressionValueMatcher<>())
    )
);

This assertion passes successfully (tested with JSONassert version 1.5.0).

0
On

I have found better alternative.Usage of JsonSchema validator would solve most of the problem (http://json-schema.org/). Write the json schema in expected response and validate it using json validator jar. (json-schema/json-schema-validator-2.0.1.jar.zip( 166 k))

0
On

I could not get the path to except an RE either. Eventually I just over-rode the compareValues of DefaultComparator to force it to apply the RegularExpressionValueMatcher to all paths:

import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.RegularExpressionValueMatcher;
import org.skyscreamer.jsonassert.ValueMatcherException;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
    ...

final RegularExpressionValueMatcher reMatcher = new RegularExpressionValueMatcher();

JSONAssert.assertEquals( "{\"key\":\"v.*\"}", "{\"key\":\"value\"}",
    new DefaultComparator( STRICT ) {

        @Override
        public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException {
            try {
                if( !reMatcher.equal(actualValue, expectedValue) ) result.fail(prefix, expectedValue, actualValue);
            } catch( ValueMatcherException e ) { result.fail(prefix, e); }
        }

    }