Skip to content

Commit 3a9d8b4

Browse files
Feature/environments (#201)
* first version of environments for android * remove unused code * add comments and license. * add unit tests for environments * cleanup from Mike's comments. Still need to think of a better name than ProjectId * slight refactor to make it simpler and use data file config * add new datafile config * use environmentUrl * remove unused strings * update extra definition * update to latest design using project id or sdk key. sdk key trumps project id if both are provided. * update to reflect feedback and to cleanup code and comments * fix copyright * remove getCacheKey and just use getKey for DatafileConfig * update to test datafile config with environments * cleanup datafile config comments and add unit tests for initializing the manager with environment instead of project id * add new builder method that takes project id and sdk key. fix tests to use sdk key * have a builder with no arguments * rename some local variables * fix compiler erors after merging master * fix local variable name
1 parent e4c140a commit 3a9d8b4

File tree

24 files changed

+562
-203
lines changed

24 files changed

+562
-203
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerTest.java

+69-15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
3535
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
3636
import com.optimizely.ab.android.sdk.test.R;
37+
import com.optimizely.ab.android.shared.DatafileConfig;
3738
import com.optimizely.ab.android.shared.ServiceScheduler;
3839
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
3940
import com.optimizely.ab.bucketing.UserProfileService;
@@ -71,6 +72,7 @@
7172
public class OptimizelyManagerTest {
7273

7374
private String testProjectId = "7595190003";
75+
private String testSdkKey = "123-2232323-343423423-435345345";
7476
private ListeningExecutorService executor;
7577
private Logger logger;
7678
private OptimizelyManager optimizelyManager;
@@ -93,7 +95,7 @@ public void setup() {
9395
executor = MoreExecutors.newDirectExecutorService();
9496
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
9597
EventHandler eventHandler = mock(DefaultEventHandler.class);
96-
optimizelyManager = new OptimizelyManager(testProjectId, logger, 3600L, datafileHandler, null, 3600L,
98+
optimizelyManager = new OptimizelyManager(testProjectId, null,logger, 3600L, datafileHandler, null, 3600L,
9799
eventHandler, null);
98100
}
99101

@@ -114,15 +116,38 @@ public void initializeInt() {
114116

115117
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
116118

117-
assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
119+
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );
118120

119-
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(InstrumentationRegistry.getTargetContext()), eq(testProjectId), eq(3600L));
121+
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(InstrumentationRegistry.getTargetContext()), eq(new DatafileConfig(testProjectId, null)), eq(3600L));
120122
assertNotNull(optimizelyManager.getOptimizely());
121123
assertNotNull(optimizelyManager.getDatafileHandler());
122124

123125
}
124126
@Test
125-
public void initializeSync() {
127+
public void initializeSyncWithoutEnvironment() {
128+
/*
129+
* Scenario#1: when datafile is not Empty
130+
* Scenario#2: when datafile is Empty
131+
*/
132+
optimizelyManager.initialize(InstrumentationRegistry.getTargetContext(), R.raw.datafile);
133+
134+
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
135+
136+
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );
137+
138+
assertNotNull(optimizelyManager.getOptimizely());
139+
assertNotNull(optimizelyManager.getDatafileHandler());
140+
141+
optimizelyManager.initialize(InstrumentationRegistry.getTargetContext(),(Integer) null);
142+
verify(logger).error(eq("Invalid datafile resource ID."));
143+
}
144+
@Test
145+
public void initializeSyncWithEnvironment() {
146+
Logger logger = mock(Logger.class);
147+
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
148+
EventHandler eventHandler = mock(DefaultEventHandler.class);
149+
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey,logger, 3600L, datafileHandler, null, 3600L,
150+
eventHandler, null);
126151
/*
127152
* Scenario#1: when datafile is not Empty
128153
* Scenario#2: when datafile is Empty
@@ -131,7 +156,7 @@ public void initializeSync() {
131156

132157
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
133158

134-
assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
159+
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/123-2232323-343423423-435345345.json" );
135160

136161
assertNotNull(optimizelyManager.getOptimizely());
137162
assertNotNull(optimizelyManager.getDatafileHandler());
@@ -169,12 +194,18 @@ public void getDatafile() {
169194
*/
170195
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
171196
String datafile = optimizelyManager.getDatafile(InstrumentationRegistry.getTargetContext(), R.raw.datafile);
172-
assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
197+
assertEquals(optimizelyManager.getDatafileUrl(), String.format("https://cdn.optimizely.com/json/%s.json", testProjectId) );
173198
assertNotNull(datafile);
174199
assertNotNull(optimizelyManager.getDatafileHandler());
175200
}
176201
@Test
177-
public void initializeAsync() {
202+
public void initializeAsyncWithEnvironment() {
203+
Logger logger = mock(Logger.class);
204+
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
205+
EventHandler eventHandler = mock(DefaultEventHandler.class);
206+
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey,logger, 3600L, datafileHandler, null, 3600L,
207+
eventHandler, null);
208+
178209
/*
179210
* Scenario#1: when datafile is not Empty
180211
* Scenario#2: when datafile is Empty
@@ -186,7 +217,7 @@ public Object answer(InvocationOnMock invocation) {
186217
((DatafileLoadedListener) invocation.getArguments()[2]).onDatafileLoaded(null);
187218
return null;
188219
}
189-
}).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(String.class),
220+
}).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class),
190221
any(DatafileLoadedListener.class));
191222

192223
OptimizelyStartListener listener = new OptimizelyStartListener() {
@@ -199,15 +230,38 @@ public void onStart(OptimizelyClient optimizely) {
199230
};
200231
optimizelyManager.initialize(InstrumentationRegistry.getContext(), R.raw.datafile, listener);
201232

202-
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(testProjectId), eq(3600L));
233+
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(new DatafileConfig(testProjectId, testSdkKey)), eq(3600L));
234+
235+
236+
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
237+
238+
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/123-2232323-343423423-435345345.json" );
239+
203240

241+
}
242+
243+
@Test
244+
public void initializeAsyncWithoutEnvironment() {
245+
/*
246+
* Scenario#1: when datafile is not Empty
247+
* Scenario#2: when datafile is Empty
248+
*/
249+
optimizelyManager.initialize(InstrumentationRegistry.getContext(), R.raw.datafile, new OptimizelyStartListener() {
250+
@Override
251+
public void onStart(OptimizelyClient optimizely) {
252+
assertNotNull(optimizelyManager.getOptimizely());
253+
assertNotNull(optimizelyManager.getDatafileHandler());
254+
assertNull(optimizelyManager.getOptimizelyStartListener());
255+
}
256+
});
204257

205258
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
206259

207-
assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
260+
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );
208261

