use of com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method upgradeFromVersion5to6.
@Test
public void upgradeFromVersion5to6() throws PersistenceException, JSONException {
/* Initialize database persistence with old version. */
ContentValues schema = new ContentValues(SCHEMA);
DatabaseManager databaseManager = new DatabaseManager(sContext, DatabasePersistence.DATABASE, DatabasePersistence.TABLE, DatabasePersistence.VERSION_TIMESTAMP_COLUMN, schema, CREATE_LOGS_SQL, mock(DatabaseManager.Listener.class));
/* Init log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory());
/* Insert old data before upgrade. */
Log oldLog = AndroidTestUtils.generateMockLog();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabasePersistence.COLUMN_GROUP, "test");
contentValues.put(DatabasePersistence.COLUMN_LOG, logSerializer.serializeLog(oldLog));
databaseManager.put(contentValues, DatabasePersistence.COLUMN_PRIORITY);
} finally {
databaseManager.close();
}
/* Upgrade. */
DatabasePersistence persistence = new DatabasePersistence(sContext);
persistence.setLogSerializer(logSerializer);
/* Prepare a common schema log. */
MockCommonSchemaLog commonSchemaLog = new MockCommonSchemaLog();
commonSchemaLog.setName("test");
commonSchemaLog.setIKey("o:test");
commonSchemaLog.setTimestamp(new Date());
commonSchemaLog.setVer("3.0");
commonSchemaLog.addTransmissionTarget("test-guid");
/* Check upgrade. */
try {
/* Verify old data cleared. */
assertEquals(0, persistence.countLogs("test"));
/* Put new data. */
persistence.putLog(commonSchemaLog, "test/one", NORMAL);
} finally {
persistence.close();
}
/* Get new data after restart. */
persistence = new DatabasePersistence(sContext);
persistence.setLogSerializer(logSerializer);
try {
/* Get new data. */
assertEquals(1, persistence.countLogs("test/one"));
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test/one", Collections.<String>emptyList(), 1, outputLogs);
assertEquals(1, outputLogs.size());
assertEquals(commonSchemaLog, outputLogs.get(0));
/* Verify target token is encrypted. */
ContentValues values = getContentValues(persistence, "test/one");
String token = values.getAsString(DatabasePersistence.COLUMN_TARGET_TOKEN);
assertNotNull(token);
assertNotEquals("test-guid", token);
assertEquals("test-guid", CryptoUtils.getInstance(sContext).decrypt(token).getDecryptedData());
/* Verify target key stored as well. */
String targetKey = values.getAsString(DatabasePersistence.COLUMN_TARGET_KEY);
assertEquals(commonSchemaLog.getIKey(), "o:" + targetKey);
/* Verify priority stored too. */
assertEquals((Integer) NORMAL, values.getAsInteger(DatabasePersistence.COLUMN_PRIORITY));
} finally {
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method getLogsFilteringOutPausedTargetKeys.
@Test
public void getLogsFilteringOutPausedTargetKeys() throws PersistenceException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence(sContext);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Test constants. */
int numberOfLogsPerKey = 10;
/* Generate and persist some logs with a first iKey. */
String pausedKey1 = "1";
generateCsLogsWithIKey(persistence, pausedKey1, numberOfLogsPerKey);
/* Generate more logs with another iKey to exclude. */
String pausedKey2 = "2";
generateCsLogsWithIKey(persistence, pausedKey2, numberOfLogsPerKey);
/* Generate logs from a third key. */
String resumedKey = "3";
generateCsLogsWithIKey(persistence, resumedKey, numberOfLogsPerKey);
/* Get logs without disabled keys. */
List<Log> outLogs = new ArrayList<>();
int limit = numberOfLogsPerKey * 3;
String batchId = persistence.getLogs("test", Arrays.asList(pausedKey1, pausedKey2), limit, outLogs);
assertNotNull(batchId);
/* Verify we get a subset of logs without the disabled keys. */
assertEquals(numberOfLogsPerKey, outLogs.size());
assertEquals(limit, persistence.countLogs("test"));
for (Log log : outLogs) {
assertTrue(log instanceof CommonSchemaLog);
assertEquals(resumedKey, ((CommonSchemaLog) log).getIKey());
}
/* Calling a second time should return nothing since the batch is in progress. */
outLogs.clear();
batchId = persistence.getLogs("test", Arrays.asList(pausedKey1, pausedKey2), limit, outLogs);
assertNull(batchId);
assertEquals(0, outLogs.size());
/* If we try to get a second batch without filtering, we should get all disabled logs. */
outLogs.clear();
batchId = persistence.getLogs("test", Collections.<String>emptyList(), limit, outLogs);
assertNotNull(batchId);
assertEquals(numberOfLogsPerKey * 2, outLogs.size());
for (Log log : outLogs) {
assertTrue(log instanceof CommonSchemaLog);
assertNotEquals(resumedKey, ((CommonSchemaLog) log).getIKey());
}
} finally {
persistence.close();
}
}
Aggregations