class RocketChatVerifier {

    public static void checkServerVersion(OkHttpClient client, String baseUrl,
                                          final VersionVerifier versionVerifier,
                                          final SimpleCallback callback) {
        checkNotNull(client, "client == null");
        checkNotNull(baseUrl, "baseUrl == null");
        checkNotNull(versionVerifier, "versionVerifier == null");
        checkNotNull(callback, "callback == null");

        final Moshi moshi = new Moshi.Builder().add(CommonJsonAdapterFactory.create()).build();

        HttpUrl url = HttpUrl.parse(baseUrl).newBuilder()
                .addPathSegment("api")
                .addPathSegment("info")
                .build();

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.onError(new RocketChatNetworkErrorException("network error", e));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    if (!response.isSuccessful()) {
                        JSONObject json = new JSONObject(response.body().string());
                        String message = json.optString("error");
                        String errorType = json.optString("errorType");
                        callback.onError(new RocketChatApiException(response.code(), message,
                                errorType));
                    }

                    JsonAdapter<ServerInfo> adapter = moshi.adapter(ServerInfo.class);
                    ServerInfo info = adapter.fromJson(response.body().string());

                    if (versionVerifier.isValidVersion(info.version())) {
                        callback.onSuccess();
                    } else {
                        callback.onError(new RocketChatException("Unsupported version: "
                                + info.version()));
                    }
                } catch (IOException | JSONException e) {
                    callback.onError(new RocketChatException(e.getMessage(), e));
                }
            }
        });
    }

}

class RocketChatVerifierTest {

    @Mock
    private RocketChatVerifier.VersionVerifier verifier;
    @Mock
    private SimpleCallback callback;
    @Captor ArgumentCaptor<String> versionCaptor;
    @Captor
    private ArgumentCaptor<RocketChatException> exceptionCaptor;
    private DefaultMockServer mockServer;
    private OkHttpClient client;
    private HttpUrl baseUrl;

    @Test
    public void shouldNotCallVersionVerifierWithInvalidResponse() {

        mockServer.expect().withPath("/api/info").andReturn(200, "INVALID_RESPONSE").once();
        RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback);

        verify(verifier, timeout(1000).times(0))
                .isValidVersion(versionCaptor.capture());
    }

}