|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.installations; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 18 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1; |
| 19 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN; |
| 20 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_CREATION_TIMESTAMP_1; |
| 21 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_FID_1; |
| 22 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_PROJECT_ID; |
| 23 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_REFRESH_TOKEN; |
| 24 | +import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP; |
| 25 | +import static org.junit.Assert.fail; |
| 26 | +import static org.mockito.ArgumentMatchers.any; |
| 27 | +import static org.mockito.ArgumentMatchers.anyString; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +import androidx.test.core.app.ApplicationProvider; |
| 31 | +import androidx.test.runner.AndroidJUnit4; |
| 32 | +import com.google.android.gms.common.util.Clock; |
| 33 | +import com.google.android.gms.tasks.Tasks; |
| 34 | +import com.google.firebase.FirebaseApp; |
| 35 | +import com.google.firebase.FirebaseOptions; |
| 36 | +import com.google.firebase.installations.local.PersistedFid; |
| 37 | +import com.google.firebase.installations.local.PersistedFidEntry; |
| 38 | +import com.google.firebase.installations.remote.FirebaseInstallationServiceClient; |
| 39 | +import com.google.firebase.installations.remote.FirebaseInstallationServiceException; |
| 40 | +import com.google.firebase.installations.remote.InstallationResponse; |
| 41 | +import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.concurrent.ExecutorService; |
| 43 | +import java.util.concurrent.SynchronousQueue; |
| 44 | +import java.util.concurrent.ThreadPoolExecutor; |
| 45 | +import java.util.concurrent.TimeUnit; |
| 46 | +import org.junit.After; |
| 47 | +import org.junit.Before; |
| 48 | +import org.junit.FixMethodOrder; |
| 49 | +import org.junit.Test; |
| 50 | +import org.junit.runner.RunWith; |
| 51 | +import org.junit.runners.MethodSorters; |
| 52 | +import org.mockito.Mock; |
| 53 | +import org.mockito.MockitoAnnotations; |
| 54 | + |
| 55 | +/** |
| 56 | + * Instrumented test, which will execute on an Android device. |
| 57 | + * |
| 58 | + * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> |
| 59 | + */ |
| 60 | +@RunWith(AndroidJUnit4.class) |
| 61 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 62 | +public class FirebaseInstallationsInstrumentedTest { |
| 63 | + private FirebaseApp firebaseApp; |
| 64 | + private ExecutorService executor; |
| 65 | + private PersistedFid persistedFid; |
| 66 | + @Mock private FirebaseInstallationServiceClient backendClientReturnsOk; |
| 67 | + @Mock private FirebaseInstallationServiceClient backendClientReturnsError; |
| 68 | + @Mock private PersistedFid persistedFidReturnsError; |
| 69 | + @Mock private Utils mockUtils; |
| 70 | + @Mock private Clock mockClock; |
| 71 | + |
| 72 | + @Before |
| 73 | + public void setUp() throws FirebaseInstallationServiceException { |
| 74 | + MockitoAnnotations.initMocks(this); |
| 75 | + FirebaseApp.clearInstancesForTest(); |
| 76 | + executor = new ThreadPoolExecutor(0, 2, 10L, TimeUnit.SECONDS, new SynchronousQueue<>()); |
| 77 | + firebaseApp = |
| 78 | + FirebaseApp.initializeApp( |
| 79 | + ApplicationProvider.getApplicationContext(), |
| 80 | + new FirebaseOptions.Builder() |
| 81 | + .setApplicationId(TEST_APP_ID_1) |
| 82 | + .setProjectId(TEST_PROJECT_ID) |
| 83 | + .setApiKey("api_key") |
| 84 | + .build()); |
| 85 | + persistedFid = new PersistedFid(firebaseApp); |
| 86 | + when(backendClientReturnsOk.createFirebaseInstallation( |
| 87 | + anyString(), anyString(), anyString(), anyString())) |
| 88 | + .thenReturn( |
| 89 | + InstallationResponse.builder() |
| 90 | + .setName("/projects/" + TEST_PROJECT_ID + "/installations/" + TEST_FID_1) |
| 91 | + .setRefreshToken(TEST_REFRESH_TOKEN) |
| 92 | + .setAuthToken( |
| 93 | + InstallationTokenResult.builder() |
| 94 | + .setToken(TEST_AUTH_TOKEN) |
| 95 | + .setTokenExpirationTimestampMillis(TEST_TOKEN_EXPIRATION_TIMESTAMP) |
| 96 | + .build()) |
| 97 | + .build()); |
| 98 | + when(backendClientReturnsError.createFirebaseInstallation( |
| 99 | + anyString(), anyString(), anyString(), anyString())) |
| 100 | + .thenThrow( |
| 101 | + new FirebaseInstallationServiceException( |
| 102 | + "SDK Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)); |
| 103 | + when(persistedFidReturnsError.insertOrUpdatePersistedFidEntry(any())).thenReturn(false); |
| 104 | + when(persistedFidReturnsError.readPersistedFidEntryValue()).thenReturn(null); |
| 105 | + when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1); |
| 106 | + when(mockClock.currentTimeMillis()).thenReturn(TEST_CREATION_TIMESTAMP_1); |
| 107 | + } |
| 108 | + |
| 109 | + @After |
| 110 | + public void cleanUp() throws Exception { |
| 111 | + persistedFid.clear(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetId_PersistedFidOk_BackendOk() throws Exception { |
| 116 | + FirebaseInstallations firebaseInstallations = |
| 117 | + new FirebaseInstallations( |
| 118 | + mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); |
| 119 | + |
| 120 | + // No exception, means success. |
| 121 | + assertWithMessage("getId Task fails.") |
| 122 | + .that(Tasks.await(firebaseInstallations.getId())) |
| 123 | + .isNotEmpty(); |
| 124 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 125 | + assertWithMessage("Persisted Fid doesn't match") |
| 126 | + .that(entryValue.getFirebaseInstallationId()) |
| 127 | + .isEqualTo(TEST_FID_1); |
| 128 | + |
| 129 | + // Waiting for Task that registers FID on the FIS Servers |
| 130 | + executor.awaitTermination(500, TimeUnit.MILLISECONDS); |
| 131 | + |
| 132 | + PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); |
| 133 | + assertWithMessage("Persisted Fid doesn't match") |
| 134 | + .that(updatedFidEntry.getFirebaseInstallationId()) |
| 135 | + .isEqualTo(TEST_FID_1); |
| 136 | + assertWithMessage("Registration status doesn't match") |
| 137 | + .that(updatedFidEntry.getRegistrationStatus()) |
| 138 | + .isEqualTo(PersistedFid.RegistrationStatus.REGISTERED); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testGetId_multipleCalls_sameFIDReturned() throws Exception { |
| 143 | + FirebaseInstallations firebaseInstallations = |
| 144 | + new FirebaseInstallations( |
| 145 | + mockClock, executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); |
| 146 | + |
| 147 | + // No exception, means success. |
| 148 | + assertWithMessage("getId Task fails.") |
| 149 | + .that(Tasks.await(firebaseInstallations.getId())) |
| 150 | + .isNotEmpty(); |
| 151 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 152 | + assertWithMessage("Persisted Fid doesn't match") |
| 153 | + .that(entryValue.getFirebaseInstallationId()) |
| 154 | + .isEqualTo(TEST_FID_1); |
| 155 | + |
| 156 | + Tasks.await(firebaseInstallations.getId()); |
| 157 | + |
| 158 | + // Waiting for Task that registers FID on the FIS Servers |
| 159 | + executor.awaitTermination(500, TimeUnit.MILLISECONDS); |
| 160 | + |
| 161 | + PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); |
| 162 | + assertWithMessage("Persisted Fid doesn't match") |
| 163 | + .that(updatedFidEntry.getFirebaseInstallationId()) |
| 164 | + .isEqualTo(TEST_FID_1); |
| 165 | + assertWithMessage("Registration status doesn't match") |
| 166 | + .that(updatedFidEntry.getRegistrationStatus()) |
| 167 | + .isEqualTo(PersistedFid.RegistrationStatus.REGISTERED); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testGetId_PersistedFidOk_BackendError() throws Exception { |
| 172 | + FirebaseInstallations firebaseInstallations = |
| 173 | + new FirebaseInstallations( |
| 174 | + mockClock, executor, firebaseApp, backendClientReturnsError, persistedFid, mockUtils); |
| 175 | + |
| 176 | + Tasks.await(firebaseInstallations.getId()); |
| 177 | + |
| 178 | + PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); |
| 179 | + assertWithMessage("Persisted Fid doesn't match") |
| 180 | + .that(entryValue.getFirebaseInstallationId()) |
| 181 | + .isEqualTo(TEST_FID_1); |
| 182 | + |
| 183 | + // Waiting for Task that registers FID on the FIS Servers |
| 184 | + executor.awaitTermination(500, TimeUnit.MILLISECONDS); |
| 185 | + |
| 186 | + PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); |
| 187 | + assertWithMessage("Persisted Fid doesn't match") |
| 188 | + .that(updatedFidEntry.getFirebaseInstallationId()) |
| 189 | + .isEqualTo(TEST_FID_1); |
| 190 | + assertWithMessage("Registration Fid doesn't match") |
| 191 | + .that(updatedFidEntry.getRegistrationStatus()) |
| 192 | + .isEqualTo(PersistedFid.RegistrationStatus.REGISTER_ERROR); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void testGetId_PersistedFidError_BackendOk() throws InterruptedException { |
| 197 | + FirebaseInstallations firebaseInstallations = |
| 198 | + new FirebaseInstallations( |
| 199 | + mockClock, |
| 200 | + executor, |
| 201 | + firebaseApp, |
| 202 | + backendClientReturnsOk, |
| 203 | + persistedFidReturnsError, |
| 204 | + mockUtils); |
| 205 | + |
| 206 | + // Expect exception |
| 207 | + try { |
| 208 | + Tasks.await(firebaseInstallations.getId()); |
| 209 | + fail(); |
| 210 | + } catch (ExecutionException expected) { |
| 211 | + Throwable cause = expected.getCause(); |
| 212 | + assertWithMessage("Exception class doesn't match") |
| 213 | + .that(cause) |
| 214 | + .isInstanceOf(FirebaseInstallationsException.class); |
| 215 | + assertWithMessage("Exception status doesn't match") |
| 216 | + .that(((FirebaseInstallationsException) cause).getStatus()) |
| 217 | + .isEqualTo(FirebaseInstallationsException.Status.CLIENT_ERROR); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments