com.google.api.client.googleapis.testing.services.MockGoogleClient

Here are the examples of the java api class com.google.api.client.googleapis.testing.services.MockGoogleClient taken from open source projects.

1. BatchRequestTest#subTestExecuteWithVoidCallback()

View license
public void subTestExecuteWithVoidCallback(boolean testServerError) throws Exception {
    MockTransport transport = new MockTransport(testServerError, false, false, false, false, false);
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, null, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> jsonHttpRequest1 = new MockGoogleClientRequest<String>(client, METHOD1, URI_TEMPLATE1, null, String.class);
    MockGoogleClientRequest<String> jsonHttpRequest2 = new MockGoogleClientRequest<String>(client, METHOD2, URI_TEMPLATE2, null, String.class);
    ObjectParser parser = new JsonObjectParser(new JacksonFactory());
    BatchRequest batchRequest = new BatchRequest(transport, null).setBatchUrl(new GenericUrl(TEST_BATCH_URL));
    HttpRequest request1 = jsonHttpRequest1.buildHttpRequest();
    request1.setParser(parser);
    HttpRequest request2 = jsonHttpRequest2.buildHttpRequest();
    request2.setParser(parser);
    batchRequest.queue(request1, MockDataClass1.class, GoogleJsonErrorContainer.class, callback1);
    batchRequest.queue(request2, Void.class, Void.class, callback3);
    batchRequest.execute();
    // Assert transport called expected number of times.
    assertEquals(1, transport.actualCalls);
}

2. AbstractGoogleClientRequestTest#testExecuteUsingHead()

View license
public void testExecuteUsingHead() throws Exception {
    HttpTransport transport = new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() {
                    assertEquals("HEAD", method);
                    assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
                    return new MockLowLevelHttpResponse();
                }
            };
        }
    };
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
    request.put("testId", "foo");
    request.executeUsingHead();
}

3. AbstractGoogleClientRequestTest#subtestBuildHttpRequest_emptyContent()

View license
private void subtestBuildHttpRequest_emptyContent(String method, boolean expectEmptyContent) throws Exception {
    HttpTransport transport = new MockHttpTransport();
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, method, URI_TEMPLATE, null, String.class);
    HttpRequest httpRequest = request.buildHttpRequest();
    if (expectEmptyContent) {
        assertTrue(httpRequest.getContent() instanceof EmptyContent);
    } else {
        assertNull(httpRequest.getContent());
    }
}

4. AbstractGoogleClientRequestTest#testCheckRequiredParameter()

View license
public void testCheckRequiredParameter() throws Exception {
    HttpTransport transport = new MockHttpTransport();
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
    // Should not throw an Exception.
    request.checkRequiredParameter("Not Null", "notNull()");
    try {
        request.checkRequiredParameter(null, "content.getTest().getAnotherTest()");
        fail("Expected " + IllegalArgumentException.class);
    } catch (IllegalArgumentException iae) {
    }
}

5. AbstractGoogleClientRequestTest#testExecute_void()

View license
public void testExecute_void() throws Exception {
    HttpTransport transport = new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() {
                    return new MockLowLevelHttpResponse().setContent("{\"a\":\"ignored\"}").setContentType(Json.MEDIA_TYPE);
                }
            };
        }
    };
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<Void> request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
    Void v = request.execute();
    assertNull(v);
}

6. AbstractGoogleClientRequestTest#testUserAgentSuffix()

View license
public void testUserAgentSuffix() throws Exception {
    AssertUserAgentTransport transport = new AssertUserAgentTransport();
    // Specify an Application Name.
    String applicationName = "Test Application";
    transport.expectedUserAgent = applicationName + " " + AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName(applicationName).build();
    MockGoogleClientRequest<Void> request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
    request.executeUnparsed();
    // Don't specify an Application Name.
    transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
    client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
    request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
    request.executeUnparsed();
}

7. BatchRequestTest#getBatchPopulatedWithRequests()

View license
private BatchRequest getBatchPopulatedWithRequests(boolean testServerError, boolean testAuthenticationError, boolean returnSuccessAuthenticatedContent, boolean testExponentialBackOff, boolean testRedirect, boolean testBinary, boolean testMissingLength) throws Exception {
    transport = new MockTransport(testServerError, testAuthenticationError, testExponentialBackOff, testRedirect, testBinary, testMissingLength);
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, null, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> jsonHttpRequest1 = new MockGoogleClientRequest<String>(client, METHOD1, URI_TEMPLATE1, null, String.class);
    MockGoogleClientRequest<String> jsonHttpRequest2 = new MockGoogleClientRequest<String>(client, METHOD2, URI_TEMPLATE2, null, String.class);
    credential = new MockCredential();
    ObjectParser parser = testBinary ? new ProtoObjectParser() : new JsonObjectParser(new JacksonFactory());
    BatchRequest batchRequest = new BatchRequest(transport, credential).setBatchUrl(new GenericUrl(TEST_BATCH_URL));
    HttpRequest request1 = jsonHttpRequest1.buildHttpRequest();
    request1.setParser(parser);
    HttpRequest request2 = jsonHttpRequest2.buildHttpRequest();
    request2.setParser(parser);
    if (testAuthenticationError) {
        request2.setUnsuccessfulResponseHandler(new MockUnsuccessfulResponseHandler(transport, returnSuccessAuthenticatedContent));
    }
    if (testExponentialBackOff) {
        request2.setBackOffPolicy(new MockExponentialBackOffPolicy());
    }
    if (testBinary) {
        batchRequest.queue(request1, MockData.Class1.class, ErrorOutput.ErrorBody.class, new TestCallback1Adapter(callback1));
        batchRequest.queue(request2, MockData.Class2.class, ErrorOutput.ErrorBody.class, new TestCallback2Adapter(callback2));
    } else {
        batchRequest.queue(request1, MockDataClass1.class, GoogleJsonErrorContainer.class, callback1);
        batchRequest.queue(request2, MockDataClass2.class, GoogleJsonErrorContainer.class, callback2);
    }
    return batchRequest;
}

8. AbstractGoogleClientRequestTest#testExecuteUnparsed_error()

View license
public void testExecuteUnparsed_error() throws Exception {
    HttpTransport transport = new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() {
                    assertEquals("GET", method);
                    assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
                    MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
                    result.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
                    result.setContentType(Json.MEDIA_TYPE);
                    result.setContent(ERROR_CONTENT);
                    return result;
                }
            };
        }
    };
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
    try {
        request.put("testId", "foo");
        request.executeUnparsed();
        fail("expected " + HttpResponseException.class);
    } catch (HttpResponseException e) {
        assertEquals("401" + StringUtils.LINE_SEPARATOR + ERROR_CONTENT, e.getMessage());
    }
}

9. CommonGoogleClientRequestInitializerTest#testInitialize()

View license
public void testInitialize() throws Exception {
    CommonGoogleClientRequestInitializer key = new CommonGoogleClientRequestInitializer("foo");
    MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null).setApplicationName("Test Application").build();
    MyRequest request = new MyRequest(client, "GET", "", null, String.class);
    assertNull(request.key);
    key.initialize(request);
    assertEquals("foo", request.key);
}