Search in sources :

Example 1 with Client

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

the class EventClientTest method sendEvents201.

@Test
public void sendEvents201() throws IOException {
    when(client.openConnection(event.getURL())).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(201);
    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)

Example 2 with Client

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

the class DatafileClientTest method request200.

@Test
public void request200() throws IOException {
    URL url = new URL(new DatafileConfig("1", null).getUrl());
    when(client.openConnection(url)).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(200);
    when(client.readStream(urlConnection)).thenReturn("{}");
    datafileClient.request(url.toString());
    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(DatafileClient.REQUEST_BACKOFF_TIMEOUT), captor2.getValue());
    assertEquals(Integer.valueOf(DatafileClient.REQUEST_RETRIES_POWER), captor3.getValue());
    Object response = captor1.getValue().execute();
    assertTrue(String.class.isInstance(response));
    assertEquals("{}", response);
    verify(logger).info("Requesting data file from {}", url);
    verify(client).saveLastModified(urlConnection);
    verify(client).readStream(urlConnection);
    verify(urlConnection).disconnect();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Client(com.optimizely.ab.android.shared.Client) URL(java.net.URL) Test(org.junit.Test)

Example 3 with Client

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

the class DatafileClientTest method request299.

@Test
public void request299() throws IOException {
    URL url = new URL(new DatafileConfig("1", null).getUrl());
    when(client.openConnection(url)).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(299);
    when(client.readStream(urlConnection)).thenReturn("{}");
    datafileClient.request(url.toString());
    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(DatafileClient.REQUEST_BACKOFF_TIMEOUT), captor2.getValue());
    assertEquals(Integer.valueOf(DatafileClient.REQUEST_RETRIES_POWER), captor3.getValue());
    Object response = captor1.getValue().execute();
    assertTrue(String.class.isInstance(response));
    assertEquals("{}", response);
    verify(logger).info("Requesting data file from {}", url);
    verify(client).saveLastModified(urlConnection);
    verify(client).readStream(urlConnection);
    verify(urlConnection).disconnect();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Client(com.optimizely.ab.android.shared.Client) URL(java.net.URL) Test(org.junit.Test)

Example 4 with Client

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

the class DatafileService method onStartCommand.

/**
 * @hide
 * @see Service#onStartCommand(Intent, int, int)
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        if (intent.hasExtra(EXTRA_DATAFILE_CONFIG)) {
            String extraDatafileConfig = intent.getStringExtra(EXTRA_DATAFILE_CONFIG);
            DatafileConfig datafileConfig = DatafileConfig.fromJSONString(extraDatafileConfig);
            DatafileClient datafileClient = new DatafileClient(new Client(new OptlyStorage(this.getApplicationContext()), LoggerFactory.getLogger(OptlyStorage.class)), LoggerFactory.getLogger(DatafileClient.class));
            DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(this.getApplicationContext(), LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
            String datafileUrl = datafileConfig.getUrl();
            DatafileLoader datafileLoader = new DatafileLoader(this, datafileClient, datafileCache, LoggerFactory.getLogger(DatafileLoader.class));
            datafileLoader.getDatafile(datafileUrl, null);
        } else {
            logger.warn("Data file service received an intent with no project id extra");
        }
    } else {
        logger.warn("Data file service received a null intent");
    }
    return super.onStartCommand(intent, flags, startId);
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache)

Example 5 with Client

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

the class DatafileServiceConnection method onServiceConnected.

/**
 * Get the bound {@link DatafileService} and set it up for download.
 *
 * @see ServiceConnection#onServiceConnected(ComponentName, IBinder)
 */
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
    if (!(service instanceof DatafileService.LocalBinder)) {
        return;
    }
    // We've bound to DatafileService, cast the IBinder and get DatafileService instance
    DatafileService.LocalBinder binder = (DatafileService.LocalBinder) service;
    final DatafileService datafileService = binder.getService();
    if (datafileService != null) {
        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.getApplicationContext(), LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
        DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, LoggerFactory.getLogger(DatafileLoader.class));
        datafileService.getDatafile(datafileConfig.getUrl(), datafileLoader, listener);
    }
    bound = true;
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache)

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