Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Sep 14, 2023
1 parent 0bedb89 commit bf91a01
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import io.split.android.client.attributes.AttributesManager;
import io.split.android.client.attributes.AttributesMerger;
Expand Down Expand Up @@ -52,14 +51,14 @@ public class TreatmentManagerTelemetryTest {
@Mock
private SplitsStorage mSplitsStorage;

private Set<String> mConfiguredFlagSets = new HashSet<>();
private FlagSetsFilter mFlagSetsFilter;
private TreatmentManagerImpl treatmentManager;
private AutoCloseable mAutoCloseable;

@Before
public void setUp() {
mAutoCloseable = MockitoAnnotations.openMocks(this);

mFlagSetsFilter = new FlagSetsFilterImpl(new HashSet<>());
treatmentManager = new TreatmentManagerImpl(
"test_key",
"test_key",
Expand All @@ -72,7 +71,7 @@ public void setUp() {
attributesManager,
attributesMerger,
telemetryStorageProducer,
mConfiguredFlagSets,
mFlagSetsFilter,
mSplitsStorage);

when(evaluator.getTreatment(anyString(), anyString(), anyString(), anyMap())).thenReturn(new EvaluationResult("test", "label"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public class TreatmentManagerTest {
ListenableEventsManager eventsManagerStub;
AttributesManager attributesManager = mock(AttributesManager.class);
TelemetryStorageProducer telemetryStorageProducer = mock(TelemetryStorageProducer.class);
private Set<String> mConfiguredFlagSets;
private FlagSetsFilter mFlagSetsFilter;
TreatmentManagerImpl treatmentManager;
private SplitsStorage mSplitsStorage;

@Before
public void loadSplitsFromFile() {
mConfiguredFlagSets = new HashSet<>();
mFlagSetsFilter = new FlagSetsFilterImpl(new HashSet<>());
mSplitsStorage = mock(SplitsStorage.class);
treatmentManager = initializeTreatmentManager();
if (evaluator == null) {
Expand Down Expand Up @@ -325,7 +325,7 @@ private TreatmentManager createTreatmentManager(String matchingKey, String bucke
new KeyValidatorImpl(), new SplitValidatorImpl(),
new ImpressionListenerMock(), config.labelsEnabled(), eventsManagerStub,
mock(AttributesManager.class), mock(AttributesMerger.class),
mock(TelemetryStorageProducer.class), mConfiguredFlagSets, mSplitsStorage);
mock(TelemetryStorageProducer.class), mFlagSetsFilter, mSplitsStorage);
}

private TreatmentManagerImpl initializeTreatmentManager() {
Expand Down Expand Up @@ -353,7 +353,7 @@ private TreatmentManagerImpl initializeTreatmentManager(Evaluator evaluator) {
attributesManager,
mock(AttributesMerger.class),
telemetryStorageProducer,
mConfiguredFlagSets,
mFlagSetsFilter,
mSplitsStorage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,19 @@ public class TreatmentManagerWithFlagSetsTest {
@Mock
private SplitsStorage mSplitsStorage;

private Set<String> mConfiguredFlagSets;
private FlagSetsFilter mFlagSetsFilter;
private TreatmentManagerImpl mTreatmentManager;
private AutoCloseable mAutoCloseable;

@Before
public void setUp() {
mAutoCloseable = MockitoAnnotations.openMocks(this);

mConfiguredFlagSets = new HashSet<>();
mFlagSetsFilter = new FlagSetsFilterImpl(new HashSet<>());
when(mEventsManager.eventAlreadyTriggered(SplitEvent.SDK_READY)).thenReturn(true);
when(mEventsManager.eventAlreadyTriggered(SplitEvent.SDK_READY_FROM_CACHE)).thenReturn(true);

mTreatmentManager = new TreatmentManagerImpl(
"matching_key",
"bucketing_key",
mEvaluator,
mKeyValidator,
mSplitValidator,
mImpressionListener,
SplitClientConfig.builder().build().labelsEnabled(),
mEventsManager,
mAttributesManager,
mAttributesMerger,
mTelemetryStorageProducer,
mConfiguredFlagSets,
mSplitsStorage);
initializeTreatmentManager();

when(mEvaluator.getTreatment(anyString(), anyString(), eq("test_1"), anyMap()))
.thenReturn(new EvaluationResult("result_1", "label"));
Expand Down Expand Up @@ -129,7 +116,7 @@ public void getTreatmentsByFlagSetWithNoConfiguredSetsInvalidSetDoesNotQueryStor

@Test
public void getTreatmentsByFlagSetWithConfiguredSetsExistingSetQueriesStorageAndUsesEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1")))
.thenReturn(new HashSet<>(Collections.singletonList("test_1")));

Expand All @@ -141,7 +128,8 @@ public void getTreatmentsByFlagSetWithConfiguredSetsExistingSetQueriesStorageAnd

@Test
public void getTreatmentsByFlagSetWithConfiguredSetsNonExistingSetDoesNotQueryStorageNorUseEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
initializeTreatmentManager();
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1")))
.thenReturn(new HashSet<>(Collections.singletonList("test_split")));

Expand All @@ -151,13 +139,30 @@ public void getTreatmentsByFlagSetWithConfiguredSetsNonExistingSetDoesNotQuerySt
verify(mEvaluator, times(0)).getTreatment(any(), any(), any(), anyMap());
}

private void initializeTreatmentManager() {
mTreatmentManager = new TreatmentManagerImpl(
"matching_key",
"bucketing_key",
mEvaluator,
mKeyValidator,
mSplitValidator,
mImpressionListener,
SplitClientConfig.builder().build().labelsEnabled(),
mEventsManager,
mAttributesManager,
mAttributesMerger,
mTelemetryStorageProducer,
mFlagSetsFilter,
mSplitsStorage);
}

@Test
public void getTreatmentsByFlagSetReturnsCorrectFormat() {
Set<String> mockNames = new HashSet<>();
mockNames.add("test_1");
mockNames.add("test_2");
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1"))).thenReturn(mockNames);
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));

Map<String, String> result = mTreatmentManager.getTreatmentsByFlagSet("set_1", null, false);

Expand Down Expand Up @@ -209,7 +214,8 @@ public void getTreatmentsByFlagSetsWithNoConfiguredSetsInvalidSetDoesNotQuerySto

@Test
public void getTreatmentsByFlagSetsWithConfiguredSetsExistingSetQueriesStorageForConfiguredSetOnlyAndUsesEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
initializeTreatmentManager();
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1")))
.thenReturn(new HashSet<>(Collections.singletonList("test_1")));

Expand All @@ -221,7 +227,8 @@ public void getTreatmentsByFlagSetsWithConfiguredSetsExistingSetQueriesStorageFo

@Test
public void getTreatmentsByFlagSetsWithConfiguredSetsNonExistingSetDoesNotQueryStorageNorUseEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
initializeTreatmentManager();

mTreatmentManager.getTreatmentsByFlagSets(Arrays.asList("set_2", "set_3"), null, false);

Expand Down Expand Up @@ -301,7 +308,7 @@ public void getTreatmentsWithConfigByFlagSetWithNoConfiguredSetsInvalidSetDoesNo

@Test
public void getTreatmentsWithConfigByFlagSetWithConfiguredSetsExistingSetQueriesStorageAndUsesEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1")))
.thenReturn(new HashSet<>(Collections.singletonList("test_1")));

Expand All @@ -313,7 +320,8 @@ public void getTreatmentsWithConfigByFlagSetWithConfiguredSetsExistingSetQueries

@Test
public void getTreatmentsWithConfigByFlagSetWithConfiguredSetsNonExistingSetDoesNotQueryStorageNorUseEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
initializeTreatmentManager();
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1")))
.thenReturn(new HashSet<>(Collections.singletonList("test_split")));

Expand All @@ -329,7 +337,7 @@ public void getTreatmentsWithConfigByFlagSetReturnsCorrectFormat() {
mockNames.add("test_1");
mockNames.add("test_2");
when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1"))).thenReturn(mockNames);
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));

Map<String, SplitResult> result = mTreatmentManager.getTreatmentsWithConfigByFlagSet("set_1", null, false);

Expand Down Expand Up @@ -381,7 +389,9 @@ public void getTreatmentsWithConfigByFlagSetsWithNoConfiguredSetsInvalidSetDoesN

@Test
public void getTreatmentsWithConfigByFlagSetsWithConfiguredSetsExistingSetQueriesStorageForConfiguredSetOnlyAndUsesEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
initializeTreatmentManager();

when(mSplitsStorage.getNamesByFlagSets(Collections.singletonList("set_1")))
.thenReturn(new HashSet<>(Collections.singletonList("test_1")));

Expand All @@ -393,7 +403,8 @@ public void getTreatmentsWithConfigByFlagSetsWithConfiguredSetsExistingSetQuerie

@Test
public void getTreatmentsWithConfigByFlagSetsWithConfiguredSetsNonExistingSetDoesNotQueryStorageNorUseEvaluator() {
mConfiguredFlagSets.add("set_1");
mFlagSetsFilter = new FlagSetsFilterImpl(Collections.singleton("set_1"));
initializeTreatmentManager();

mTreatmentManager.getTreatmentsWithConfigByFlagSets(Arrays.asList("set_2", "set_3"), null, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import io.split.android.client.FlagSetsFilter;
import io.split.android.client.FlagSetsFilterImpl;
import io.split.android.client.SplitClient;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.api.Key;
Expand Down Expand Up @@ -52,14 +53,14 @@ public class LocalhostSplitClientContainerImplTest {
private SplitClientConfig mConfig;
@Mock
private SplitTaskExecutor mTaskExecutor;
private Set<String> mConfiguredSets;
private FlagSetsFilter mFlagSetsFilter;
private LocalhostSplitClientContainerImpl mClientContainer;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
when(mAttributesManagerFactory.getManager(any(), any())).thenReturn(mock(AttributesManager.class));
mConfiguredSets = new HashSet<>();
mFlagSetsFilter = new FlagSetsFilterImpl(new HashSet<>());
mClientContainer = getClientContainer();
}

Expand Down Expand Up @@ -108,6 +109,6 @@ private LocalhostSplitClientContainerImpl getClientContainer() {
mTelemetryStorageProducer,
mEventsManagerCoordinator,
mTaskExecutor,
mConfiguredSets);
mFlagSetsFilter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import java.util.Collections;

import io.split.android.client.EvaluatorImpl;
import io.split.android.client.EventsTracker;
import io.split.android.client.FlagSetsFilterImpl;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SplitClientImpl;
import io.split.android.client.SplitFactory;
Expand Down Expand Up @@ -39,7 +39,7 @@ public static SplitClientImpl get(Key key, SplitsStorage splitsStorage) {
TreatmentManagerFactory treatmentManagerFactory = new TreatmentManagerFactoryImpl(
new KeyValidatorImpl(), new SplitValidatorImpl(), new ImpressionListener.NoopImpressionListener(),
false, new AttributesMergerImpl(), telemetryStorage, splitParser,
Collections.emptySet(), splitsStorage);
new FlagSetsFilterImpl(Collections.emptySet()), splitsStorage);

AttributesManager attributesManager = mock(AttributesManager.class);
SplitClientImpl c = new SplitClientImpl(
Expand Down

0 comments on commit bf91a01

Please sign in to comment.