/** * Tests that IOExceptions from executing an upload request are propagated properly. */ @SuppressWarnings("rawtypes") @Test public void testUploadBatchJobOperations_ioException_fails() throws Exception { final IOException ioException = new IOException("mock IO exception"); MockLowLevelHttpRequest lowLevelHttpRequest = new MockLowLevelHttpRequest(){ @Override public LowLevelHttpResponse execute() throws IOException { throw ioException; } }; when(uploadBodyProvider.getHttpContent(request, true, true)) .thenReturn(new ByteArrayContent(null, "foo".getBytes(UTF_8))); MockHttpTransport transport = new MockHttpTransport.Builder() .setLowLevelHttpRequest(lowLevelHttpRequest).build(); uploader = new BatchJobUploader(adWordsSession, transport, batchJobLogger); BatchJobUploadStatus uploadStatus = new BatchJobUploadStatus(0, URI.create("http://www.example.com")); thrown.expect(BatchJobException.class); thrown.expectCause(Matchers.sameInstance(ioException)); uploader.uploadIncrementalBatchJobOperations(request, true, uploadStatus); }
/** * Tests that IOExceptions from initiating an upload are propagated properly. */ @SuppressWarnings("rawtypes") @Test public void testUploadBatchJobOperations_initiateFails_fails() throws Exception { final IOException ioException = new IOException("mock IO exception"); MockLowLevelHttpRequest lowLevelHttpRequest = new MockLowLevelHttpRequest(){ @Override public LowLevelHttpResponse execute() throws IOException { throw ioException; } }; when(uploadBodyProvider.getHttpContent(request, true, true)) .thenReturn(new ByteArrayContent(null, "foo".getBytes(UTF_8))); MockHttpTransport transport = new MockHttpTransport.Builder() .setLowLevelHttpRequest(lowLevelHttpRequest).build(); uploader = new BatchJobUploader(adWordsSession, transport, batchJobLogger); thrown.expect(BatchJobException.class); thrown.expectCause(Matchers.sameInstance(ioException)); thrown.expectMessage("initiate upload"); uploader.uploadIncrementalBatchJobOperations(request, true, new BatchJobUploadStatus( 0, URI.create("http://www.example.com"))); }
new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build();
.setLowLevelHttpRequest( new MockLowLevelHttpRequest() { @Override
.setLowLevelHttpRequest( new MockLowLevelHttpRequest() { @Override
@Before public void setUp() { MockitoAnnotations.initMocks(this); // Set up the MockHttpRequest for future inspection request = new MockLowLevelHttpRequest() { @Override public LowLevelHttpResponse execute() throws IOException { return response; } }; // A mock transport that lets us mock the API responses. MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build(); // A sample BigQuery API client that uses default JsonFactory and RetryHttpInitializer. bigquery = new Bigquery.Builder( transport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()) .build(); }
/** * Returns a new {@link MockHttpTransport} with a sample {@link MockLowLevelHttpResponse}. The * response includes sample TokenResponse content as specified in DEFAULT_TOKEN_RESPONSE_JSON. This * is meant to produce a minimal implementation that allows methods such as * {@link GoogleCredential#executeRefreshToken()} to be called without failing abruptly. This * content is static. If you are making assertions based on the content of the request, then * MockTokenServerTransport should be used instead. * * @return mockHttpTransport */ public static MockHttpTransport newMockHttpTransportWithSampleTokenResponse() { MockLowLevelHttpResponse mockLowLevelHttpResponse = new MockLowLevelHttpResponse() .setContentType(Json.MEDIA_TYPE) .setContent(DEFAULT_TOKEN_RESPONSE_JSON); MockLowLevelHttpRequest request = new MockLowLevelHttpRequest() .setResponse(mockLowLevelHttpResponse); return new MockHttpTransport.Builder() .setLowLevelHttpRequest(request) .build(); }