HttpConnectionServerChecker extends ServerChecker { @Override public boolean isAvailable() { HttpURLConnection httpURLConnection = null; try { httpURLConnection = createConnection(url); httpURLConnection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(3)); httpURLConnection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(3)); httpURLConnection.setRequestProperty(CONNECTION_HEADER, CONNECTION_CLOSE); if (token != null) { httpURLConnection.setRequestProperty(AUTHORIZATION_HEADER, "Bearer " + token); } return isConnectionSuccessful(httpURLConnection); } catch (IOException e) { return false; } finally { if (httpURLConnection != null) { httpURLConnection.disconnect(); } } } HttpConnectionServerChecker(
      URL url,
      String machineName,
      String serverRef,
      long period,
      long timeout,
      int successThreshold,
      TimeUnit timeUnit,
      Timer timer,
      String token); @Override boolean isAvailable();  }
@Test public void shouldSetTimeoutsToConnection() throws Exception { checker.isAvailable(); verify(conn).setReadTimeout((int) TimeUnit.SECONDS.toMillis(3)); verify(conn).setConnectTimeout((int) TimeUnit.SECONDS.toMillis(3)); }
@Test public void shouldBeAbleToConfirmAvailability() throws Exception { assertTrue(checker.isAvailable()); }
@Test public void shouldBeAbleToRejectAvailability() throws Exception { when(conn.getResponseCode()).thenReturn(401); assertFalse(checker.isAvailable()); }
@Test public void shouldRejectAvailabilityInCaseOfExceptionOnResponseCodeRetrieving() throws Exception { when(conn.getResponseCode()).thenThrow(new IOException()); assertFalse(checker.isAvailable()); }
@Test public void shouldDisconnectIfAvailable() throws Exception { assertTrue(checker.isAvailable()); verify(conn).disconnect(); }
@Test public void shouldDisconnectIfNotAvailable() throws Exception { when(conn.getResponseCode()).thenReturn(401); assertFalse(checker.isAvailable()); verify(conn).disconnect(); }