use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class AnalyticsSerializerTest method someBatch.
@Test
public void someBatch() throws JSONException {
LogContainer expectedContainer = new LogContainer();
Device device = new Device();
device.setSdkName("appcenter.android");
device.setSdkVersion("1.2.3");
device.setModel("S5");
device.setOemName("HTC");
device.setOsName("Android");
device.setOsVersion("4.0.3");
device.setOsBuild("LMY47X");
device.setOsApiLevel(15);
device.setLocale("en_US");
device.setTimeZoneOffset(120);
device.setScreenSize("800x600");
device.setAppVersion("3.2.1");
device.setAppBuild("42");
List<Log> logs = new ArrayList<>();
{
StartSessionLog startSessionLog = new StartSessionLog();
startSessionLog.setTimestamp(new Date());
logs.add(startSessionLog);
}
expectedContainer.setLogs(logs);
{
PageLog pageLog = new PageLog();
pageLog.setTimestamp(new Date());
pageLog.setName("home");
logs.add(pageLog);
}
{
PageLog pageLog = new PageLog();
pageLog.setTimestamp(new Date());
pageLog.setName("settings");
pageLog.setProperties(new HashMap<String, String>() {
{
put("from", "home_menu");
put("orientation", "portrait");
}
});
logs.add(pageLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setTimestamp(new Date());
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("subscribe");
logs.add(eventLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setTimestamp(new Date());
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("click");
eventLog.setProperties(new HashMap<String, String>() {
{
put("x", "1");
put("y", "2");
}
});
logs.add(eventLog);
}
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
log.setDevice(device);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
android.util.Log.v(TAG, payload);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class ErrorModelTest method handledErrorLog.
@Test
public void handledErrorLog() throws JSONException {
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(HandledErrorLog.TYPE, HandledErrorLogFactory.getInstance());
HandledErrorLog errorLog1 = new HandledErrorLog();
compareSelfNullClass(errorLog1);
Date timestamp = new Date();
errorLog1.setTimestamp(timestamp);
HandledErrorLog errorLog2 = new HandledErrorLog();
checkNotEquals(errorLog1, errorLog2);
errorLog2.setTimestamp(timestamp);
checkEquals(errorLog1, errorLog2);
{
errorLog1.setId(UUID.randomUUID());
checkNotEquals(errorLog1, errorLog2);
errorLog2.setId(UUID.randomUUID());
checkNotEquals(errorLog1, errorLog2);
errorLog2.setId(errorLog1.getId());
checkEquals(errorLog1, errorLog2);
}
checkSerialization(errorLog1, serializer);
{
Exception exception1 = new Exception();
exception1.setMessage("1");
Exception exception2 = new Exception();
exception2.setMessage("2");
errorLog1.setException(exception1);
checkNotEquals(errorLog1, errorLog2);
errorLog2.setException(exception2);
checkNotEquals(errorLog1, errorLog2);
errorLog2.setException(exception1);
checkEquals(errorLog1, errorLog2);
}
checkSerialization(errorLog1, serializer);
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class PushSerializerTest method serialize.
@Test
public void serialize() throws JSONException {
LogContainer expectedContainer = new LogContainer();
List<Log> logs = new ArrayList<>();
{
PushInstallationLog log = new PushInstallationLog();
log.setTimestamp(new Date());
log.setPushToken("TEST");
logs.add(log);
}
expectedContainer.setLogs(logs);
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogFails.
@Test
public void putLargeLogFails() throws PersistenceException, IOException {
/* Initialize database persistence. */
String path = Constants.FILES_PATH;
Constants.FILES_PATH = null;
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);
persistence.putLog("test-p1", log);
fail("putLog was expected to fail");
} catch (Persistence.PersistenceException e) {
assertTrue(e.getCause() instanceof IOException);
/* Make sure database entry has been removed. */
assertEquals(0, persistence.countLogs("test-p1"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
/* Restore path. */
Constants.FILES_PATH = path;
}
}
use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putTooManyLogs.
@Test
public void putTooManyLogs() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putTooManyLogs", 1, 2);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate too many logs 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-p1", log3);
persistence.putLog("test-p1", log4);
/* Get logs from persistence. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", 4, outputLogs);
assertEquals(2, outputLogs.size());
assertEquals(log3, outputLogs.get(0));
assertEquals(log4, outputLogs.get(1));
assertEquals(2, persistence.countLogs("test-p1"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
Aggregations