1 /* 2 * Copyright 2016 Google LLC 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 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.api.client.googleapis.json.GoogleJsonError; 30 import com.google.cloud.BaseServiceException; 31 import com.google.cloud.RetryHelper.RetryHelperException; 32 import java.io.IOException; 33 import java.net.SocketTimeoutException; 34 import org.junit.Test; 35 36 public class DnsExceptionTest { 37 38 @Test testDnsException()39 public void testDnsException() throws Exception { 40 IOException cause = new SocketTimeoutException("socketTimeoutMessage"); 41 DnsException exception = new DnsException(500, "message", cause); 42 assertEquals(500, exception.getCode()); 43 assertEquals("message", exception.getMessage()); 44 assertNull(exception.getReason()); 45 assertTrue(exception.isRetryable()); 46 assertSame(cause, exception.getCause()); 47 48 exception = new DnsException(502, "message", cause); 49 assertEquals(502, exception.getCode()); 50 assertEquals("message", exception.getMessage()); 51 assertNull(exception.getReason()); 52 assertTrue(exception.isRetryable()); 53 assertSame(cause, exception.getCause()); 54 55 exception = new DnsException(503, "message", cause); 56 assertEquals(503, exception.getCode()); 57 assertEquals("message", exception.getMessage()); 58 assertNull(exception.getReason()); 59 assertTrue(exception.isRetryable()); 60 assertSame(cause, exception.getCause()); 61 62 exception = new DnsException(429, "message", cause); 63 assertEquals(429, exception.getCode()); 64 assertEquals("message", exception.getMessage()); 65 assertNull(exception.getReason()); 66 assertTrue(exception.isRetryable()); 67 assertSame(cause, exception.getCause()); 68 69 exception = new DnsException(404, "message", cause); 70 assertEquals(404, exception.getCode()); 71 assertEquals("message", exception.getMessage()); 72 assertNull(exception.getReason()); 73 assertFalse(exception.isRetryable()); 74 assertSame(cause, exception.getCause()); 75 76 exception = new DnsException(cause, true); 77 assertEquals(DnsException.UNKNOWN_CODE, exception.getCode()); 78 assertNull(exception.getReason()); 79 assertEquals("socketTimeoutMessage", exception.getMessage()); 80 assertEquals(cause, exception.getCause()); 81 assertTrue(exception.isRetryable()); 82 assertSame(cause, exception.getCause()); 83 84 GoogleJsonError error = new GoogleJsonError(); 85 error.setCode(503); 86 error.setMessage("message"); 87 exception = new DnsException(error, true); 88 assertEquals(503, exception.getCode()); 89 assertEquals("message", exception.getMessage()); 90 assertTrue(exception.isRetryable()); 91 } 92 93 @Test testTranslateAndThrow()94 public void testTranslateAndThrow() throws Exception { 95 IOException timeoutException = new SocketTimeoutException("message"); 96 Exception cause = new DnsException(timeoutException, true); 97 RetryHelperException exceptionMock = createMock(RetryHelperException.class); 98 expect(exceptionMock.getCause()).andReturn(cause).times(2); 99 replay(exceptionMock); 100 try { 101 DnsException.translateAndThrow(exceptionMock); 102 } catch (BaseServiceException ex) { 103 assertEquals(DnsException.UNKNOWN_CODE, ex.getCode()); 104 assertNull(ex.getReason()); 105 assertEquals("message", ex.getMessage()); 106 assertEquals(timeoutException, ex.getCause()); 107 assertTrue(ex.isRetryable()); 108 } finally { 109 verify(exceptionMock); 110 } 111 cause = new IllegalArgumentException("message"); 112 exceptionMock = createMock(RetryHelperException.class); 113 expect(exceptionMock.getMessage()).andReturn("message").times(1); 114 expect(exceptionMock.getCause()).andReturn(cause).times(2); 115 replay(exceptionMock); 116 try { 117 DnsException.translateAndThrow(exceptionMock); 118 } catch (BaseServiceException ex) { 119 assertEquals(DnsException.UNKNOWN_CODE, ex.getCode()); 120 assertEquals("message", ex.getMessage()); 121 assertFalse(ex.isRetryable()); 122 assertSame(cause, ex.getCause()); 123 } finally { 124 verify(exceptionMock); 125 } 126 } 127 } 128