Search in sources :

Example 1 with MockGoogleJsonClient

use of com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient in project google-api-java-client by google.

the class AbstractGoogleJsonClientTest method testExecuteUnparsed_error.

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

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

                @Override
                public LowLevelHttpResponse execute() {
                    MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
                    result.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
                    result.setContentType(Json.MEDIA_TYPE);
                    result.setContent("{\"error\":{\"code\":401,\"errors\":[{\"domain\":\"global\"," + "\"location\":\"Authorization\",\"locationType\":\"header\"," + "\"message\":\"me\",\"reason\":\"authError\"}],\"message\":\"me\"}}");
                    return result;
                }
            };
        }
    };
    JsonFactory jsonFactory = new JacksonFactory();
    MockGoogleJsonClient client = new MockGoogleJsonClient.Builder(transport, jsonFactory, HttpTesting.SIMPLE_URL, "", null, false).setApplicationName("Test Application").build();
    MockGoogleJsonClientRequest<String> request = new MockGoogleJsonClientRequest<String>(client, "GET", "foo", null, String.class);
    try {
        request.executeUnparsed();
        fail("expected " + GoogleJsonResponseException.class);
    } catch (GoogleJsonResponseException e) {
        // expected
        GoogleJsonError details = e.getDetails();
        assertEquals("me", details.getMessage());
        assertEquals("me", details.getErrors().get(0).getMessage());
    }
}
Also used : MockGoogleJsonClientRequest(com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClientRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) JsonFactory(com.google.api.client.json.JsonFactory) MockGoogleJsonClient(com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 2 with MockGoogleJsonClient

use of com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient in project google-api-java-client by google.

the class CommonGoogleJsonClientRequestInitializerTest method testInitialize.

public void testInitialize() throws Exception {
    CommonGoogleJsonClientRequestInitializer key = new CommonGoogleJsonClientRequestInitializer("foo");
    MockGoogleJsonClient client = new MockGoogleJsonClient.Builder(new MockHttpTransport(), new JacksonFactory(), HttpTesting.SIMPLE_URL, "test/", null, false).setApplicationName("Test Application").build();
    MyRequest request = new MyRequest(client, "GET", "", null, String.class);
    assertNull(request.key);
    key.initialize(request);
    assertEquals("foo", request.key);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockGoogleJsonClient(com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Aggregations

MockGoogleJsonClient (com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient)2 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)2 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 MockGoogleJsonClientRequest (com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClientRequest)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)1 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)1