TelemetryDiagnosticControls { public void checkTransmission() throws Exception { telemetryClient.disconnect(); int currentRetry = 1; while (! telemetryClient.getOnlineStatus() && currentRetry <= 3) { telemetryClient.connect(DIAGNOSTIC_CHANNEL_CONNECTION_STRING); currentRetry ++; } if (! telemetryClient.getOnlineStatus()) { throw new IllegalStateException("Unable to connect."); } telemetryClient.send(TelemetryClient.DIAGNOSTIC_MESSAGE); diagnosticInfo = telemetryClient.receive(); } TelemetryDiagnosticControls(TelemetryClient telemetryClient); String getDiagnosticInfo(); void setDiagnosticInfo(String diagnosticInfo); void checkTransmission(); static final String DIAGNOSTIC_CHANNEL_CONNECTION_STRING; }
@Test public void disconnects() throws Exception { controls.checkTransmission(); verify(client).disconnect(); }
@Test public void sends() throws Exception { controls.checkTransmission(); verify(client).send(TelemetryClient.DIAGNOSTIC_MESSAGE); }
@Test(expected = Exception.class) public void throwsWhenNotOnline() throws Exception { when(client.getOnlineStatus()).thenReturn(false); controls.checkTransmission(); }
@Test public void triesToConnectIfNotOnline() throws Exception { when(client.getOnlineStatus()).thenReturn(false, true); controls.checkTransmission(); verify(client).connect(TelemetryDiagnosticControls.DIAGNOSTIC_CHANNEL_CONNECTION_STRING); }
@Test public void doesntRetryMoreThan3Times() throws Exception { when(client.getOnlineStatus()).thenReturn(false); try { controls.checkTransmission(); } catch (Exception e) { } verify(client, times(3)).connect(TelemetryDiagnosticControls.DIAGNOSTIC_CHANNEL_CONNECTION_STRING); }