Search in sources :

Example 6 with EventHandler

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");
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) EventHandler(com.optimizely.ab.event.EventHandler) Test(org.junit.Test)

Example 7 with EventHandler

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();
}
Also used : BatchEventProcessor(com.optimizely.ab.event.BatchEventProcessor) EventProcessor(com.optimizely.ab.event.EventProcessor) EventHandler(com.optimizely.ab.event.EventHandler) Test(org.junit.Test)

Example 8 with EventHandler

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));
}
Also used : IntentFilter(android.content.IntentFilter) LogEvent(com.optimizely.ab.event.LogEvent) EventRescheduler(com.optimizely.ab.android.event_handler.EventRescheduler) EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) OptimizelyManager(com.optimizely.ab.android.sdk.OptimizelyManager)

Example 9 with EventHandler

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);
}
Also used : Context(android.content.Context) LogEvent(com.optimizely.ab.event.LogEvent) EventHandler(com.optimizely.ab.event.EventHandler) Test(org.junit.Test)

Example 10 with EventHandler

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."));
}
Also used : EventProcessor(com.optimizely.ab.event.EventProcessor) EventHandler(com.optimizely.ab.event.EventHandler) DefaultEventHandler(com.optimizely.ab.android.event_handler.DefaultEventHandler) Logger(org.slf4j.Logger) DatafileHandler(com.optimizely.ab.android.datafile_handler.DatafileHandler) DefaultDatafileHandler(com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler) Test(org.junit.Test)

Aggregations

EventHandler (com.optimizely.ab.event.EventHandler)10 DefaultEventHandler (com.optimizely.ab.android.event_handler.DefaultEventHandler)6 Test (org.junit.Test)6 DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)4 EventProcessor (com.optimizely.ab.event.EventProcessor)4 LogEvent (com.optimizely.ab.event.LogEvent)3 Logger (org.slf4j.Logger)3 Context (android.content.Context)2 DatafileHandler (com.optimizely.ab.android.datafile_handler.DatafileHandler)2 OptimizelyManager (com.optimizely.ab.android.sdk.OptimizelyManager)2 BatchEventProcessor (com.optimizely.ab.event.BatchEventProcessor)2 IntentFilter (android.content.IntentFilter)1 Optimizely (com.optimizely.ab.Optimizely)1 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)1 EventRescheduler (com.optimizely.ab.android.event_handler.EventRescheduler)1 OptimizelyClient (com.optimizely.ab.android.sdk.OptimizelyClient)1 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)1 DatafileProjectConfig (com.optimizely.ab.config.DatafileProjectConfig)1 ProjectConfig (com.optimizely.ab.config.ProjectConfig)1 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)1