Skip to content

getId() implementation with instrumented tests. #703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
30cf6ed
getId() implementation with instrumented tests.
ankitaj224 Aug 13, 2019
d034bbb
Addressing rayo's comments.
ankitaj224 Aug 14, 2019
f8b468c
Addressing rayo's comments.
ankitaj224 Aug 14, 2019
8db7967
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 14, 2019
b507a3c
Merge branch 'create_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 14, 2019
f5a7c05
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 14, 2019
90cdbaa
Addresing comments to resoleve the following:
ankitaj224 Aug 16, 2019
0dfa987
Addressing Ciaran and Rayo's comments.
ankitaj224 Aug 26, 2019
a71a1f3
Addressing Ciaran's comments
ankitaj224 Aug 27, 2019
0a92e2d
Addressing Ciaran's comments
ankitaj224 Aug 27, 2019
4cc0cee
Merge branch 'create_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 28, 2019
f16db3f
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 29, 2019
f8e75fd
Adding param comments and checking if registration status is valid.
ankitaj224 Aug 30, 2019
227bf3b
Correcting lint warning: uses-permission should be declared before
ankitaj224 Aug 30, 2019
4be3847
Adding custom assertThat with more readable assertions
ankitaj224 Aug 30, 2019
68acde5
Correcting instrumented tests to be reliable.
ankitaj224 Aug 30, 2019
16544a6
Merge branch 'ankita_fis' of github.com:firebase/firebase-android-sdk…
ankitaj224 Aug 30, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.firebase.installations;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1;
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN;
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_FID_1;
Expand All @@ -37,6 +37,10 @@
import com.google.firebase.installations.remote.FirebaseInstallationServiceException;
import com.google.firebase.installations.remote.InstallationResponse;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
Expand All @@ -55,15 +59,18 @@
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FirebaseInstallationsInstrumentedTest {
private FirebaseApp firebaseApp;
private Executor executor;
private PersistedFid persistedFid;
@Mock private FirebaseInstallationServiceClient backendClientReturnsOk;
@Mock private FirebaseInstallationServiceClient backendClientReturnsError;
private PersistedFid persistedFid;
@Mock private PersistedFid persistedFidReturnsError;
@Mock private Utils mockUtils;

@Before
public void setUp() throws FirebaseInstallationServiceException {
MockitoAnnotations.initMocks(this);
FirebaseApp.clearInstancesForTest();
executor = new ThreadPoolExecutor(0, 2, 30L, TimeUnit.SECONDS, new SynchronousQueue<>());
firebaseApp =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
Expand Down Expand Up @@ -92,6 +99,7 @@ public void setUp() throws FirebaseInstallationServiceException {
"SDK Error", FirebaseInstallationServiceException.Status.SERVER_ERROR));
when(persistedFidReturnsError.insertOrUpdatePersistedFidEntry(any())).thenReturn(false);
when(persistedFidReturnsError.readPersistedFidEntryValue()).thenReturn(null);
when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1);
}

@After
Expand All @@ -100,53 +108,112 @@ public void cleanUp() throws Exception {
}

