Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
64da61a
first version of environments for android
thomaszurkan-optimizely May 25, 2018
20e93d1
remove unused code
thomaszurkan-optimizely May 25, 2018
c869041
add comments and license.
thomaszurkan-optimizely May 25, 2018
467de7f
add unit tests for environments
thomaszurkan-optimizely May 25, 2018
ae85af6
cleanup from Mike's comments. Still need to think of a better name t…
thomaszurkan-optimizely May 25, 2018
64ce5be
slight refactor to make it simpler and use data file config
thomaszurkan-optimizely May 25, 2018
c633f75
add new datafile config
thomaszurkan-optimizely May 25, 2018
30f3b84
use environmentUrl
thomaszurkan-optimizely May 25, 2018
08abfc6
remove unused strings
thomaszurkan-optimizely May 25, 2018
d3d367f
update extra definition
thomaszurkan-optimizely Jun 4, 2018
a6b19b5
update to latest design using project id or sdk key. sdk key trumps …
thomaszurkan-optimizely Jun 8, 2018
eaac76c
update to reflect feedback and to cleanup code and comments
thomaszurkan-optimizely Jun 10, 2018
66ec0fa
fix copyright
thomaszurkan-optimizely Jun 10, 2018
d089b7b
remove getCacheKey and just use getKey for DatafileConfig
thomaszurkan-optimizely Jun 11, 2018
f2868f2
update to test datafile config with environments
thomaszurkan-optimizely Jun 11, 2018
d91b5d9
cleanup datafile config comments and add unit tests for initializing …
thomaszurkan-optimizely Jun 11, 2018
80aff7d
add new builder method that takes project id and sdk key. fix tests t…
thomaszurkan-optimizely Jun 12, 2018
4a0d5f0
have a builder with no arguments
thomaszurkan-optimizely Jun 12, 2018
2bbf30f
rename some local variables
thomaszurkan-optimizely Jun 12, 2018
93dffda
Merge branch 'master' into feature/environments
thomaszurkan-optimizely Jun 12, 2018
5c00b7c
fix compiler erors after merging master
thomaszurkan-optimizely Jun 12, 2018
1ee6584
fix local variable name
thomaszurkan-optimizely Jun 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
import com.optimizely.ab.android.sdk.test.R;
import com.optimizely.ab.android.shared.DatafileConfig;
import com.optimizely.ab.android.shared.ServiceScheduler;
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
import com.optimizely.ab.bucketing.UserProfileService;
Expand Down Expand Up @@ -71,6 +72,7 @@
public class OptimizelyManagerTest {

private String testProjectId = "7595190003";
private String testSdkKey = "123-2232323-343423423-435345345";
private ListeningExecutorService executor;
private Logger logger;
private OptimizelyManager optimizelyManager;
Expand All @@ -93,7 +95,7 @@ public void setup() {
executor = MoreExecutors.newDirectExecutorService();
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
optimizelyManager = new OptimizelyManager(testProjectId, logger, 3600L, datafileHandler, null, 3600L,
optimizelyManager = new OptimizelyManager(testProjectId, null,logger, 3600L, datafileHandler, null, 3600L,
eventHandler, null);
}

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

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

assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );

verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(InstrumentationRegistry.getTargetContext()), eq(testProjectId), eq(3600L));
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(eq(InstrumentationRegistry.getTargetContext()), eq(new DatafileConfig(testProjectId, null)), eq(3600L));
assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());

}
@Test
public void initializeSync() {
public void initializeSyncWithoutEnvironment() {
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
*/
optimizelyManager.initialize(InstrumentationRegistry.getTargetContext(), R.raw.datafile);

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

assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );

assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());

optimizelyManager.initialize(InstrumentationRegistry.getTargetContext(),(Integer) null);
verify(logger).error(eq("Invalid datafile resource ID."));
}
@Test
public void initializeSyncWithEnvironment() {
Logger logger = mock(Logger.class);
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey,logger, 3600L, datafileHandler, null, 3600L,
eventHandler, null);
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
Expand All @@ -131,7 +156,7 @@ public void initializeSync() {

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

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

assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());
Expand Down Expand Up @@ -169,12 +194,18 @@ public void getDatafile() {
*/
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
String datafile = optimizelyManager.getDatafile(InstrumentationRegistry.getTargetContext(), R.raw.datafile);
assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
assertEquals(optimizelyManager.getDatafileUrl(), String.format("https://cdn.optimizely.com/json/%s.json", testProjectId) );
assertNotNull(datafile);
assertNotNull(optimizelyManager.getDatafileHandler());
}
@Test
public void initializeAsync() {
public void initializeAsyncWithEnvironment() {
Logger logger = mock(Logger.class);
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey,logger, 3600L, datafileHandler, null, 3600L,
eventHandler, null);

/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
Expand All @@ -186,7 +217,7 @@ public Object answer(InvocationOnMock invocation) {
((DatafileLoadedListener) invocation.getArguments()[2]).onDatafileLoaded(null);
return null;
}
}).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(String.class),
}).when(optimizelyManager.getDatafileHandler()).downloadDatafile(any(Context.class), any(DatafileConfig.class),
any(DatafileLoadedListener.class));

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

verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(testProjectId), eq(3600L));
verify(optimizelyManager.getDatafileHandler()).startBackgroundUpdates(any(Context.class), eq(new DatafileConfig(testProjectId, testSdkKey)), eq(3600L));


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

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


}

@Test
public void initializeAsyncWithoutEnvironment() {
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
*/
optimizelyManager.initialize(InstrumentationRegistry.getContext(), R.raw.datafile, new OptimizelyStartListener() {
@Override
public void onStart(OptimizelyClient optimizely) {
assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());
assertNull(optimizelyManager.getOptimizelyStartListener());
}
});

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

assertEquals(optimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/json/1.json" );
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/7595190003.json" );


}

@Test
public void initializeWithEmptyDatafile() {
Context context = mock(Context.class);
Expand Down Expand Up @@ -266,7 +320,7 @@ public void stop() {
Context appContext = mock(Context.class);
when(context.getApplicationContext()).thenReturn(appContext);

optimizelyManager.getDatafileHandler().downloadDatafile(context, optimizelyManager.getProjectId(), null);
optimizelyManager.getDatafileHandler().downloadDatafile(context, optimizelyManager.getDatafileConfig(), null);

optimizelyManager.stop(context);

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

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

}

Expand All @@ -309,7 +363,7 @@ public void injectOptimizelyWithDatafileLisener() {
fail("Timed out");
}

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

Intent intent = new Intent(context, DatafileService.class);
intent.putExtra(DatafileService.EXTRA_PROJECT_ID, optimizelyManager.getProjectId());
intent.putExtra(DatafileService.EXTRA_DATAFILE_CONFIG, optimizelyManager.getDatafileConfig().toJSONString());
serviceScheduler.schedule(intent, optimizelyManager.getDatafileDownloadInterval() * 1000);

try {
Expand All @@ -356,7 +410,7 @@ public void injectOptimizelyNullListener() {

Intent intent2 = captor.getValue();
assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
assertEquals(optimizelyManager.getProjectId(), intent2.getStringExtra(DatafileService.EXTRA_PROJECT_ID));
assertEquals(optimizelyManager.getDatafileConfig().toJSONString(), intent2.getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
Expand Down
Loading