|
14 | 14 |
|
15 | 15 | package com.google.firebase.segmentation;
|
16 | 16 |
|
| 17 | +import static com.google.common.truth.Truth.assertThat; |
17 | 18 | import static org.junit.Assert.assertNull;
|
| 19 | +import static org.junit.Assert.fail; |
| 20 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
| 22 | +import static org.mockito.Mockito.any; |
| 23 | +import static org.mockito.Mockito.when; |
18 | 24 |
|
| 25 | +import androidx.annotation.NonNull; |
19 | 26 | import androidx.test.core.app.ApplicationProvider;
|
20 | 27 | import androidx.test.ext.junit.runners.AndroidJUnit4;
|
| 28 | +import com.google.android.gms.tasks.Tasks; |
21 | 29 | import com.google.firebase.FirebaseApp;
|
22 | 30 | import com.google.firebase.FirebaseOptions;
|
| 31 | +import com.google.firebase.iid.FirebaseInstanceId; |
| 32 | +import com.google.firebase.iid.InstanceIdResult; |
| 33 | +import com.google.firebase.segmentation.local.CustomInstallationIdCache; |
| 34 | +import com.google.firebase.segmentation.local.CustomInstallationIdCacheEntryValue; |
| 35 | +import com.google.firebase.segmentation.remote.SegmentationServiceClient; |
| 36 | +import java.util.concurrent.ExecutionException; |
| 37 | +import org.junit.After; |
23 | 38 | import org.junit.Before;
|
| 39 | +import org.junit.FixMethodOrder; |
24 | 40 | import org.junit.Test;
|
25 | 41 | import org.junit.runner.RunWith;
|
| 42 | +import org.junit.runners.MethodSorters; |
| 43 | +import org.mockito.Mock; |
| 44 | +import org.mockito.MockitoAnnotations; |
26 | 45 |
|
27 | 46 | /**
|
28 | 47 | * Instrumented test, which will execute on an Android device.
|
29 | 48 | *
|
30 | 49 | * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
31 | 50 | */
|
32 | 51 | @RunWith(AndroidJUnit4.class)
|
| 52 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
33 | 53 | public class FirebaseSegmentationInstrumentedTest {
|
34 | 54 |
|
| 55 | + private static final String CUSTOM_INSTALLATION_ID = "123"; |
| 56 | + private static final String FIREBASE_INSTANCE_ID = "cAAAAAAAAAA"; |
| 57 | + |
35 | 58 | private FirebaseApp firebaseApp;
|
| 59 | + @Mock private FirebaseInstanceId firebaseInstanceId; |
| 60 | + @Mock private SegmentationServiceClient backendClientReturnsOk; |
| 61 | + @Mock private SegmentationServiceClient backendClientReturnsError; |
| 62 | + private CustomInstallationIdCache actualCache; |
| 63 | + @Mock private CustomInstallationIdCache cacheReturnsError; |
36 | 64 |
|
37 | 65 | @Before
|
38 | 66 | public void setUp() {
|
| 67 | + MockitoAnnotations.initMocks(this); |
39 | 68 | FirebaseApp.clearInstancesForTest();
|
40 | 69 | firebaseApp =
|
41 | 70 | FirebaseApp.initializeApp(
|
42 | 71 | ApplicationProvider.getApplicationContext(),
|
43 | 72 | new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
|
| 73 | + actualCache = new CustomInstallationIdCache(firebaseApp); |
| 74 | + |
| 75 | + when(backendClientReturnsOk.updateCustomInstallationId( |
| 76 | + anyLong(), anyString(), anyString(), anyString())) |
| 77 | + .thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK)); |
| 78 | + when(backendClientReturnsOk.clearCustomInstallationId(anyLong(), anyString(), anyString())) |
| 79 | + .thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK)); |
| 80 | + when(backendClientReturnsError.updateCustomInstallationId( |
| 81 | + anyLong(), anyString(), anyString(), anyString())) |
| 82 | + .thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR)); |
| 83 | + when(backendClientReturnsError.clearCustomInstallationId(anyLong(), anyString(), anyString())) |
| 84 | + .thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR)); |
| 85 | + when(firebaseInstanceId.getInstanceId()) |
| 86 | + .thenReturn( |
| 87 | + Tasks.forResult( |
| 88 | + new InstanceIdResult() { |
| 89 | + @NonNull |
| 90 | + @Override |
| 91 | + public String getId() { |
| 92 | + return FIREBASE_INSTANCE_ID; |
| 93 | + } |
| 94 | + |
| 95 | + @NonNull |
| 96 | + @Override |
| 97 | + public String getToken() { |
| 98 | + return "iid_token"; |
| 99 | + } |
| 100 | + })); |
| 101 | + when(cacheReturnsError.insertOrUpdateCacheEntry(any())).thenReturn(Tasks.forResult(false)); |
| 102 | + when(cacheReturnsError.readCacheEntryValue()).thenReturn(null); |
| 103 | + } |
| 104 | + |
| 105 | + @After |
| 106 | + public void cleanUp() throws Exception { |
| 107 | + Tasks.await(actualCache.clear()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testUpdateCustomInstallationId_CacheOk_BackendOk() throws Exception { |
| 112 | + FirebaseSegmentation firebaseSegmentation = |
| 113 | + new FirebaseSegmentation( |
| 114 | + firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk); |
| 115 | + |
| 116 | + // No exception, means success. |
| 117 | + assertNull(Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID))); |
| 118 | + CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue(); |
| 119 | + assertThat(entryValue.getCustomInstallationId()).isEqualTo(CUSTOM_INSTALLATION_ID); |
| 120 | + assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID); |
| 121 | + assertThat(entryValue.getCacheStatus()).isEqualTo(CustomInstallationIdCache.CacheStatus.SYNCED); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testUpdateCustomInstallationId_CacheOk_BackendError() throws InterruptedException { |
| 126 | + FirebaseSegmentation firebaseSegmentation = |
| 127 | + new FirebaseSegmentation( |
| 128 | + firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsError); |
| 129 | + |
| 130 | + // Expect exception |
| 131 | + try { |
| 132 | + Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID)); |
| 133 | + fail(); |
| 134 | + } catch (ExecutionException expected) { |
| 135 | + Throwable cause = expected.getCause(); |
| 136 | + assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class); |
| 137 | + assertThat(((SetCustomInstallationIdException) cause).getStatus()) |
| 138 | + .isEqualTo(SetCustomInstallationIdException.Status.BACKEND_ERROR); |
| 139 | + } |
| 140 | + |
| 141 | + CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue(); |
| 142 | + assertThat(entryValue.getCustomInstallationId()).isEqualTo(CUSTOM_INSTALLATION_ID); |
| 143 | + assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID); |
| 144 | + assertThat(entryValue.getCacheStatus()) |
| 145 | + .isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING_UPDATE); |
44 | 146 | }
|
45 | 147 |
|
46 | 148 | @Test
|
47 |
| - public void useAppContext() { |
48 |
| - assertNull(FirebaseSegmentation.getInstance().setCustomInstallationId("123123").getResult()); |
| 149 | + public void testUpdateCustomInstallationId_CacheError_BackendOk() throws InterruptedException { |
| 150 | + FirebaseSegmentation firebaseSegmentation = |
| 151 | + new FirebaseSegmentation( |
| 152 | + firebaseApp, firebaseInstanceId, cacheReturnsError, backendClientReturnsOk); |
| 153 | + |
| 154 | + // Expect exception |
| 155 | + try { |
| 156 | + Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID)); |
| 157 | + fail(); |
| 158 | + } catch (ExecutionException expected) { |
| 159 | + Throwable cause = expected.getCause(); |
| 160 | + assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class); |
| 161 | + assertThat(((SetCustomInstallationIdException) cause).getStatus()) |
| 162 | + .isEqualTo(SetCustomInstallationIdException.Status.CLIENT_ERROR); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testClearCustomInstallationId_CacheOk_BackendOk() throws Exception { |
| 168 | + Tasks.await( |
| 169 | + actualCache.insertOrUpdateCacheEntry( |
| 170 | + CustomInstallationIdCacheEntryValue.create( |
| 171 | + CUSTOM_INSTALLATION_ID, |
| 172 | + FIREBASE_INSTANCE_ID, |
| 173 | + CustomInstallationIdCache.CacheStatus.SYNCED))); |
| 174 | + FirebaseSegmentation firebaseSegmentation = |
| 175 | + new FirebaseSegmentation( |
| 176 | + firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk); |
| 177 | + |
| 178 | + // No exception, means success. |
| 179 | + assertNull(Tasks.await(firebaseSegmentation.setCustomInstallationId(null))); |
| 180 | + CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue(); |
| 181 | + assertNull(entryValue); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testClearCustomInstallationId_CacheOk_BackendError() throws Exception { |
| 186 | + Tasks.await( |
| 187 | + actualCache.insertOrUpdateCacheEntry( |
| 188 | + CustomInstallationIdCacheEntryValue.create( |
| 189 | + CUSTOM_INSTALLATION_ID, |
| 190 | + FIREBASE_INSTANCE_ID, |
| 191 | + CustomInstallationIdCache.CacheStatus.SYNCED))); |
| 192 | + FirebaseSegmentation firebaseSegmentation = |
| 193 | + new FirebaseSegmentation( |
| 194 | + firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsError); |
| 195 | + |
| 196 | + // Expect exception |
| 197 | + try { |
| 198 | + Tasks.await(firebaseSegmentation.setCustomInstallationId(null)); |
| 199 | + fail(); |
| 200 | + } catch (ExecutionException expected) { |
| 201 | + Throwable cause = expected.getCause(); |
| 202 | + assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class); |
| 203 | + assertThat(((SetCustomInstallationIdException) cause).getStatus()) |
| 204 | + .isEqualTo(SetCustomInstallationIdException.Status.BACKEND_ERROR); |
| 205 | + } |
| 206 | + |
| 207 | + CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue(); |
| 208 | + assertThat(entryValue.getCustomInstallationId().isEmpty()).isTrue(); |
| 209 | + assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID); |
| 210 | + assertThat(entryValue.getCacheStatus()) |
| 211 | + .isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING_CLEAR); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void testClearCustomInstallationId_CacheError_BackendOk() throws InterruptedException { |
| 216 | + FirebaseSegmentation firebaseSegmentation = |
| 217 | + new FirebaseSegmentation( |
| 218 | + firebaseApp, firebaseInstanceId, cacheReturnsError, backendClientReturnsOk); |
| 219 | + |
| 220 | + // Expect exception |
| 221 | + try { |
| 222 | + Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID)); |
| 223 | + fail(); |
| 224 | + } catch (ExecutionException expected) { |
| 225 | + Throwable cause = expected.getCause(); |
| 226 | + assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class); |
| 227 | + assertThat(((SetCustomInstallationIdException) cause).getStatus()) |
| 228 | + .isEqualTo(SetCustomInstallationIdException.Status.CLIENT_ERROR); |
| 229 | + } |
49 | 230 | }
|
50 | 231 | }
|
0 commit comments