|
| 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.cloud.dns; |
| 18 | + |
| 19 | +import com.google.cloud.BaseServiceException; |
| 20 | +import com.google.cloud.RetryHelper.RetryHelperException; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.SocketTimeoutException; |
| 26 | + |
| 27 | +import static org.easymock.EasyMock.createMock; |
| 28 | +import static org.easymock.EasyMock.expect; |
| 29 | +import static org.easymock.EasyMock.replay; |
| 30 | +import static org.easymock.EasyMock.verify; |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertNull; |
| 34 | +import static org.junit.Assert.assertTrue; |
| 35 | + |
| 36 | +public class DnsExceptionTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testDnsException() throws Exception { |
| 40 | + IOException cause = new SocketTimeoutException("message"); |
| 41 | + DnsException exception = new DnsException(cause, true); |
| 42 | + assertEquals(DnsException.UNKNOWN_CODE, exception.code()); |
| 43 | + assertNull(exception.reason()); |
| 44 | + assertEquals("message", exception.getMessage()); |
| 45 | + assertEquals(cause, exception.getCause()); |
| 46 | + assertTrue(exception.retryable()); |
| 47 | + assertTrue(exception.idempotent()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testTranslateAndThrow() throws Exception { |
| 52 | + IOException timeoutException = new SocketTimeoutException("message"); |
| 53 | + Exception cause = new DnsException(timeoutException, true); |
| 54 | + RetryHelperException exceptionMock = createMock(RetryHelperException.class); |
| 55 | + expect(exceptionMock.getCause()).andReturn(cause).times(2); |
| 56 | + replay(exceptionMock); |
| 57 | + try { |
| 58 | + DnsException.translateAndThrow(exceptionMock); |
| 59 | + } catch (BaseServiceException ex) { |
| 60 | + assertEquals(DnsException.UNKNOWN_CODE, ex.code()); |
| 61 | + assertNull(ex.reason()); |
| 62 | + assertEquals("message", ex.getMessage()); |
| 63 | + assertEquals(timeoutException, ex.getCause()); |
| 64 | + assertTrue(ex.retryable()); |
| 65 | + assertTrue(ex.idempotent()); |
| 66 | + } finally { |
| 67 | + verify(exceptionMock); |
| 68 | + } |
| 69 | + cause = new IllegalArgumentException("message"); |
| 70 | + exceptionMock = createMock(RetryHelperException.class); |
| 71 | + expect(exceptionMock.getMessage()).andReturn("message").times(1); |
| 72 | + expect(exceptionMock.getCause()).andReturn(cause).times(2); |
| 73 | + replay(exceptionMock); |
| 74 | + try { |
| 75 | + DnsException.translateAndThrow(exceptionMock); |
| 76 | + } catch (BaseServiceException ex) { |
| 77 | + assertEquals(DnsException.UNKNOWN_CODE, ex.code()); |
| 78 | + assertEquals("message", ex.getMessage()); |
| 79 | + assertFalse(ex.retryable()); |
| 80 | + assertTrue(ex.idempotent()); |
| 81 | + assertEquals(cause, ex.getCause()); |
| 82 | + } finally { |
| 83 | + verify(exceptionMock); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments