From 1ff6a3ab2d9252d3aa9e873db96a9525a12a5451 Mon Sep 17 00:00:00 2001 From: Gaston Thea Date: Tue, 31 Oct 2023 16:54:43 -0300 Subject: [PATCH] More tests --- .../sets/FlagSetsStreamingTest.java | 2 +- .../java/tests/storage/SplitsStorageTest.java | 83 +++++++++++++++---- .../storage/splits/SplitsStorageImpl.java | 2 +- .../service/SplitInPlaceUpdateTaskTest.java | 31 ++++++- 4 files changed, 101 insertions(+), 17 deletions(-) diff --git a/src/androidTest/java/tests/integration/sets/FlagSetsStreamingTest.java b/src/androidTest/java/tests/integration/sets/FlagSetsStreamingTest.java index d61a46e06..20d3afa06 100644 --- a/src/androidTest/java/tests/integration/sets/FlagSetsStreamingTest.java +++ b/src/androidTest/java/tests/integration/sets/FlagSetsStreamingTest.java @@ -134,7 +134,7 @@ public void sdkWithSetsConfiguredDeletedDueToNonMatchingSets() throws IOExceptio assertTrue(firstChange); assertTrue(secondChange); assertTrue(thirdChange); - assertTrue(fourthChange); + assertFalse(fourthChange); // SDK_UPDATE should not be triggered since there were no changes to storage } @Test diff --git a/src/androidTest/java/tests/storage/SplitsStorageTest.java b/src/androidTest/java/tests/storage/SplitsStorageTest.java index 1669c1660..e818c5ee3 100644 --- a/src/androidTest/java/tests/storage/SplitsStorageTest.java +++ b/src/androidTest/java/tests/storage/SplitsStorageTest.java @@ -1,5 +1,8 @@ package tests.storage; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import android.content.Context; import androidx.test.platform.app.InstrumentationRegistry; @@ -282,8 +285,8 @@ public void updatedSplitTrafficType() { mSplitsStorage.update(new ProcessedSplitChange(Arrays.asList(s2), empty, 1L, 0L)); mSplitsStorage.update(new ProcessedSplitChange(empty, Arrays.asList(s2ar), 1L, 0L)); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("tt")); - Assert.assertFalse(mSplitsStorage.isValidTrafficType("mytt")); + assertTrue(mSplitsStorage.isValidTrafficType("tt")); + assertFalse(mSplitsStorage.isValidTrafficType("mytt")); } @Test @@ -300,8 +303,8 @@ public void changedTrafficTypeForSplit() { mSplitsStorage.update(new ProcessedSplitChange(Arrays.asList(s1t1), empty, 1L, 0L)); mSplitsStorage.update(new ProcessedSplitChange(Arrays.asList(s1t2), empty, 1L, 0L)); - Assert.assertFalse(mSplitsStorage.isValidTrafficType("tt")); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("mytt")); + assertFalse(mSplitsStorage.isValidTrafficType("tt")); + assertTrue(mSplitsStorage.isValidTrafficType("mytt")); } @Test @@ -320,8 +323,8 @@ public void existingChangedTrafficTypeForSplit() { mSplitsStorage.update(new ProcessedSplitChange(Arrays.asList(s1t1), empty, 1L, 0L)); mSplitsStorage.update(new ProcessedSplitChange(Arrays.asList(s1t2), empty, 1L, 0L)); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("tt")); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("mytt")); + assertTrue(mSplitsStorage.isValidTrafficType("tt")); + assertTrue(mSplitsStorage.isValidTrafficType("mytt")); } @Test @@ -331,9 +334,9 @@ public void trafficTypesAreLoadedInMemoryWhenLoadingLocalSplits() { mSplitsStorage.loadLocal(); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("test_type")); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("test_type_2")); - Assert.assertFalse(mSplitsStorage.isValidTrafficType("invalid_type")); + assertTrue(mSplitsStorage.isValidTrafficType("test_type")); + assertTrue(mSplitsStorage.isValidTrafficType("test_type_2")); + assertFalse(mSplitsStorage.isValidTrafficType("invalid_type")); } @Test @@ -346,9 +349,9 @@ public void loadedFromStorageTrafficTypesAreCorrectlyUpdated() { Split updatedSplit = newSplit("split_test", Status.ACTIVE, "new_type"); mSplitsStorage.update(new ProcessedSplitChange(Collections.singletonList(updatedSplit), Collections.emptyList(), 1L, 0L)); - Assert.assertFalse(mSplitsStorage.isValidTrafficType("test_type")); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("new_type")); - Assert.assertTrue(mSplitsStorage.isValidTrafficType("test_type_2")); + assertFalse(mSplitsStorage.isValidTrafficType("test_type")); + assertTrue(mSplitsStorage.isValidTrafficType("new_type")); + assertTrue(mSplitsStorage.isValidTrafficType("test_type_2")); } @Test @@ -382,7 +385,7 @@ public void flagSetsAreRemovedWhenUpdating() { Collections.singletonList(newSplit("split_test", Status.ACTIVE, "test_type")), Collections.emptyList(), 1L, 0L)); - Assert.assertFalse(initialSet1.isEmpty()); + assertFalse(initialSet1.isEmpty()); Assert.assertEquals(Collections.emptySet(), mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1"))); Assert.assertEquals(initialSet2, mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_2"))); } @@ -398,11 +401,63 @@ public void updateWithoutChecksRemovesFromFlagSet() { mSplitsStorage.updateWithoutChecks(newSplit("split_test", Status.ACTIVE, "test_type")); - Assert.assertFalse(initialSet1.isEmpty()); + assertFalse(initialSet1.isEmpty()); Assert.assertEquals(Collections.emptySet(), mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1"))); Assert.assertEquals(initialSet2, mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_2"))); } + @Test + public void updateReturnsTrueWhenFlagsHaveBeenRemovedFromStorage() { + mRoomDb.clearAllTables(); + mRoomDb.splitDao().insert(Arrays.asList(newSplitEntity("split_test", "test_type", Collections.singleton("set_1")), newSplitEntity("split_test_2", "test_type_2", Collections.singleton("set_2")))); + mSplitsStorage.loadLocal(); + + ArrayList archivedSplits = new ArrayList<>(); + archivedSplits.add(newSplit("split_test", Status.ARCHIVED, "test_type")); + boolean update = mSplitsStorage.update(new ProcessedSplitChange(new ArrayList<>(), archivedSplits, 1L, 0L)); + + assertTrue(update); + } + + @Test + public void updateReturnsTrueWhenFlagsWereAddedToStorage() { + mRoomDb.clearAllTables(); + mRoomDb.splitDao().insert(Arrays.asList(newSplitEntity("split_test", "test_type", Collections.singleton("set_1")), newSplitEntity("split_test_2", "test_type_2", Collections.singleton("set_2")))); + mSplitsStorage.loadLocal(); + + ArrayList activeSplits = new ArrayList<>(); + activeSplits.add(newSplit("split_test_3", Status.ACTIVE, "test_type_2", Collections.singleton("set_2"))); + boolean update = mSplitsStorage.update(new ProcessedSplitChange(activeSplits, new ArrayList<>(), 1L, 0L)); + + assertTrue(update); + } + + @Test + public void updateReturnsTrueWhenFlagsWereUpdatedInStorage() { + mRoomDb.clearAllTables(); + mRoomDb.splitDao().insert(Arrays.asList(newSplitEntity("split_test", "test_type", Collections.singleton("set_1")), newSplitEntity("split_test_2", "test_type_2", Collections.singleton("set_2")))); + mSplitsStorage.loadLocal(); + + ArrayList activeSplits = new ArrayList<>(); + activeSplits.add(newSplit("split_test", Status.ACTIVE, "test_type", Collections.singleton("set_2"))); + boolean update = mSplitsStorage.update(new ProcessedSplitChange(activeSplits, new ArrayList<>(), 1L, 0L)); + + assertTrue(update); + } + + @Test + public void updateReturnsFalseWhenFlagsThatAreNotInStorageAreAttemptedToBeRemoved() { + mRoomDb.clearAllTables(); + mRoomDb.splitDao().insert(Arrays.asList(newSplitEntity("split_test", "test_type", Collections.singleton("set_1")), newSplitEntity("split_test_2", "test_type_2", Collections.singleton("set_2")))); + mSplitsStorage.loadLocal(); + + ArrayList archivedSplits = new ArrayList<>(); + archivedSplits.add(newSplit("split_test_3", Status.ACTIVE, "test_type_2", Collections.singleton("set_2"))); + boolean update = mSplitsStorage.update(new ProcessedSplitChange(new ArrayList<>(), archivedSplits, 1L, 0L)); + + assertFalse(update); + } + private Split newSplit(String name, Status status, String trafficType) { return newSplit(name, status, trafficType, Collections.emptySet()); } diff --git a/src/main/java/io/split/android/client/storage/splits/SplitsStorageImpl.java b/src/main/java/io/split/android/client/storage/splits/SplitsStorageImpl.java index d79e4a59e..d18012b27 100644 --- a/src/main/java/io/split/android/client/storage/splits/SplitsStorageImpl.java +++ b/src/main/java/io/split/android/client/storage/splits/SplitsStorageImpl.java @@ -82,7 +82,7 @@ public boolean update(ProcessedSplitChange splitChange) { return false; } - // return true if there is at least one split change; otherwise false + // this will be true if there is at least one split change; otherwise false boolean result = false; List activeSplits = splitChange.getActiveSplits(); diff --git a/src/test/java/io/split/android/client/service/SplitInPlaceUpdateTaskTest.java b/src/test/java/io/split/android/client/service/SplitInPlaceUpdateTaskTest.java index 1ba725bbb..90ecd75b5 100644 --- a/src/test/java/io/split/android/client/service/SplitInPlaceUpdateTaskTest.java +++ b/src/test/java/io/split/android/client/service/SplitInPlaceUpdateTaskTest.java @@ -61,7 +61,6 @@ public void sseUpdateIsRecordedInTelemetryWhenOperationIsSuccessful() { verify(mSplitChangeProcessor).process(mSplit, 123L); verify(mSplitsStorage).update(processedSplitChange); - verify(mEventsManager).notifyInternalEvent(SplitInternalEvent.SPLITS_UPDATED); verify(mTelemetryRuntimeProducer).recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); assertEquals(result.getStatus(), SplitTaskExecutionStatus.SUCCESS); @@ -98,4 +97,34 @@ public void exceptionDuringStorageUpdateReturnsErrorExecutionInfo() { assertEquals(result.getStatus(), SplitTaskExecutionStatus.ERROR); } + + @Test + public void sdkUpdateIsNotTriggeredWhenStorageUpdateReturnsFalse() { + ProcessedSplitChange processedSplitChange = new ProcessedSplitChange(new ArrayList<>(), new ArrayList<>(), 0L, 0); + + when(mSplitChangeProcessor.process(mSplit, 123L)).thenReturn(processedSplitChange); + when(mSplitsStorage.update(processedSplitChange)).thenReturn(false); + + SplitTaskExecutionInfo result = mSplitInPlaceUpdateTask.execute(); + + verify(mSplitChangeProcessor).process(mSplit, 123L); + verify(mSplitsStorage).update(processedSplitChange); + verify(mEventsManager, never()).notifyInternalEvent(any()); + verify(mTelemetryRuntimeProducer).recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + } + + @Test + public void sdkUpdateIsTriggeredWhenStorageUpdateReturnsTrue() { + ProcessedSplitChange processedSplitChange = new ProcessedSplitChange(new ArrayList<>(), new ArrayList<>(), 0L, 0); + + when(mSplitChangeProcessor.process(mSplit, 123L)).thenReturn(processedSplitChange); + when(mSplitsStorage.update(processedSplitChange)).thenReturn(true); + + SplitTaskExecutionInfo result = mSplitInPlaceUpdateTask.execute(); + + verify(mSplitChangeProcessor).process(mSplit, 123L); + verify(mSplitsStorage).update(processedSplitChange); + verify(mEventsManager).notifyInternalEvent(SplitInternalEvent.SPLITS_UPDATED); + verify(mTelemetryRuntimeProducer).recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + } }