BallExecutorDelivery implements BallResponseDelivery { @Override public void postResponse(BallRequest<?> request, BallResponse<?> response) { request.addMarker(MARKER_POST_RESPONSE); mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, null)); } BallExecutorDelivery(final Handler handler, PriorityBlockingQueue<BallRequest> networkQueue);  BallExecutorDelivery(Executor executor); @Override void postResponse(BallRequest<?> request, BallResponse<?> response); @Override void postResponseAndForwardToNetwork(BallRequest<?> request, BallResponse<?> response); @Override void postError(BallRequest<?> request, VolleyError error); @Override void postEmptyIntermediateResponse(BallRequest request, BallResponse.ResponseSource responseSource); static final String MARKER_POST_EMPTY_INTERMEDIATE_RESPONSE; static final String MARKER_POST_RESPONSE; static final String MARKER_POST_ERROR; static final String MARKER_INTERMEDIATE_RESPONSE_ALREADY_DELIVERED; static final String MARKER_DONE_WITH_RESPONSE_FROM; static final String MARKER_ERROR_IN_FINAL_RESPONSE_LET_INTERMEDIATE_CONTINUE; static final String MARKER_DONE_WITH_INTERMEDIATE_EMPTY_RESPONSE; static final String MARKER_DONE_WITH_INTERMEDIATE_RESPONSE; }
@Test public void shouldDeliverFinalSuccessResponse() { Object response = new Object(); BallResponse.ResponseSource responseSource = BallResponse.ResponseSource.NETWORK; when(mRequest.isIntermediateResponseDelivered()).thenReturn(true); when(mResponse.isIntermediate()).thenReturn(false); when(mResponse.isSuccess()).thenReturn(true); when(mResponse.isIdentical()).thenReturn(false); when(mResponse.getResult()).thenReturn(response); when(mResponse.getResponseSource()).thenReturn(responseSource); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest).setFinalResponseDelivered(true); verify(mRequest).deliverFinalResponse(response, responseSource); verify(mRequest, new LastInteraction()).finish(String.format(BallExecutorDelivery.MARKER_DONE_WITH_RESPONSE_FROM, mResponse.getResponseSource().toString().toLowerCase())); }
@Test public void shouldDeliverFinalIdenticalSuccessResponse() { Object response = new Object(); BallResponse.ResponseSource responseSource = BallResponse.ResponseSource.NETWORK; when(mRequest.isIntermediateResponseDelivered()).thenReturn(true); when(mResponse.isIntermediate()).thenReturn(false); when(mResponse.isSuccess()).thenReturn(true); when(mResponse.isIdentical()).thenReturn(true); when(mResponse.getResult()).thenReturn(response); when(mResponse.getResponseSource()).thenReturn(responseSource); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest).setFinalResponseDelivered(true); verify(mRequest).deliverIdenticalFinalResponse(responseSource); verify(mRequest, new LastInteraction()).finish(String.format(BallExecutorDelivery.MARKER_DONE_WITH_RESPONSE_FROM, mResponse.getResponseSource().toString().toLowerCase())); }
@Test public void shouldDeliverFinalErrorResponseWhenIntermediateIsDelivered() { VolleyError error = new VolleyError("Some error"); when(mRequest.isIntermediateResponseDelivered()).thenReturn(true); when(mResponse.isIntermediate()).thenReturn(false); when(mResponse.isSuccess()).thenReturn(false); when(mResponse.getError()).thenReturn(error); when(mResponse.getResponseSource()).thenReturn(BallResponse.ResponseSource.NETWORK); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest).deliverError(error); verify(mRequest, new LastInteraction()).finish(String.format(BallExecutorDelivery.MARKER_DONE_WITH_RESPONSE_FROM, mResponse.getResponseSource().toString().toLowerCase())); }
@Test public void shouldNotDeliverFinalErrorResponseWhenIntermediateIsNotYetDelivered() { VolleyError error = new VolleyError("Some error"); when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mResponse.isIntermediate()).thenReturn(false); when(mResponse.isSuccess()).thenReturn(false); when(mResponse.getError()).thenReturn(error); when(mResponse.getResponseSource()).thenReturn(BallResponse.ResponseSource.NETWORK); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest).addMarker(BallExecutorDelivery.MARKER_ERROR_IN_FINAL_RESPONSE_LET_INTERMEDIATE_CONTINUE); verify(mRequest, new LastInteraction()).setFinalResponseError(error); verify(mRequest, never()).deliverError(any(VolleyError.class)); }
@Test public void shouldDeliverIntermediateAndThenStoredFinalErrorResponse() { VolleyError error = new VolleyError("Some error"); Object response = new Object(); when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mRequest.isFinalResponseDelivered()).thenReturn(true); when(mRequest.getFinalResponseError()).thenReturn(error); when(mResponse.isIntermediate()).thenReturn(true); when(mResponse.isSuccess()).thenReturn(true); when(mResponse.getResult()).thenReturn(response); when(mResponse.getResponseSource()).thenReturn(BallResponse.ResponseSource.CACHE); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest).deliverIntermediateResponse(response, BallResponse.ResponseSource.CACHE); verify(mRequest).deliverError(error); verify(mRequest, new LastInteraction()).finish(BallExecutorDelivery.MARKER_DONE_WITH_INTERMEDIATE_RESPONSE); }
@Test(expected = BallException.class) public void shouldThrowWhenDeliverIntermediateAfterFinalWithoutStoredError() { when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mRequest.isFinalResponseDelivered()).thenReturn(true); when(mResponse.isIntermediate()).thenReturn(true); when(mResponse.isSuccess()).thenReturn(true); when(mResponse.getResponseSource()).thenReturn(BallResponse.ResponseSource.CACHE); mResponseDelivery.postResponse(mRequest, mResponse); }
@Test public void shouldPostResponseAndAddMarkerLog() { Executor executorMock = mock(Executor.class); BallResponseDelivery responseDelivery = new BallExecutorDelivery(executorMock); final Runnable runnable = new Runnable() { @Override public void run() { doThrow(new MockitoException("Runnable should not run")); } }; doNothing().when(executorMock).execute(any(Runnable.class)); responseDelivery.postResponse(mRequest, mResponse); responseDelivery.postResponse(mRequest, mResponse, runnable); verify(mRequest, times(2)).addMarker(BallExecutorDelivery.MARKER_POST_RESPONSE); verify(executorMock).execute(argThat(new ArgumentMatcher<BallExecutorDelivery.ResponseDeliveryRunnable>() { @Override public boolean matches(Object argument) { BallExecutorDelivery.ResponseDeliveryRunnable r = (BallExecutorDelivery.ResponseDeliveryRunnable) argument; return r.mRequest == mRequest && r.mResponse == mResponse && r.mRunnable == null; } })); verify(executorMock).execute(argThat(new ArgumentMatcher<BallExecutorDelivery.ResponseDeliveryRunnable>() { @Override public boolean matches(Object argument) { BallExecutorDelivery.ResponseDeliveryRunnable r = (BallExecutorDelivery.ResponseDeliveryRunnable) argument; return r.mRequest == mRequest && r.mResponse == mResponse && r.mRunnable == runnable; } })); }
@Test public void shouldIgnoreSecondIntermediateResponse() { when(mRequest.isIntermediateResponseDelivered()).thenReturn(true); when(mResponse.isIntermediate()).thenReturn(true); when(mResponse.getResponseSource()).thenReturn(BallResponse.ResponseSource.LOCAL); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest, new LastInteraction()).addMarker(BallExecutorDelivery.MARKER_INTERMEDIATE_RESPONSE_ALREADY_DELIVERED); }
@Test public void shouldDeliverIntermediateSuccessResponse() { Object response = new Object(); BallResponse.ResponseSource responseSource = BallResponse.ResponseSource.LOCAL; when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mResponse.isIntermediate()).thenReturn(true); when(mResponse.isSuccess()).thenReturn(true); when(mResponse.getResult()).thenReturn(response); when(mResponse.getResponseSource()).thenReturn(responseSource); mResponseDelivery.postResponse(mRequest, mResponse); verify(mRequest).setIntermediateResponseDelivered(true); verify(mRequest).deliverIntermediateResponse(response, responseSource); verify(mRequest, new LastInteraction()).isFinalResponseDelivered(); }
@Test public void shouldDeliverIntermediateSuccessResponseWithPostRunnable() { Object response = new Object(); BallResponse.ResponseSource responseSource = BallResponse.ResponseSource.LOCAL; Runnable postRunnable = mock(Runnable.class); when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mResponse.isIntermediate()).thenReturn(true); when(mResponse.isSuccess()).thenReturn(true); when(mResponse.getResult()).thenReturn(response); when(mResponse.getResponseSource()).thenReturn(responseSource); mResponseDelivery.postResponse(mRequest, mResponse, postRunnable); verify(mRequest).setIntermediateResponseDelivered(true); verify(mRequest).deliverIntermediateResponse(response, responseSource); verify(mRequest, new LastInteraction()).isFinalResponseDelivered(); verify(postRunnable).run(); }
@Test(expected = BallException.class) public void shouldThrowWhenIntermediateErrorResponse() { when(mRequest.isIntermediateResponseDelivered()).thenReturn(false); when(mResponse.isIntermediate()).thenReturn(true); when(mResponse.isSuccess()).thenReturn(false); when(mResponse.getResponseSource()).thenReturn(BallResponse.ResponseSource.LOCAL); mResponseDelivery.postResponse(mRequest, mResponse); }