@Test public void fetchAgentMessages() throws Exception { IAgentMessage<?> message = mock(IAgentMessage.class); List<IAgentMessage<?>> messages = Arrays.<IAgentMessage<?>> asList(message); when(client.isConnected()).thenReturn(true); long id = 7; when(agentService.fetchAgentMessages(id)).thenReturn(messages); List<IAgentMessage<?>> result = connection.fetchAgentMessages(id); assertThat(result, is(equalTo(messages))); verify(agentService).fetchAgentMessages(id); verifyNoMoreInteractions(agentService); }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void notConnected() throws Exception { when(client.isConnected()).thenReturn(false); long id = 7; try { connection.fetchAgentMessages(id); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { verifyZeroInteractions(agentService); } } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void timeout() throws Exception { when(client.isConnected()).thenReturn(true); long id = 7; when(agentService.fetchAgentMessages(id)).thenThrow(TimeoutException.class); try { connection.fetchAgentMessages(id); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(true)); throw e; } finally { verify(agentService).fetchAgentMessages(id); verifyNoMoreInteractions(agentService); } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void remoteException() throws Exception { when(client.isConnected()).thenReturn(true); long id = 7; when(agentService.fetchAgentMessages(id)).thenThrow(RuntimeException.class); try { connection.fetchAgentMessages(id); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { // fail fast call, only one attempt verify(agentService).fetchAgentMessages(id); verifyNoMoreInteractions(agentService); verify(client).close(); } }