Search in sources :

Example 21 with LogSerializer

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

the class DatabasePersistenceTest method clearPendingLogState.

@Test
public void clearPendingLogState() throws JSONException {
    /* groupCount should be <= 9. */
    final int groupCount = 4;
    final int logCount = 10;
    /* Mock logs. */
    List<List<ContentValues>> list = new ArrayList<>();
    for (int i = 0; i < groupCount; i++) {
        List<ContentValues> iterator = new ArrayList<>();
        for (long l = 1; l <= logCount; l++) {
            ContentValues values = mock(ContentValues.class);
            when(values.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(l + i * logCount);
            when(values.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("{}");
            iterator.add(values);
        }
        list.add(iterator);
    }
    /* Mock instances. */
    mockStatic(StorageHelper.DatabaseStorage.class);
    StorageHelper.DatabaseStorage mockDatabaseStorage = mock(StorageHelper.DatabaseStorage.class);
    when(StorageHelper.DatabaseStorage.getDatabaseStorage(anyString(), anyString(), anyInt(), any(ContentValues.class), anyInt(), any(StorageHelper.DatabaseStorage.DatabaseErrorListener.class))).thenReturn(mockDatabaseStorage);
    for (int i = 0; i < groupCount; i++) {
        StorageHelper.DatabaseStorage.DatabaseScanner mockDatabaseScanner = mock(StorageHelper.DatabaseStorage.DatabaseScanner.class);
        when(mockDatabaseScanner.iterator()).thenReturn(list.get(i).iterator());
        when(mockDatabaseStorage.getScanner(COLUMN_GROUP, String.valueOf(i))).thenReturn(mockDatabaseScanner);
    }
    LogSerializer mockLogSerializer = mock(LogSerializer.class);
    when(mockLogSerializer.deserializeLog(anyString())).thenReturn(mock(Log.class));
    /* Instantiate Database Persistence. */
    DatabasePersistence persistence = new DatabasePersistence();
    persistence.setLogSerializer(mockLogSerializer);
    /* Get logs. */
    for (int i = 0; i < groupCount; i++) {
        persistence.getLogs(String.valueOf(i), logCount, new ArrayList<Log>());
    }
    /* Verify there are 4 pending groups. */
    assertEquals(groupCount, persistence.mPendingDbIdentifiersGroups.size());
    assertEquals(groupCount * logCount, persistence.mPendingDbIdentifiers.size());
    /* Clear all pending groups and verify. */
    persistence.clearPendingLogState();
    assertEquals(0, persistence.mPendingDbIdentifiersGroups.size());
    assertEquals(0, persistence.mPendingDbIdentifiers.size());
}
Also used : ContentValues(android.content.ContentValues) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) 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) ArrayList(java.util.ArrayList) List(java.util.List) StorageHelper(com.microsoft.appcenter.utils.storage.StorageHelper) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with LogSerializer

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

the class DatabasePersistenceTest method getLogsWithCorruption.

