Skip to content

Commit 684ab9c

Browse files
mnoman09aliabbasrizvi
authored andcommitted
Refac: Refactored Decision Listener SourceInfo as per discussion (#286)
1 parent 82691ed commit 684ab9c

File tree

6 files changed

+109
-31
lines changed

6 files changed

+109
-31
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
import com.optimizely.ab.event.internal.BuildVersionInfo;
3636
import com.optimizely.ab.event.internal.EventFactory;
3737
import com.optimizely.ab.event.internal.payload.EventBatch.ClientEngine;
38-
import com.optimizely.ab.notification.DecisionNotification;
39-
import com.optimizely.ab.notification.NotificationCenter;
38+
import com.optimizely.ab.notification.*;
4039
import org.slf4j.Logger;
4140
import org.slf4j.LoggerFactory;
4241

@@ -388,11 +387,10 @@ public Boolean isFeatureEnabled(@Nonnull String featureKey,
388387

389388
Map<String, ?> copiedAttributes = copyAttributes(attributes);
390389
FeatureDecision.DecisionSource decisionSource = FeatureDecision.DecisionSource.ROLLOUT;
391-
String sourceExperimentKey = null;
392-
String sourceVariationKey = null;
393390

394391
FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, copiedAttributes);
395392
Boolean featureEnabled = false;
393+
SourceInfo sourceInfo = new RolloutSourceInfo();
396394
if (featureDecision.variation != null) {
397395
if (featureDecision.decisionSource.equals(FeatureDecision.DecisionSource.FEATURE_TEST)) {
398396
sendImpression(
@@ -402,8 +400,7 @@ public Boolean isFeatureEnabled(@Nonnull String featureKey,
402400
copiedAttributes,
403401
featureDecision.variation);
404402
decisionSource = featureDecision.decisionSource;
405-
sourceVariationKey = featureDecision.variation.getKey();
406-
sourceExperimentKey = featureDecision.experiment.getKey();
403+
sourceInfo = new FeatureTestSourceInfo(featureDecision.experiment.getKey(), featureDecision.variation.getKey());
407404
} else {
408405
logger.info("The user \"{}\" is not included in an experiment for feature \"{}\".",
409406
userId, featureKey);
@@ -420,8 +417,7 @@ public Boolean isFeatureEnabled(@Nonnull String featureKey,
420417
.withFeatureKey(featureKey)
421418
.withFeatureEnabled(featureEnabled)
422419
.withSource(decisionSource)
423-
.withExperimentKey(sourceExperimentKey)
424-
.withVariationKey(sourceVariationKey)
420+
.withSourceInfo(sourceInfo)
425421
.build();
426422
notificationCenter.sendNotifications(decisionNotification);
427423

core-api/src/main/java/com/optimizely/ab/notification/DecisionNotification.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.HashMap;
2727
import java.util.Map;
2828

29+
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
30+
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
31+
2932
public class DecisionNotification {
3033
protected String type;
3134
protected String userId;
@@ -126,13 +129,10 @@ public static class FeatureDecisionNotificationBuilder {
126129
public final static String FEATURE_ENABLED = "featureEnabled";
127130
public final static String SOURCE = "source";
128131
public final static String SOURCE_INFO = "sourceInfo";
129-
public final static String EXPERIMENT_KEY = "experimentKey";
130-
public final static String VARIATION_KEY = "variationKey";
131132

132133
private String featureKey;
133134
private Boolean featureEnabled;
134-
private String experimentKey;
135-
private String variationKey;
135+
private SourceInfo sourceInfo;
136136
private FeatureDecision.DecisionSource source;
137137
private String userId;
138138
private Map<String, ?> attributes;
@@ -148,13 +148,8 @@ public FeatureDecisionNotificationBuilder withAttributes(Map<String, ?> attribut
148148
return this;
149149
}
150150

151-
public FeatureDecisionNotificationBuilder withExperimentKey(String experimentKey) {
152-
this.experimentKey = experimentKey;
153-
return this;
154-
}
155-
156-
public FeatureDecisionNotificationBuilder withVariationKey(String variationKey) {
157-
this.variationKey = variationKey;
151+
public FeatureDecisionNotificationBuilder withSourceInfo(SourceInfo sourceInfo) {
152+
this.sourceInfo = sourceInfo;
158153
return this;
159154
}
160155

@@ -179,12 +174,7 @@ public DecisionNotification build() {
179174
decisionInfo.put(FEATURE_ENABLED, featureEnabled);
180175
decisionInfo.put(SOURCE, source.toString());
181176

182-
Map<String, String> sourceInfo = new HashMap<>();
183-
if (source.equals(FeatureDecision.DecisionSource.FEATURE_TEST)) {
184-
sourceInfo.put(EXPERIMENT_KEY, experimentKey);
185-
sourceInfo.put(VARIATION_KEY, variationKey);
186-
}
187-
decisionInfo.put(SOURCE_INFO, sourceInfo);
177+
decisionInfo.put(SOURCE_INFO, sourceInfo.get());
188178

189179
return new DecisionNotification(
190180
NotificationCenter.DecisionNotificationType.FEATURE.toString(),
@@ -204,8 +194,6 @@ public static class FeatureVariableDecisionNotificationBuilder {
204194
public static final String FEATURE_ENABLED = "featureEnabled";
205195
public static final String SOURCE = "source";
206196
public static final String SOURCE_INFO = "sourceInfo";
207-
public static final String EXPERIMENT_KEY = "experimentKey";
208-
public static final String VARIATION_KEY = "variationKey";
209197
public static final String VARIABLE_KEY = "variableKey";
210198
public static final String VARIABLE_TYPE = "variableType";
211199
public static final String VARIABLE_VALUE = "variableValue";
@@ -270,15 +258,15 @@ public DecisionNotification build() {
270258
decisionInfo.put(VARIABLE_KEY, variableKey);
271259
decisionInfo.put(VARIABLE_TYPE, variableType);
272260
decisionInfo.put(VARIABLE_VALUE, variableValue);
273-
Map<String, String> sourceInfo = new HashMap<>();
261+
SourceInfo sourceInfo = new RolloutSourceInfo();
262+
274263
if (featureDecision != null && FeatureDecision.DecisionSource.FEATURE_TEST.equals(featureDecision.decisionSource)) {
275-
sourceInfo.put(EXPERIMENT_KEY, featureDecision.experiment.getKey());
276-
sourceInfo.put(VARIATION_KEY, featureDecision.variation.getKey());
264+
sourceInfo = new FeatureTestSourceInfo(featureDecision.experiment.getKey(), featureDecision.variation.getKey());
277265
decisionInfo.put(SOURCE, featureDecision.decisionSource.toString());
278266
} else {
279267
decisionInfo.put(SOURCE, FeatureDecision.DecisionSource.ROLLOUT.toString());
280268
}
281-
decisionInfo.put(SOURCE_INFO, sourceInfo);
269+
decisionInfo.put(SOURCE_INFO, sourceInfo.get());
282270

283271
return new DecisionNotification(
284272
NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE.toString(),
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/****************************************************************************
2+
* Copyright 2019, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.notification;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
23+
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
24+
25+
public class FeatureTestSourceInfo implements SourceInfo {
26+
private String experimentKey;
27+
private String variationKey;
28+
29+
public FeatureTestSourceInfo(String experimentKey, String variationKey) {
30+
this.experimentKey = experimentKey;
31+
this.variationKey = variationKey;
32+
}
33+
34+
@Override
35+
public Map<String, String> get() {
36+
Map<String, String> sourceInfo = new HashMap<>();
37+
sourceInfo.put(EXPERIMENT_KEY, experimentKey);
38+
sourceInfo.put(VARIATION_KEY, variationKey);
39+
40+
return sourceInfo;
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/****************************************************************************
2+
* Copyright 2019, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.notification;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
public class RolloutSourceInfo implements SourceInfo {
23+
@Override
24+
public Map<String, String> get() {
25+
return Collections.EMPTY_MAP;
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/****************************************************************************
2+
* Copyright 2019, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package com.optimizely.ab.notification;
18+
19+
import java.util.Map;
20+
21+
public interface SourceInfo {
22+
Map<String, String> get();
23+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
import static com.optimizely.ab.config.ProjectConfigTestUtils.*;
6767
import static com.optimizely.ab.config.ValidProjectConfigV4.*;
6868
import static com.optimizely.ab.event.LogEvent.RequestMethod;
69+
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
70+
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
6971
import static com.optimizely.ab.notification.DecisionNotification.FeatureVariableDecisionNotificationBuilder.*;
7072
import static java.util.Arrays.asList;
7173
import static junit.framework.TestCase.assertTrue;

0 commit comments

Comments
 (0)