Skip to content

Commit

Permalink
General info storage
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Nov 26, 2024
1 parent f39d3fc commit ddf6879
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply plugin: 'kotlin-android'
apply from: 'spec.gradle'

ext {
splitVersion = '5.0.1'
splitVersion = '5.1.0-alpha.1'
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public void firstRequestChangeNumber() throws Exception {
String apiKey = apiKeyAndDb.first;
SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, INITIAL_CHANGE_NUMBER));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, System.currentTimeMillis()));

Expand Down
3 changes: 0 additions & 3 deletions src/androidTest/java/tests/integration/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void testAll() throws Exception {

SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 2));
SplitClient client;
SplitManager manager;
Expand Down Expand Up @@ -249,7 +248,6 @@ public void testNoReadyFromCache() throws Exception {

SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, -1));
SplitClient client;
SplitManager manager;
Expand Down Expand Up @@ -304,7 +302,6 @@ public void testGetTreatmentFromCache() throws Exception {
mContext = InstrumentationRegistry.getInstrumentation().getContext();
mRoomDb = DatabaseHelper.getTestDatabase(mContext);
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 10));
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, 1));

SplitChange change = Json.fromJson(mJsonChanges.get(0), SplitChange.class);
List<SplitEntity> entities = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public void test() throws Exception {

SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, -1));

ImpressionListenerHelper impListener = new ImpressionListenerHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void testAll() throws Exception {
SplitRoomDatabase.class)
.build();
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 2));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_FILTER_QUERY_STRING, expectedQs));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, System.currentTimeMillis()));
Expand Down
3 changes: 0 additions & 3 deletions src/androidTest/java/tests/integration/TrackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.split.android.client.dtos.Event;
import io.split.android.client.events.SplitEvent;
import io.split.android.client.exceptions.SplitInstantiationException;
import io.split.android.client.storage.db.GeneralInfoEntity;
import io.split.android.client.storage.db.SplitRoomDatabase;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -120,7 +119,6 @@ public void test() throws Exception {
String apiKey = IntegrationHelper.dummyApiKey();
SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));

ImpressionListenerHelper impListener = new ImpressionListenerHelper();

Expand Down Expand Up @@ -220,7 +218,6 @@ public void largeNumberInPropertiesTest() throws InterruptedException, SplitInst
String apiKey = IntegrationHelper.dummyApiKey();
SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));

final String url = mWebServer.url("/").url().toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ private void insertSplitsFromFileIntoDB() {
return result;
}).collect(Collectors.toList());

mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 1));

mRoomDb.splitDao().insert(entities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ private void insertSplitsFromFileIntoDB() {
return result;
}).collect(Collectors.toList());

testDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
testDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 1));
testDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, System.currentTimeMillis()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class GeneralInfoEntity {
public static final String SPLITS_UPDATE_TIMESTAMP = "splitsUpdateTimestamp";
public static final String CHANGE_NUMBER_INFO = "splitChangesChangeNumber";
public static final String SPLITS_FILTER_QUERY_STRING = "splitsFilterQueryString";
public static final String DATBASE_MIGRATION_STATUS = "databaseMigrationStatus";
public static final int DATBASE_MIGRATION_STATUS_DONE = 1;
public static final String DATABASE_ENCRYPTION_MODE = "databaseEncryptionMode";
public static final String FLAGS_SPEC = "flagsSpec";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.split.android.client.storage.general;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public interface GeneralInfoStorage {

long getSplitsUpdateTimestamp();

void setSplitsUpdateTimestamp(long timestamp);

long getChangeNumber();

void setChangeNumber(long changeNumber);

@NonNull
String getSplitsFilterQueryString();

void setSplitsFilterQueryString(String queryString);

String getDatabaseEncryptionMode();

void setDatabaseEncryptionMode(String value);

@Nullable
String getFlagsSpec();

void setFlagsSpec(String value);

long getRolloutCacheLastClearTimestamp();

void setRolloutCacheLastClearTimestamp(long timestamp);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.split.android.client.storage.general;

import static io.split.android.client.utils.Utils.checkNotNull;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.split.android.client.storage.db.GeneralInfoDao;
import io.split.android.client.storage.db.GeneralInfoEntity;

public class GeneralInfoStorageImpl implements GeneralInfoStorage{

private static final String ROLLOUT_CACHE_LAST_CLEAR_TIMESTAMP = "rolloutCacheLastClearTimestamp";

private final GeneralInfoDao mGeneralInfoDao;

public GeneralInfoStorageImpl(GeneralInfoDao generalInfoDao) {
mGeneralInfoDao = checkNotNull(generalInfoDao);
}

@Override
public long getSplitsUpdateTimestamp() {
GeneralInfoEntity entity = mGeneralInfoDao.getByName(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP);
return entity != null ? entity.getLongValue() : 0L;
}

@Override
public void setSplitsUpdateTimestamp(long timestamp) {
mGeneralInfoDao.update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, timestamp));
}

@Override
public long getChangeNumber() {
GeneralInfoEntity entity = mGeneralInfoDao.getByName(GeneralInfoEntity.CHANGE_NUMBER_INFO);
return entity != null ? entity.getLongValue() : -1L;
}

@Override
public void setChangeNumber(long changeNumber) {
mGeneralInfoDao.update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, changeNumber));
}

