Search in sources :

Example 76 with MockHttpTransport

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

the class GcsUtilTest method testGetSizeBytesWhenFileNotFoundBatchRetry.

@Test
public void testGetSizeBytesWhenFileNotFoundBatchRetry() throws Exception {
    JsonFactory jsonFactory = new JacksonFactory();
    String contentBoundary = "batch_foobarbaz";
    String contentBoundaryLine = "--" + contentBoundary;
    String endOfContentBoundaryLine = "--" + contentBoundary + "--";
    GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
    error.setFactory(jsonFactory);
    String content = contentBoundaryLine + "\n" + "Content-Type: application/http\n" + "\n" + "HTTP/1.1 404 Not Found\n" + "Content-Length: -1\n" + "\n" + error.toString() + "\n" + "\n" + endOfContentBoundaryLine + "\n";
    thrown.expect(FileNotFoundException.class);
    final LowLevelHttpResponse[] mockResponses = new LowLevelHttpResponse[] { Mockito.mock(LowLevelHttpResponse.class), Mockito.mock(LowLevelHttpResponse.class) };
    when(mockResponses[0].getContentType()).thenReturn("text/plain");
    when(mockResponses[1].getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);
    // 429: Too many requests, then 200: OK.
    when(mockResponses[0].getStatusCode()).thenReturn(429);
    when(mockResponses[1].getStatusCode()).thenReturn(200);
    when(mockResponses[0].getContent()).thenReturn(toStream("error"));
    when(mockResponses[1].getContent()).thenReturn(toStream(content));
    // A mock transport that lets us mock the API responses.
    MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpRequest(new MockLowLevelHttpRequest() {

        int index = 0;

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            return mockResponses[index++];
        }
    }).build();
    GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
    gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
    gcsUtil.fileSizes(ImmutableList.of(GcsPath.fromComponents("testbucket", "testobject")));
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) JsonFactory(com.google.api.client.json.JsonFactory) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 77 with MockHttpTransport

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

the class GcsUtilTest method testRemoveWhenFileNotFound.

@Test
public void testRemoveWhenFileNotFound() throws Exception {
    JsonFactory jsonFactory = new JacksonFactory();
    String contentBoundary = "batch_foobarbaz";
    String contentBoundaryLine = "--" + contentBoundary;
    String endOfContentBoundaryLine = "--" + contentBoundary + "--";
    GenericJson error = new GenericJson().set("error", new GenericJson().set("code", 404));
    error.setFactory(jsonFactory);
    String content = contentBoundaryLine + "\n" + "Content-Type: application/http\n" + "\n" + "HTTP/1.1 404 Not Found\n" + "Content-Length: -1\n" + "\n" + error.toString() + "\n" + "\n" + endOfContentBoundaryLine + "\n";
    final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class);
    when(mockResponse.getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);
    when(mockResponse.getStatusCode()).thenReturn(200);
    when(mockResponse.getContent()).thenReturn(toStream(content));
    // A mock transport that lets us mock the API responses.
    MockLowLevelHttpRequest request = new MockLowLevelHttpRequest() {

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            return mockResponse;
        }
    };
    MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpRequest(request).build();
    GcsUtil gcsUtil = gcsOptionsWithTestCredential().getGcsUtil();
    gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), new RetryHttpRequestInitializer()));
    gcsUtil.remove(Arrays.asList("gs://some-bucket/already-deleted"));
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) JsonFactory(com.google.api.client.json.JsonFactory) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 78 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project druid by druid-io.

the class GcpModuleTest method testSimpleModuleLoads.

@Test
public void testSimpleModuleLoads() {
    final Injector injector = Guice.createInjector(Modules.override(new GcpModule()).with(new GcpMockModule() {

        @Override
        public void configure(Binder binder) {
            binder.bindScope(LazySingleton.class, Scopes.SINGLETON);
        }
    }));
    Assert.assertTrue(injector.getInstance(HttpRequestInitializer.class) instanceof MockGoogleCredential);
    Assert.assertTrue(injector.getInstance(HttpTransport.class) instanceof MockHttpTransport);
}
Also used : Binder(com.google.inject.Binder) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Injector(com.google.inject.Injector) MockGoogleCredential(com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential) Test(org.junit.Test)

Example 79 with MockHttpTransport

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

the class PackageUtilTest method googleJsonResponseException.

/**
 * Builds a fake GoogleJsonResponseException for testing API error handling.
 */
private static GoogleJsonResponseException googleJsonResponseException(final int status, final String reason, final String message) throws IOException {
    final JsonFactory jsonFactory = new JacksonFactory();
    HttpTransport transport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.setReason(reason);
            errorInfo.setMessage(message);
            errorInfo.setFactory(jsonFactory);
            GenericJson error = new GenericJson();
            error.set("code", status);
            error.set("errors", Arrays.asList(errorInfo));
            error.setFactory(jsonFactory);
            GenericJson errorResponse = new GenericJson();
            errorResponse.set("error", error);
            errorResponse.setFactory(jsonFactory);
            return new MockLowLevelHttpRequest().setResponse(new MockLowLevelHttpResponse().setContent(errorResponse.toPrettyString()).setContentType(Json.MEDIA_TYPE).setStatusCode(status));
        }
    };
    HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
    request.setThrowExceptionOnExecuteError(false);
    HttpResponse response = request.execute();
    return GoogleJsonResponseException.from(jsonFactory, response);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) ErrorInfo(com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 80 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project copybara by google.

the class GithubArchiveTest method setup.

@Before
public void setup() throws IOException {
    httpTransport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) {
            String requestString = method + " " + url;
            MockLowLevelHttpRequest request = new MockLowLevelHttpRequest();
            MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
            request.setResponse(response);
            response.setStatusCode(200);
            response.setContent(responseContent);
            if (!url.equals(expectedRequest)) {
                response.setStatusCode(404);
                response.setContent(String.format("UNEXPECTED REQUEST (Returning 404) REQUEST: %s, expected: %s", requestString, expectedRequest));
            }
            return request;
        }
    };
    RemoteFileOptions options = new RemoteFileOptions();
    options.transport = () -> new GclientHttpStreamFactory(httpTransport, Duration.ofSeconds(20));
    Console console = new TestingConsole();
    OptionsBuilder optionsBuilder = new OptionsBuilder().setConsole(console);
    optionsBuilder.remoteFile = options;
    skylark = new SkylarkTestExecutor(optionsBuilder);
}
Also used : TestingConsole(com.google.copybara.util.console.testing.TestingConsole) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Before(org.junit.Before)

Aggregations

MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)83 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)44 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)42 Test (org.junit.Test)32 HttpTransport (com.google.api.client.http.HttpTransport)30 IOException (java.io.IOException)29 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)26 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)23 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)19 HttpRequest (com.google.api.client.http.HttpRequest)14 JsonFactory (com.google.api.client.json.JsonFactory)13 GenericJson (com.google.api.client.json.GenericJson)12 Storage (com.google.api.services.storage.Storage)9 HttpResponse (com.google.api.client.http.HttpResponse)7 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)6 GenericUrl (com.google.api.client.http.GenericUrl)6 MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)5 Objectify (com.googlecode.objectify.Objectify)5 MockTokenServerTransport (com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)4 ByteArrayContent (com.google.api.client.http.ByteArrayContent)4