Search in sources :

Example 6 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method deleteLogsForGroup.

@Test
public void deleteLogsForGroup() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogsForGroup", 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);
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        String id1 = persistence.getLogs("test-p1", 5, outputLogs);
        String id2 = persistence.getLogs("test-p2", 5, outputLogs);
        assertNotNull(id1);
        assertNotNull(id2);
        /* Delete. */
        persistence.deleteLogs("test-p1");
        persistence.deleteLogs("test-p3");
        /* Try another get for verification. */
        outputLogs.clear();
        persistence.getLogs("test-p3", 5, outputLogs);
        /* Verify. */
        Map<String, List<Long>> pendingGroups = persistence.mPendingDbIdentifiersGroups;
        assertNull(pendingGroups.get("test-p1" + id1));
        assertEquals(1, pendingGroups.get("test-p2" + id2).size());
        assertEquals(1, pendingGroups.size());
        assertEquals(0, outputLogs.size());
        assertEquals(1, persistence.mDatabaseStorage.size());
        /* Verify one log still persists in the database. */
        persistence.clearPendingLogState();
        outputLogs.clear();
        persistence.getLogs("test-p2", 5, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(log3, outputLogs.get(0));
        /* Count for groups. */
        assertEquals(0, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(0, persistence.countLogs("test-p3"));
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 7 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method getLogs.

@Test
public void getLogs() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogs = 10;
        int sizeForGetLogs = 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. */
        getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
        /* Clear ids, we should be able to get the logs again in the same sequence. */
        persistence.clearPendingLogState();
        getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
        /* Count. */
        assertEquals(10, persistence.countLogs("test"));
        /* Clear. Nothing to get after. */
        persistence.mDatabaseStorage.clear();
        List<Log> outputLogs = new ArrayList<>();
        assertNull(persistence.getLogs("test", sizeForGetLogs, outputLogs));
        assertTrue(outputLogs.isEmpty());
        assertEquals(0, persistence.countLogs("test"));
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 8 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class ErrorModelTest method errorAttachmentLog.

@Test
public void errorAttachmentLog() throws JSONException {
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(ErrorAttachmentLog.TYPE, ErrorAttachmentLogFactory.getInstance());
    Date timestamp = new Date();
    ErrorAttachmentLog attachmentLog1 = new ErrorAttachmentLog();
    attachmentLog1.setTimestamp(timestamp);
    ErrorAttachmentLog attachmentLog2 = new ErrorAttachmentLog();
    attachmentLog2.setTimestamp(timestamp);
    compareSelfNullClass(attachmentLog1);
    checkEquals(attachmentLog1, attachmentLog2);
    checkEquals(attachmentLog1.getType(), ErrorAttachmentLog.TYPE);
    {
        attachmentLog1.setId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setId(attachmentLog1.getId());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setErrorId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setErrorId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setErrorId(attachmentLog1.getErrorId());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setContentType("1");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setContentType("2");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setContentType(attachmentLog1.getContentType());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setFileName("1");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setFileName("2");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setFileName(attachmentLog1.getFileName());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setData("1".getBytes(CHARSET));
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setData("2".getBytes(CHARSET));
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setData(attachmentLog1.getData());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setSid(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setSid(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setSid(attachmentLog1.getSid());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        /* Check serialization without filename. */
        attachmentLog2.setFileName(null);
    }
    {
        checkSerialization(attachmentLog1, serializer);
        checkSerialization(attachmentLog2, serializer);
    }
}
Also used : DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Date(java.util.Date) Test(org.junit.Test)

Example 9 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer in project mobile-center-sdk-android by Microsoft.

the class ErrorModelTest method managedErrorLog.

@Test
public void managedErrorLog() throws JSONException {
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(ManagedErrorLog.TYPE, ManagedErrorLogFactory.getInstance());
    Date timestamp = new Date();
    ManagedErrorLog errorLog1 = new ManagedErrorLog();
    errorLog1.setTimestamp(timestamp);
    ManagedErrorLog errorLog2 = new ManagedErrorLog();
    errorLog2.setTimestamp(timestamp);
    compareSelfNullClass(errorLog1);
    checkEquals(errorLog1, errorLog2);
    {
        errorLog1.setId(UUID.randomUUID());
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setId(UUID.randomUUID());
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setId(errorLog1.getId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setProcessId(1);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setProcessId(2);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setProcessId(errorLog1.getProcessId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setProcessName("1");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setProcessName("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setProcessName(errorLog1.getProcessName());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setParentProcessId(1);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setParentProcessId(2);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setParentProcessId(errorLog1.getParentProcessId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setParentProcessName("1");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setParentProcessName("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setParentProcessName(errorLog1.getParentProcessName());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setErrorThreadId(1L);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setErrorThreadId(2L);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setErrorThreadId(errorLog1.getErrorThreadId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setErrorThreadName("1");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setErrorThreadName("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setErrorThreadName(errorLog1.getErrorThreadName());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setFatal(true);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setFatal(false);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setFatal(errorLog1.getFatal());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setAppLaunchTimestamp(new Date(1L));
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setAppLaunchTimestamp(new Date(2L));
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setAppLaunchTimestamp(errorLog1.getAppLaunchTimestamp());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setArchitecture("1");
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setArchitecture("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setArchitecture(errorLog1.getArchitecture());
        checkEquals(errorLog1, errorLog2);
    }
    {
        Exception exception1 = new Exception();
        Exception exception2 = new Exception();
        compareSelfNullClass(exception1);
        checkEquals(exception1, exception2);
        {
            exception1.setType("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setType("2");
            checkNotEquals(exception1, exception2);
            exception2.setType(exception1.getType());
            checkEquals(exception1, exception2);
        }
        {
            exception1.setMessage("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setMessage("2");
            checkNotEquals(exception1, exception2);
            exception2.setMessage(exception1.getMessage());
            checkEquals(exception1, exception2);
        }
        {
            exception1.setStackTrace("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setStackTrace("2");
            checkNotEquals(exception1, exception2);
            exception2.setStackTrace(exception1.getStackTrace());
            checkEquals(exception1, exception2);
        }
        {
            errorLog1.setException(exception1);
            errorLog2.setException(exception2);
            StackFrame frame1 = new StackFrame();
            StackFrame frame2 = new StackFrame();
            compareSelfNullClass(frame1);
            checkEquals(frame1, frame2);
            {
                frame1.setClassName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setClassName("2");
                checkNotEquals(frame1, frame2);
                frame2.setClassName(frame1.getClassName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setMethodName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setMethodName("2");
                checkNotEquals(frame1, frame2);
                frame2.setMethodName(frame1.getMethodName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setLineNumber(1);
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setLineNumber(2);
                checkNotEquals(frame1, frame2);
                frame2.setLineNumber(frame1.getLineNumber());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setFileName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setFileName("2");
                checkNotEquals(frame1, frame2);
                frame2.setFileName(frame1.getFileName());
                checkEquals(frame1, frame2);
            }
        }
        {
            exception1.setWrapperSdkName("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setWrapperSdkName("2");
            checkNotEquals(exception1, exception2);
            exception2.setWrapperSdkName(exception1.getWrapperSdkName());
            checkEquals(exception1, exception2);
        }
    }
    {
        Thread thread1 = new Thread();
        Thread thread2 = new Thread();
        compareSelfNullClass(thread1);
        checkEquals(thread1, thread2);
        {
            thread1.setId(1L);
            checkNotEquals(thread1, thread2);
            checkThreads(serializer, errorLog1, errorLog2, thread1, thread2);
            thread2.setId(2L);
            checkNotEquals(thread1, thread2);
            thread2.setId(thread1.getId());
            checkEquals(thread1, thread2);
        }
        {
            thread1.setName("1");
            checkNotEquals(thread1, thread2);
            checkThreads(serializer, errorLog1, errorLog2, thread1, thread2);
            thread2.setName("2");
            checkNotEquals(thread1, thread2);
            thread2.setName(thread1.getName());
            checkEquals(thread1, thread2);
        }
        {
            errorLog1.setThreads(singletonList(thread1));
            errorLog2.setThreads(singletonList(thread2));
            StackFrame frame1 = new StackFrame();
            StackFrame frame2 = new StackFrame();
            compareSelfNullClass(frame1);
            checkEquals(frame1, frame2);
            {
                frame1.setClassName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setClassName("2");
                checkNotEquals(frame1, frame2);
                frame2.setClassName(frame1.getClassName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setMethodName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setMethodName("2");
                checkNotEquals(frame1, frame2);
                frame2.setMethodName(frame1.getMethodName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setLineNumber(1);
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setLineNumber(2);
                checkNotEquals(frame1, frame2);
                frame2.setLineNumber(frame1.getLineNumber());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setFileName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setFileName("2");
                checkNotEquals(frame1, frame2);
                frame2.setFileName(frame1.getFileName());
                checkEquals(frame1, frame2);
            }
        }
    }
    checkSerialization(errorLog1, serializer);
}
Also used : DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Date(java.util.Date) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 10 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer 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);
}
Also used : DistributionStartSessionLogFactory(com.microsoft.appcenter.distribute.ingestion.models.json.DistributionStartSessionLogFactory) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) Log(com.microsoft.appcenter.ingestion.models.Log) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) UUID(java.util.UUID) Date(java.util.Date) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Aggregations

DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)16 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)15 Test (org.junit.Test)15 Log (com.microsoft.appcenter.ingestion.models.Log)11 ArrayList (java.util.ArrayList)11 MediumTest (android.support.test.filters.MediumTest)9 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)8 Date (java.util.Date)6 SuppressLint (android.annotation.SuppressLint)5 Mockito.anyString (org.mockito.Mockito.anyString)5 HashMap (java.util.HashMap)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)3 LogWithProperties (com.microsoft.appcenter.ingestion.models.LogWithProperties)3 UUID (java.util.UUID)3 JSONException (org.json.JSONException)3 File (java.io.File)2 WorkerThread (android.support.annotation.WorkerThread)1 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)1 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)1 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)1