|
| 1 | +/* |
| 2 | + * Copyright 2015 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.gcloud.bigquery; |
| 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.gcloud.BaseServiceException; |
| 29 | +import com.google.gcloud.RetryHelper.RetryHelperException; |
| 30 | + |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.net.SocketTimeoutException; |
| 35 | + |
| 36 | +public class BigQueryExceptionTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testBigqueryException() { |
| 40 | + BigQueryException exception = new BigQueryException(500, "message"); |
| 41 | + assertEquals(500, exception.code()); |
| 42 | + assertEquals("message", exception.getMessage()); |
| 43 | + assertNull(exception.reason()); |
| 44 | + assertNull(exception.error()); |
| 45 | + assertTrue(exception.retryable()); |
| 46 | + assertTrue(exception.idempotent()); |
| 47 | + |
| 48 | + exception = new BigQueryException(502, "message"); |
| 49 | + assertEquals(502, exception.code()); |
| 50 | + assertEquals("message", exception.getMessage()); |
| 51 | + assertNull(exception.reason()); |
| 52 | + assertNull(exception.error()); |
| 53 | + assertTrue(exception.retryable()); |
| 54 | + assertTrue(exception.idempotent()); |
| 55 | + |
| 56 | + exception = new BigQueryException(503, "message"); |
| 57 | + assertEquals(503, exception.code()); |
| 58 | + assertEquals("message", exception.getMessage()); |
| 59 | + assertNull(exception.reason()); |
| 60 | + assertNull(exception.error()); |
| 61 | + assertTrue(exception.retryable()); |
| 62 | + assertTrue(exception.idempotent()); |
| 63 | + |
| 64 | + exception = new BigQueryException(504, "message"); |
| 65 | + assertEquals(504, exception.code()); |
| 66 | + assertEquals("message", exception.getMessage()); |
| 67 | + assertNull(exception.reason()); |
| 68 | + assertNull(exception.error()); |
| 69 | + assertTrue(exception.retryable()); |
| 70 | + assertTrue(exception.idempotent()); |
| 71 | + |
| 72 | + exception = new BigQueryException(400, "message"); |
| 73 | + assertEquals(400, exception.code()); |
| 74 | + assertEquals("message", exception.getMessage()); |
| 75 | + assertNull(exception.reason()); |
| 76 | + assertNull(exception.error()); |
| 77 | + assertFalse(exception.retryable()); |
| 78 | + assertTrue(exception.idempotent()); |
| 79 | + |
| 80 | + BigQueryError error = new BigQueryError("reason", null, null); |
| 81 | + exception = new BigQueryException(504, "message", error); |
| 82 | + assertEquals(504, exception.code()); |
| 83 | + assertEquals("message", exception.getMessage()); |
| 84 | + assertEquals("reason", exception.reason()); |
| 85 | + assertEquals(error, exception.error()); |
| 86 | + assertTrue(exception.retryable()); |
| 87 | + assertTrue(exception.idempotent()); |
| 88 | + |
| 89 | + IOException cause = new SocketTimeoutException(); |
| 90 | + exception = new BigQueryException(cause); |
| 91 | + assertNull(exception.reason()); |
| 92 | + assertNull(exception.getMessage()); |
| 93 | + assertTrue(exception.retryable()); |
| 94 | + assertTrue(exception.idempotent()); |
| 95 | + assertEquals(cause, exception.getCause()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testTranslateAndThrow() throws Exception { |
| 100 | + BigQueryException cause = new BigQueryException(503, "message"); |
| 101 | + RetryHelperException exceptionMock = createMock(RetryHelperException.class); |
| 102 | + expect(exceptionMock.getCause()).andReturn(cause).times(2); |
| 103 | + replay(exceptionMock); |
| 104 | + try { |
| 105 | + BigQueryException.translateAndThrow(exceptionMock); |
| 106 | + } catch (BaseServiceException ex) { |
| 107 | + assertEquals(503, ex.code()); |
| 108 | + assertEquals("message", ex.getMessage()); |
| 109 | + assertTrue(ex.retryable()); |
| 110 | + assertTrue(ex.idempotent()); |
| 111 | + } finally { |
| 112 | + verify(exceptionMock); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments