### Question:
PwnedPasswordClient { CompletableFuture<List<Hex>> fetchHashesAsync(Hex hashedPassword) { Call call = httpClient.newCall(buildRequest(hashedPassword)); CompletableFuture<List<Hex>> future = new CompletableFuture<>(); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { future.completeExceptionally(e); } @Override public void onResponse(Call call, Response response) { try { if (!response.isSuccessful()) { future.completeExceptionally(new ConnectException("Call failed to " + url + " got " + response)); } else { future.complete(parseResponse(hashedPassword.firstFive(), response)); } } catch (IOException e) { future.completeExceptionally(e); } finally { response.close(); } } }); return future; } PwnedPasswordClient(OkHttpClient httpClient, String url, String userAgent);   }### Answer:
@Test public void wrongURLShouldThrowException() { PwnedPasswordClient pwnedPasswordClient = new PwnedPasswordClient(new OkHttpClient(), "http: try { pwnedPasswordClient.fetchHashesAsync(Hex.from("password")).get(); fail(); } catch (InterruptedException e) { fail(); } catch (ExecutionException e) { assertTrue(e.getCause().getClass() == ConnectException.class); } }
@Test public void responseIsNotFoundShouldThrowException() { PwnedPasswordClient pwnedPasswordClient = new PwnedPasswordClient(new OkHttpClient(), "http: try { pwnedPasswordClient.fetchHashesAsync(Hex.from("password")).get(); fail(); } catch (ExecutionException e) { assertTrue(e.getCause().getClass() == ConnectException.class); } catch (InterruptedException e) { fail(); } }
@Test public void responseIsEmptyShouldReturnEmptyList() throws ExecutionException, InterruptedException { instanceRule.stubFor(WireMock.get(urlEqualTo("/range/5BAA6")).willReturn(aResponse().withBody("003D68EB55068C33ACE09247EE4C639306B:3\n" + "012C192B2F16F82EA0EB9EF18D9D539B0DD:1"))); PwnedPasswordClient pwnedPasswordClient = new PwnedPasswordClient(new OkHttpClient(), "http: assertThat(pwnedPasswordClient.fetchHashesAsync(Hex.from("5BAA6")).get().size(), is(2)); }
@Test public void wrongHashReturns400() { instanceRule.stubFor(WireMock.get(urlEqualTo("/range/5BAA6A")).willReturn(aResponse().withStatus(400))); PwnedPasswordClient pwnedPasswordClient = new PwnedPasswordClient(new OkHttpClient(), "http: try { pwnedPasswordClient.fetchHashesAsync(Hex.from("5BAA6A")).get(); fail(); } catch (ExecutionException e) { assertTrue(e.getCause().getClass() == ConnectException.class); } catch (InterruptedException e) { fail(); } }