|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.grpc; |
| 18 | + |
| 19 | +import static org.easymock.EasyMock.createMock; |
| 20 | +import static org.easymock.EasyMock.expect; |
| 21 | +import static org.easymock.EasyMock.replay; |
| 22 | +import static org.easymock.EasyMock.verify; |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertFalse; |
| 25 | +import static org.junit.Assert.assertNull; |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | + |
| 28 | +import com.google.api.gax.grpc.ApiException; |
| 29 | +import com.google.cloud.BaseServiceException; |
| 30 | +import com.google.cloud.RetryHelper; |
| 31 | +import io.grpc.Status.Code; |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.SocketException; |
| 34 | +import java.net.SocketTimeoutException; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class BaseGrpcServiceExceptionTest { |
| 38 | + |
| 39 | + private static final int CODE = 1; |
| 40 | + private static final String MESSAGE = "some message"; |
| 41 | + private static final boolean NOT_RETRYABLE = false; |
| 42 | + private static final boolean IDEMPOTENT = true; |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testBaseServiceException() { |
| 46 | + BaseGrpcServiceException serviceException = null; |
| 47 | + |
| 48 | + IOException exception = new SocketTimeoutException(); |
| 49 | + serviceException = new BaseGrpcServiceException(exception, IDEMPOTENT); |
| 50 | + assertTrue(serviceException.isRetryable()); |
| 51 | + assertNull(serviceException.getMessage()); |
| 52 | + assertEquals(exception, serviceException.getCause()); |
| 53 | + assertNull(serviceException.getReason()); |
| 54 | + assertNull(serviceException.getLocation()); |
| 55 | + assertNull(serviceException.getDebugInfo()); |
| 56 | + |
| 57 | + exception = new SocketException(); |
| 58 | + serviceException = new BaseGrpcServiceException(exception, IDEMPOTENT); |
| 59 | + assertTrue(serviceException.isRetryable()); |
| 60 | + assertNull(serviceException.getMessage()); |
| 61 | + assertEquals(exception, serviceException.getCause()); |
| 62 | + assertNull(serviceException.getReason()); |
| 63 | + assertNull(serviceException.getLocation()); |
| 64 | + assertNull(serviceException.getDebugInfo()); |
| 65 | + |
| 66 | + exception = new IOException("insufficient data written"); |
| 67 | + serviceException = new BaseGrpcServiceException(exception, IDEMPOTENT); |
| 68 | + assertTrue(serviceException.isRetryable()); |
| 69 | + assertEquals("insufficient data written", serviceException.getMessage()); |
| 70 | + assertEquals(exception, serviceException.getCause()); |
| 71 | + assertNull(serviceException.getReason()); |
| 72 | + assertNull(serviceException.getLocation()); |
| 73 | + assertNull(serviceException.getDebugInfo()); |
| 74 | + |
| 75 | + Exception cause = new IllegalArgumentException("bad arg"); |
| 76 | + ApiException apiException = new ApiException(MESSAGE, cause, Code.INTERNAL, NOT_RETRYABLE); |
| 77 | + serviceException = new BaseGrpcServiceException(apiException); |
| 78 | + assertFalse(serviceException.isRetryable()); |
| 79 | + assertEquals(MESSAGE, serviceException.getMessage()); |
| 80 | + assertEquals(apiException, serviceException.getCause()); |
| 81 | + assertEquals(Code.INTERNAL.value(), serviceException.getCode()); |
| 82 | + assertEquals(Code.INTERNAL.name(), serviceException.getReason()); |
| 83 | + assertNull(serviceException.getLocation()); |
| 84 | + assertNull(serviceException.getDebugInfo()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testTranslateAndThrow() throws Exception { |
| 89 | + IOException exception = new SocketTimeoutException(); |
| 90 | + BaseGrpcServiceException cause = new BaseGrpcServiceException(exception, IDEMPOTENT); |
| 91 | + RetryHelper.RetryHelperException exceptionMock = |
| 92 | + createMock(RetryHelper.RetryHelperException.class); |
| 93 | + expect(exceptionMock.getCause()).andReturn(cause).times(2); |
| 94 | + replay(exceptionMock); |
| 95 | + try { |
| 96 | + BaseServiceException.translate(exceptionMock); |
| 97 | + } catch (BaseServiceException ex) { |
| 98 | + assertEquals(0, ex.getCode()); |
| 99 | + assertNull(ex.getMessage()); |
| 100 | + assertTrue(ex.isRetryable()); |
| 101 | + } finally { |
| 102 | + verify(exceptionMock); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments