Skip to content

Commit 4962230

Browse files
kabeer-uberlfkellogg
authored andcommitted
If modelHash is null pass emptyString to logger which requires non-null modelHash (#3941)
1 parent 74958f7 commit 4962230

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/CustomModelDownloadService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ public Task<CustomModel> getCustomModelDetails(
176176
exceptionCode = FirebaseMlException.NO_NETWORK_CONNECTION;
177177
}
178178
eventLogger.logDownloadFailureWithReason(
179-
new CustomModel(modelName, modelHash, 0, 0L), false, errorCode.getValue());
179+
new CustomModel(modelName, modelHash != null ? modelHash : "", 0, 0L),
180+
false,
181+
errorCode.getValue());
180182
return Tasks.forException(new FirebaseMlException(errorMessage, exceptionCode));
181183
}
182184

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/internal/CustomModelDownloadServiceTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
2121
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
2222
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
23+
import static com.google.common.truth.Truth.assertThat;
2324
import static com.google.common.truth.Truth.assertWithMessage;
2425
import static org.mockito.ArgumentMatchers.any;
2526
import static org.mockito.ArgumentMatchers.anyBoolean;
@@ -52,6 +53,7 @@
5253
import org.junit.Rule;
5354
import org.junit.Test;
5455
import org.junit.runner.RunWith;
56+
import org.mockito.ArgumentCaptor;
5557
import org.mockito.Mock;
5658
import org.mockito.MockitoAnnotations;
5759
import org.robolectric.RobolectricTestRunner;
@@ -708,6 +710,38 @@ public void downloadService_unauthenticatedToken() {
708710
any(), eq(false), eq(ErrorCode.MODEL_INFO_DOWNLOAD_CONNECTION_FAILED.getValue()));
709711
}
710712

713+
@Test
714+
public void downloadService_nullModelHashPassedUnauthenticatedToken() {
715+
when(installationsApiMock.getToken(anyBoolean()))
716+
.thenReturn(Tasks.forException(new IllegalArgumentException("bad request")));
717+
718+
CustomModelDownloadService service =
719+
new CustomModelDownloadService(
720+
ApplicationProvider.getApplicationContext(),
721+
installationsApiMock,
722+
directExecutor,
723+
API_KEY,
724+
PACKAGE_FINGERPRINT_HASH,
725+
TEST_ENDPOINT,
726+
mockEventLogger);
727+
728+
Task<CustomModel> modelTask = service.getCustomModelDetails(PROJECT_ID, MODEL_NAME, null);
729+
730+
Assert.assertTrue(modelTask.getException() instanceof FirebaseMlException);
731+
Assert.assertEquals(
732+
((FirebaseMlException) modelTask.getException()).getCode(),
733+
FirebaseMlException.UNAUTHENTICATED);
734+
Assert.assertTrue(modelTask.getException().getMessage().contains("authentication error"));
735+
736+
ArgumentCaptor<CustomModel> captor = ArgumentCaptor.forClass(CustomModel.class);
737+
verify(mockEventLogger, times(1))
738+
.logDownloadFailureWithReason(
739+
captor.capture(),
740+
eq(false),
741+
eq(ErrorCode.MODEL_INFO_DOWNLOAD_CONNECTION_FAILED.getValue()));
742+
assertThat(captor.getValue().getModelHash()).isNotNull();
743+
}
744+
711745
@Test
712746
public void downloadService_malFormedUrl() {
713747
CustomModelDownloadService service =

0 commit comments

Comments
 (0)