All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.skyscreamer.jsonassert.JSONCompareMode Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
package org.skyscreamer.jsonassert;

/**
 * 

These different modes define different behavior for the comparison of JSON for testing. * Each mode encapsulates two underlying behaviors: extensibility and strict ordering.

* * * * * * * *
 ExtensibleStrict Ordering
STRICTnoyes
LENIENTyesno
NON_EXTENSIBLEnono
STRICT_ORDERyesyes
* *

If extensibility not allowed, then all of the expected values must match in what's being tested, * but any additional fields will cause the test to fail. When extensibility is allowed, all values * must still match. For example, if you're expecting:

* * {id:1,name:"Carter"} * *

Then the following will pass when extensible, and will fail when not:

* * {id:1,name:"Carter",favoriteColor:"blue"} * *

If strict ordering is enabled, JSON arrays must be in strict sequence. For example, if you're expecting:

* * {id:1,friends:[{id:2},{id:3}]} * *

Then the following will fail strict ordering, but will otherwise pass:

* * {id:1,friends:[{id:3},{id:2}]} * */ public enum JSONCompareMode { /** * Strict checking. Not extensible, and strict array ordering. */ STRICT(false, true), /** * Lenient checking. Extensible, and non-strict array ordering. */ LENIENT(true, false), /** * Non-extensible checking. Not extensible, and non-strict array ordering. */ NON_EXTENSIBLE(false, false), /** * Strict order checking. Extensible, and strict array ordering. */ STRICT_ORDER(true, true); private final boolean _extensible; private final boolean _strictOrder; private JSONCompareMode(boolean extensible, boolean strictOrder) { _extensible = extensible; _strictOrder = strictOrder; } /** * Is extensible * @return True if results can be extended from what's expected, otherwise false. */ public boolean isExtensible() { return _extensible; } /** * Strict order required * @return True if results require strict array ordering, otherwise false. */ public boolean hasStrictOrder() { return _strictOrder; } /** * Get the equivalent {@code JSONCompareMode} with or without strict ordering. * * @return the equivalent {@code JSONCompareMode} */ public JSONCompareMode withStrictOrdering(boolean strictOrdering) { if (strictOrdering) { return isExtensible() ? STRICT_ORDER : STRICT; } else { return isExtensible() ? LENIENT : NON_EXTENSIBLE; } } /** * Get the equivalent {@code JSONCompareMode} with or without extensibility. * * @return the equivalent {@code JSONCompareMode} */ public JSONCompareMode withExtensible(boolean extensible) { if (extensible) { return hasStrictOrder() ? STRICT_ORDER : LENIENT; } else { return hasStrictOrder() ? STRICT : NON_EXTENSIBLE; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy