Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.unitvectory.jsonassertify;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -306,4 +307,12 @@ public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithEvenMor
JSONAssert.assertEquals("{a:9}", ARRAY_OF_JSONARRAYS,
new CustomComparator(JSONCompareMode.LENIENT, customization));
}

@Test
public void testEqualMethodReturnsFalse() {
ArrayValueMatcher<Object> matcher = new ArrayValueMatcher<>(comparator);
// The non-location-aware equal() method always returns false by design;
// use the LocationAwareValueMatcher.equal() method for actual comparisons.
assertFalse(matcher.equal("value1", "value2"));
}
}
97 changes: 97 additions & 0 deletions src/test/java/com/unitvectory/jsonassertify/CustomizationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.unitvectory.jsonassertify;

import static org.junit.jupiter.api.Assertions.*;

import org.json.JSONException;
import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link Customization}
*/
public class CustomizationTest {

@Test
public void testStaticFactoryMethod() {
ValueMatcher<Object> matcher = (o1, o2) -> true;
Customization customization = Customization.customization("path.to.field", matcher);
assertNotNull(customization);
assertTrue(customization.appliesToPath("path.to.field"));
}

@Test
public void testAppliesToPathExactMatch() {
ValueMatcher<Object> matcher = (o1, o2) -> true;
Customization customization = new Customization("user.name", matcher);
assertTrue(customization.appliesToPath("user.name"));
assertFalse(customization.appliesToPath("user.id"));
}

@Test
public void testAppliesToPathWithSingleWildcard() {
ValueMatcher<Object> matcher = (o1, o2) -> true;
Customization customization = new Customization("user.*.name", matcher);
assertTrue(customization.appliesToPath("user.profile.name"));
assertTrue(customization.appliesToPath("user.account.name"));
assertFalse(customization.appliesToPath("user.profile.id"));
}

@Test
public void testAppliesToPathWithDoubleWildcard() {
ValueMatcher<Object> matcher = (o1, o2) -> true;
Customization customization = new Customization("user.**.name", matcher);
assertTrue(customization.appliesToPath("user.name"));
assertTrue(customization.appliesToPath("user.profile.name"));
assertTrue(customization.appliesToPath("user.profile.details.name"));
}

@Test
public void testAppliesToPathWithRootDoubleWildcard() {
ValueMatcher<Object> matcher = (o1, o2) -> true;
Customization customization = new Customization("**.name", matcher);
assertTrue(customization.appliesToPath("name"));
assertTrue(customization.appliesToPath("user.name"));
assertTrue(customization.appliesToPath("user.profile.name"));
}

@Test
public void testMatchesWithLocationAwareValueMatcher() throws JSONException {
LocationAwareValueMatcher<Object> matcher = new LocationAwareValueMatcher<Object>() {
@Override
public boolean equal(Object o1, Object o2) {
return o1.equals(o2);
}

@Override
public boolean equal(String prefix, Object actual, Object expected, JSONCompareResult result) {
return actual.equals(expected);
}
};

Customization customization = new Customization("test", matcher);
JSONCompareResult result = new JSONCompareResult();
assertTrue(customization.matches("test", "value", "value", result));
assertFalse(customization.matches("test", "value1", "value2", result));
}

@Test
public void testMatchesWithRegularValueMatcher() throws JSONException {
ValueMatcher<Object> matcher = (o1, o2) -> o1.equals(o2);
Customization customization = new Customization("test", matcher);
JSONCompareResult result = new JSONCompareResult();
assertTrue(customization.matches("test", "value", "value", result));
assertFalse(customization.matches("test", "value1", "value2", result));
}
}
195 changes: 195 additions & 0 deletions src/test/java/com/unitvectory/jsonassertify/JSONAssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,199 @@ private void verifyErrorMessage(String message, AssertionError ae) {
assertTrue(ae.getMessage().contains(message));
assertTrue(ae.getMessage().startsWith(message));
}

// ===== Type mismatch tests =====

@Test
public void testAssertEqualsExpectingArrayButPassingObject() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", 1);
// Passing an array expected string but a JSONObject actual should throw
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals("[1,2,3]", actual, LENIENT));
}

