Search in sources :

Example 36 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project elasticsearch by elastic.

the class GceMockUtils method configureMock.

protected static HttpTransport configureMock() {
    return new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    response.setContentType(Json.MEDIA_TYPE);
                    if (url.startsWith(GCE_METADATA_URL)) {
                        logger.info("--> Simulate GCE Auth/Metadata response for [{}]", url);
                        response.setContent(readGoogleInternalJsonResponse(url));
                    } else {
                        logger.info("--> Simulate GCE API response for [{}]", url);
                        response.setContent(readGoogleApiJsonResponse(url));
                    }
                    return response;
                }
            };
        }
    };
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 37 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) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) Test(org.junit.Test)

Example 38 with MockLowLevelHttpResponse

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

the class UploadIdResponseInterceptorTest method buildHttpResponse.

/**
   * 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();
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl)

Example 39 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse 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 40 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse 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

MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)63 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)40 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)38 Test (org.junit.Test)31 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)24 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)22 IOException (java.io.IOException)21 HttpTransport (com.google.api.client.http.HttpTransport)10 GenericJson (com.google.api.client.json.GenericJson)9 HttpResponse (com.google.api.client.http.HttpResponse)8 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)8 JsonFactory (com.google.api.client.json.JsonFactory)7 HttpRequest (com.google.api.client.http.HttpRequest)6 GenericUrl (com.google.api.client.http.GenericUrl)5 Objectify (com.googlecode.objectify.Objectify)5 Storage (com.google.api.services.storage.Storage)4 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)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