Skip to content

Commit 5bc6c83

Browse files
committed
test cleanup
1 parent 17e3492 commit 5bc6c83

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/test/java/com/saasquatch/json_schema_inferrer/JsonSchemaInferrerExamplesTest.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.hc.client5.http.config.RequestConfig;
2323
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
2424
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
25-
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
2625
import org.apache.hc.client5.http.impl.classic.HttpClients;
2726
import org.apache.hc.core5.http.HttpEntity;
2827
import org.apache.hc.core5.http.io.entity.EntityUtils;
@@ -44,8 +43,7 @@ public class JsonSchemaInferrerExamplesTest {
4443

4544
private static final String quicktypeCommitHash = "f75f66bff3d1f812b61c481637c12173778a29b8";
4645
private static CloseableHttpClient httpClient;
47-
private final ObjectMapper mapper = new ObjectMapper();
48-
private final Collection<JsonSchemaInferrer> testInferrers = getTestInferrers();
46+
private static final ObjectMapper mapper = new ObjectMapper();
4947

5048
@BeforeAll
5149
public static void beforeAll() {
@@ -64,7 +62,7 @@ public void test() {
6462
}
6563
}
6664

67-
private void doTestForJsonUrl(String jsonUrl) {
65+
private static void doTestForJsonUrl(String jsonUrl) {
6866
final JsonNode sampleJson;
6967
// Not being able to load the sample JSON should not be considered a failure
7068
try {
@@ -78,7 +76,7 @@ private void doTestForJsonUrl(String jsonUrl) {
7876
return;
7977
}
8078
System.out.printf(Locale.ROOT, "Got valid JSON from url[%s]\n", jsonUrl);
81-
for (JsonSchemaInferrer inferrer : testInferrers) {
79+
for (JsonSchemaInferrer inferrer : getTestInferrers()) {
8280
final ObjectNode schemaJson = inferrer.infer(sampleJson);
8381
assertNotNull(schemaJson, format("Inferred schema for url[%s] is null", jsonUrl));
8482
final Schema schema;
@@ -97,6 +95,7 @@ private void doTestForJsonUrl(String jsonUrl) {
9795
schema.validate(mapper.convertValue(sampleJson, Object.class));
9896
}
9997
} catch (ValidationException e) {
98+
System.out.println(e.getClass().getSimpleName() + " encountered");
10099
System.out.println(schemaJson.toPrettyString());
101100
System.out.println("Error messages:");
102101
e.getAllMessages().forEach(System.out::println);
@@ -105,18 +104,18 @@ private void doTestForJsonUrl(String jsonUrl) {
105104
}
106105
}
107106

108-
private Iterable<String> getSampleJsonUrls() {
107+
private static Iterable<String> getSampleJsonUrls() {
109108
return () -> getQuicktypeSampleJsonUrls().iterator();
110109
}
111110

112-
private Stream<String> getQuicktypeSampleJsonUrls() {
111+
private static Stream<String> getQuicktypeSampleJsonUrls() {
113112
return Stream
114113
.of("/test/inputs/json/misc", "/test/inputs/json/priority", "/test/inputs/json/samples")
115114
.map("https://api.github.com/repos/quicktype/quicktype/contents"::concat)
116115
.flatMap(dirBaseUrl -> getJsonDownloadUrls(dirBaseUrl, quicktypeCommitHash));
117116
}
118117

119-
private Stream<String> getJsonDownloadUrls(String dirBaseUrl, String commitHash) {
118+
private static Stream<String> getJsonDownloadUrls(String dirBaseUrl, String commitHash) {
120119
final JsonNode respJson;
121120
try {
122121
final HttpGet request = new HttpGet(
@@ -140,7 +139,7 @@ public JsonNode handleEntity(HttpEntity entity) throws IOException {
140139
.map(downloadUrl -> processGitHubDownloadUrl(downloadUrl, commitHash));
141140
}
142141

143-
private String processGitHubDownloadUrl(String url, String commitHash) {
142+
private static String processGitHubDownloadUrl(String url, String commitHash) {
144143
try {
145144
final String host = new URL(url).getHost();
146145
url = url.replaceFirst(host, "cdn.jsdelivr.net/gh");
@@ -177,36 +176,34 @@ private static Collection<JsonSchemaInferrer> getTestInferrers() {
177176
return Collections.unmodifiableList(inferrers);
178177
}
179178

180-
private static String format(String format, Object... args) {
181-
return String.format(Locale.ROOT, format, args);
182-
}
183-
184-
private Map<String, Object> toMap(Object o) {
185-
return mapper.convertValue(o, new TypeReference<Map<String, Object>>() {});
186-
}
187-
188179
@Nullable
189-
private JsonNode loadJsonFromUrl(String jsonUrl) throws IOException {
180+
private static JsonNode loadJsonFromUrl(String jsonUrl) throws IOException {
190181
final HttpGet request = new HttpGet(jsonUrl);
191182
request.setConfig(RequestConfig.custom()
192183
.setConnectTimeout(1, TimeUnit.SECONDS)
193184
.setConnectionRequestTimeout(1, TimeUnit.SECONDS)
194185
.setResponseTimeout(2500, TimeUnit.MILLISECONDS)
195186
.build());
196-
try (CloseableHttpResponse response = httpClient.execute(request)) {
197-
final int status = response.getCode();
198-
if (status >= 300) {
199-
System.out.printf(Locale.ROOT, "status[%d] received for url[%s]\n", status, jsonUrl);
200-
return null;
201-
}
202-
final byte[] byteArray = EntityUtils.toByteArray(response.getEntity());
203-
if (byteArray.length > 1 << 20) {
204-
System.out.printf(Locale.ROOT, "JSON at url[%s] is too large [%d].\n", jsonUrl,
205-
byteArray.length);
206-
return null;
187+
return httpClient.execute(request, new AbstractHttpClientResponseHandler<JsonNode>() {
188+
@Override
189+
public JsonNode handleEntity(HttpEntity entity) throws IOException {
190+
final byte[] byteArray = EntityUtils.toByteArray(entity);
191+
if (byteArray.length > 1 << 20) {
192+
System.out.printf(Locale.ROOT, "JSON at url[%s] is too large [%d].\n", jsonUrl,
193+
byteArray.length);
194+
return null;
195+
}
196+
return mapper.readTree(byteArray);
207197
}
208-
return mapper.readTree(byteArray);
209-
}
198+
});
199+
}
200+
201+
private static String format(String format, Object... args) {
202+
return String.format(Locale.ROOT, format, args);
203+
}
204+
205+
private static Map<String, Object> toMap(Object o) {
206+
return mapper.convertValue(o, new TypeReference<Map<String, Object>>() {});
210207
}
211208

212209
}

0 commit comments

Comments
 (0)