use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogAndDeleteAll.
@Test
public void putLargeLogAndDeleteAll() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLargeLog", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Initial count is 0. */
assertEquals(0, persistence.countLogs("test-p1"));
/* Generate a large log and persist. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 2 * 1024 * 1024;
StringBuilder largeValue = new StringBuilder(size);
for (int i = 0; i < size; i++) {
largeValue.append("x");
}
Map<String, String> properties = new HashMap<>();
properties.put("key", largeValue.toString());
log.setProperties(properties);
long id = persistence.putLog("test-p1", log);
/* Count logs. */
assertEquals(1, persistence.countLogs("test-p1"));
/* Get a log from persistence. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", 1, outputLogs);
assertEquals(1, outputLogs.size());
assertEquals(log, outputLogs.get(0));
assertEquals(1, persistence.countLogs("test-p1"));
/* Verify large file. */
File file = persistence.getLargePayloadFile(persistence.getLargePayloadGroupDirectory("test-p1"), id);
assertNotNull(file);
String fileLog = StorageHelper.InternalStorage.read(file);
assertNotNull(fileLog);
assertTrue(fileLog.length() >= size);
/* Delete entire group. */
persistence.deleteLogs("test-p1");
assertEquals(0, persistence.countLogs("test-p1"));
/* Verify file delete and also parent directory since we used group deletion. */
assertFalse(file.exists());
assertFalse(file.getParentFile().exists());
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method deleteLogs.
@Test
public void deleteLogs() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogs", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate a log and persist. */
Log log1 = AndroidTestUtils.generateMockLog();
Log log2 = AndroidTestUtils.generateMockLog();
Log log3 = AndroidTestUtils.generateMockLog();
Log log4 = AndroidTestUtils.generateMockLog();
persistence.putLog("test-p1", log1);
persistence.putLog("test-p1", log2);
persistence.putLog("test-p2", log3);
persistence.putLog("test-p3", log4);
assertEquals(2, persistence.countLogs("test-p1"));
assertEquals(1, persistence.countLogs("test-p2"));
assertEquals(1, persistence.countLogs("test-p3"));
/* Get a log from persistence. */
List<Log> outputLogs1 = new ArrayList<>();
List<Log> outputLogs2 = new ArrayList<>();
List<Log> outputLogs3 = new ArrayList<>();
String id = persistence.getLogs("test-p1", 5, outputLogs1);
persistence.getLogs("test-p2", 5, outputLogs2);
persistence.getLogs("test-p3", 5, outputLogs3);
/* Verify. */
assertNotNull(id);
assertNotEquals("", id);
assertEquals(2, outputLogs1.size());
assertEquals(1, outputLogs2.size());
assertEquals(1, outputLogs3.size());
/* Delete. */
persistence.deleteLogs("", id);
/* Access DatabaseStorage directly to verify the deletions. */
DatabaseScanner scanner1 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
DatabaseScanner scanner2 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p2");
DatabaseScanner scanner3 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p3");
// noinspection TryFinallyCanBeTryWithResources
try {
/* Verify. */
assertEquals(2, getIteratorSize(scanner1.iterator()));
assertEquals(1, getIteratorSize(scanner2.iterator()));
assertEquals(1, getIteratorSize(scanner3.iterator()));
} finally {
/* Close. */
scanner1.close();
scanner2.close();
scanner3.close();
}
/* Delete. */
persistence.deleteLogs("test-p1", id);
/* Access DatabaseStorage directly to verify the deletions. */
DatabaseScanner scanner4 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
// noinspection TryFinallyCanBeTryWithResources
try {
/* Verify. */
assertEquals(0, getIteratorSize(scanner4.iterator()));
} finally {
/* Close. */
scanner4.close();
}
/* Count logs after delete. */
assertEquals(0, persistence.countLogs("test-p1"));
assertEquals(1, persistence.countLogs("test-p2"));
assertEquals(1, persistence.countLogs("test-p3"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLog.
@Test
public void putLog() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLog", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Initial count is 0. */
assertEquals(0, persistence.countLogs("test-p1"));
/* Generate a log and persist. */
Log log = AndroidTestUtils.generateMockLog();
persistence.putLog("test-p1", log);
/* Count logs. */
assertEquals(1, persistence.countLogs("test-p1"));
/* Get a log from persistence. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", 1, outputLogs);
assertEquals(1, outputLogs.size());
assertEquals(log, outputLogs.get(0));
assertEquals(1, persistence.countLogs("test-p1"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogFailsToRead.
@Test
public void putLargeLogFailsToRead() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLargeLogException", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Initial count is 0. */
assertEquals(0, persistence.countLogs("test-p1"));
/* Generate a large log and persist. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 2 * 1024 * 1024;
StringBuilder largeValue = new StringBuilder(size);
for (int i = 0; i < size; i++) {
largeValue.append("x");
}
Map<String, String> properties = new HashMap<>();
properties.put("key", largeValue.toString());
log.setProperties(properties);
long id = persistence.putLog("test-p1", log);
assertEquals(1, persistence.countLogs("test-p1"));
/* Verify large file. */
File file = persistence.getLargePayloadFile(persistence.getLargePayloadGroupDirectory("test-p1"), id);
assertNotNull(file);
String fileLog = StorageHelper.InternalStorage.read(file);
assertNotNull(fileLog);
assertTrue(fileLog.length() >= size);
/* Delete the file. */
assertTrue(file.delete());
/* We won't be able to read the log now but persistence should delete the SQLite log on error. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", 1, outputLogs);
assertEquals(0, outputLogs.size());
assertEquals(0, persistence.countLogs("test-p1"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method getLogsException.
@Test
public void getLogsException() throws PersistenceException, IOException, JSONException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = spy(new DefaultLogSerializer());
/* Throw a JSON exception for the first call. */
doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).when(logSerializer).deserializeLog(anyString());
persistence.setLogSerializer(logSerializer);
try {
/* Test constants. */
int numberOfLogs = 4;
/* Generate a log and persist. */
Log[] logs = new Log[numberOfLogs];
for (int i = 0; i < logs.length; i++) logs[i] = AndroidTestUtils.generateMockLog();
/* Put. */
for (Log log : logs) persistence.putLog("test", log);
/* Get. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test", 10, outputLogs);
assertEquals(numberOfLogs / 2, outputLogs.size());
assertEquals(2, persistence.mDatabaseStorage.size());
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
Aggregations