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)); } } }); }  static void checkServerVersion(OkHttpClient client, String baseUrl,
                                          final VersionVerifier versionVerifier,
                                          final SimpleCallback callback);  }
@Test public void shouldNotCallVersionVerifierOnNetworkError() { mockServer.shutdown(); RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback); verify(verifier, timeout(1000).times(0)) .isValidVersion(versionCaptor.capture()); }
@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()); }
@Test public void shouldCallVersionVerifierWithRightVersion() { mockServer.expect().withPath("/api/info") .andReturn(200, "{\"version\":\"0.59.0\"}").once(); RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback); verify(verifier, timeout(1000)).isValidVersion(versionCaptor.capture()); assertThat(versionCaptor.getValue(), is(equalTo("0.59.0"))); }
@Test public void shouldCallCallbackOnNetworkError() { mockServer.shutdown(); RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback); verify(callback, timeout(1000) .times(1)) .onError(exceptionCaptor.capture()); assertThat(exceptionCaptor.getValue(), is(instanceOf(RocketChatNetworkErrorException.class))); }
@Test public void shouldCallCallbackOnInvalidResponse() { mockServer.expect().withPath("/api/info").andReturn(200, "INVALID_RESPONSE").once(); RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback); verify(callback, timeout(1000) .times(1)) .onError(exceptionCaptor.capture()); assertThat(exceptionCaptor.getValue(), is(instanceOf(RocketChatException.class))); assertThat(exceptionCaptor.getValue().getCause(), is(instanceOf(JsonEncodingException.class))); assertThat(exceptionCaptor.getValue().getMessage(), containsString("Use JsonReader.setLenient")); }
@Test public void shouldSuccessOnVersionVerified() { given(verifier.isValidVersion(anyString())).willReturn(true); mockServer.expect().withPath("/api/info") .andReturn(200, "{\"version\":\"0.59.0\"}").once(); RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback); verify(callback, timeout(1000).times(1)).onSuccess(); }
@Test public void shouldFailIfVersionNotVerified() { given(verifier.isValidVersion(anyString())).willReturn(false); mockServer.expect().withPath("/api/info") .andReturn(200, "{\"version\":\"0.59.0\"}").once(); RocketChatVerifier.checkServerVersion(client, baseUrl.toString(), verifier, callback); verify(callback, timeout(1000).times(1)) .onError(exceptionCaptor.capture()); assertThat(exceptionCaptor.getValue(), is(instanceOf(RocketChatException.class))); assertThat(exceptionCaptor.getValue().getMessage(), is(equalTo("Unsupported version: 0.59.0"))); }