Skip to content

Commit 5169873

Browse files
author
Mike Davis
committed
Limit typed audience tests to v4+.
1 parent 8bdfa26 commit 5169873

File tree

1 file changed

+35
-56
lines changed

1 file changed

+35
-56
lines changed

core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.optimizely.ab.notification.*;
3232
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3333
import org.junit.Before;
34-
import org.junit.Ignore;
3534
import org.junit.Rule;
3635
import org.junit.Test;
3736
import org.junit.rules.ExpectedException;
@@ -43,7 +42,6 @@
4342

4443
import java.io.Closeable;
4544
import java.io.IOException;
46-
import java.util.ArrayList;
4745
import java.util.Arrays;
4846
import java.util.Collection;
4947
import java.util.Collections;
@@ -247,15 +245,11 @@ public void activateEndToEnd() throws Exception {
247245
*/
248246
@Test
249247
public void activateEndToEndWithTypedAudienceInt() throws Exception {
250-
Experiment activatedExperiment;
251-
Map<String, Object> testUserAttributes = new HashMap<>();
252-
if (datafileVersion >= 4) {
253-
activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_TYPEDAUDIENCE_EXPERIMENT_KEY);
254-
testUserAttributes.put(ATTRIBUTE_INTEGER_KEY, 2); // should be gt 1.
255-
} else {
256-
activatedExperiment = validProjectConfig.getExperiments().get(0);
257-
testUserAttributes.put("browser_type", "chrome");
258-
}
248+
assumeTrue(datafileVersion >= 4);
249+
250+
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping()
251+
.get(EXPERIMENT_TYPEDAUDIENCE_EXPERIMENT_KEY);
252+
Map<String, Object> testUserAttributes = Collections.singletonMap(ATTRIBUTE_INTEGER_KEY, 2); // should be gt 1.
259253

260254
logbackVerifier.expectMessage(Level.DEBUG, "This decision will not be saved since the UserProfileService is null.");
261255
logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" +
@@ -308,15 +302,11 @@ public void activateEndToEndWithTypedAudienceIntExact() throws Exception {
308302
*/
309303
@Test
310304
public void activateEndToEndWithTypedAudienceBool() throws Exception {
311-
Experiment activatedExperiment;
312-
Map<String, Object> testUserAttributes = new HashMap<>();
313-
if (datafileVersion >= 4) {
314-
activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_TYPEDAUDIENCE_EXPERIMENT_KEY);
315-
testUserAttributes.put(ATTRIBUTE_BOOLEAN_KEY, true); // should be eq true.
316-
} else {
317-
activatedExperiment = validProjectConfig.getExperiments().get(0);
318-
testUserAttributes.put("browser_type", "chrome");
319-
}
305+
assumeTrue(datafileVersion >= 4);
306+
307+
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping()
308+
.get(EXPERIMENT_TYPEDAUDIENCE_EXPERIMENT_KEY);
309+
Map<String, Object> testUserAttributes = Collections.singletonMap(ATTRIBUTE_BOOLEAN_KEY, true); // should be eq true.
320310

321311
logbackVerifier.expectMessage(Level.DEBUG, "This decision will not be saved since the UserProfileService is null.");
322312
logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" +
@@ -335,15 +325,11 @@ public void activateEndToEndWithTypedAudienceBool() throws Exception {
335325
*/
336326
@Test
337327
public void activateEndToEndWithTypedAudienceDouble() throws Exception {
338-
Experiment activatedExperiment;
339-
Map<String, Object> testUserAttributes = new HashMap<>();
340-
if (datafileVersion >= 4) {
341-
activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_TYPEDAUDIENCE_EXPERIMENT_KEY);
342-
testUserAttributes.put(ATTRIBUTE_DOUBLE_KEY, 99.9); // should be lt 100.
343-
} else {
344-
activatedExperiment = validProjectConfig.getExperiments().get(0);
345-
testUserAttributes.put("browser_type", "chrome");
346-
}
328+
assumeTrue(datafileVersion >= 4);
329+
330+
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping()
331+
.get(EXPERIMENT_TYPEDAUDIENCE_EXPERIMENT_KEY);
332+
Map<String, Object> testUserAttributes = Collections.singletonMap(ATTRIBUTE_DOUBLE_KEY, 99.9); // should be lt 100.
347333

348334
logbackVerifier.expectMessage(Level.DEBUG, "This decision will not be saved since the UserProfileService is null.");
349335
logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" +
@@ -379,17 +365,14 @@ public void activateEndToEndWithTypedAudienceBoolWithAndAudienceConditions() thr
379365
*/
380366
@Test
381367
public void activateEndToEndWithTypedAudienceWithAnd() throws Exception {
382-
Experiment activatedExperiment;
368+
assumeTrue(datafileVersion >= 4);
369+
370+
Experiment activatedExperiment = validProjectConfig.getExperimentKeyMapping()
371+
.get(EXPERIMENT_TYPEDAUDIENCE_WITH_AND_EXPERIMENT_KEY);
383372
Map<String, Object> testUserAttributes = new HashMap<>();
384-
if (datafileVersion >= 4) {
385-
activatedExperiment = validProjectConfig.getExperimentKeyMapping().get(EXPERIMENT_TYPEDAUDIENCE_WITH_AND_EXPERIMENT_KEY);
386-
testUserAttributes.put(ATTRIBUTE_DOUBLE_KEY, 99.9); // should be lt 100.
387-
testUserAttributes.put(ATTRIBUTE_BOOLEAN_KEY, true); // should be eq true.
388-
testUserAttributes.put(ATTRIBUTE_INTEGER_KEY, 2); // should be gt 1.
389-
} else {
390-
activatedExperiment = validProjectConfig.getExperiments().get(0);
391-
testUserAttributes.put("browser_type", "chrome");
392-
}
373+
testUserAttributes.put(ATTRIBUTE_DOUBLE_KEY, 99.9); // should be lt 100.
374+
testUserAttributes.put(ATTRIBUTE_BOOLEAN_KEY, true); // should be eq true.
375+
testUserAttributes.put(ATTRIBUTE_INTEGER_KEY, 2); // should be gt 1.
393376

394377
logbackVerifier.expectMessage(Level.DEBUG, "This decision will not be saved since the UserProfileService is null.");
395378
logbackVerifier.expectMessage(Level.INFO, "Activating user \"userId\" in experiment \"" +
@@ -483,7 +466,7 @@ public void getVariationWithExperimentKeyForced() throws Exception {
483466
Variation forcedVariation = activatedExperiment.getVariations().get(0);
484467
Variation bucketedVariation = activatedExperiment.getVariations().get(1);
485468

486-
Map<String, String> testUserAttributes = new HashMap<String, String>();
469+
Map<String, String> testUserAttributes = new HashMap<>();
487470
if (datafileVersion >= 4) {
488471
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
489472
} else {
@@ -537,7 +520,7 @@ public void activateWithExperimentKey() throws Exception {
537520
Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
538521
Variation bucketedVariation = activatedExperiment.getVariations().get(1);
539522

540-
Map<String, String> testUserAttributes = new HashMap<String, String>();
523+
Map<String, String> testUserAttributes = new HashMap<>();
541524
if (datafileVersion >= 4) {
542525
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
543526
} else {
@@ -836,7 +819,7 @@ public void activateForGroupExperimentWithMatchingAttributes() throws Exception
836819
.get(0);
837820
String userId = testUserId;
838821

839-
Map<String, String> attributes = new HashMap<String, String>();
822+
Map<String, String> attributes = new HashMap<>();
840823
if (datafileVersion == 4) {
841824
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
842825
userId = "user"; // To make sure the user gets allocated.
@@ -1348,7 +1331,7 @@ public void getVariation() throws Exception {
13481331

13491332
when(mockBucketer.bucket(activatedExperiment, testUserId, validProjectConfig)).thenReturn(bucketedVariation);
13501333

1351-
Map<String, String> testUserAttributes = new HashMap<String, String>();
1334+
Map<String, String> testUserAttributes = new HashMap<>();
13521335
testUserAttributes.put("browser_type", "chrome");
13531336

13541337
// activate the experiment
@@ -1442,7 +1425,7 @@ public void getVariationWithAudiences() throws Exception {
14421425

14431426
Optimizely optimizely = optimizelyBuilder.withBucketing(mockBucketer).build();
14441427

1445-
Map<String, String> testUserAttributes = new HashMap<String, String>();
1428+
Map<String, String> testUserAttributes = new HashMap<>();
14461429
testUserAttributes.put("browser_type", "chrome");
14471430

14481431
Variation actualVariation = optimizely.getVariation(experiment.getKey(), testUserId, testUserAttributes);
@@ -1548,7 +1531,7 @@ public void getVariationForGroupExperimentWithMatchingAttributes() throws Except
15481531
.get(0);
15491532
Variation variation = experiment.getVariations().get(0);
15501533

1551-
Map<String, String> attributes = new HashMap<String, String>();
1534+
Map<String, String> attributes = new HashMap<>();
15521535
if (datafileVersion >= 4) {
15531536
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
15541537
} else {
@@ -1773,7 +1756,7 @@ public void getEnabledFeaturesWithListenerMultipleFeatureEnabled() throws Except
17731756
assertEquals(decisionNotification.getType(), NotificationCenter.DecisionNotificationType.FEATURE.toString());
17741757
});
17751758

1776-
ArrayList<String> featureFlags = (ArrayList<String>) optimizely.getEnabledFeatures(testUserId, Collections.emptyMap());
1759+
List<String> featureFlags = optimizely.getEnabledFeatures(testUserId, Collections.emptyMap());
17771760
assertEquals(2, featureFlags.size());
17781761

17791762
// Why is there only a single impression when there are 2 enabled features?
@@ -1805,8 +1788,7 @@ public void getEnabledFeaturesWithNoFeatureEnabled() throws Exception {
18051788

18061789
int notificationId = spyOptimizely.addDecisionNotificationHandler( decisionNotification -> { });
18071790

1808-
ArrayList<String> featureFlags = (ArrayList<String>) spyOptimizely.getEnabledFeatures(genericUserId,
1809-
Collections.<String, String>emptyMap());
1791+
List<String> featureFlags = spyOptimizely.getEnabledFeatures(genericUserId, Collections.emptyMap());
18101792
assertTrue(featureFlags.isEmpty());
18111793

18121794
// Verify that listener not being called
@@ -2311,7 +2293,7 @@ public void activateWithListener() throws Exception {
23112293
final Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
23122294
final Variation bucketedVariation = activatedExperiment.getVariations().get(1);
23132295

2314-
final Map<String, String> testUserAttributes = new HashMap<String, String>();
2296+
final Map<String, String> testUserAttributes = new HashMap<>();
23152297
if (datafileVersion >= 4) {
23162298
testUserAttributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
23172299
} else {
@@ -2429,7 +2411,7 @@ public void removeNotificationListenerNotificationCenter() throws Exception {
24292411

24302412
Optimizely optimizely = optimizelyBuilder.build();
24312413

2432-
Map<String, String> attributes = new HashMap<String, String>();
2414+
Map<String, String> attributes = new HashMap<>();
24332415
if (datafileVersion >= 4) {
24342416
attributes.put(ATTRIBUTE_HOUSE_KEY, AUDIENCE_GRYFFINDOR_VALUE);
24352417
} else {
@@ -3303,8 +3285,7 @@ public void getEnabledFeatureWithValidUserId() throws Exception {
33033285
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
33043286

33053287
Optimizely optimizely = optimizelyBuilder.build();
3306-
ArrayList<String> featureFlags = (ArrayList<String>) optimizely.getEnabledFeatures(genericUserId,
3307-
new HashMap<String, String>());
3288+
List<String> featureFlags = optimizely.getEnabledFeatures(genericUserId, Collections.emptyMap());
33083289
assertFalse(featureFlags.isEmpty());
33093290

33103291
eventHandler.expectImpression("1786133852", "1619235542", genericUserId);
@@ -3340,8 +3321,7 @@ public void getEnabledFeatureWithNullUserID() throws Exception {
33403321
assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
33413322
String userID = null;
33423323
Optimizely optimizely = optimizelyBuilder.build();
3343-
ArrayList<String> featureFlags = (ArrayList<String>) optimizely.getEnabledFeatures(userID,
3344-
new HashMap<String, String>());
3324+
List<String> featureFlags = optimizely.getEnabledFeatures(userID, Collections.emptyMap());
33453325
assertTrue(featureFlags.isEmpty());
33463326

33473327
logbackVerifier.expectMessage(
@@ -3367,8 +3347,7 @@ public void getEnabledFeatureWithMockIsFeatureEnabledToReturnFalse() throws Exce
33673347
eq(genericUserId),
33683348
eq(Collections.<String, String>emptyMap())
33693349
);
3370-
ArrayList<String> featureFlags = (ArrayList<String>) spyOptimizely.getEnabledFeatures(genericUserId,
3371-
Collections.<String, String>emptyMap());
3350+
List<String> featureFlags = spyOptimizely.getEnabledFeatures(genericUserId, Collections.emptyMap());
33723351
assertTrue(featureFlags.isEmpty());
33733352
}
33743353

0 commit comments

Comments
 (0)