Search in sources :

Example 16 with DatafileConfig

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

the class DatafileWorker method doWork.

@NonNull
@Override
public Result doWork() {
    DatafileConfig datafileConfig = getDataConfig(getInputData());
    String datafileUrl = datafileConfig.getUrl();
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(this.getApplicationContext(), LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
    datafileLoader.getDatafile(datafileUrl, datafileCache, null);
    return Result.success();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Cache(com.optimizely.ab.android.shared.Cache) NonNull(androidx.annotation.NonNull)

Example 17 with DatafileConfig

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

the class DatafileClientTest method request300.

@Test
public void request300() throws IOException {
    URL url = new URL(new DatafileConfig("1", null).getUrl());
    when(client.openConnection(url)).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(300);
    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();
    assertNull(response);
    verify(logger).error("Unexpected response from data file cdn, status: {}", 300);
    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 18 with DatafileConfig

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

the class DatafileClientTest method handlesIOException.

@Test
public void handlesIOException() throws IOException {
    URL url = new URL(new DatafileConfig("1", null).getUrl());
    when(client.openConnection(url)).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(200);
    doThrow(new IOException()).when(urlConnection).connect();
    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();
    assertNull(response);
    verify(logger).error(contains("Error making request"), any(IOException.class));
    verify(urlConnection).disconnect();
    verify(urlConnection).disconnect();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) IOException(java.io.IOException) Client(com.optimizely.ab.android.shared.Client) URL(java.net.URL) Test(org.junit.Test)

Example 19 with DatafileConfig

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

the class DatafileClientTest method request201.

@Test
public void request201() throws IOException {
    URL url = new URL(new DatafileConfig("1", null).getUrl());
    when(client.openConnection(url)).thenReturn(urlConnection);
    when(urlConnection.getResponseCode()).thenReturn(201);
    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 20 with DatafileConfig

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

the class DatafileClientTest method handlesNullResponse.

@Test
public void handlesNullResponse() throws MalformedURLException {
    URL url = new URL(new DatafileConfig("1", null).getUrl());
    when(client.execute(any(Client.Request.class), eq(DatafileClient.REQUEST_BACKOFF_TIMEOUT), eq(DatafileClient.REQUEST_RETRIES_POWER))).thenReturn(null);
    assertNull(datafileClient.request(url.toString()));
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) URL(java.net.URL) Test(org.junit.Test)

Aggregations

DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)38 Test (org.junit.Test)34 Context (android.content.Context)15 URL (java.net.URL)8 Client (com.optimizely.ab.android.shared.Client)7 Cache (com.optimizely.ab.android.shared.Cache)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 DatafileLoadedListener (com.optimizely.ab.android.datafile_handler.DatafileLoadedListener)4 Intent (android.content.Intent)2 Data (androidx.work.Data)2 DefaultDatafileHandler (com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler)2 DefaultUserProfileService (com.optimizely.ab.android.user_profile.DefaultUserProfileService)2 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Logger (org.slf4j.Logger)2 AlarmManager (android.app.AlarmManager)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1