Search in sources :

Example 51 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project beam by apache.

the class GcsUtilTest method testFileSizeWhenFileNotFoundNonBatch.

@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"));
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) Test(org.junit.Test)

Example 52 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project beam by apache.

the class RetryHttpRequestInitializerTest method createMockResponseWithStatusCode.

MockLowLevelHttpResponse[] createMockResponseWithStatusCode(int... statusCodes) {
    MockLowLevelHttpResponse[] responses = new MockLowLevelHttpResponse[statusCodes.length];
    for (int i = 0; i < statusCodes.length; ++i) {
        MockLowLevelHttpResponse response = mock(MockLowLevelHttpResponse.class);
        when(response.getStatusCode()).thenReturn(statusCodes[i]);
        responses[i] = response;
    }
    return responses;
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse)

Example 53 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project beam by apache.

the class RetryHttpRequestInitializerTest method testErrorCodeForbidden.

/**
 * Tests that a non-retriable error is not retried.
 */
@Test
public void testErrorCodeForbidden() throws IOException {
    MockLowLevelHttpResponse[] responses = createMockResponseWithStatusCode(// Non-retryable error.
    403, // Shouldn't happen.
    200);
    when(mockLowLevelRequest.execute()).thenReturn(responses[0], responses[1]);
    try {
        Storage.Buckets.Get result = storage.buckets().get("test");
        HttpResponse response = result.executeUnparsed();
        assertNotNull(response);
    } catch (HttpResponseException e) {
        assertThat(e.getMessage(), Matchers.containsString("403"));
    }
    verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class));
    verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString());
    verify(mockLowLevelRequest).setTimeout(anyInt(), anyInt());
    verify(mockLowLevelRequest).setWriteTimeout(anyInt());
    verify(mockLowLevelRequest).execute();
    verify(responses[0], atLeastOnce()).getStatusCode();
    verify(responses[1], never()).getStatusCode();
    expectedLogs.verifyWarn("Request failed with code 403");
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpResponse(com.google.api.client.http.HttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) Test(org.junit.Test)

Aggregations

MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)53 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)35 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)32 Test (org.junit.Test)28 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)22 IOException (java.io.IOException)20 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)18 HttpTransport (com.google.api.client.http.HttpTransport)9 GenericJson (com.google.api.client.json.GenericJson)9 HttpResponse (com.google.api.client.http.HttpResponse)8 JsonFactory (com.google.api.client.json.JsonFactory)7 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)7 HttpRequest (com.google.api.client.http.HttpRequest)5 Objectify (com.googlecode.objectify.Objectify)5 GenericUrl (com.google.api.client.http.GenericUrl)4 Storage (com.google.api.services.storage.Storage)4 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)3 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)3 MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)3 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)3