Skip to content

Commit af7f3a4

Browse files
committed
Adding missing unit tests
1 parent 684ab9c commit af7f3a4

File tree

5 files changed

+265
-3
lines changed

5 files changed

+265
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ <T extends Object> T getFeatureVariableValueForType(@Nonnull String featureKey,
683683

684684
Object convertedValue = convertStringToType(variableValue, variableType);
685685

686-
DecisionNotification decisionNotification = DecisionNotification.newFeatureVariableBuilder()
686+
DecisionNotification decisionNotification = DecisionNotification.newFeatureVariableDecisionNotificationBuilder()
687687
.withUserId(userId)
688688
.withAttributes(copiedAttributes)
689689
.withFeatureKey(featureKey)

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.optimizely.ab.notification;
1818

1919

20+
import com.optimizely.ab.OptimizelyRuntimeException;
2021
import com.optimizely.ab.bucketing.FeatureDecision;
22+
import com.optimizely.ab.config.Experiment;
2123
import com.optimizely.ab.config.FeatureVariable;
2224
import com.optimizely.ab.config.Variation;
2325

@@ -29,6 +31,20 @@
2931
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY;
3032
import static com.optimizely.ab.notification.DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY;
3133

34+
/**
35+
* DecisionNotification encapsulates the arguments and responses when using the following methods:
36+
*
37+
* activate {@link com.optimizely.ab.Optimizely#activate}
38+
* getEnabledFeatures {@link com.optimizely.ab.Optimizely#getEnabledFeatures}
39+
* getFeatureVariableBoolean {@link com.optimizely.ab.Optimizely#getFeatureVariableBoolean}
40+
* getFeatureVariableDouble {@link com.optimizely.ab.Optimizely#getFeatureVariableDouble}
41+
* getFeatureVariableInteger {@link com.optimizely.ab.Optimizely#getFeatureVariableInteger}
42+
* getFeatureVariableString {@link com.optimizely.ab.Optimizely#getFeatureVariableString}
43+
* getVariation {@link com.optimizely.ab.Optimizely#getVariation}
44+
* isFeatureEnabled {@link com.optimizely.ab.Optimizely#isFeatureEnabled}
45+
*
46+
* @see <a href="https://docs.developers.optimizely.com/full-stack/docs/register-notification-listeners">Notification Listeners</a>
47+
*/
3248
public class DecisionNotification {
3349
protected String type;
3450
protected String userId;
@@ -108,6 +124,14 @@ public ExperimentDecisionNotificationBuilder withVariation(Variation variation)
108124
}
109125

110126
public DecisionNotification build() {
127+
if (type == null) {
128+
throw new OptimizelyRuntimeException("type not set");
129+
}
130+
131+
if (experimentKey == null) {
132+
throw new OptimizelyRuntimeException("experimentKey not set");
133+
}
134+
111135
decisionInfo = new HashMap<>();
112136
decisionInfo.put(EXPERIMENT_KEY, experimentKey);
113137
decisionInfo.put(VARIATION_KEY, variation != null ? variation.getKey() : null);
@@ -169,11 +193,22 @@ public FeatureDecisionNotificationBuilder withFeatureEnabled(Boolean featureEnab
169193
}
170194

171195
public DecisionNotification build() {
196+
if (source == null) {
197+
throw new OptimizelyRuntimeException("source not set");
198+
}
199+
200+
if (featureKey == null) {
201+
throw new OptimizelyRuntimeException("featureKey not set");
202+
}
203+
204+
if (featureEnabled == null) {
205+
throw new OptimizelyRuntimeException("featureEnabled not set");
206+
}
207+
172208
decisionInfo = new HashMap<>();
173209
decisionInfo.put(FEATURE_KEY, featureKey);
174210
decisionInfo.put(FEATURE_ENABLED, featureEnabled);
175211
decisionInfo.put(SOURCE, source.toString());
176-
177212
decisionInfo.put(SOURCE_INFO, sourceInfo.get());
178213

179214
return new DecisionNotification(
@@ -184,7 +219,7 @@ public DecisionNotification build() {
184219
}
185220
}
186221

187-
public static FeatureVariableDecisionNotificationBuilder newFeatureVariableBuilder() {
222+
public static FeatureVariableDecisionNotificationBuilder newFeatureVariableDecisionNotificationBuilder() {
188223
return new FeatureVariableDecisionNotificationBuilder();
189224
}
190225

@@ -252,6 +287,22 @@ public FeatureVariableDecisionNotificationBuilder withVariableValue(Object varia
252287
}
253288

254289
public DecisionNotification build() {
290+
if (featureKey == null) {
291+
throw new OptimizelyRuntimeException("featureKey not set");
292+
}
293+
294+
if (featureEnabled == null) {
295+
throw new OptimizelyRuntimeException("featureEnabled not set");
296+
}
297+
298+
if (variableKey == null) {
299+
throw new OptimizelyRuntimeException("variableKey not set");
300+
}
301+
302+
if (variableType == null) {
303+
throw new OptimizelyRuntimeException("variableType not set");
304+
}
305+
255306
decisionInfo = new HashMap<>();
256307
decisionInfo.put(FEATURE_KEY, featureKey);
257308
decisionInfo.put(FEATURE_ENABLED, featureEnabled);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
*
3+
* Copyright 2019, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.optimizely.ab.notification;
19+
20+
import com.optimizely.ab.bucketing.FeatureDecision;
21+
import com.optimizely.ab.config.FeatureVariable;
22+
import com.optimizely.ab.config.Variation;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertFalse;
32+
import static org.junit.Assert.assertTrue;
33+
import static org.mockito.Mockito.mock;
34+
35+
public class DecisionNotificationTest {
36+
37+
private static final Boolean FEATURE_ENABLED = Boolean.FALSE;
38+
private static final String EXPERIMENT_KEY = "experimentKey";
39+
private static final String FEATURE_KEY = "featureKey";
40+
private static final String FEATURE_VARIABLE_KEY = "featureVariableKey";
41+
private static final String USER_ID = "userID";
42+
private static final Map<String, String> USER_ATTRIBUTES = Collections.singletonMap("user", "attr");
43+
private static final RolloutSourceInfo rolloutSourceInfo = mock(RolloutSourceInfo.class);
44+
private static final Variation VARIATION = mock(Variation.class);
45+
46+
private DecisionNotification experimentDecisionNotification;
47+
private DecisionNotification featureDecisionNotification;
48+
private DecisionNotification featureVariableDecisionNotification;
49+
50+
@Before
51+
public void setUp() {
52+
experimentDecisionNotification = DecisionNotification.newExperimentDecisionNotificationBuilder()
53+
.withUserId(USER_ID)
54+
.withAttributes(USER_ATTRIBUTES)
55+
.withExperimentKey(EXPERIMENT_KEY)
56+
.withVariation(VARIATION)
57+
.withType(NotificationCenter.DecisionNotificationType.AB_TEST.toString())
58+
.build();
59+
featureDecisionNotification = DecisionNotification.newFeatureDecisionNotificationBuilder()
60+
.withUserId(USER_ID)
61+
.withFeatureKey(FEATURE_KEY)
62+
.withFeatureEnabled(FEATURE_ENABLED)
63+
.withSource(FeatureDecision.DecisionSource.ROLLOUT)
64+
.withAttributes(USER_ATTRIBUTES)
65+
.withSourceInfo(rolloutSourceInfo)
66+
.build();
67+
featureVariableDecisionNotification = DecisionNotification.newFeatureVariableDecisionNotificationBuilder()
68+
.withUserId(USER_ID)
69+
.withFeatureKey(FEATURE_KEY)
70+
.withFeatureEnabled(Boolean.TRUE)
71+
.withVariableKey(FEATURE_VARIABLE_KEY)
72+
.withVariableType(FeatureVariable.VariableType.STRING)
73+
.withAttributes(USER_ATTRIBUTES)
74+
.build();
75+
}
76+
77+
@Test
78+
public void testGetType() {
79+
assertEquals(NotificationCenter.DecisionNotificationType.AB_TEST.toString(), experimentDecisionNotification.getType());
80+
assertEquals(NotificationCenter.DecisionNotificationType.FEATURE.toString(), featureDecisionNotification.getType());
81+
assertEquals(NotificationCenter.DecisionNotificationType.FEATURE_VARIABLE.toString(), featureVariableDecisionNotification.getType());
82+
}
83+
84+
@Test
85+
public void testGetUserId() {
86+
assertEquals(USER_ID, experimentDecisionNotification.getUserId());
87+
assertEquals(USER_ID, featureDecisionNotification.getUserId());
88+
assertEquals(USER_ID, featureVariableDecisionNotification.getUserId());
89+
}
90+
91+
@Test
92+
public void testGetAttributes() {
93+
assertEquals(USER_ATTRIBUTES, experimentDecisionNotification.getAttributes());
94+
assertEquals(USER_ATTRIBUTES, featureDecisionNotification.getAttributes());
95+
assertEquals(USER_ATTRIBUTES, featureVariableDecisionNotification.getAttributes());
96+
}
97+
98+
@Test
99+
public void testGetDecisionInfo() {
100+
// Assert for Experiment's DecisionInfo
101+
HashMap<String, String> expectedExperimentDecisionInfo = new HashMap<>();
102+
expectedExperimentDecisionInfo.put(DecisionNotification.ExperimentDecisionNotificationBuilder.EXPERIMENT_KEY, EXPERIMENT_KEY);
103+
expectedExperimentDecisionInfo.put(DecisionNotification.ExperimentDecisionNotificationBuilder.VARIATION_KEY, VARIATION.getKey());
104+
assertEquals(expectedExperimentDecisionInfo, experimentDecisionNotification.getDecisionInfo());
105+
106+
// Assert for Feature's DecisionInfo
107+
Map<String, ?> actualFeatureDecisionInfo = featureDecisionNotification.getDecisionInfo();
108+
assertFalse((Boolean) actualFeatureDecisionInfo.get(DecisionNotification.FeatureDecisionNotificationBuilder.FEATURE_ENABLED));
109+
assertEquals(FEATURE_KEY, actualFeatureDecisionInfo.get(DecisionNotification.FeatureDecisionNotificationBuilder.FEATURE_KEY));
110+
assertEquals(FeatureDecision.DecisionSource.ROLLOUT.toString(), actualFeatureDecisionInfo.get(DecisionNotification.FeatureDecisionNotificationBuilder.SOURCE));
111+
assertEquals(rolloutSourceInfo.get(), actualFeatureDecisionInfo.get(DecisionNotification.FeatureDecisionNotificationBuilder.SOURCE_INFO));
112+
113+
// Assert for Feature Variable's DecisionInfo
114+
Map<String, ?> actualFeatureVariableDecisionInfo = featureVariableDecisionNotification.getDecisionInfo();
115+
assertTrue((Boolean) actualFeatureVariableDecisionInfo.get(DecisionNotification.FeatureVariableDecisionNotificationBuilder.FEATURE_ENABLED));
116+
assertEquals(FEATURE_KEY, actualFeatureVariableDecisionInfo.get(DecisionNotification.FeatureVariableDecisionNotificationBuilder.FEATURE_KEY));
117+
assertEquals(FEATURE_VARIABLE_KEY, actualFeatureVariableDecisionInfo.get(DecisionNotification.FeatureVariableDecisionNotificationBuilder.VARIABLE_KEY));
118+
assertEquals(FeatureVariable.VariableType.STRING, actualFeatureVariableDecisionInfo.get(DecisionNotification.FeatureVariableDecisionNotificationBuilder.VARIABLE_TYPE));
119+
assertEquals(FeatureDecision.DecisionSource.ROLLOUT.toString(), actualFeatureVariableDecisionInfo.get(DecisionNotification.FeatureVariableDecisionNotificationBuilder.SOURCE));
120+
assertEquals(rolloutSourceInfo.get(), actualFeatureVariableDecisionInfo.get(DecisionNotification.FeatureVariableDecisionNotificationBuilder.SOURCE_INFO));
121+
}
122+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
*
3+
* Copyright 2019, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.optimizely.ab.notification;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import java.util.HashMap;
24+
25+
import static org.junit.Assert.assertEquals;
26+
27+
public class FeatureTestSourceInfoTest {
28+
29+
private static final String EXPERIMENT_KEY = "featureTestKey";
30+
private static final String VARIATION_KEY = "featureTestVariationKey";
31+
32+
private FeatureTestSourceInfo featureSourceInfo;
33+
34+
@Before
35+
public void setUp() {
36+
featureSourceInfo = new FeatureTestSourceInfo(EXPERIMENT_KEY, VARIATION_KEY);
37+
}
38+
39+
@Test
40+
public void testGet() {
41+
HashMap<String, String> expectedSourceInfo = new HashMap<>();
42+
expectedSourceInfo.put("experimentKey", EXPERIMENT_KEY);
43+
expectedSourceInfo.put("variationKey", VARIATION_KEY);
44+
45+
assertEquals(expectedSourceInfo, featureSourceInfo.get());
46+
}
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
*
3+
* Copyright 2019, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.optimizely.ab.notification;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import java.util.Collections;
24+
import java.util.Map;
25+
26+
import static org.junit.Assert.assertEquals;
27+
28+
public class RolloutSourceInfoTest {
29+
30+
private RolloutSourceInfo rolloutSourceInfo;
31+
32+
@Before
33+
public void setUp() {
34+
rolloutSourceInfo = new RolloutSourceInfo();
35+
}
36+
37+
@Test
38+
public void testGet() {
39+
Map<String, String> expectedInfo = Collections.EMPTY_MAP;
40+
assertEquals(expectedInfo, rolloutSourceInfo.get());
41+
}
42+
}

0 commit comments

Comments
 (0)