Skip to content

Commit 217c35b

Browse files
authored
Merge pull request #19 from optimizely/android-optimizely-unit-tests
Adds unit tests for AndroidOptimizely proxy
2 parents 45449a3 + 2d68617 commit 217c35b

File tree

3 files changed

+235
-23
lines changed

3 files changed

+235
-23
lines changed

android-sdk/src/main/java/com/optimizely/ab/android/sdk/AndroidOptimizely.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
*/
4343
public class AndroidOptimizely {
4444

45-
Logger logger = LoggerFactory.getLogger(AndroidOptimizely.class);
45+
private final Logger logger;
4646

4747
@Nullable private Optimizely optimizely;
4848

49-
AndroidOptimizely(@Nullable Optimizely optimizely) {
49+
AndroidOptimizely(@Nullable Optimizely optimizely, @NonNull Logger logger) {
5050
this.optimizely = optimizely;
51+
this.logger = logger;
5152
}
5253

5354
/**
@@ -58,7 +59,7 @@ public class AndroidOptimizely {
5859
if (optimizely != null) {
5960
return optimizely.activate(experimentKey, userId);
6061
} else {
61-
logger.warn("Optimizely is not initialized, can't activate experiment {} for {}",
62+
logger.warn("Optimizely is not initialized, can't activate experiment {} for user {}",
6263
experimentKey, userId);
6364
return null;
6465
}
@@ -99,7 +100,7 @@ public class AndroidOptimizely {
99100
if (optimizely != null) {
100101
return optimizely.activate(experiment, userId, attributes);
101102
} else {
102-
logger.warn("Optimizely is not initialized, can't activate experiment {} for user {}, " +
103+
logger.warn("Optimizely is not initialized, can't activate experiment {} for user {} " +
103104
"with attributes", experiment.getKey(), userId);
104105
return null;
105106
}
@@ -182,7 +183,7 @@ public void track(@NonNull String eventName,
182183
public @Nullable Variation getVariation(@NonNull String experimentKey,
183184
@NonNull String userId) throws UnknownExperimentException{
184185
if (optimizely != null) {
185-
return getVariation(experimentKey, userId);
186+
return optimizely.getVariation(experimentKey, userId);
186187
} else {
187188
logger.warn("Optimizely is not initialized, could not get variation for experiment {} " +
188189
"for user {}", experimentKey, userId);
@@ -204,20 +205,4 @@ public void track(@NonNull String eventName,
204205
return null;
205206
}
206207
}
207-
208-
/**
209-
* @see Optimizely#getVariation(ProjectConfig, Experiment, Map, String)
210-
*/
211-
public @Nullable Variation getVariation(@NonNull ProjectConfig projectConfig,
212-
@NonNull Experiment experiment,
213-
@NonNull Map<String, String> attributes,
214-
@NonNull String userId) {
215-
if (optimizely != null) {
216-
return optimizely.getVariation(projectConfig, experiment, attributes, userId);
217-
} else {
218-
logger.warn("Optimizely is not initialized, could not get variation for experiment {} " +
219-
"for user {} with attributes and project config", experiment.getKey(), userId);
220-
return null;
221-
}
222-
}
223208
}

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
* Handles loading the Optimizely data file
5252
*/
5353
public class OptimizelyManager {
54-
@NonNull private static AndroidOptimizely androidOptimizely = new AndroidOptimizely(null);
54+
@NonNull private static AndroidOptimizely androidOptimizely = new AndroidOptimizely(null,
55+
LoggerFactory.getLogger(AndroidOptimizely.class));
5556
@NonNull private final String projectId;
5657
@NonNull private final Long eventHandlerDispatchInterval;
5758
@NonNull private final TimeUnit eventHandlerDispatchIntervalTimeUnit;
@@ -211,7 +212,7 @@ private AndroidOptimizely buildOptimizely(@NonNull Context context, @NonNull Str
211212
.withClientEngine(Event.ClientEngine.ANDROID_SDK)
212213
.withClientVersion(BuildConfig.CLIENT_VERSION)
213214
.build();
214-
return new AndroidOptimizely(optimizely);
215+
return new AndroidOptimizely(optimizely, LoggerFactory.getLogger(AndroidOptimizely.class));
215216
}
216217

217218
static class OptlyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright 2016, Optimizely
3+
* <p/>
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+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
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.android.sdk;
18+
19+
import com.optimizely.ab.Optimizely;
20+
import com.optimizely.ab.config.Experiment;
21+
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
26+
import org.slf4j.Logger;
27+
28+
import java.util.HashMap;
29+
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.verify;
32+
import static org.mockito.Mockito.when;
33+
34+
/**
35+
* Tests for {@link AndroidOptimizely}
36+
*/
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class AndroidOptimizelyTest {
39+
40+
@Mock Logger logger;
41+
@Mock Optimizely optimizely;
42+
43+
@Test
44+
public void testGoodActivation1() {
45+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
46+
androidOptimizely.activate("1", "1");
47+
verify(optimizely).activate("1", "1");
48+
}
49+
50+
@Test
51+
public void testBadActivation1() {
52+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
53+
androidOptimizely.activate("1", "1");
54+
verify(logger).warn("Optimizely is not initialized, can't activate experiment {} " +
55+
"for user {}", "1", "1");
56+
}
57+
58+
@Test
59+
public void testGoodActivation2() {
60+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
61+
final HashMap<String, String> attributes = new HashMap<>();
62+
androidOptimizely.activate("1", "1", attributes);
63+
verify(optimizely).activate("1", "1", attributes);
64+
}
65+
66+
@Test
67+
public void testBadActivation2() {
68+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
69+
androidOptimizely.activate("1", "1", new HashMap<String, String>());
70+
verify(logger).warn("Optimizely is not initialized, can't activate experiment {} " +
71+
"for user {} with attributes", "1", "1");
72+
}
73+
74+
@Test
75+
public void testGoodActivation3() {
76+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
77+
final Experiment experiment = mock(Experiment.class);
78+
when(experiment.getKey()).thenReturn("1");
79+
androidOptimizely.activate(experiment, "1");
80+
verify(optimizely).activate(experiment, "1");
81+
}
82+
83+
@Test
84+
public void testBadActivation3() {
85+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
86+
final Experiment experiment = mock(Experiment.class);
87+
when(experiment.getKey()).thenReturn("1");
88+
androidOptimizely.activate(experiment, "1");
89+
verify(logger).warn("Optimizely is not initialized, can't activate experiment {} " +
90+
"for user {}", "1", "1");
91+
}
92+
93+
@Test
94+
public void testGoodActivation4() {
95+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
96+
final Experiment experiment = mock(Experiment.class);
97+
when(experiment.getKey()).thenReturn("1");
98+
final HashMap<String, String> attributes = new HashMap<>();
99+
androidOptimizely.activate(experiment, "1", attributes);
100+
verify(optimizely).activate(experiment, "1", attributes);
101+
}
102+
103+
@Test
104+
public void testBadActivation4() {
105+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
106+
final Experiment experiment = mock(Experiment.class);
107+
when(experiment.getKey()).thenReturn("1");
108+
androidOptimizely.activate(experiment, "1", new HashMap<String, String>());
109+
verify(logger).warn("Optimizely is not initialized, can't activate experiment {} " +
110+
"for user {} with attributes", "1", "1");
111+
}
112+
113+
@Test
114+
public void testGoodTrack1() {
115+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
116+
androidOptimizely.track("event1", "1");
117+
verify(optimizely).track("event1", "1");
118+
}
119+
120+
@Test
121+
public void testBadTrack1() {
122+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
123+
androidOptimizely.track("event1", "1");
124+
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {}", "event1", "1");
125+
}
126+
127+
@Test
128+
public void testGoodTrack2() {
129+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
130+
final HashMap<String, String> attributes = new HashMap<>();
131+
androidOptimizely.track("event1", "1", attributes);
132+
verify(optimizely).track("event1", "1", attributes);
133+
}
134+
135+
@Test
136+
public void testBadTrack2() {
137+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
138+
final HashMap<String, String> attributes = new HashMap<>();
139+
androidOptimizely.track("event1", "1", attributes);
140+
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {} " +
141+
"with attributes", "event1", "1");
142+
}
143+
144+
@Test
145+
public void testGoodTrack3() {
146+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
147+
androidOptimizely.track("event1", "1", 1L);
148+
verify(optimizely).track("event1", "1", 1L);
149+
}
150+
151+
@Test
152+
public void testBadTrack3() {
153+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
154+
androidOptimizely.track("event1", "1", 1L);
155+
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {} " +
156+
"with value {}", "event1", "1", 1L);
157+
}
158+
159+
@Test
160+
public void testGoodTrack4() {
161+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
162+
final HashMap<String, String> attributes = new HashMap<>();
163+
androidOptimizely.track("event1", "1", attributes, 1L);
164+
verify(optimizely).track("event1", "1", attributes, 1L);
165+
}
166+
167+
@Test
168+
public void testBadTrack4() {
169+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
170+
final HashMap<String, String> attributes = new HashMap<>();
171+
androidOptimizely.track("event1", "1", attributes, 1L);
172+
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {} " +
173+
"with value {} and attributes", "event1", "1", 1L);
174+
}
175+
176+
@Test
177+
public void testGoodGetVariation1() {
178+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
179+
androidOptimizely.getVariation("1", "1");
180+
verify(optimizely).getVariation("1", "1");
181+
}
182+
183+
@Test
184+
public void testBadGetVariation1() {
185+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
186+
androidOptimizely.getVariation("1", "1");
187+
verify(logger).warn("Optimizely is not initialized, could not get variation for experiment {} " +
188+
"for user {}", "1", "1");
189+
}
190+
191+
@Test
192+
public void testGoodGetVariation2() {
193+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
194+
Experiment experiment = mock(Experiment.class);
195+
when(experiment.getKey()).thenReturn("1");
196+
androidOptimizely.getVariation(experiment, "1");
197+
verify(optimizely).getVariation(experiment, "1");
198+
}
199+
200+
@Test
201+
public void testBadGetVariation2() {
202+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
203+
Experiment experiment = mock(Experiment.class);
204+
when(experiment.getKey()).thenReturn("1");
205+
androidOptimizely.getVariation(experiment, "1");
206+
verify(logger).warn("Optimizely is not initialized, could not get variation for experiment {} " +
207+
"for user {}", experiment.getKey(), "1");
208+
}
209+
210+
@Test
211+
public void testGoodGetVariation3() {
212+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
213+
final HashMap<String, String> attributes = new HashMap<>();
214+
androidOptimizely.getVariation("1", "1", attributes);
215+
verify(optimizely).getVariation("1", "1", attributes);
216+
}
217+
218+
@Test
219+
public void testBadGetVariation3() {
220+
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
221+
final HashMap<String, String> attributes = new HashMap<>();
222+
androidOptimizely.getVariation("1", "1", attributes);
223+
verify(logger).warn("Optimizely is not initialized, could not get variation for experiment {} " +
224+
"for user {} with attributes", "1", "1");
225+
}
226+
}

0 commit comments

Comments
 (0)