@Test
public void getLogsWithCorruption() throws JSONException {
    /* Mock instances. */
    int logCount = 3;
    mockStatic(StorageHelper.DatabaseStorage.class);
    StorageHelper.DatabaseStorage databaseStorage = mock(StorageHelper.DatabaseStorage.class);
    when(StorageHelper.DatabaseStorage.getDatabaseStorage(anyString(), anyString(), anyInt(), any(ContentValues.class), anyInt(), any(StorageHelper.DatabaseStorage.DatabaseErrorListener.class))).thenReturn(databaseStorage);
    /* Make 3 logs, the second one will be corrupted. */
    Collection<ContentValues> fieldValues = new ArrayList<>(logCount);
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(0L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("first");
        fieldValues.add(contentValues);
    }
    {
        /* Empty record, "corrupted", cause identifier is null (and no other field either). */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(null);
        fieldValues.add(contentValues);
    }
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(2L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("last");
        fieldValues.add(contentValues);
    }
    /* Mock log sequence retrieved from scanner. */
    StorageHelper.DatabaseStorage.DatabaseScanner databaseScanner = mock(StorageHelper.DatabaseStorage.DatabaseScanner.class);
    when(databaseStorage.getScanner(anyString(), anyString())).thenReturn(databaseScanner);
    when(databaseStorage.getScanner(anyString(), anyString(), eq(false))).thenReturn(databaseScanner);
    when(databaseScanner.iterator()).thenReturn(fieldValues.iterator());
    /* Mock second scanner with identifiers only. */
    Collection<ContentValues> idValues = new ArrayList<>(logCount);
    for (long i = 0; i < logCount; i++) {
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(i);
        idValues.add(contentValues);
    }
    StorageHelper.DatabaseStorage.DatabaseScanner idDatabaseScanner = mock(StorageHelper.DatabaseStorage.DatabaseScanner.class);
    when(databaseStorage.getScanner(anyString(), anyString(), eq(true))).thenReturn(idDatabaseScanner);
    when(idDatabaseScanner.iterator()).thenReturn(idValues.iterator());
    /* Mock serializer and eventually the database. */
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenAnswer(new Answer<Log>() {

        @Override
        public Log answer(InvocationOnMock invocation) throws Throwable {
            /* Hack serializer to return type = payload to simplify checking. */
            Log log = mock(Log.class);
            when(log.getType()).thenReturn((String) invocation.getArguments()[0]);
            return log;
        }
    });
    DatabasePersistence persistence = new DatabasePersistence();
    persistence.setLogSerializer(logSerializer);
    /* Get logs and verify we get only non corrupted logs. */
    ArrayList<Log> outLogs = new ArrayList<>();
    persistence.getLogs("mock", 50, outLogs);
    assertEquals(logCount - 1, outLogs.size());
    assertEquals("first", outLogs.get(0).getType());
    assertEquals("last", outLogs.get(1).getType());
    /* Verify we detected and deleted the corrupted log, the second one. */
    verify(databaseStorage).delete(1);
    /* Verify next call is empty logs as they are pending. */
    outLogs = new ArrayList<>();
    persistence.getLogs("mock", 50, outLogs);
    assertEquals(0, outLogs.size());
    /*
         * Add new logs with corruption again. First 2 logs are still there.
         * Also this time the corrupted log will not even return its identifier when scanning
         * with only id fields, to test that the delete fails gracefully and that we can still
         * work with other logs.
         */
    logCount = 4;
    fieldValues = new ArrayList<>(logCount);
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(0L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("first");
        fieldValues.add(contentValues);
    }
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(2L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("last");
        fieldValues.add(contentValues);
    }
    {
        /* New corrupted record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(null);
        fieldValues.add(contentValues);
    }
    {
        /* Valid new record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(4L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("true last");
        fieldValues.add(contentValues);
    }
    when(databaseScanner.iterator()).thenReturn(fieldValues.iterator());
    idValues = new ArrayList<>(4);
    /* Here the id scanner will also skip the new corrupted log which id would be 3. */
    for (long i = 0; i < logCount; i += 2) {
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(i);
        idValues.add(contentValues);
    }
    when(idDatabaseScanner.iterator()).thenReturn(idValues.iterator());
    /* Verify next call is only the new valid log as others are marked pending. */
    outLogs = new ArrayList<>();
    persistence.getLogs("mock", 50, outLogs);
    assertEquals(1, outLogs.size());
    assertEquals("true last", outLogs.get(0).getType());
    /* Verify that the only log we deleted in the entire test was the one from previous test (id=1). */
    verify(databaseStorage).delete(anyLong());
}
Also used : ContentValues(android.content.ContentValues) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) 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) Matchers.anyString(org.mockito.Matchers.anyString) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageHelper(com.microsoft.appcenter.utils.storage.StorageHelper) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with LogSerializer

use of com.microsoft.appcenter.ingestion.models.json.LogSerializer 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 24 with LogSerializer

use of com.microsoft.appcenter.ingestion.models.json.LogSerializer 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 25 with LogSerializer

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

the class CrashesTest method manualProcessing.

