Search in sources :

Example 6 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse 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) Matchers.anyString(org.mockito.Matchers.anyString) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Aggregations

MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)6 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)6 GenericJson (com.google.api.client.json.GenericJson)3 JsonFactory (com.google.api.client.json.JsonFactory)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)3 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)2 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpResponse (com.google.api.client.http.HttpResponse)2 HttpTransport (com.google.api.client.http.HttpTransport)2 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)2 Storage (com.google.api.services.storage.Storage)2 Test (org.junit.Test)2 GenericUrl (com.google.api.client.http.GenericUrl)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)1 Matchers.anyString (org.mockito.Matchers.anyString)1