### Question:
RequestExecutor { public BufferedHttpEntity execute() throws IOException, InterruptedException, FailedHttpResponseException { CloseableHttpClient httpClient = factory.makeHttpClient(); return executeRequest(httpClient); } RequestExecutor(String url, HttpClientFactory factory, Settings settings);   RequestExecutor(HttpUriRequest request, HttpClientFactory factory, Settings settings); RequestExecutor setCredentials(String username, String password); RequestExecutor setCredentials(UsernamePasswordCredentials credentials); BufferedHttpEntity execute(); @Override String toString();  }### Answer:
@Test public void executorDoesNotSetCredentialsToRequestIfExecutorHasNoCredentials() throws FailedHttpResponseException, IOException, InterruptedException { HttpUriRequest request = mockRequest(); executor = new RequestExecutor(request, factory, mock(Settings.class)); executor.execute(); verify(request, times(0)).addHeader(any(Header.class)); }
@Test(expected = IOException.class) public void executeRequestThrowsIoExceptionIfRequestNotAbortedAndHttpClientThrowsIOException() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); when(request.isAborted()).thenReturn(false); when(client.execute(any(HttpUriRequest.class))).thenThrow(new IOException("Foo")); executor = new RequestExecutor(request, factory, mock(Settings.class)); executor.execute(); }
@Test(expected = InterruptedException.class) public void executeThrowsInterruptedExceptionsIfRequestIsAbortedAndHttpClientThrowsIOException() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); when(request.isAborted()).thenReturn(true); when(client.execute(any(HttpUriRequest.class))).thenThrow(new IOException("Foo")); executor = new RequestExecutor(request, factory, mock(Settings.class)); executor.execute(); }
@Test(expected = IOException.class) public void executeThrowsIOExceptionIfResponseEntityIsNull() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); when(response.getEntity()).thenReturn(null); executor = new RequestExecutor(request, factory, mock(Settings.class)); executor.execute(); }
@Test public void userIsLoggedInIfStatusIsOK() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); Settings settings = mock(Settings.class); executor = new RequestExecutor(request, factory, settings); executor.execute(); verify(settings, times(1)).setLoggedIn(true); }
@Test public void entityIsReturnedIfStatusIsOK() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); Settings settings = mock(Settings.class); executor = new RequestExecutor(request, factory, settings); assertNotNull(executor.execute()); }
@Test public void userIsNotLoggedInIfStatusIs403() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); when(line.getStatusCode()).thenReturn(403); Settings settings = mock(Settings.class); executor = new RequestExecutor(request, factory, settings); try { executor.execute(); } catch (FailedHttpResponseException ex) { } verify(settings, times(1)).setLoggedIn(false); }
@Test(expected = FailedHttpResponseException.class) public void executeThrowsFailedHttpResponseExceptionOn() throws ClientProtocolException, IOException, FailedHttpResponseException, InterruptedException { HttpUriRequest request = mockRequest(); when(line.getStatusCode()).thenReturn(403); Settings settings = mock(Settings.class); executor = new RequestExecutor(request, factory, settings); executor.execute(); }