Search in sources :

Example 6 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class ResourceManagerExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new ResourceManagerException(503, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        ResourceManagerException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(503, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        ResourceManagerException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(ResourceManagerException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Example 7 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class DnsExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    IOException timeoutException = new SocketTimeoutException("message");
    Exception cause = new DnsException(timeoutException, true);
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        DnsException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(DnsException.UNKNOWN_CODE, ex.getCode());
        assertNull(ex.getReason());
        assertEquals("message", ex.getMessage());
        assertEquals(timeoutException, ex.getCause());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        DnsException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(DnsException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) BaseServiceException(com.google.cloud.BaseServiceException) IOException(java.io.IOException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Example 8 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new StorageException(503, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        StorageException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(503, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        StorageException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(StorageException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) HttpResponseException(com.google.api.client.http.HttpResponseException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Example 9 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class TranslateExceptionTest method testTranslateAndThrow.

@Test
public void testTranslateAndThrow() throws Exception {
    Exception cause = new TranslateException(500, "message");
    RetryHelperException exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        TranslateException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(500, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertTrue(ex.isRetryable());
    } finally {
        verify(exceptionMock);
    }
    cause = new IllegalArgumentException("message");
    exceptionMock = createMock(RetryHelperException.class);
    expect(exceptionMock.getMessage()).andReturn("message").times(1);
    expect(exceptionMock.getCause()).andReturn(cause).times(2);
    replay(exceptionMock);
    try {
        TranslateException.translateAndThrow(exceptionMock);
    } catch (BaseServiceException ex) {
        assertEquals(TranslateException.UNKNOWN_CODE, ex.getCode());
        assertEquals("message", ex.getMessage());
        assertFalse(ex.isRetryable());
        assertSame(cause, ex.getCause());
    } finally {
        verify(exceptionMock);
    }
}
Also used : BaseServiceException(com.google.cloud.BaseServiceException) BaseServiceException(com.google.cloud.BaseServiceException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException) Test(org.junit.Test)

Example 10 with RetryHelperException

use of com.google.cloud.RetryHelper.RetryHelperException in project google-cloud-java by GoogleCloudPlatform.

the class StorageImpl method testIamPermissions.

@Override
public List<Boolean> testIamPermissions(final String bucket, final List<String> permissions) {
    try {
        TestIamPermissionsResponse response = runWithRetries(new Callable<TestIamPermissionsResponse>() {

            @Override
            public TestIamPermissionsResponse call() {
                return storageRpc.testIamPermissions(bucket, permissions);
            }
        }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock());
        final Set<String> heldPermissions = response.getPermissions() != null ? ImmutableSet.copyOf(response.getPermissions()) : ImmutableSet.<String>of();
        return Lists.transform(permissions, new Function<String, Boolean>() {

            @Override
            public Boolean apply(String permission) {
                return heldPermissions.contains(permission);
            }
        });
    } catch (RetryHelperException e) {
        throw StorageException.translateAndThrow(e);
    }
}
Also used : TestIamPermissionsResponse(com.google.api.services.storage.model.TestIamPermissionsResponse) RetryHelperException(com.google.cloud.RetryHelper.RetryHelperException)

Aggregations

RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)14 StorageObject (com.google.api.services.storage.model.StorageObject)7 BaseServiceException (com.google.cloud.BaseServiceException)6 IOException (java.io.IOException)6 SocketTimeoutException (java.net.SocketTimeoutException)6 Test (org.junit.Test)6 Callable (java.util.concurrent.Callable)3 HttpResponseException (com.google.api.client.http.HttpResponseException)2 TestIamPermissionsResponse (com.google.api.services.storage.model.TestIamPermissionsResponse)1 RewriteResponse (com.google.cloud.storage.spi.v1.StorageRpc.RewriteResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1