|
| 1 | +package com.auth0; |
| 2 | + |
| 3 | +import org.hamcrest.Description; |
| 4 | +import org.hamcrest.Matcher; |
| 5 | +import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 6 | + |
| 7 | +import java.lang.reflect.Array; |
| 8 | +import java.util.Iterator; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class JsonMatcher extends TypeSafeDiagnosingMatcher<String> { |
| 13 | + |
| 14 | + private final String entry; |
| 15 | + private final String key; |
| 16 | + private final Matcher matcher; |
| 17 | + |
| 18 | + private JsonMatcher(String key, Object value, Matcher valueMatcher) { |
| 19 | + this.key = key; |
| 20 | + this.matcher = valueMatcher; |
| 21 | + if (value != null) { |
| 22 | + String stringValue = objectToString(value); |
| 23 | + entry = getStringKey(key) + stringValue; |
| 24 | + } else { |
| 25 | + entry = null; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + protected boolean matchesSafely(String item, Description mismatchDescription) { |
| 31 | + if (item == null) { |
| 32 | + mismatchDescription.appendText("JSON was null"); |
| 33 | + return false; |
| 34 | + } |
| 35 | + if (matcher != null) { |
| 36 | + if (!matcher.matches(item)) { |
| 37 | + matcher.describeMismatch(item, mismatchDescription); |
| 38 | + return false; |
| 39 | + } |
| 40 | + if (!item.contains(getStringKey(key))) { |
| 41 | + mismatchDescription.appendText("JSON didn't contained the key ").appendValue(key); |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + if (entry != null && !item.contains(entry)) { |
| 46 | + mismatchDescription.appendText("JSON was ").appendValue(item); |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void describeTo(Description description) { |
| 55 | + if (matcher == null) { |
| 56 | + description.appendText("A JSON with entry ") |
| 57 | + .appendValue(entry); |
| 58 | + } else { |
| 59 | + matcher.describeTo(description); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static JsonMatcher hasEntry(String key, Object value) { |
| 64 | + return new JsonMatcher(key, value, null); |
| 65 | + } |
| 66 | + |
| 67 | + public static JsonMatcher hasEntry(String key, Matcher valueMatcher) { |
| 68 | + return new JsonMatcher(key, null, valueMatcher); |
| 69 | + } |
| 70 | + |
| 71 | + private String getStringKey(String key) { |
| 72 | + return "\"" + key + "\":"; |
| 73 | + } |
| 74 | + |
| 75 | + private String objectToString(Object value) { |
| 76 | + String stringValue; |
| 77 | + if (value == null) { |
| 78 | + stringValue = "null"; |
| 79 | + } else if (value instanceof String) { |
| 80 | + stringValue = "\"" + value + "\""; |
| 81 | + } else if (value instanceof Map) { |
| 82 | + stringValue = mapToString((Map<String, Object>) value); |
| 83 | + } else if (value instanceof Array) { |
| 84 | + stringValue = arrayToString((Object[]) value); |
| 85 | + } else if (value instanceof List) { |
| 86 | + stringValue = listToString((List<Object>) value); |
| 87 | + } else { |
| 88 | + stringValue = value.toString(); |
| 89 | + } |
| 90 | + return stringValue; |
| 91 | + } |
| 92 | + |
| 93 | + private String arrayToString(Object[] array) { |
| 94 | + StringBuilder sb = new StringBuilder(); |
| 95 | + sb.append("["); |
| 96 | + for (int i = 0; i < array.length; i++) { |
| 97 | + Object o = array[i]; |
| 98 | + sb.append(objectToString(o)); |
| 99 | + if (i + 1 < array.length) { |
| 100 | + sb.append(","); |
| 101 | + } |
| 102 | + } |
| 103 | + sb.append("]"); |
| 104 | + return sb.toString(); |
| 105 | + } |
| 106 | + |
| 107 | + private String listToString(List<Object> list) { |
| 108 | + StringBuilder sb = new StringBuilder(); |
| 109 | + sb.append("["); |
| 110 | + Iterator<Object> it = list.iterator(); |
| 111 | + while (it.hasNext()) { |
| 112 | + Object o = it.next(); |
| 113 | + sb.append(objectToString(o)); |
| 114 | + if (it.hasNext()) { |
| 115 | + sb.append(","); |
| 116 | + } |
| 117 | + } |
| 118 | + sb.append("]"); |
| 119 | + return sb.toString(); |
| 120 | + } |
| 121 | + |
| 122 | + private String mapToString(Map<String, Object> map) { |
| 123 | + StringBuilder sb = new StringBuilder(); |
| 124 | + sb.append("{"); |
| 125 | + Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); |
| 126 | + while (it.hasNext()) { |
| 127 | + Map.Entry<String, Object> e = it.next(); |
| 128 | + sb.append("\"" + e.getKey() + "\":" + objectToString(e.getValue())); |
| 129 | + if (it.hasNext()) { |
| 130 | + sb.append(","); |
| 131 | + } |
| 132 | + } |
| 133 | + sb.append("}"); |
| 134 | + return sb.toString(); |
| 135 | + } |
| 136 | +} |
0 commit comments