Search in sources :

Example 41 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project googleads-java-lib by googleads.

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_logging.

@Test
public void testUploadIncrementalBatchJobOperations_logging() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(10, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "<mutate>testUpload</mutate>";
    when(uploadBodyProvider.getHttpContent(request, false, true)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    mockHttpServer.setMockResponse(new MockResponse("testUploadResponse"));
    String expectedBody = "testUpload</mutate>";
    expectedBody = Strings.padEnd(expectedBody, BatchJobUploader.REQUIRED_CONTENT_LENGTH_INCREMENT, ' ');
    // Invoke the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, true, status);
    verify(batchJobLogger, times(1)).logUpload(expectedBody, status.getResumableUploadUri(), response, null);
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Example 42 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project googleads-java-lib by googleads.

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_notFirst_notLast.

@Test
public void testUploadIncrementalBatchJobOperations_notFirst_notLast() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(10, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "<mutate>testUpload</mutate>";
    when(uploadBodyProvider.getHttpContent(request, false, false)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    mockHttpServer.setMockResponse(new MockResponse("testUploadResponse"));
    // Invoked the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, false, status);
    assertEquals("Should have made one request", 1, mockHttpServer.getAllResponses().size());
    // Check the request.
    String firstRequest = mockHttpServer.getLastResponse().getRequestBody();
    String expectedBody = "testUpload";
    expectedBody = Strings.padEnd(expectedBody, BatchJobUploader.REQUIRED_CONTENT_LENGTH_INCREMENT, ' ');
    assertEquals("Request body is incorrect", expectedBody, firstRequest);
    assertEquals("Request should have succeeded", 200, response.getHttpStatus());
    // Check the BatchJobUploadStatus.
    BatchJobUploadStatus expectedStatus = new BatchJobUploadStatus(status.getTotalContentLength() + expectedBody.getBytes(UTF_8).length, URI.create(mockHttpServer.getServerUrl()));
    BatchJobUploadStatus actualStatus = response.getBatchJobUploadStatus();
    assertEquals("Status total content length is incorrect", expectedStatus.getTotalContentLength(), actualStatus.getTotalContentLength());
    assertEquals("Status resumable upload URI is incorrect", expectedStatus.getResumableUploadUri(), actualStatus.getResumableUploadUri());
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Example 43 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project data-transfer-project by google.

the class GoogleVideosInterface method uploadVideoContent.

String uploadVideoContent(InputStream inputStream, String filename) throws IOException {
    InputStreamContent content = new InputStreamContent(null, inputStream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    content.writeTo(outputStream);
    byte[] contentBytes = outputStream.toByteArray();
    HttpContent httpContent = new ByteArrayContent(null, contentBytes);
    return makePostRequest(BASE_URL + "uploads/", Optional.empty(), httpContent, String.class);
}
Also used : InputStreamContent(com.google.api.client.http.InputStreamContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayContent(com.google.api.client.http.ByteArrayContent) HttpContent(com.google.api.client.http.HttpContent) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent)

Aggregations

ByteArrayContent (com.google.api.client.http.ByteArrayContent)43 GenericUrl (com.google.api.client.http.GenericUrl)24 HttpRequest (com.google.api.client.http.HttpRequest)17 IOException (java.io.IOException)12 Test (org.junit.Test)12 HttpResponse (com.google.api.client.http.HttpResponse)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)7 HttpContent (com.google.api.client.http.HttpContent)6 HttpHeaders (com.google.api.client.http.HttpHeaders)6 Gson (com.google.gson.Gson)6 MockResponse (com.google.api.ads.common.lib.testing.MockResponse)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 File (com.google.api.services.drive.model.File)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Credential (com.google.api.client.auth.oauth2.Credential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 HttpResponseException (com.google.api.client.http.HttpResponseException)3 InputStreamContent (com.google.api.client.http.InputStreamContent)3 AbstractInputStreamContent (com.google.api.client.http.AbstractInputStreamContent)2