Skip to content

Commit 387f74a

Browse files
authored
Merge pull request #52 from optimizely/mng/datafile-versioning
Add versioned datafile path and public API to retrieve it.
2 parents be4b2b0 + 8f8afcc commit 387f74a

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/DataFileClientTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void setup() {
6262

6363
@Test
6464
public void request200() throws IOException {
65-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
65+
URL url = new URL(DataFileService.getDatafileUrl("1"));
6666
when(client.openConnection(url)).thenReturn(urlConnection);
6767
when(urlConnection.getResponseCode()).thenReturn(200);
6868
when(client.readStream(urlConnection)).thenReturn("{}");
@@ -87,7 +87,7 @@ public void request200() throws IOException {
8787

8888
@Test
8989
public void request201() throws IOException {
90-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
90+
URL url = new URL(DataFileService.getDatafileUrl("1"));
9191
when(client.openConnection(url)).thenReturn(urlConnection);
9292
when(urlConnection.getResponseCode()).thenReturn(201);
9393
when(client.readStream(urlConnection)).thenReturn("{}");
@@ -112,7 +112,7 @@ public void request201() throws IOException {
112112

113113
@Test
114114
public void request299() throws IOException {
115-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
115+
URL url = new URL(DataFileService.getDatafileUrl("1"));
116116
when(client.openConnection(url)).thenReturn(urlConnection);
117117
when(urlConnection.getResponseCode()).thenReturn(299);
118118
when(client.readStream(urlConnection)).thenReturn("{}");
@@ -137,7 +137,7 @@ public void request299() throws IOException {
137137

138138
@Test
139139
public void request300() throws IOException {
140-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
140+
URL url = new URL(DataFileService.getDatafileUrl("1"));
141141
when(client.openConnection(url)).thenReturn(urlConnection);
142142
when(urlConnection.getResponseCode()).thenReturn(300);
143143

@@ -157,7 +157,7 @@ public void request300() throws IOException {
157157

158158
@Test
159159
public void handlesIOException() throws IOException {
160-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
160+
URL url = new URL(DataFileService.getDatafileUrl("1"));
161161
when(client.openConnection(url)).thenReturn(urlConnection);
162162
when(urlConnection.getResponseCode()).thenReturn(200);
163163
doThrow(new IOException()).when(client).readStream(urlConnection);
@@ -179,14 +179,14 @@ public void handlesIOException() throws IOException {
179179

180180
@Test
181181
public void handlesNullResponse() throws MalformedURLException {
182-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
182+
URL url = new URL(DataFileService.getDatafileUrl("1"));
183183
when(client.execute(any(Client.Request.class), eq(2), eq(3))).thenReturn(null);
184184
assertNull(dataFileClient.request(url.toString()));
185185
}
186186

187187
@Test
188188
public void handlesEmptyStringResponse() throws MalformedURLException {
189-
URL url = new URL(String.format(DataFileLoader.FORMAT_CDN_URL, "1"));
189+
URL url = new URL(DataFileService.getDatafileUrl("1"));
190190
when(client.execute(any(Client.Request.class), eq(2), eq(3))).thenReturn("");
191191
assertEquals("", dataFileClient.request(url.toString()));
192192
}

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/DatafileServiceTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.util.concurrent.TimeoutException;
3636

37+
import static junit.framework.Assert.assertEquals;
3738
import static junit.framework.Assert.assertTrue;
3839
import static org.mockito.Mockito.mock;
3940
import static org.mockito.Mockito.verify;
@@ -112,4 +113,14 @@ public void testNoProjectIdIntentStart() throws TimeoutException {
112113
dataFileService.onStartCommand(intent, 0, 0);
113114
verify(logger).warn("Data file service received an intent with no project id extra");
114115
}
116+
117+
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
118+
@Test
119+
public void testGetDatafileUrl(){
120+
// HARD-CODING link here to make sure we don't unintentionally mess up the datafile version
121+
// and url by accidentally changing those constants. Bumping datafile versions will force
122+
// us to update this test.
123+
String datafileUrl = DataFileService.getDatafileUrl("1");
124+
assertEquals("https://cdn.optimizely.com/public/1/datafile_v3.json", datafileUrl);
125+
}
115126
}

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
@RunWith(AndroidJUnit4.class)
5252
public class OptimizelyManagerTest {
5353

54+
private String testProjectId = "7595190003";
5455
private ListeningExecutorService executor;
5556
private Logger logger;
5657
private OptimizelyManager optimizelyManager;
@@ -61,7 +62,7 @@ public class OptimizelyManagerTest {
6162
"audiences: [ ],\n" +
6263
"groups: [ ],\n" +
6364
"attributes: [ ],\n" +
64-
"projectId: \"7595190003\",\n" +
65+
"projectId: \"" + testProjectId + "\",\n" +
6566
"accountId: \"6365361536\",\n" +
6667
"events: [ ],\n" +
6768
"revision: \"1\"\n" +
@@ -71,7 +72,7 @@ public class OptimizelyManagerTest {
7172
public void setup() {
7273
logger = mock(Logger.class);
7374
executor = MoreExecutors.newDirectExecutorService();
74-
optimizelyManager = new OptimizelyManager("7595190003", 1L, TimeUnit.HOURS, 1L, TimeUnit.HOURS, executor, logger);
75+
optimizelyManager = new OptimizelyManager(testProjectId, 1L, TimeUnit.HOURS, 1L, TimeUnit.HOURS, executor, logger);
7576
}
7677

7778

android-sdk/src/main/java/com/optimizely/ab/android/sdk/DataFileLoader.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class DataFileLoader {
3838
@NonNull private final Logger logger;
3939
@NonNull private final DataFileCache dataFileCache;
4040
@NonNull private final DataFileClient dataFileClient;
41-
static final String FORMAT_CDN_URL = "https://cdn.optimizely.com/json/%s.json";
4241

4342
private boolean hasNotifiedListener = false;
4443

@@ -55,9 +54,9 @@ class DataFileLoader {
5554
}
5655

5756
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
58-
void getDataFile(@NonNull String projectId, @Nullable DataFileLoadedListener dataFileLoadedListener) {
57+
void getDataFile(@NonNull String datafileUrl, @Nullable DataFileLoadedListener dataFileLoadedListener) {
5958
RequestDataFileFromClientTask requestDataFileFromClientTask =
60-
new RequestDataFileFromClientTask(projectId,
59+
new RequestDataFileFromClientTask(datafileUrl,
6160
dataFileService,
6261
dataFileCache,
6362
dataFileClient,
@@ -113,22 +112,22 @@ protected void onPostExecute(JSONObject dataFile) {
113112

114113
private static class RequestDataFileFromClientTask extends AsyncTask<Void, Void, String> {
115114

116-
@NonNull private final String projectId;
115+
@NonNull private final String datafileUrl;
117116
@NonNull private final DataFileService dataFileService;
118117
@NonNull private final DataFileCache dataFileCache;
119118
@NonNull private final DataFileClient dataFileClient;
120119
@NonNull private final DataFileLoader dataFileLoader;
121120
@NonNull private final Logger logger;
122121
@Nullable private final DataFileLoadedListener dataFileLoadedListener;
123122

124-
RequestDataFileFromClientTask(@NonNull String projectId,
123+
RequestDataFileFromClientTask(@NonNull String datafileUrl,
125124
@NonNull DataFileService dataFileService,
126125
@NonNull DataFileCache dataFileCache,
127126
@NonNull DataFileClient dataFileClient,
128127
@NonNull DataFileLoader dataFileLoader,
129128
@Nullable DataFileLoadedListener dataFileLoadedListener,
130129
@NonNull Logger logger) {
131-
this.projectId = projectId;
130+
this.datafileUrl = datafileUrl;
132131
this.dataFileService = dataFileService;
133132
this.dataFileCache = dataFileCache;
134133
this.dataFileClient = dataFileClient;
@@ -139,7 +138,7 @@ private static class RequestDataFileFromClientTask extends AsyncTask<Void, Void,
139138

140139
@Override
141140
protected String doInBackground(Void... params) {
142-
String dataFile = dataFileClient.request(String.format(FORMAT_CDN_URL, projectId));
141+
String dataFile = dataFileClient.request(datafileUrl);
143142
if (dataFile != null && !dataFile.isEmpty()) {
144143
if (dataFileCache.exists()) {
145144
if (!dataFileCache.delete()) {

android-sdk/src/main/java/com/optimizely/ab/android/sdk/DataFileService.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.os.Build;
2323
import android.os.IBinder;
2424
import android.support.annotation.NonNull;
25+
import android.support.annotation.Nullable;
2526
import android.support.annotation.RequiresApi;
2627

2728
import com.optimizely.ab.android.shared.Cache;
@@ -43,6 +44,9 @@ public class DataFileService extends Service {
4344
* Extra containing the project id this instance of Optimizely was built with
4445
*/
4546
public static final String EXTRA_PROJECT_ID = "com.optimizely.ab.android.EXTRA_PROJECT_ID";
47+
public static final String FORMAT_VERSIONED_CDN_URL = "https://cdn.optimizely.com/public/%s/datafile_v%s.json";
48+
static final String DATAFILE_VERSION = "3";
49+
4650
@NonNull private final IBinder binder = new LocalBinder();
4751
Logger logger = LoggerFactory.getLogger(getClass());
4852
private boolean isBound;
@@ -64,8 +68,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
6468
projectId,
6569
new Cache(getApplicationContext(), LoggerFactory.getLogger(Cache.class)),
6670
LoggerFactory.getLogger(DataFileCache.class));
71+
72+
String datafileUrl = getDatafileUrl(projectId);
6773
DataFileLoader dataFileLoader = new DataFileLoader(this, dataFileClient, dataFileCache, Executors.newSingleThreadExecutor(), LoggerFactory.getLogger(DataFileLoader.class));
68-
dataFileLoader.getDataFile(projectId, null);
74+
dataFileLoader.getDataFile(datafileUrl, null);
6975
BackgroundWatchersCache backgroundWatchersCache = new BackgroundWatchersCache(
7076
new Cache(this, LoggerFactory.getLogger(Cache.class)),
7177
LoggerFactory.getLogger(BackgroundWatchersCache.class));
@@ -106,13 +112,18 @@ public boolean isBound() {
106112
return isBound;
107113
}
108114

115+
public static @NonNull String getDatafileUrl(@NonNull String projectId) {
116+
return String.format(FORMAT_VERSIONED_CDN_URL, projectId, DATAFILE_VERSION);
117+
}
118+
109119
void stop() {
110120
stopSelf();
111121
}
112122

113123
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
114124
void getDataFile(String projectId, DataFileLoader dataFileLoader, DataFileLoadedListener loadedListener) {
115-
dataFileLoader.getDataFile(projectId, loadedListener);
125+
String datafileUrl = getDatafileUrl(projectId);
126+
dataFileLoader.getDataFile(datafileUrl, loadedListener);
116127
}
117128

118129
class LocalBinder extends Binder {

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ public boolean isDatafileCached(Context context) {
320320
return dataFileCache.exists();
321321
}
322322

323+
/**
324+
* Returns the URL of the versioned datafile that this SDK expects to use
325+
* @return the CDN location of the datafile
326+
*/
327+
public @NonNull String getDatafileUrl() {
328+
return DataFileService.getDatafileUrl(projectId);
329+
}
330+
323331
@NonNull
324332
String getProjectId() {
325333
return projectId;

0 commit comments

Comments
 (0)