@Test public void register() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(client.isConnected()).thenReturn(true); doReturn(agentConfiguration).when(agentService).register(Matchers.<List<String>> any(), anyString(), anyString()); String agentName = "agentName"; String version = "version"; AgentConfig receivedAgentConfiguration = connection.register(agentName, version); assertThat(receivedAgentConfiguration, is(agentConfiguration)); verify(agentService, times(1)).register(Matchers.<List<String>> any(), eq(agentName), eq(version)); verifyNoMoreInteractions(agentService); }
@Test(expectedExceptions = { BusinessException.class }) public void businessException() throws Exception { when(client.isConnected()).thenReturn(true); doThrow(BusinessException.class).when(agentService).register(Matchers.<List<String>> any(), anyString(), anyString()); String agentName = "agentName"; String version = "version"; try { connection.register(agentName, version); } finally { verify(agentService, times(1)).register(Matchers.<List<String>> any(), eq(agentName), eq(version)); verifyNoMoreInteractions(agentService); } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void notConnected() throws Exception { when(client.isConnected()).thenReturn(false); String agentName = "agentName"; String version = "version"; try { connection.register(agentName, version); } 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); doThrow(TimeoutException.class).when(agentService).register(Matchers.<List<String>> any(), anyString(), anyString()); String agentName = "agentName"; String version = "version"; try { connection.register(agentName, version); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(true)); throw e; } finally { verify(agentService, times(1)).register(Matchers.<List<String>> any(), eq(agentName), eq(version)); verifyNoMoreInteractions(agentService); } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void remoteException() throws Exception { when(client.isConnected()).thenReturn(true); doThrow(RuntimeException.class).when(agentService).register(Matchers.<List<String>> any(), anyString(), anyString()); String agentName = "agentName"; String version = "version"; try { connection.register(agentName, version); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { // fail fast call, only one attempt verify(agentService, times(1)).register(Matchers.<List<String>> any(), eq(agentName), eq(version)); verifyNoMoreInteractions(agentService); verify(client).close(); } }