6

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.

4 Answers 4

7

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, do you know of any other library that does that? Because JSONAssert has not been committed to in a long time.
@user3629892, yes, take a look at JsonUnit, for example.
1

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))

Comments

0

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); }
        }

    }

Comments

0

This checks if there is a date present, but does not assert its content. Useful if a property is non-deterministic:

var expected = """{ "randomDate": "<ignoreme>" }""";
var actual = """{ "randomDate": "2024-03-01" }""";

assertEquals(expected, actual, new CustomComparator(
                STRICT,
                new Customization("randomDate", new RegularExpressionValueMatcher<>(".+"))
        ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.