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