|
32 | 32 | import static org.mockito.ArgumentMatchers.any;
|
33 | 33 | import static org.mockito.ArgumentMatchers.anyString;
|
34 | 34 | import static org.mockito.Mockito.doAnswer;
|
| 35 | +import static org.mockito.Mockito.doNothing; |
| 36 | +import static org.mockito.Mockito.doThrow; |
| 37 | +import static org.mockito.Mockito.never; |
35 | 38 | import static org.mockito.Mockito.times;
|
36 | 39 | import static org.mockito.Mockito.verify;
|
37 | 40 | import static org.mockito.Mockito.when;
|
@@ -163,6 +166,16 @@ public void setUp() throws FirebaseInstallationServiceException {
|
163 | 166 | when(persistedFidReturnsError.readPersistedFidEntryValue()).thenReturn(null);
|
164 | 167 | when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1);
|
165 | 168 | when(mockClock.currentTimeMillis()).thenReturn(TEST_CREATION_TIMESTAMP_1);
|
| 169 | + // Mocks success on FIS deletion |
| 170 | + doNothing() |
| 171 | + .when(backendClientReturnsOk) |
| 172 | + .deleteFirebaseInstallation(anyString(), anyString(), anyString(), anyString()); |
| 173 | + // Mocks server error on FIS deletion |
| 174 | + doThrow( |
| 175 | + new FirebaseInstallationServiceException( |
| 176 | + "Server Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)) |
| 177 | + .when(backendClientReturnsError) |
| 178 | + .deleteFirebaseInstallation(anyString(), anyString(), anyString(), anyString()); |
166 | 179 | }
|
167 | 180 |
|
168 | 181 | @After
|
@@ -483,4 +496,77 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th
|
483 | 496 | verify(backendClientReturnsOk, times(2))
|
484 | 497 | .generateAuthToken(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN);
|
485 | 498 | }
|
| 499 | + |
| 500 | + @Test |
| 501 | + public void testDelete_registeredFID_successful() throws Exception { |
| 502 | + // Update local storage with a registered fid entry |
| 503 | + persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY); |
| 504 | + FirebaseInstallations firebaseInstallations = |
| 505 | + new FirebaseInstallations( |
| 506 | + mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); |
| 507 | + |
| 508 | + Tasks.await(firebaseInstallations.delete()); |
| 509 | + |
| 510 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 511 | + assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull(); |
| 512 | + verify(backendClientReturnsOk, times(1)) |
| 513 | + .deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); |
| 514 | + } |
| 515 | + |
| 516 | + @Test |
| 517 | + public void testDelete_unregisteredFID_successful() throws Exception { |
| 518 | + // Update local storage with a unregistered fid entry |
| 519 | + persistedFid.insertOrUpdatePersistedFidEntry(UNREGISTERED_FID_ENTRY); |
| 520 | + FirebaseInstallations firebaseInstallations = |
| 521 | + new FirebaseInstallations( |
| 522 | + mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); |
| 523 | + |
| 524 | + Tasks.await(firebaseInstallations.delete()); |
| 525 | + |
| 526 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 527 | + assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull(); |
| 528 | + verify(backendClientReturnsOk, never()) |
| 529 | + .deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); |
| 530 | + } |
| 531 | + |
| 532 | + @Test |
| 533 | + public void testDelete_emptyPersistedFidEntry_successful() throws Exception { |
| 534 | + FirebaseInstallations firebaseInstallations = |
| 535 | + new FirebaseInstallations( |
| 536 | + mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); |
| 537 | + |
| 538 | + Tasks.await(firebaseInstallations.delete()); |
| 539 | + |
| 540 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 541 | + assertWithMessage("Persisted Fid Entry is not null.").that(entryValue).isNull(); |
| 542 | + verify(backendClientReturnsOk, never()) |
| 543 | + .deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); |
| 544 | + } |
| 545 | + |
| 546 | + @Test |
| 547 | + public void testDelete_serverError_failure() throws Exception { |
| 548 | + // Update local storage with a registered fid entry |
| 549 | + persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY); |
| 550 | + FirebaseInstallations firebaseInstallations = |
| 551 | + new FirebaseInstallations( |
| 552 | + mockClock, executor, firebaseApp, backendClientReturnsError, persistedFid, mockUtils); |
| 553 | + |
| 554 | + // Expect exception |
| 555 | + try { |
| 556 | + Tasks.await(firebaseInstallations.delete()); |
| 557 | + fail("delete() failed due to Server Error."); |
| 558 | + } catch (ExecutionException expected) { |
| 559 | + assertWithMessage("Exception class doesn't match") |
| 560 | + .that(expected) |
| 561 | + .hasCauseThat() |
| 562 | + .isInstanceOf(FirebaseInstallationsException.class); |
| 563 | + assertWithMessage("Exception status doesn't match") |
| 564 | + .that(((FirebaseInstallationsException) expected.getCause()).getStatus()) |
| 565 | + .isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); |
| 566 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 567 | + assertWithMessage("Persisted Fid Entry doesn't match") |
| 568 | + .that(entryValue) |
| 569 | + .isEqualTo(REGISTERED_FID_ENTRY); |
| 570 | + } |
| 571 | + } |
486 | 572 | }
|
0 commit comments