209262

210263
}
264+
211265
@Test
212266
public void initializeWithEmptyDatafile() {
213267
Context context = mock(Context.class);
@@ -266,7 +320,7 @@ public void stop() {
266320
Context appContext = mock(Context.class);
267321
when(context.getApplicationContext()).thenReturn(appContext);
268322

269-
optimizelyManager.getDatafileHandler().downloadDatafile(context, optimizelyManager.getProjectId(), null);
323+
optimizelyManager.getDatafileHandler().downloadDatafile(context, optimizelyManager.getDatafileConfig(), null);
270324

271325
optimizelyManager.stop(context);
272326

@@ -290,7 +344,7 @@ public void injectOptimizely() {
290344

291345
verify(logger).info("Sending Optimizely instance to listener");
292346
verify(startListener).onStart(any(OptimizelyClient.class));
293-
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(context), eq(testProjectId), eq(3600L));
347+
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(context), eq(new DatafileConfig(testProjectId, null)), eq(3600L));
294348

295349
}
296350

@@ -309,7 +363,7 @@ public void injectOptimizelyWithDatafileLisener() {
309363
fail("Timed out");
310364
}
311365

312-
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(context), eq(testProjectId), eq(3600L));
366+
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(context), eq(new DatafileConfig(testProjectId, null)), eq(3600L));
313367
verify(logger).info("Sending Optimizely instance to listener");
314368
verify(startListener).onStart(any(OptimizelyClient.class));
315369
}
@@ -342,7 +396,7 @@ public void injectOptimizelyNullListener() {
342396
.PendingIntentFactory(context);
343397

344398
Intent intent = new Intent(context, DatafileService.class);
345-
intent.putExtra(DatafileService.EXTRA_PROJECT_ID, optimizelyManager.getProjectId());
399+
intent.putExtra(DatafileService.EXTRA_DATAFILE_CONFIG, optimizelyManager.getDatafileConfig().toJSONString());
346400
serviceScheduler.schedule(intent, optimizelyManager.getDatafileDownloadInterval() * 1000);
347401

348402
try {
@@ -356,7 +410,7 @@ public void injectOptimizelyNullListener() {
356410

357411
Intent intent2 = captor.getValue();
358412
assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
359-
assertEquals(optimizelyManager.getProjectId(), intent2.getStringExtra(DatafileService.EXTRA_PROJECT_ID));
413+
assertEquals(optimizelyManager.getDatafileConfig().toJSONString(), intent2.getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG));
360414
}
361415

362416
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)

0 commit comments

Comments
 (0)