@Override
@NonNull
public String getSplitsFilterQueryString() {
GeneralInfoEntity entity = mGeneralInfoDao.getByName(GeneralInfoEntity.SPLITS_FILTER_QUERY_STRING);
return entity != null ? entity.getStringValue() : "";
}

@Override
public void setSplitsFilterQueryString(String queryString) {
mGeneralInfoDao.update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_FILTER_QUERY_STRING, queryString));
}

@Override
public String getDatabaseEncryptionMode() {
GeneralInfoEntity entity = mGeneralInfoDao.getByName(GeneralInfoEntity.DATABASE_ENCRYPTION_MODE);
return entity != null ? entity.getStringValue() : "";
}

@Override
public void setDatabaseEncryptionMode(String value) {
mGeneralInfoDao.update(new GeneralInfoEntity(GeneralInfoEntity.DATABASE_ENCRYPTION_MODE, value));
}

@Override
@Nullable
public String getFlagsSpec() {
GeneralInfoEntity entity = mGeneralInfoDao.getByName(GeneralInfoEntity.FLAGS_SPEC);
return entity != null ? entity.getStringValue() : "";
}

@Override
public void setFlagsSpec(String value) {
mGeneralInfoDao.update(new GeneralInfoEntity(GeneralInfoEntity.FLAGS_SPEC, value));
}

@Override
public long getRolloutCacheLastClearTimestamp() {
GeneralInfoEntity entity = mGeneralInfoDao.getByName(ROLLOUT_CACHE_LAST_CLEAR_TIMESTAMP);
return entity != null ? entity.getLongValue() : 0L;
}

