@Override protected InstrumentationDefinition performRemoteCall(IAgentService service) throws Exception { return agentService.analyze(platformIdent, hash, type); } };
@Test public void analyzeAndInstrument() throws Exception { InstrumentationDefinition instrumentationResult = mock(InstrumentationDefinition.class); when(client.isConnected()).thenReturn(true); doReturn(instrumentationResult).when(agentService).analyze(anyLong(), anyString(), Matchers.<Type> any()); long id = 7; String hash = "hash"; Type type = mock(Type.class); InstrumentationDefinition receivedResult = connection.analyze(id, hash, type); assertThat(receivedResult, is(instrumentationResult)); verify(agentService, times(1)).analyze(id, hash, type); verifyNoMoreInteractions(agentService); }
@Test(expectedExceptions = { BusinessException.class }) public void businessException() throws Exception { when(client.isConnected()).thenReturn(true); doThrow(BusinessException.class).when(agentService).analyze(anyLong(), anyString(), Matchers.<Type> any()); long id = 7; String hash = "hash"; Type type = mock(Type.class); try { connection.analyze(id, hash, type); } finally { verify(agentService, times(1)).analyze(id, hash, type); verifyNoMoreInteractions(agentService); } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void timeout() throws Exception { when(client.isConnected()).thenReturn(true); doThrow(TimeoutException.class).when(agentService).analyze(anyLong(), anyString(), Matchers.<Type> any()); long id = 7; String hash = "hash"; Type type = mock(Type.class); try { connection.analyze(id, hash, type); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(true)); throw e; } finally { verify(agentService, times(1)).analyze(id, hash, type); verifyNoMoreInteractions(agentService); } }
@Test(expectedExceptions = { ServerUnavailableException.class }) public void remoteException() throws Exception { when(client.isConnected()).thenReturn(true); doThrow(RuntimeException.class).when(agentService).analyze(anyLong(), anyString(), Matchers.<Type> any()); long id = 7; String hash = "hash"; Type type = mock(Type.class); try { connection.analyze(id, hash, type); } catch (ServerUnavailableException e) { assertThat(e.isServerTimeout(), is(false)); throw e; } finally { // fail fast call, only one attempt verify(agentService, times(1)).analyze(id, hash, type); verifyNoMoreInteractions(agentService); verify(client).close(); } }