Search in sources :

Example 11 with Client

use of com.optimizely.ab.android.shared.Client in project android-sdk by optimizely.

the class DatafileLoaderTest method setup.

@Before
public void setup() {
    logger = mock(Logger.class);
    final Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    datafileCache = new DatafileCache("1", new Cache(targetContext, logger), logger);
    client = mock(Client.class);
    datafileClient = new DatafileClient(client, logger);
    datafileLoadedListener = mock(DatafileLoadedListener.class);
}
Also used : Context(android.content.Context) Logger(org.slf4j.Logger) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache) Before(org.junit.Before)

Example 12 with Client

use of com.optimizely.ab.android.shared.Client in project android-sdk by optimizely.

the class DatafileServiceTest method testBinding.

@Test
@Ignore
public void testBinding() throws TimeoutException {
    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    Intent intent = new Intent(context, DatafileService.class);
    IBinder binder = null;
    int it = 0;
    while ((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION) {
        it++;
    }
    final Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    Logger logger = mock(Logger.class);
    DatafileCache datafileCache = new DatafileCache("1", new Cache(targetContext, logger), logger);
    Client client = mock(Client.class);
    DatafileClient datafileClient = new DatafileClient(client, logger);
    DatafileLoadedListener datafileLoadedListener = mock(DatafileLoadedListener.class);
    DatafileService datafileService = ((DatafileService.LocalBinder) binder).getService();
    DatafileLoader datafileLoader = new DatafileLoader(targetContext, datafileClient, datafileCache, mock(Logger.class));
    datafileService.getDatafile("1", datafileLoader, datafileLoadedListener);
    assertTrue(datafileService.isBound());
}
Also used : Context(android.content.Context) Intent(android.content.Intent) Logger(org.slf4j.Logger) IBinder(android.os.IBinder) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with Client

use of com.optimizely.ab.android.shared.Client in project android-sdk by optimizely.

the class EventIntentService method onCreate.

/**
 * Create the event dispatcher {@link EventDispatcher}
 * @see IntentService#onCreate()
 */
@Override
public void onCreate() {
    super.onCreate();
    OptlyStorage optlyStorage = new OptlyStorage(this);
    EventClient eventClient = new EventClient(new Client(optlyStorage, LoggerFactory.getLogger(Client.class)), LoggerFactory.getLogger(EventClient.class));
    EventDAO eventDAO = EventDAO.getInstance(this, "1", LoggerFactory.getLogger(EventDAO.class));
    ServiceScheduler serviceScheduler = new ServiceScheduler(this, new ServiceScheduler.PendingIntentFactory(this), LoggerFactory.getLogger(ServiceScheduler.class));
    eventDispatcher = new EventDispatcher(this, optlyStorage, eventDAO, eventClient, serviceScheduler, LoggerFactory.getLogger(EventDispatcher.class));
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) Client(com.optimizely.ab.android.shared.Client) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler)

Example 14 with Client

use of com.optimizely.ab.android.shared.Client in project android-sdk by optimizely.

the class DefaultDatafileHandler method downloadDatafile.

/**
 * Asynchronous download data file.
 * <p>
 * We create a DatafileService intent, create a DataService connection, and bind it to the application context.
 * After we receive the datafile, we unbind the service and cleanup the service connection.
 * This gets the project file from the Optimizely CDN.
 *
 * @param context   application context
 * @param datafileConfig DatafileConfig for the datafile to get
 * @param listener  listener to call when datafile download complete
 */
public void downloadDatafile(final Context context, DatafileConfig datafileConfig, final DatafileLoadedListener listener) {
    DatafileClient datafileClient = new DatafileClient(new Client(new OptlyStorage(context.getApplicationContext()), LoggerFactory.getLogger(OptlyStorage.class)), LoggerFactory.getLogger(DatafileClient.class));
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(context, LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
    String datafileUrl = datafileConfig.getUrl();
    DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, LoggerFactory.getLogger(DatafileLoader.class));
    datafileLoader.getDatafile(datafileUrl, new DatafileLoadedListener() {

        @Override
        public void onDatafileLoaded(@Nullable String dataFile) {
            if (listener != null) {
                listener.onDatafileLoaded(dataFile);
            }
        }
    });
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache)

Example 15 with Client

use of com.optimizely.ab.android.shared.Client in project android-sdk by optimizely.

the class EventClientTest method sendEvents200.

@Test
public void sendEvents200() throws IOException {
    when(client.openConnection(event.getURL())).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(200);
    InputStream inputStream = mock(InputStream.class);
    when(urlConnection.getInputStream()).thenReturn(inputStream);
    eventClient.sendEvent(event);
    ArgumentCaptor<Client.Request> captor1 = ArgumentCaptor.forClass(Client.Request.class);
    ArgumentCaptor<Integer> captor2 = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<Integer> captor3 = ArgumentCaptor.forClass(Integer.class);
    verify(client).execute(captor1.capture(), captor2.capture(), captor3.capture());
    assertEquals(Integer.valueOf(2), captor2.getValue());
    assertEquals(Integer.valueOf(5), captor3.getValue());
    Object response = captor1.getValue().execute();
    assertEquals(Boolean.TRUE, response);
    verify(logger).info("Dispatching event: {}", event);
}
Also used : InputStream(java.io.InputStream) Client(com.optimizely.ab.android.shared.Client) Test(org.junit.Test)

Aggregations

Client (com.optimizely.ab.android.shared.Client)16 Test (org.junit.Test)10 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)7 URL (java.net.URL)6 Cache (com.optimizely.ab.android.shared.Cache)5 OptlyStorage (com.optimizely.ab.android.shared.OptlyStorage)5 InputStream (java.io.InputStream)3 Context (android.content.Context)2 Logger (org.slf4j.Logger)2 Intent (android.content.Intent)1 IBinder (android.os.IBinder)1 ServiceScheduler (com.optimizely.ab.android.shared.ServiceScheduler)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1