Skip to content

Commit 715d427

Browse files
committed
Addressing Ciaran's comment on mock utils.
1 parent 314b3bb commit 715d427

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class FirebaseInstallationsInstrumentedTest {
9292
.setFirebaseInstallationId(TEST_FID_1)
9393
.setAuthToken(TEST_AUTH_TOKEN)
9494
.setRefreshToken(TEST_REFRESH_TOKEN)
95-
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1)
95+
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2)
9696
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP)
9797
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED)
9898
.build();
@@ -117,6 +117,16 @@ public class FirebaseInstallationsInstrumentedTest {
117117
.setRegistrationStatus(PersistedFid.RegistrationStatus.UNREGISTERED)
118118
.build();
119119

120+
private static final PersistedFidEntry UPDATED_AUTH_TOKEN_ENTRY =
121+
PersistedFidEntry.builder()
122+
.setFirebaseInstallationId(TEST_FID_1)
123+
.setAuthToken(TEST_AUTH_TOKEN_2)
124+
.setRefreshToken(TEST_REFRESH_TOKEN)
125+
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2)
126+
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP)
127+
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED)
128+
.build();
129+
120130
@Before
121131
public void setUp() throws FirebaseInstallationServiceException {
122132
MockitoAnnotations.initMocks(this);
@@ -152,7 +162,6 @@ public void setUp() throws FirebaseInstallationServiceException {
152162

153163
when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1);
154164
when(mockUtils.currentTimeInSecs()).thenReturn(TEST_CREATION_TIMESTAMP_2);
155-
when(mockUtils.isAuthTokenExpired(any())).thenReturn(false);
156165

157166
// Mocks success on FIS deletion
158167
doNothing()
@@ -173,6 +182,7 @@ public void cleanUp() throws Exception {
173182

174183
@Test
175184
public void testGetId_PersistedFidOk_BackendOk() throws Exception {
185+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
176186
FirebaseInstallations firebaseInstallations =
177187
new FirebaseInstallations(
178188
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -194,6 +204,7 @@ public void testGetId_PersistedFidOk_BackendOk() throws Exception {
194204

195205
@Test
196206
public void testGetId_multipleCalls_sameFIDReturned() throws Exception {
207+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
197208
FirebaseInstallations firebaseInstallations =
198209
new FirebaseInstallations(
199210
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -266,6 +277,7 @@ public void testGetId_fidRegistrationUncheckedException_statusUpdated() throws E
266277
invocation -> {
267278
throw new InterruptedException();
268279
});
280+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
269281

270282
FirebaseInstallations firebaseInstallations =
271283
new FirebaseInstallations(executor, firebaseApp, mockClient, persistedFid, mockUtils);
@@ -294,7 +306,7 @@ public void testGetId_expiredAuthTokenUncheckedException_statusUpdated() throws
294306
invocation -> {
295307
throw new InterruptedException();
296308
});
297-
when(mockUtils.isAuthTokenExpired(any())).thenReturn(true);
309+
when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true);
298310

299311
FirebaseInstallations firebaseInstallations =
300312
new FirebaseInstallations(executor, firebaseApp, mockClient, persistedFid, mockUtils);
@@ -316,9 +328,10 @@ public void testGetId_expiredAuthTokenUncheckedException_statusUpdated() throws
316328

317329
@Test
318330
public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception {
319-
when(mockUtils.isAuthTokenExpired(any())).thenReturn(true);
320331
// Update local storage with fid entry that has auth token expired.
321332
persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY);
333+
when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true);
334+
322335
FirebaseInstallations firebaseInstallations =
323336
new FirebaseInstallations(
324337
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -343,6 +356,7 @@ public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception {
343356

344357
@Test
345358
public void testGetAuthToken_fidDoesNotExist_successful() throws Exception {
359+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
346360
FirebaseInstallations firebaseInstallations =
347361
new FirebaseInstallations(
348362
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -355,6 +369,7 @@ public void testGetAuthToken_fidDoesNotExist_successful() throws Exception {
355369

356370
@Test
357371
public void testGetAuthToken_PersistedFidError_failure() throws Exception {
372+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
358373
FirebaseInstallations firebaseInstallations =
359374
new FirebaseInstallations(
360375
executor, firebaseApp, backendClientReturnsOk, persistedFidReturnsError, mockUtils);
@@ -378,6 +393,8 @@ public void testGetAuthToken_PersistedFidError_failure() throws Exception {
378393
@Test
379394
public void testGetAuthToken_fidExists_successful() throws Exception {
380395
when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY);
396+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
397+
381398
FirebaseInstallations firebaseInstallations =
382399
new FirebaseInstallations(
383400
executor, firebaseApp, backendClientReturnsOk, mockPersistedFid, mockUtils);
@@ -395,8 +412,10 @@ public void testGetAuthToken_fidExists_successful() throws Exception {
395412

396413
@Test
397414
public void testGetAuthToken_expiredAuthToken_fetchedNewTokenFromFIS() throws Exception {
398-
when(mockUtils.isAuthTokenExpired(any())).thenReturn(true, true, false);
399415
persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY);
416+
when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true);
417+
when(mockUtils.isAuthTokenExpired(UPDATED_AUTH_TOKEN_ENTRY)).thenReturn(false);
418+
400419
FirebaseInstallations firebaseInstallations =
401420
new FirebaseInstallations(
402421
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -417,6 +436,8 @@ public void testGetAuthToken_unregisteredFid_fetchedNewTokenFromFIS() throws Exc
417436
// Update local storage with a unregistered fid entry to validate that getAuthToken calls getId
418437
// to ensure FID registration and returns a valid auth token.
419438
persistedFid.insertOrUpdatePersistedFidEntry(UNREGISTERED_FID_ENTRY);
439+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
440+
420441
FirebaseInstallations firebaseInstallations =
421442
new FirebaseInstallations(
422443
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -440,6 +461,8 @@ public void testGetAuthToken_serverError_failure() throws Exception {
440461
.thenThrow(
441462
new FirebaseInstallationServiceException(
442463
"Server Error", FirebaseInstallationServiceException.Status.SERVER_ERROR));
464+
when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false);
465+
443466
FirebaseInstallations firebaseInstallations =
444467
new FirebaseInstallations(
445468
executor, firebaseApp, backendClientReturnsError, mockPersistedFid, mockUtils);
@@ -466,7 +489,9 @@ public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce(
466489
// triggered simultaneously. Task2 waits for Task1 to complete. On task1 completion, task2 reads
467490
// the UPDATED_AUTH_TOKEN_FID_ENTRY generated by Task1.
468491
persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY);
469-
when(mockUtils.isAuthTokenExpired(any())).thenReturn(true, true, true, false);
492+
when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true);
493+
when(mockUtils.isAuthTokenExpired(UPDATED_AUTH_TOKEN_ENTRY)).thenReturn(false);
494+
470495
FirebaseInstallations firebaseInstallations =
471496
new FirebaseInstallations(
472497
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);
@@ -514,6 +539,7 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th
514539
.build()))
515540
.when(backendClientReturnsOk)
516541
.generateAuthToken(anyString(), anyString(), anyString(), anyString());
542+
when(mockUtils.isAuthTokenExpired(any())).thenReturn(false);
517543

518544
FirebaseInstallations firebaseInstallations =
519545
new FirebaseInstallations(

0 commit comments

Comments
 (0)