1 /* 2 * Copyright 2022 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 package com.google.android.libraries.mobiledatadownload.downloader.offroad; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import com.google.android.downloader.ErrorDetails; 21 import com.google.android.downloader.RequestException; 22 import com.google.android.libraries.mobiledatadownload.DownloadException; 23 import com.google.android.libraries.mobiledatadownload.DownloadException.DownloadResultCode; 24 import com.google.common.collect.ImmutableMap; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.robolectric.RobolectricTestRunner; 28 29 @RunWith(RobolectricTestRunner.class) 30 public final class ExceptionHandlerTest { 31 32 @Test mapToDownloadException_withDefaultImpl_handlesHttpStatusErrors()33 public void mapToDownloadException_withDefaultImpl_handlesHttpStatusErrors() throws Exception { 34 ErrorDetails errorDetails = 35 ErrorDetails.createFromHttpErrorResponse( 36 /* httpResponseCode= */ 404, 37 /* httpResponseHeaders= */ ImmutableMap.of(), 38 /* message= */ "404 response"); 39 RequestException requestException = new RequestException(errorDetails); 40 41 ExceptionHandler handler = ExceptionHandler.withDefaultHandling(); 42 43 DownloadException remappedException = 44 handler.mapToDownloadException("test exception", requestException); 45 46 assertThat(remappedException.getDownloadResultCode()) 47 .isEqualTo(DownloadResultCode.ANDROID_DOWNLOADER_HTTP_ERROR); 48 assertThat(remappedException).hasMessageThat().isEqualTo("test exception"); 49 } 50 51 @Test 52 public void mapToDownloadException_withDefaultImpl_handlesHttpStatusErrorsWithDownloadExceptionUnwrapping()53 mapToDownloadException_withDefaultImpl_handlesHttpStatusErrorsWithDownloadExceptionUnwrapping() 54 throws Exception { 55 ErrorDetails errorDetails = 56 ErrorDetails.createFromHttpErrorResponse( 57 /* httpResponseCode= */ 404, 58 /* httpResponseHeaders= */ ImmutableMap.of(), 59 /* message= */ "404 response"); 60 RequestException requestException = new RequestException(errorDetails); 61 62 com.google.android.downloader.DownloadException wrappedException = 63 new com.google.android.downloader.DownloadException(requestException); 64 65 ExceptionHandler handler = ExceptionHandler.withDefaultHandling(); 66 67 DownloadException remappedException = 68 handler.mapToDownloadException("test exception", wrappedException); 69 70 assertThat(remappedException.getDownloadResultCode()) 71 .isEqualTo(DownloadResultCode.ANDROID_DOWNLOADER_HTTP_ERROR); 72 assertThat(remappedException).hasMessageThat().isEqualTo("test exception"); 73 } 74 75 @Test mapToDownloadException_withDefaultImpl_handlesGeneralDownloadExceptionError()76 public void mapToDownloadException_withDefaultImpl_handlesGeneralDownloadExceptionError() 77 throws Exception { 78 com.google.android.downloader.DownloadException generalException = 79 new com.google.android.downloader.DownloadException("general error"); 80 81 ExceptionHandler handler = ExceptionHandler.withDefaultHandling(); 82 83 DownloadException remappedException = 84 handler.mapToDownloadException("test exception", generalException); 85 86 assertThat(remappedException.getDownloadResultCode()) 87 .isEqualTo(DownloadResultCode.ANDROID_DOWNLOADER2_ERROR); 88 assertThat(remappedException).hasMessageThat().isEqualTo("test exception"); 89 } 90 91 @Test mapToDownloadException_withDefaultImpl_returnsUnknownExceptionWhenCommonMappingFails()92 public void mapToDownloadException_withDefaultImpl_returnsUnknownExceptionWhenCommonMappingFails() 93 throws Exception { 94 Exception testException = new Exception("test"); 95 96 ExceptionHandler handler = ExceptionHandler.withDefaultHandling(); 97 98 DownloadException remappedException = 99 handler.mapToDownloadException("test exception", testException); 100 101 assertThat(remappedException.getDownloadResultCode()) 102 .isEqualTo(DownloadResultCode.UNKNOWN_ERROR); 103 assertThat(remappedException).hasMessageThat().isEqualTo("test exception"); 104 } 105 106 @Test 107 public void mapToDownloadException_withDefaultImpl_returnsUnknownExceptionWhenRequestExceptionMappingFails()108 mapToDownloadException_withDefaultImpl_returnsUnknownExceptionWhenRequestExceptionMappingFails() 109 throws Exception { 110 RequestException requestException = new RequestException("generic request exception"); 111 112 ExceptionHandler handler = ExceptionHandler.withDefaultHandling(); 113 114 DownloadException remappedException = 115 handler.mapToDownloadException("test exception", requestException); 116 117 assertThat(remappedException.getDownloadResultCode()) 118 .isEqualTo(DownloadResultCode.UNKNOWN_ERROR); 119 assertThat(remappedException).hasMessageThat().isEqualTo("test exception"); 120 } 121 122 @Test mapToDownloadException_withDefaultImpl_handlesMalformedExceptionChain()123 public void mapToDownloadException_withDefaultImpl_handlesMalformedExceptionChain() 124 throws Exception { 125 Exception cause1 = new Exception("test cause 1"); 126 Exception cause2 = new Exception("test cause 2", cause1); 127 cause1.initCause(cause2); 128 129 ExceptionHandler handler = ExceptionHandler.withDefaultHandling(); 130 131 DownloadException remappedException = handler.mapToDownloadException("test exception", cause1); 132 133 assertThat(remappedException.getDownloadResultCode()) 134 .isEqualTo(DownloadResultCode.UNKNOWN_ERROR); 135 assertThat(remappedException).hasMessageThat().isEqualTo("test exception"); 136 } 137 } 138