@Test
public void manualProcessing() throws Exception {
    /* Setup mock for a crash in disk. */
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorReport report1 = new ErrorReport();
    report1.setId(UUIDUtils.randomUUID().toString());
    ErrorReport report2 = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report1).thenReturn(report2);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenAnswer(new Answer<ManagedErrorLog>() {

        @Override
        public ManagedErrorLog answer(InvocationOnMock invocation) throws Throwable {
            ManagedErrorLog log = mock(ManagedErrorLog.class);
            when(log.getId()).thenReturn(UUID.randomUUID());
            return log;
        }
    });
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    /* Create listener for user confirmation. */
    CrashesListener listener = mock(CrashesListener.class);
    Crashes.setListener(listener);
    /* Set manual processing. */
    WrapperSdkExceptionManager.setAutomaticProcessing(false);
    /* Start crashes. */
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, "", mockChannel);
    /* No log queued. */
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
    /* Get crash reports. */
    Collection<ErrorReport> reports = WrapperSdkExceptionManager.getUnprocessedErrorReports().get();
    assertNotNull(reports);
    assertEquals(2, reports.size());
    Iterator<ErrorReport> iterator = reports.iterator();
    assertEquals(report1, iterator.next());
    assertEquals(report2, iterator.next());
    /* Listener not called yet on anything on manual processing. */
    verifyZeroInteractions(listener);
    /* Send only the first. */
    assertFalse(WrapperSdkExceptionManager.sendCrashReportsOrAwaitUserConfirmation(Collections.singletonList(report1.getId())).get());
    /* We used manual process function, listener not called. */
    verifyZeroInteractions(listener);
    /* No log sent until manual user confirmation in that mode (we are not in always send). */
    verify(mockChannel, never()).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
    /* Confirm with always send. */
    Crashes.notifyUserConfirmation(Crashes.ALWAYS_SEND);
    verifyStatic();
    StorageHelper.PreferencesStorage.putBoolean(Crashes.PREF_KEY_ALWAYS_SEND, true);
    when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
    /* 1 log sent. Other one is filtered. */
    verify(mockChannel).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
    /* We can send attachments via wrapper instead of using listener (both work but irrelevant to test with listener). */
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    WrapperSdkExceptionManager.sendErrorAttachments(report1.getId(), Collections.singletonList(mockAttachment));
    verify(mockChannel).enqueue(eq(mockAttachment), eq(crashes.getGroupName()));
    /* Send attachment with invalid UUID format for report identifier. */
    mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    WrapperSdkExceptionManager.sendErrorAttachments("not-a-uuid", Collections.singletonList(mockAttachment));
    verify(mockChannel, never()).enqueue(eq(mockAttachment), eq(crashes.getGroupName()));
    /* We used manual process function, listener not called and our mock channel does not send events. */
    verifyZeroInteractions(listener);
    /* Reset instance to test another tine with always send. */
    Crashes.unsetInstance();
    crashes = Crashes.getInstance();
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report1).thenReturn(report2);
    WrapperSdkExceptionManager.setAutomaticProcessing(false);
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    mockChannel = mock(Channel.class);
    crashes.onStarted(mockContext, "", mockChannel);
    assertTrue(Crashes.isEnabled().get());
    verify(mockChannel, never()).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
    /* Get crash reports, check always sent was returned and sent without confirmation. */
    assertTrue(WrapperSdkExceptionManager.sendCrashReportsOrAwaitUserConfirmation(Collections.singletonList(report2.getId())).get());
    verify(mockChannel).enqueue(any(ManagedErrorLog.class), eq(crashes.getGroupName()));
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Channel(com.microsoft.appcenter.channel.Channel) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)44 Test (org.junit.Test)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 Log (com.microsoft.appcenter.ingestion.models.Log)26 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)23 File (java.io.File)22 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)19 Context (android.content.Context)16 ArrayList (java.util.ArrayList)16 JSONException (org.json.JSONException)16 UUID (java.util.UUID)15 SessionContext (com.microsoft.appcenter.SessionContext)14 Channel (com.microsoft.appcenter.channel.Channel)14 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)14 MediumTest (android.support.test.filters.MediumTest)10 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)10 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)10 IOException (java.io.IOException)9 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)8 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)8