Created
September 9, 2015 20:01
-
-
Save kmajetic/60f3c6707556e9a3818e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.kata.util.matchers; | |
import org.hamcrest.Description; | |
import org.json.JSONException; | |
import org.mockito.ArgumentMatcher; | |
import org.skyscreamer.jsonassert.JSONCompare; | |
import org.skyscreamer.jsonassert.JSONCompareMode; | |
import org.skyscreamer.jsonassert.JSONCompareResult; | |
import static org.mockito.Matchers.argThat; | |
/** | |
* Created by kmajetic on 14.05.15.. | |
*/ | |
public class JsonStringMatcher extends ArgumentMatcher<String> { | |
private final String expected; | |
public JsonStringMatcher(String expected) { | |
this.expected = expected; | |
} | |
public static String jsonEq(String expected) { | |
return argThat(new JsonStringMatcher(expected)); | |
} | |
@Override | |
public boolean matches(Object argument) { | |
if (expected == null) | |
return argument == null; | |
if (!(argument instanceof String)) | |
return false; | |
try { | |
return compare(expected, (String) argument); | |
} catch (JSONException e) { | |
return false; | |
} | |
} | |
private boolean compare(String expected, String argument) throws JSONException { | |
JSONCompareResult result = JSONCompare.compareJSON(expected, argument, JSONCompareMode.LENIENT); | |
if (result.failed()) { | |
System.out.println(String.format("expected=%s actual=%s", expected, argument)); | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(expected.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment