Skip to content

Commit 07c649d

Browse files
committed
add JSONMatcher and refactor json tests
1 parent a32efc8 commit 07c649d

31 files changed

Lines changed: 361 additions & 45 deletions

src/main/java/com/auth0/MgmtAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public MgmtAPI(String domain, String apiToken) {
3030
this.apiToken = apiToken;
3131

3232
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
33-
logging.setLevel(HttpLoggingInterceptor.Level.NONE);
33+
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
3434
client = new OkHttpClient.Builder()
3535
.addInterceptor(logging)
3636
.build();

src/main/java/com/auth0/exception/AuthAPIException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public AuthAPIException(String payload, int statusCode, Throwable cause) {
1717
public AuthAPIException(Map<String, Object> values, int statusCode) {
1818
super(createMessage(obtainExceptionMessage(values), statusCode));
1919
this.error = (String) (values.containsKey("error") ? values.get("error") : values.get("code"));
20-
this.description = (String) (values.containsKey("error_description") ? values.get("error_description") : values.get("description"));
20+
this.description = obtainExceptionMessage(values);
2121
this.statusCode = statusCode;
2222
}
2323

@@ -28,6 +28,9 @@ private static String obtainExceptionMessage(Map<String, Object> values) {
2828
if (values.containsKey("description")) {
2929
return (String) values.get("description");
3030
}
31+
if (values.containsKey("message")) {
32+
return (String) values.get("message");
33+
}
3134
if (values.containsKey("error")) {
3235
return (String) values.get("error");
3336
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
package com.auth0.json.auth;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.hamcrest.collection.IsMapContaining;
56
import org.junit.Test;
67

78
import static org.hamcrest.MatcherAssert.assertThat;
8-
import static org.hamcrest.Matchers.*;
9+
import static org.hamcrest.Matchers.is;
10+
import static org.hamcrest.Matchers.notNullValue;
911

1012
public class UserInfoTest extends JsonTest<UserInfo> {
1113

12-
private static final String json = "{\"email\":\"test.account@userinfo.com\",\"client_id\":\"q2hnj2iu...\"}";
14+
private static final String json = "{\"email_verified\":false,\"client_id\":\"q2hnj2iu...\",\"updated_at\":\"2016-12-05T15:15:40.545Z\",\"name\":\"test.account@userinfo.com\",\"email\":\"test.account@userinfo.com\"}";
1315

1416
@Test
1517
public void shouldSerialize() throws Exception {
1618
UserInfo info = new UserInfo();
19+
info.setValue("email_verified", false);
1720
info.setValue("email", "test.account@userinfo.com");
1821
info.setValue("client_id", "q2hnj2iu...");
22+
info.setValue("updated_at", "2016-12-05T15:15:40.545Z");
23+
info.setValue("name", "test.account@userinfo.com");
1924

2025
String serialized = toJSON(info);
2126
assertThat(serialized, is(notNullValue()));
22-
assertThat(serialized, is(equalTo(json)));
27+
assertThat(serialized, JsonMatcher.hasEntry("email_verified", false));
28+
assertThat(serialized, JsonMatcher.hasEntry("email", "test.account@userinfo.com"));
29+
assertThat(serialized, JsonMatcher.hasEntry("client_id", "q2hnj2iu..."));
30+
assertThat(serialized, JsonMatcher.hasEntry("updated_at", "2016-12-05T15:15:40.545Z"));
31+
assertThat(serialized, JsonMatcher.hasEntry("name", "test.account@userinfo.com"));
2332
}
2433

2534
@Test
@@ -28,7 +37,10 @@ public void shouldDeserialize() throws Exception {
2837

2938
assertThat(info, is(notNullValue()));
3039

40+
assertThat(info.getValues(), IsMapContaining.hasEntry("email_verified", (Object) false));
3141
assertThat(info.getValues(), IsMapContaining.hasEntry("email", (Object) "test.account@userinfo.com"));
3242
assertThat(info.getValues(), IsMapContaining.hasEntry("client_id", (Object) "q2hnj2iu..."));
43+
assertThat(info.getValues(), IsMapContaining.hasEntry("updated_at", (Object) "2016-12-05T15:15:40.545Z"));
44+
assertThat(info.getValues(), IsMapContaining.hasEntry("name", (Object) "test.account@userinfo.com"));
3345
}
3446
}

src/test/java/com/auth0/json/mgmt/ConnectionTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.json.mgmt;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.junit.Test;
56

@@ -22,7 +23,10 @@ public void shouldSerialize() throws Exception {
2223

2324
String serialized = toJSON(connection);
2425
assertThat(serialized, is(notNullValue()));
25-
assertThat(serialized, is(equalTo(json)));
26+
assertThat(serialized, JsonMatcher.hasEntry("name", "my-connection"));
27+
assertThat(serialized, JsonMatcher.hasEntry("strategy", "auth0"));
28+
assertThat(serialized, JsonMatcher.hasEntry("options", notNullValue()));
29+
assertThat(serialized, JsonMatcher.hasEntry("enabled_clients", Arrays.asList("client1", "client2")));
2630
}
2731

2832
@Test

src/test/java/com/auth0/json/mgmt/DeviceCredentialsTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.json.mgmt;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.junit.Test;
56

@@ -18,7 +19,12 @@ public void shouldSerialize() throws Exception {
1819

1920
String serialized = toJSON(credentials);
2021
assertThat(serialized, is(notNullValue()));
21-
assertThat(serialized, is(equalTo(json)));
22+
assertThat(serialized, JsonMatcher.hasEntry("device_name", "devName"));
23+
assertThat(serialized, JsonMatcher.hasEntry("type", "publicKey"));
24+
assertThat(serialized, JsonMatcher.hasEntry("value", "val123"));
25+
assertThat(serialized, JsonMatcher.hasEntry("device_id", "dev123"));
26+
assertThat(serialized, JsonMatcher.hasEntry("client_id", "client123"));
27+
assertThat(serialized, JsonMatcher.hasEntry("user_id", "theUserId"));
2228
}
2329

2430
@Test

src/test/java/com/auth0/json/mgmt/EmailProviderCredentialsTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.json.mgmt;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.junit.Test;
56

@@ -24,7 +25,15 @@ public void shouldSerialize() throws Exception {
2425

2526
String serialized = toJSON(credentials);
2627
assertThat(serialized, is(notNullValue()));
27-
assertThat(serialized, is(equalTo(json)));
28+
assertThat(serialized, JsonMatcher.hasEntry("api_key", "key123"));
29+
assertThat(serialized, JsonMatcher.hasEntry("api_user", "username"));
30+
assertThat(serialized, JsonMatcher.hasEntry("accessKeyId", "id"));
31+
assertThat(serialized, JsonMatcher.hasEntry("secretAccessKey", "secret"));
32+
assertThat(serialized, JsonMatcher.hasEntry("region", "ar"));
33+
assertThat(serialized, JsonMatcher.hasEntry("smtp_host", "host"));
34+
assertThat(serialized, JsonMatcher.hasEntry("smtp_port", 1234));
35+
assertThat(serialized, JsonMatcher.hasEntry("smtp_user", "usr"));
36+
assertThat(serialized, JsonMatcher.hasEntry("smtp_pass", "pwd"));
2837
}
2938

3039
@Test

src/test/java/com/auth0/json/mgmt/EmailProviderTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.auth0.json.mgmt;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.junit.Test;
56

67
import java.util.Collections;
78

89
import static org.hamcrest.MatcherAssert.assertThat;
9-
import static org.hamcrest.Matchers.*;
10+
import static org.hamcrest.Matchers.is;
11+
import static org.hamcrest.Matchers.notNullValue;
1012

1113
public class EmailProviderTest extends JsonTest<EmailProvider> {
1214

@@ -22,7 +24,11 @@ public void shouldSerialize() throws Exception {
2224

2325
String serialized = toJSON(provider);
2426
assertThat(serialized, is(notNullValue()));
25-
assertThat(serialized, is(equalTo(json)));
27+
assertThat(serialized, JsonMatcher.hasEntry("name", "provider"));
28+
assertThat(serialized, JsonMatcher.hasEntry("enabled", true));
29+
assertThat(serialized, JsonMatcher.hasEntry("default_from_address", "https://google.com"));
30+
assertThat(serialized, JsonMatcher.hasEntry("credentials", Collections.singletonMap("api_key", "key123")));
31+
assertThat(serialized, JsonMatcher.hasEntry("settings", notNullValue()));
2632
}
2733

2834
@Test

src/test/java/com/auth0/json/mgmt/EmailVerificationTicketTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.json.mgmt;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.junit.Test;
56

@@ -19,7 +20,9 @@ public void shouldSerialize() throws Exception {
1920

2021
String serialized = toJSON(ticket);
2122
assertThat(serialized, is(notNullValue()));
22-
assertThat(serialized, is(equalTo(json)));
23+
assertThat(serialized, JsonMatcher.hasEntry("user_id", "usr123"));
24+
assertThat(serialized, JsonMatcher.hasEntry("result_url", "https://page.auth0.com/result"));
25+
assertThat(serialized, JsonMatcher.hasEntry("ttl_sec", 36000));
2326
}
2427

2528
@Test

src/test/java/com/auth0/json/mgmt/GuardianEnrollmentTicketTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.auth0.json.mgmt;
22

3+
import com.auth0.JsonMatcher;
34
import com.auth0.json.JsonTest;
45
import org.junit.Test;
56

@@ -19,7 +20,9 @@ public void shouldSerialize() throws Exception {
1920

2021
String serialized = toJSON(ticket);
2122
assertThat(serialized, is(notNullValue()));
22-
assertThat(serialized, is(equalTo(json)));
23+
assertThat(serialized, JsonMatcher.hasEntry("user_id", "1"));
24+
assertThat(serialized, JsonMatcher.hasEntry("send_email", true));
25+
assertThat(serialized, JsonMatcher.hasEntry("email", "me@auth0.com"));
2326
}
2427

2528
@Test

0 commit comments

Comments
 (0)