use of com.optimizely.ab.event.EventHandler in project java-sdk by optimizely.
the class OptimizelyBuilderTest method withClientInfo.
@Test
public void withClientInfo() throws Exception {
Optimizely optimizely;
EventHandler eventHandler;
ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);
// default client-engine info (java-sdk)
eventHandler = mock(EventHandler.class);
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler).build();
optimizely.track("basic_event", "tester");
verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "java-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildVersionInfo.getClientVersion());
// invalid override with null inputs
reset(eventHandler);
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler).withClientInfo(null, null).build();
optimizely.track("basic_event", "tester");
verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "java-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildVersionInfo.getClientVersion());
// override client-engine info
reset(eventHandler);
optimizely = Optimizely.builder(validConfigJsonV4(), eventHandler).withClientInfo(EventBatch.ClientEngine.ANDROID_SDK, "1.2.3").build();
optimizely.track("basic_event", "tester");
verify(eventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), "1.2.3");
}
use of com.optimizely.ab.event.EventHandler in project java-sdk by optimizely.
the class OptimizelyTest method testClose.
@Test
public void testClose() throws Exception {
EventHandler mockEventHandler = mock(EventHandler.class, withSettings().extraInterfaces(AutoCloseable.class));
ProjectConfigManager mockProjectConfigManager = mock(ProjectConfigManager.class, withSettings().extraInterfaces(AutoCloseable.class));
EventProcessor mockEventProcessor = mock(EventProcessor.class, withSettings().extraInterfaces(AutoCloseable.class));
Optimizely optimizely = Optimizely.builder().withEventHandler(mockEventHandler).withEventProcessor(mockEventProcessor).withConfigManager(mockProjectConfigManager).build();
optimizely.close();
verify((AutoCloseable) mockEventHandler).close();
verify((AutoCloseable) mockProjectConfigManager).close();
verify((AutoCloseable) mockEventProcessor).close();
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class APISamplesInJava method samplesForDoc_EventDispatcher.
public static void samplesForDoc_EventDispatcher(Context context) {
// -- sample starts here
// Using an anonymous class here to implement the EventHandler interface.
// Feel free to create an explicit class that implements the interface instead.
EventHandler eventHandler = new EventHandler() {
@Override
public void dispatchEvent(LogEvent logEvent) throws Exception {
// Send event to our log endpoint as documented in
// https://developers.optimizely.com/x/events/api/index.html
}
};
// Build a manager
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("<Your_SDK_Key>").withEventDispatchInterval(60, TimeUnit.SECONDS).withEventHandler(eventHandler).build(context);
// With the new Android O differences, you need to register the
// service for the intent filter you desire in code instead of in the manifest.
EventRescheduler eventRescheduler = new EventRescheduler();
context.registerReceiver(eventRescheduler, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION));
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class OptimizelyManagerEventHandlerTest method eventClientNameAndVersion.
@Test
public void eventClientNameAndVersion() throws Exception {
EventHandler mockEventHandler = mock(EventHandler.class);
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
OptimizelyManager optimizelyManager = OptimizelyManager.builder().withSDKKey("any-sdk-key").withEventDispatchInterval(0, TimeUnit.SECONDS).withEventHandler(mockEventHandler).build(context);
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, minDatafileWithEvent);
optimizelyClient.track("test_event", "tester");
ArgumentCaptor<LogEvent> argument = ArgumentCaptor.forClass(LogEvent.class);
verify(mockEventHandler, timeout(5000)).dispatchEvent(argument.capture());
assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk");
assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildConfig.CLIENT_VERSION);
}
use of com.optimizely.ab.event.EventHandler in project android-sdk by optimizely.
the class OptimizelyManagerTest method initializeSyncWithEnvironment.
@Test
public void initializeSyncWithEnvironment() {
Logger logger = mock(Logger.class);
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
EventHandler eventHandler = mock(DefaultEventHandler.class);
EventProcessor eventProcessor = mock(EventProcessor.class);
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L, eventHandler, eventProcessor, null, null, null);
/*
* Scenario#1: when datafile is not Empty
* Scenario#2: when datafile is Empty
*/
optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getTargetContext(), R.raw.datafile);
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getInstrumentation().getTargetContext()), false);
assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey));
assertNotNull(optimizelyManager.getOptimizely());
assertNotNull(optimizelyManager.getDatafileHandler());
optimizelyManager.initialize(InstrumentationRegistry.getInstrumentation().getTargetContext(), (Integer) null);
verify(logger).error(eq("Invalid datafile resource ID."));
}
Aggregations