use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DistributeSerializerTest method serialize.
@Test
public void serialize() throws JSONException {
LogContainer expectedContainer = new LogContainer();
List<Log> logs = new ArrayList<>();
{
DistributionStartSessionLog log = new DistributionStartSessionLog();
log.setTimestamp(new Date());
logs.add(log);
}
expectedContainer.setLogs(logs);
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
}
/* Serialize and deserialize logs container. */
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(DistributionStartSessionLog.TYPE, new DistributionStartSessionLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
LogContainer actualContainer = serializer.deserializeContainer(payload);
/* Verify that logs container successfully deserialized. */
Assert.assertEquals(expectedContainer, actualContainer);
}
use of com.microsoft.appcenter.ingestion.models.json.LogSerializer 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.LogSerializer 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.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLogException.
@Test(expected = PersistenceException.class)
public void putLogException() throws PersistenceException, IOException, JSONException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLogException", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = mock(LogSerializer.class);
doThrow(new JSONException("JSON exception")).when(logSerializer).serializeLog(any(Log.class));
persistence.setLogSerializer(logSerializer);
try {
/* Generate a log and persist. */
Log log = AndroidTestUtils.generateMockLog();
persistence.putLog("test-p1", log);
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.LogSerializer 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();
}
}
Aggregations