Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix config parameter #556

Merged
merged 10 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apply plugin: 'signing'
apply plugin: 'kotlin-android'

ext {
splitVersion = '3.4.0'
splitVersion = '3.5.0-alpha-1'
}

android {
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ossrhUsername=
ossrhPassword=
android.enableJetifier=true
android.useAndroidX=true

kotlin.stdlib.default.dependency=false
10 changes: 9 additions & 1 deletion src/androidTest/java/helper/TestableSplitConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class TestableSplitConfigBuilder {
private long mDefaultSSEConnectionDelayInSecs = ServiceConstants.DEFAULT_SSE_CONNECTION_DELAY_SECS;
private long mSSEDisconnectionDelayInSecs = 60L;

private String mPrefix = "";

public TestableSplitConfigBuilder() {
mServiceEndpoints = ServiceEndpoints.builder().build();
}
Expand Down Expand Up @@ -249,6 +251,11 @@ public TestableSplitConfigBuilder sseDisconnectionDelayInSecs(long seconds) {
return this;
}

public TestableSplitConfigBuilder prefix(String prefix) {
this.mPrefix = prefix;
return this;
}

public SplitClientConfig build() {
Constructor constructor = SplitClientConfig.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
Expand Down Expand Up @@ -300,7 +307,8 @@ public SplitClientConfig build() {
mUserConsent,
mEncryptionEnabled,
mDefaultSSEConnectionDelayInSecs,
mSSEDisconnectionDelayInSecs);
mSSEDisconnectionDelayInSecs,
mPrefix);
return config;
} catch (Exception e) {
Logger.e("Error creating Testable Split client builder: "
Expand Down
186 changes: 186 additions & 0 deletions src/androidTest/java/tests/database/DatabaseInitializationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package tests.database;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.concurrent.LinkedBlockingDeque;

import fake.HttpClientMock;
import fake.HttpResponseMock;
import fake.HttpResponseMockDispatcher;
import fake.HttpStreamResponseMock;
import helper.IntegrationHelper;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SplitFactory;
import io.split.android.client.api.Key;
import io.split.android.client.network.HttpMethod;

public class DatabaseInitializationTest {

private HttpClientMock mHttpClientMock;
private Context mContext;

@Before
public void setUp() throws IOException {
mContext = InstrumentationRegistry.getInstrumentation().getContext();
mHttpClientMock = new HttpClientMock(new HttpResponseMockDispatcher() {
@Override
public HttpResponseMock getResponse(URI uri, HttpMethod method, String body) {
return new HttpResponseMock(400, "");
}

@Override
public HttpStreamResponseMock getStreamResponse(URI uri) {
try {
return new HttpStreamResponseMock(400, new LinkedBlockingDeque<>(0));
} catch (IOException e) {
return null;
}
}
});
}

@Test
public void initializationWithNullApiKeyResultsInNullFactory() throws IOException {
SplitFactory factory = IntegrationHelper.buildFactory(null,
new Key("matchingKey"),
IntegrationHelper.basicConfig(),
mContext,
mHttpClientMock);

assertNull(factory);
}

@Test
public void initializationWithoutPrefixCreatesCorrectDatabaseName() {
String[] initialDatabaseList = getDbList(mContext);
SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
IntegrationHelper.basicConfig(),
mContext,
mHttpClientMock);

String[] finalDatabaseList = getDbList(mContext);

assertNotNull(factory);
assertEquals(0, initialDatabaseList.length);
assertEquals("abcdijkl", finalDatabaseList[0]);
}

@Test
public void initializationWithPrefixCreatesCorrectDatabaseName() {
String[] initialDatabaseList = getDbList(mContext);
SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
SplitClientConfig.builder().prefix("my_prefix").build(),
mContext,
mHttpClientMock);

String[] finalDatabaseList = getDbList(mContext);

assertNotNull(factory);
assertEquals(0, initialDatabaseList.length);
assertEquals("my_prefixabcdijkl", finalDatabaseList[0]);
}

@Test
public void factoriesWithSameSdkKeyCreateOnlyOneDatabase() {
String[] initialDatabaseList = getDbList(mContext);
SplitFactory factory1 = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
IntegrationHelper.basicConfig(),
mContext,
mHttpClientMock);

SplitFactory factory2 = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey2"),
IntegrationHelper.basicConfig(),
mContext,
mHttpClientMock);

String[] finalDatabaseList = getDbList(mContext);

assertNotNull(factory1);
assertNotNull(factory2);
assertEquals(0, initialDatabaseList.length);
assertEquals(1, finalDatabaseList.length);
assertEquals("abcdijkl", finalDatabaseList[0]);
}

@Test
public void oneFactoryWithPrefixCreatesNewDatabase() {
String[] initialDatabaseList = getDbList(mContext);
SplitFactory factory1 = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
SplitClientConfig.builder().build(),
mContext,
mHttpClientMock);

SplitFactory factory2 = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
SplitClientConfig.builder().prefix("my_prefix").build(),
mContext,
mHttpClientMock);

String[] finalDatabaseList = getDbList(mContext);

assertNotNull(factory1);
assertNotNull(factory2);
assertEquals(0, initialDatabaseList.length);
assertEquals(2, finalDatabaseList.length);
assertEquals("abcdijkl", finalDatabaseList[0]);
assertEquals("my_prefixabcdijkl", finalDatabaseList[1]);
}

@Test
public void usingInvalidPrefixResultsInIgnoredPrefix() {
String[] initialDatabaseList = getDbList(mContext);

SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
SplitClientConfig.builder().prefix(":l").build(),
mContext,
mHttpClientMock);

String[] finalDatabaseList = getDbList(mContext);

assertNotNull(factory);
assertEquals(0, initialDatabaseList.length);
assertEquals(1, finalDatabaseList.length);
assertEquals("abcdijkl", finalDatabaseList[0]);
}

@Test
public void usingNullPrefixResultsInIgnoredPrefix() {
String[] initialDatabaseList = getDbList(mContext);

SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl",
new Key("matchingKey"),
SplitClientConfig.builder().prefix(null).build(),
mContext,
mHttpClientMock);

String[] finalDatabaseList = getDbList(mContext);

assertNotNull(factory);
assertEquals(0, initialDatabaseList.length);
assertEquals(1, finalDatabaseList.length);
assertEquals("abcdijkl", finalDatabaseList[0]);
}

private static String[] getDbList(Context context) {
// remove -journal dbs since we're not interested in them
return Arrays.stream(context.databaseList()).filter(db -> !db.endsWith("-journal")).toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -39,7 +38,7 @@

public class FlagSetsEvaluationTest {

private final FileHelper fileHelper = new FileHelper();
private final FileHelper mFileHelper = new FileHelper();
private final Context mContext = InstrumentationRegistry.getInstrumentation().getContext();

@Test
Expand Down Expand Up @@ -163,7 +162,7 @@ private SplitClient getClient(
}

private String loadSplitChangeWithSet(int setsCount) {
String change = fileHelper.loadFileContent(mContext, "split_changes_flag_set-" + setsCount + ".json");
String change = mFileHelper.loadFileContent(mContext, "split_changes_flag_set-" + setsCount + ".json");
SplitChange parsedChange = Json.fromJson(change, SplitChange.class);
parsedChange.since = parsedChange.till;

Expand Down
Loading