@Test
public void testCreateFirebaseInstallation_PersistedFidOk_BackendOk() throws Exception {
public void testGetId_PersistedFidOk_BackendOk() throws Exception {
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(firebaseApp, persistedFid, backendClientReturnsOk);
new FirebaseInstallations(
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);

// No exception, means success.
assertThat(Tasks.await(firebaseInstallations.getId())).isNotEmpty();
assertWithMessage("getId Task fails.")
.that(Tasks.await(firebaseInstallations.getId()))
.isNotEmpty();
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertThat(entryValue.getFirebaseInstallationId()).isNotEmpty();
assertThat(entryValue.getPersistedStatus()).isEqualTo(PersistedFid.PersistedStatus.REGISTERED);
assertWithMessage("Persisted Fid doesn't match")
.that(entryValue.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Registration status doesn't match")
.that(entryValue.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.PENDING);

// Waiting for Task that registers FID on the FIS Servers
Thread.sleep(500);

PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid doesn't match")
.that(updatedFidEntry.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Registration status doesn't match")
.that(updatedFidEntry.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.REGISTERED);
}

@Test
public void testCreateFirebaseInstallation_PersistedFidOk_BackendError() throws Exception {
public void testGetId_multipleCalls_sameFIDReturned() throws Exception {
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(firebaseApp, persistedFid, backendClientReturnsError);
new FirebaseInstallations(
executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils);

// Expect exception
try {
Tasks.await(firebaseInstallations.getId());
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertThat(cause).isInstanceOf(FirebaseInstallationsException.class);
assertThat(((FirebaseInstallationsException) cause).getStatus())
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR);
}
// No exception, means success.
assertWithMessage("getId Task fails.")
.that(Tasks.await(firebaseInstallations.getId()))
.isNotEmpty();
PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid doesn't match")
.that(entryValue.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Registration status doesn't match")
.that(entryValue.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.PENDING);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what prevents the FID from being registered at this point in the test code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, nothing. I thought about it. But the test runs have been consistent with registration taking some time. Is there a better way to test it or do i skip the status check altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ciarand Your thoughts.


Tasks.await(firebaseInstallations.getId());

// Waiting for Task that registers FID on the FIS Servers
Thread.sleep(500);

PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid doesn't match")
.that(updatedFidEntry.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Registration status doesn't match")
.that(updatedFidEntry.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.REGISTERED);
}

@Test
public void testGetId_PersistedFidOk_BackendError() throws Exception {
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(
executor, firebaseApp, backendClientReturnsError, persistedFid, mockUtils);

Tasks.await(firebaseInstallations.getId());

PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue();
assertThat(entryValue.getFirebaseInstallationId()).isNotEmpty();
assertThat(entryValue.getPersistedStatus())
.isEqualTo(PersistedFid.PersistedStatus.REGISTER_ERROR);
assertWithMessage("Persisted Fid doesn't match")
.that(entryValue.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Registration Fid doesn't match")
.that(entryValue.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.PENDING);

// Waiting for Task that registers FID on the FIS Servers
Thread.sleep(500);

PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue();
assertWithMessage("Persisted Fid doesn't match")
.that(updatedFidEntry.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Registration Fid doesn't match")
.that(updatedFidEntry.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.REGISTER_ERROR);
}

@Test
public void testCreateFirebaseInstallation_PersistedFidError_BackendOk()
throws InterruptedException {
public void testGetId_PersistedFidError_BackendOk() throws InterruptedException {
FirebaseInstallations firebaseInstallations =
new FirebaseInstallations(firebaseApp, persistedFidReturnsError, backendClientReturnsOk);
new FirebaseInstallations(
executor, firebaseApp, backendClientReturnsOk, persistedFidReturnsError, mockUtils);

// Expect exception
try {
Tasks.await(firebaseInstallations.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity:
Does this framework not have "assertThrows"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isnt one. However, I found RC using one by adding Asssert in Test Utils.

https://github.com/firebase/firebase-android-sdk/blob/5301121df4727b357293b3b7de00555362f90170/firebase-config/src/test/java/com/google/firebase/remoteconfig/testutil/Assert.java

Rest of the firebase-android-sdk teams have handled the same way as me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertThat(cause).isInstanceOf(FirebaseInstallationsException.class);
assertThat(((FirebaseInstallationsException) cause).getStatus())
assertWithMessage("Exception class doesn't match")
.that(cause)
.isInstanceOf(FirebaseInstallationsException.class);
assertWithMessage("Exception status doesn't match")
.that(((FirebaseInstallationsException) cause).getStatus())
.isEqualTo(FirebaseInstallationsException.Status.CLIENT_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.firebase.installations.local;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1;
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_2;
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN;
Expand All @@ -41,8 +41,8 @@ public class PersistedFidTest {

private FirebaseApp firebaseApp0;
private FirebaseApp firebaseApp1;
private PersistedFid cache0;
private PersistedFid cache1;
private PersistedFid persistedFid0;
private PersistedFid persistedFid1;

@Before
public void setUp() {
Expand All @@ -56,60 +56,89 @@ public void setUp() {
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder().setApplicationId(TEST_APP_ID_2).build(),
"firebase_app_1");
cache0 = new PersistedFid(firebaseApp0);
cache1 = new PersistedFid(firebaseApp1);
persistedFid0 = new PersistedFid(firebaseApp0);
persistedFid1 = new PersistedFid(firebaseApp1);
}

@After
public void cleanUp() throws Exception {
cache0.clear();
cache1.clear();
persistedFid0.clear();
persistedFid1.clear();
}

@Test
public void testReadCacheEntry_Null() {
assertNull(cache0.readPersistedFidEntryValue());
assertNull(cache1.readPersistedFidEntryValue());
public void testReadPersistedFidEntry_Null() {
assertNull(persistedFid0.readPersistedFidEntryValue());
assertNull(persistedFid1.readPersistedFidEntryValue());
}

@Test
public void testUpdateAndReadCacheEntry() throws Exception {
public void testUpdateAndReadPersistedFidEntry_successful() throws Exception {
// Insert Persisted Fid Entry with Unregistered status in Shared Prefs
assertTrue(
cache0.insertOrUpdatePersistedFidEntry(
persistedFid0.insertOrUpdatePersistedFidEntry(
PersistedFidEntry.builder()
.setFirebaseInstallationId(TEST_FID_1)
.setAuthToken(TEST_AUTH_TOKEN)
.setRefreshToken(TEST_REFRESH_TOKEN)
.setPersistedStatus(PersistedFid.PersistedStatus.UNREGISTERED)
.setRegistrationStatus(PersistedFid.RegistrationStatus.UNREGISTERED)
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1)
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP)
.build()));
PersistedFidEntry entryValue = cache0.readPersistedFidEntryValue();
assertThat(entryValue.getFirebaseInstallationId()).isEqualTo(TEST_FID_1);
assertThat(entryValue.getAuthToken()).isEqualTo(TEST_AUTH_TOKEN);
assertThat(entryValue.getRefreshToken()).isEqualTo(TEST_REFRESH_TOKEN);
assertThat(entryValue.getPersistedStatus())
.isEqualTo(PersistedFid.PersistedStatus.UNREGISTERED);
assertThat(entryValue.getExpiresInSecs()).isEqualTo(TEST_TOKEN_EXPIRATION_TIMESTAMP);
assertThat(entryValue.getTokenCreationEpochInSecs()).isEqualTo(TEST_CREATION_TIMESTAMP_1);
assertNull(cache1.readPersistedFidEntryValue());
PersistedFidEntry entryValue = persistedFid0.readPersistedFidEntryValue();

// Validate insertion was successful
assertWithMessage("Persisted Fid doesn't match")
.that(entryValue.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Persisted Auth Token doesn't match")
.that(entryValue.getAuthToken())
.isEqualTo(TEST_AUTH_TOKEN);
assertWithMessage("Persisted Refresh Token doesn't match")
.that(entryValue.getRefreshToken())
.isEqualTo(TEST_REFRESH_TOKEN);
assertWithMessage("Persisted Registration Status doesn't match")
.that(entryValue.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.UNREGISTERED);
assertWithMessage("Persisted Token expiration timestamp doesn't match")
.that(entryValue.getExpiresInSecs())
.isEqualTo(TEST_TOKEN_EXPIRATION_TIMESTAMP);
assertWithMessage("Persisted Creation time doesn't match")
.that(entryValue.getTokenCreationEpochInSecs())
.isEqualTo(TEST_CREATION_TIMESTAMP_1);
assertNull(persistedFid1.readPersistedFidEntryValue());

// Update Persisted Fid Entry with Registered status in Shared Prefs
assertTrue(
cache0.insertOrUpdatePersistedFidEntry(
persistedFid0.insertOrUpdatePersistedFidEntry(
PersistedFidEntry.builder()
.setFirebaseInstallationId(TEST_FID_1)
.setAuthToken(TEST_AUTH_TOKEN)
.setRefreshToken(TEST_REFRESH_TOKEN)
.setPersistedStatus(PersistedFid.PersistedStatus.REGISTERED)
.setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED)
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2)
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP)
.build()));
entryValue = cache0.readPersistedFidEntryValue();
assertThat(entryValue.getFirebaseInstallationId()).isEqualTo(TEST_FID_1);
assertThat(entryValue.getAuthToken()).isEqualTo(TEST_AUTH_TOKEN);
assertThat(entryValue.getRefreshToken()).isEqualTo(TEST_REFRESH_TOKEN);
assertThat(entryValue.getPersistedStatus()).isEqualTo(PersistedFid.PersistedStatus.REGISTERED);
assertThat(entryValue.getExpiresInSecs()).isEqualTo(TEST_TOKEN_EXPIRATION_TIMESTAMP);
assertThat(entryValue.getTokenCreationEpochInSecs()).isEqualTo(TEST_CREATION_TIMESTAMP_2);
entryValue = persistedFid0.readPersistedFidEntryValue();

// Validate update was successful
assertWithMessage("Persisted Fid doesn't match")
.that(entryValue.getFirebaseInstallationId())
.isEqualTo(TEST_FID_1);
assertWithMessage("Persisted Auth Token doesn't match")
.that(entryValue.getAuthToken())
.isEqualTo(TEST_AUTH_TOKEN);
assertWithMessage("Persisted Refresh Token doesn't match")
.that(entryValue.getRefreshToken())
.isEqualTo(TEST_REFRESH_TOKEN);
assertWithMessage("Persisted Registration Status doesn't match")
.that(entryValue.getRegistrationStatus())
.isEqualTo(PersistedFid.RegistrationStatus.REGISTERED);
assertWithMessage("Persisted Token expiration timestamp doesn't match")
.that(entryValue.getExpiresInSecs())
.isEqualTo(TEST_TOKEN_EXPIRATION_TIMESTAMP);
assertWithMessage("Persisted Creation time doesn't match")
.that(entryValue.getTokenCreationEpochInSecs())
.isEqualTo(TEST_CREATION_TIMESTAMP_2);
}
}
Loading