/** * 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();
MockHttpTransport otherTransport = new MockHttpTransport.Builder() .setLowLevelHttpResponse(otherServiceUnavaiableLowLevelResponse) .build(); HttpRequest otherRequest = otherTransport .createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
/** * Builds a HttpResponse with the given string response. * * @param header header value to provide or null if none. * @param uploadId upload id to provide in the url upload id param or null if none. * @param uploadType upload type to provide in url upload type param or null if none. * @return HttpResponse with the given parameters * @throws IOException */ private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType) throws IOException { MockHttpTransport.Builder builder = new MockHttpTransport.Builder(); MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse(); builder.setLowLevelHttpResponse(resp); resp.setStatusCode(200); GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL); if (header != null) { resp.addHeader("X-GUploader-UploadID", header); } if (uploadId != null) { url.put("upload_id", uploadId); } if (uploadType != null) { url.put("uploadType", uploadType); } return builder.build().createRequestFactory().buildGetRequest(url).execute(); }
new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();
@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(); }
HttpResponseException makeHttpException(int status) throws IOException { MockHttpTransport.Builder builder = new MockHttpTransport.Builder(); MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse(); resp.setStatusCode(status); builder.setLowLevelHttpResponse(resp); try { HttpResponse res = builder.build() .createRequestFactory() .buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL) .execute(); return new HttpResponseException(res); } catch (HttpResponseException exception) { return exception; // Throws the exception we want anyway, so just return it. } }
@Test public void testFileSizeWhenFileNotFoundNonBatch() throws Exception { MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse(); notFoundResponse.setContent(""); notFoundResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build(); GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null)); thrown.expect(FileNotFoundException.class); gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject")); }
@Override public MockGoogleCredential build() { if (getTransport() == null) { setTransport(new MockHttpTransport.Builder().build()); } if (getClientAuthentication() == null) { setClientAuthentication(new MockClientAuthentication()); } if (getJsonFactory() == null) { setJsonFactory(new JacksonFactory()); } return new MockGoogleCredential(this); } }