Search in sources :

Example 11 with LowLevelHttpResponse

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

the class BigQueryServicesImplTest method verifyAllResponsesAreRead.

/**
 * Verifies the test interacted the mock objects in {@link #responses}.
 *
 * <p>The implementation of google-api-client or google-http-client may influence the number of
 * interaction in future
 */
private void verifyAllResponsesAreRead() throws IOException {
    Verify.verify(responses != null, "The test setup is incorrect. Responses are not setup");
    for (LowLevelHttpResponse response : responses) {
        // Google-http-client reads the field twice per response.
        verify(response, atLeastOnce()).getStatusCode();
        verify(response, times(1)).getContent();
        verify(response, times(1)).getContentType();
    }
}
Also used : LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse)

Example 12 with LowLevelHttpResponse

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

the class BigQueryServicesImplTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    // Set up the MockHttpRequest for future inspection.
    request = new MockLowLevelHttpRequest() {

        int index = 0;

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            Verify.verify(index < responses.length, "The number of HttpRequest invocation exceeded the number of prepared mock requests. Index: %d", index);
            return responses[index++];
        }
    };
    // 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();
    // Setup the ProcessWideContainer for testing metrics are set.
    MetricsContainerImpl container = new MetricsContainerImpl(null);
    MetricsEnvironment.setProcessWideContainer(container);
    MetricsEnvironment.setCurrentContainer(container);
}
Also used : RetryHttpRequestInitializer(org.apache.beam.sdk.extensions.gcp.util.RetryHttpRequestInitializer) MetricsContainerImpl(org.apache.beam.runners.core.metrics.MetricsContainerImpl) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Before(org.junit.Before)

Example 13 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse 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 mockResponse = Mockito.mock(LowLevelHttpResponse.class);
    when(mockResponse.getContentType()).thenReturn("multipart/mixed; boundary=" + contentBoundary);
    // 429: Too many requests, then 200: OK.
    when(mockResponse.getStatusCode()).thenReturn(429, 200);
    when(mockResponse.getContent()).thenReturn(toStream("error"), toStream(content));
    // A mock transport that lets us mock the API responses.
    MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpRequest(new MockLowLevelHttpRequest() {

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            return mockResponse;
        }
    }).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) 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 14 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class DeleteServletTest method doPost_deleteGame.

@Test
public void doPost_deleteGame() throws Exception {
    // Insert a game
    Objectify ofy = ObjectifyService.ofy();
    Game game = new Game(USER_ID, "my-opponent", "         ", true);
    ofy.save().entity(game).now();
    String gameKey = game.getId();
    when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Mockito.doReturn(null).when(servletUnderTest).getServletContext();
    servletUnderTest.doPost(mockRequest, mockResponse);
    verify(mockHttpTransport, times(1)).buildRequest(eq("DELETE"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) Objectify(com.googlecode.objectify.Objectify) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 15 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method sendFirebaseMessage_delete.

@Test
public void sendFirebaseMessage_delete() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    firebaseChannel.sendFirebaseMessage("my_key", null);
    verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/channels/my_key.json");
}
Also used : 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) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Aggregations

LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)24 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)21 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)20 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)18 IOException (java.io.IOException)18 Test (org.junit.Test)16 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)13 GenericJson (com.google.api.client.json.GenericJson)6 Objectify (com.googlecode.objectify.Objectify)5 JsonFactory (com.google.api.client.json.JsonFactory)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 Storage (com.google.api.services.storage.Storage)3 HttpResponse (com.google.api.client.http.HttpResponse)2 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)2 Mockito.anyString (org.mockito.Mockito.anyString)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 JsonWebSignature (com.google.api.client.json.webtoken.JsonWebSignature)1 Get (com.google.api.services.storage.Storage.Objects.Get)1