@Override
public void setRolloutCacheLastClearTimestamp(long timestamp) {
mGeneralInfoDao.update(new GeneralInfoEntity(ROLLOUT_CACHE_LAST_CLEAR_TIMESTAMP, timestamp));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package io.split.android.client.storage.general;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

import io.split.android.client.storage.db.GeneralInfoDao;
import io.split.android.client.storage.db.GeneralInfoEntity;

public class GeneralInfoStorageImplTest {

private GeneralInfoDao mGeneralInfoDao;
private GeneralInfoStorageImpl mGeneralInfoStorage;

@Before
public void setUp() {
mGeneralInfoDao = mock(GeneralInfoDao.class);
mGeneralInfoStorage = new GeneralInfoStorageImpl(mGeneralInfoDao);
}

@Test
public void setSplitsUpdateTimestampSetsValueOnDao() {
mGeneralInfoStorage.setSplitsUpdateTimestamp(123L);

verify(mGeneralInfoDao).update(argThat(entity -> entity.getName().equals("splitsUpdateTimestamp") && entity.getLongValue() == 123L));
}

@Test
public void setSplitsUpdateTimestampGetsValueFromDao() {
when(mGeneralInfoDao.getByName("splitsUpdateTimestamp")).thenReturn(new GeneralInfoEntity("splitsUpdateTimestamp", 123L));
long splitsUpdateTimestamp = mGeneralInfoStorage.getSplitsUpdateTimestamp();

assertEquals(123L, splitsUpdateTimestamp);
verify(mGeneralInfoDao).getByName("splitsUpdateTimestamp");
}

@Test
public void setChangeNumberSetsValueOnDao() {
mGeneralInfoStorage.setChangeNumber(123L);

verify(mGeneralInfoDao).update(argThat(entity -> entity.getName().equals("splitChangesChangeNumber") && entity.getLongValue() == 123L));
}

@Test
public void setChangeNumberGetsValueFromDao() {
when(mGeneralInfoDao.getByName("splitChangesChangeNumber")).thenReturn(new GeneralInfoEntity("splitChangesChangeNumber", 123L));
long changeNumber = mGeneralInfoStorage.getChangeNumber();

assertEquals(123L, changeNumber);
verify(mGeneralInfoDao).getByName("splitChangesChangeNumber");
}

@Test
public void getSplitsFilterQueryStringGetsValueFromDao() {
when(mGeneralInfoDao.getByName("splitsFilterQueryString")).thenReturn(new GeneralInfoEntity("splitsFilterQueryString", "queryString"));
String splitsFilterQueryString = mGeneralInfoStorage.getSplitsFilterQueryString();

assertEquals("queryString", splitsFilterQueryString);
verify(mGeneralInfoDao).getByName("splitsFilterQueryString");
}

@Test
public void setSplitsFilterQueryStringSetsValueOnDao() {
mGeneralInfoStorage.setSplitsFilterQueryString("queryString");

verify(mGeneralInfoDao).update(argThat(entity -> entity.getName().equals("splitsFilterQueryString") && entity.getStringValue().equals("queryString")));
}

@Test
public void getDatabaseEncryptionModeGetsValueFromDao() {
when(mGeneralInfoDao.getByName("databaseEncryptionMode")).thenReturn(new GeneralInfoEntity("databaseEncryptionMode", "value"));
String databaseEncryptionMode = mGeneralInfoStorage.getDatabaseEncryptionMode();

assertEquals("value", databaseEncryptionMode);
verify(mGeneralInfoDao).getByName("databaseEncryptionMode");
}

@Test
public void setDatabaseEncryptionModeSetsValueOnDao() {
mGeneralInfoStorage.setDatabaseEncryptionMode("value");

verify(mGeneralInfoDao).update(argThat(entity -> entity.getName().equals("databaseEncryptionMode") && entity.getStringValue().equals("value")));
}

@Test
public void getFlagsSpecGetsValueFromDao() {
when(mGeneralInfoDao.getByName("flagsSpec")).thenReturn(new GeneralInfoEntity("flagsSpec", "value"));
String flagsSpec = mGeneralInfoStorage.getFlagsSpec();

assertEquals("value", flagsSpec);
verify(mGeneralInfoDao).getByName("flagsSpec");
}

@Test
public void setFlagsSpecSetsValueOnDao() {
mGeneralInfoStorage.setFlagsSpec("value");

verify(mGeneralInfoDao).update(argThat(entity -> entity.getName().equals("flagsSpec") && entity.getStringValue().equals("value")));
}

@Test
public void getRolloutCacheLastClearTimestampGetsValueFromDao() {
when(mGeneralInfoDao.getByName("rolloutCacheLastClearTimestamp")).thenReturn(new GeneralInfoEntity("rolloutCacheLastClearTimestamp", 123L));
long rolloutCacheLastClearTimestamp = mGeneralInfoStorage.getRolloutCacheLastClearTimestamp();

assertEquals(123L, rolloutCacheLastClearTimestamp);
verify(mGeneralInfoDao).getByName("rolloutCacheLastClearTimestamp");
}

@Test
public void setRolloutCacheLastClearTimestampSetsValueOnDao() {
mGeneralInfoStorage.setRolloutCacheLastClearTimestamp(123L);

verify(mGeneralInfoDao).update(argThat(entity -> entity.getName().equals("rolloutCacheLastClearTimestamp") && entity.getLongValue() == 123L));
}

@Test
public void getChangeNumberReturnsMinusOneIfEntityIsNull() {
when(mGeneralInfoDao.getByName("splitChangesChangeNumber")).thenReturn(null);
long changeNumber = mGeneralInfoStorage.getChangeNumber();

assertEquals(-1L, changeNumber);
}

@Test
public void getSplitsUpdateTimestampReturnsZeroIfEntityIsNull() {
when(mGeneralInfoDao.getByName("splitsUpdateTimestamp")).thenReturn(null);
long timestamp = mGeneralInfoStorage.getSplitsUpdateTimestamp();

assertEquals(0L, timestamp);
}

@Test
public void getSplitsFilterQueryStringReturnsEmptyStringIfEntityIsNull() {
when(mGeneralInfoDao.getByName("splitsFilterQueryString")).thenReturn(null);
String queryString = mGeneralInfoStorage.getSplitsFilterQueryString();

assertEquals("", queryString);
}

@Test
public void getDatabaseEncryptionModeReturnsEmptyStringIfEntityIsNull() {
when(mGeneralInfoDao.getByName("databaseEncryptionMode")).thenReturn(null);
String value = mGeneralInfoStorage.getDatabaseEncryptionMode();

assertEquals("", value);
}

@Test
public void getFlagsSpecReturnsEmptyStringIfEntityIsNull() {
when(mGeneralInfoDao.getByName("flagsSpec")).thenReturn(null);
String value = mGeneralInfoStorage.getFlagsSpec();

assertEquals("", value);
}

@Test
public void getRolloutCacheLastClearTimestampReturnsZeroIfEntityIsNull() {
when(mGeneralInfoDao.getByName("rolloutCacheLastClearTimestamp")).thenReturn(null);
long timestamp = mGeneralInfoStorage.getRolloutCacheLastClearTimestamp();

assertEquals(0L, timestamp);
}
}

0 comments on commit ddf6879

Please sign in to comment.