@Test
public void testAssertEqualsExpectingArrayButPassingObjectWithMessage() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", 1);
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals("Custom message", "[1,2,3]", actual, LENIENT));
}

@Test
public void testAssertNotEqualsExpectingArrayButPassingObject() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", 1);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals("[1,2,3]", actual, LENIENT));
}

@Test
public void testAssertNotEqualsExpectingArrayButPassingObjectWithMessage() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", 1);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals("Custom message", "[1,2,3]", actual, LENIENT));
}

@Test
public void testAssertEqualsExpectingObjectButPassingArray() throws JSONException {
JSONArray actual = new JSONArray();
actual.put(1);
actual.put(2);
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals("{id:1}", actual, LENIENT));
}

@Test
public void testAssertNotEqualsExpectingObjectButPassingArray() throws JSONException {
JSONArray actual = new JSONArray();
actual.put(1);
actual.put(2);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals("{id:1}", actual, LENIENT));
}

@Test
public void testAssertNotEqualsExpectingObjectButPassingArrayWithMessage() throws JSONException {
JSONArray actual = new JSONArray();
actual.put(1);
actual.put(2);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals("Message", "{id:1}", actual, LENIENT));
}

// ===== Null string tests =====

@Test
public void testAssertEqualsStringNullExpected() throws JSONException {
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals(null, "{id:1}", LENIENT));
}

@Test
public void testAssertEqualsStringNullActual() throws JSONException {
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals("{id:1}", (String) null, LENIENT));
}

// ===== Comparator-based assertion tests =====

@Test
public void testAssertNotEqualsWithComparator() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONAssert.assertNotEquals("{id:1}", "{id:2}", comparator);
}

@Test
public void testAssertNotEqualsWithComparatorAndMessage() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONAssert.assertNotEquals("Message", "{id:1}", "{id:2}", comparator);
}

@Test
public void testAssertNotEqualsWithComparatorFailsWhenEqual() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals("{id:1}", "{id:1}", comparator));
}

@Test
public void testAssertNotEqualsWithComparatorAndMessageFailsWhenEqual() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals("Message", "{id:1}", "{id:1}", comparator));
}

@Test
public void testAssertEqualsJSONObjectWithComparator() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", 1);
actual.put("id", 1);
JSONAssert.assertEquals(expected, actual, comparator);
}

@Test
public void testAssertEqualsJSONObjectWithComparatorAndMessage() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", 1);
actual.put("id", 1);
JSONAssert.assertEquals("Message", expected, actual, comparator);
}

@Test
public void testAssertEqualsJSONObjectWithComparatorFails() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", 1);
actual.put("id", 2);
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals(expected, actual, comparator));
}

@Test
public void testAssertNotEqualsJSONObjectWithComparator() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", 1);
actual.put("id", 2);
JSONAssert.assertNotEquals(expected, actual, comparator);
}

@Test
public void testAssertNotEqualsJSONObjectWithComparatorAndMessage() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", 1);
actual.put("id", 2);
JSONAssert.assertNotEquals("Message", expected, actual, comparator);
}

@Test
public void testAssertNotEqualsJSONObjectWithComparatorFails() throws JSONException {
JSONComparator comparator = new com.unitvectory.jsonassertify.comparator.DefaultComparator(LENIENT);
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", 1);
actual.put("id", 1);
assertThrows(AssertionError.class, () ->
JSONAssert.assertNotEquals(expected, actual, comparator));
}

@Test
public void testAssertEqualsJSONArrayWithCompareMode() throws JSONException {
JSONArray expected = new JSONArray();
JSONArray actual = new JSONArray();
expected.put(1);
expected.put(2);
actual.put(1);
actual.put(2);
JSONAssert.assertEquals(expected, actual, LENIENT);
}

@Test
public void testAssertEqualsJSONArrayWithCompareModeFails() throws JSONException {
JSONArray expected = new JSONArray();
JSONArray actual = new JSONArray();
expected.put(1);
expected.put(2);
actual.put(1);
actual.put(3);
assertThrows(AssertionError.class, () ->
JSONAssert.assertEquals(expected, actual, LENIENT));
}

@Test
public void testAssertEqualsSameStringReference() throws JSONException {
String json = "{id:1}";
JSONAssert.assertEquals(json, json, LENIENT);
}
}
Loading
Loading