Search in sources :

Example 26 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesTest method testNativeCrashLog.

private ManagedErrorLog testNativeCrashLog(long appStartTime, long crashTime, boolean correlateSession) throws Exception {
    /* Setup mock for a crash in disk. */
    File minidumpFile = mock(File.class);
    when(minidumpFile.getName()).thenReturn("mockFile");
    when(minidumpFile.lastModified()).thenReturn(crashTime);
    mockStatic(SessionContext.class);
    SessionContext sessionContext = mock(SessionContext.class);
    when(SessionContext.getInstance()).thenReturn(sessionContext);
    if (correlateSession) {
        SessionContext.SessionInfo sessionInfo = mock(SessionContext.SessionInfo.class);
        when(sessionContext.getSessionAt(crashTime)).thenReturn(sessionInfo);
        when(sessionInfo.getAppLaunchTimestamp()).thenReturn(appStartTime);
    }
    mockStatic(DeviceInfoHelper.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class));
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(mock(File.class));
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[] { minidumpFile });
    File pendingDir = mock(File.class);
    Whitebox.setInternalState(pendingDir, "path", "");
    when(ErrorLogHelper.getPendingMinidumpDirectory()).thenReturn(pendingDir);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new NativeException());
    LogSerializer logSerializer = mock(LogSerializer.class);
    ArgumentCaptor<Log> log = ArgumentCaptor.forClass(Log.class);
    when(logSerializer.serializeLog(log.capture())).thenReturn("{}");
    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;
        }
    });
    /* Start crashes. */
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mock(Channel.class));
    /* Verify timestamps on the crash log. */
    assertTrue(Crashes.hasCrashedInLastSession().get());
    assertTrue(log.getValue() instanceof ManagedErrorLog);
    return (ManagedErrorLog) log.getValue();
}
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) Device(com.microsoft.appcenter.ingestion.models.Device) 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) SessionContext(com.microsoft.appcenter.SessionContext) UUID(java.util.UUID) File(java.io.File) NativeException(com.microsoft.appcenter.crashes.model.NativeException)

Example 27 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesTest method manuallyReportNullList.

@Test
public void manuallyReportNullList() 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);
    /* 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());
    /* Send nothing using null. */
    assertFalse(WrapperSdkExceptionManager.sendCrashReportsOrAwaitUserConfirmation(null).get());
    /* No log sent. */
    verify(mockChannel, never()).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) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 28 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesTest method sendMoreThan2ErrorAttachments.

@Test
public void sendMoreThan2ErrorAttachments() throws IOException, ClassNotFoundException, JSONException {
    int MAX_ATTACHMENT_PER_CRASH = 2;
    int numOfAttachments = MAX_ATTACHMENT_PER_CRASH + 1;
    ArrayList<ErrorAttachmentLog> errorAttachmentLogs = new ArrayList<>(3);
    for (int i = 0; i < numOfAttachments; ++i) {
        ErrorAttachmentLog log = mock(ErrorAttachmentLog.class);
        when(log.isValid()).thenReturn(true);
        errorAttachmentLogs.add(log);
    }
    CrashesListener listener = mock(CrashesListener.class);
    when(listener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    when(listener.getErrorAttachments(any(ErrorReport.class))).thenReturn(errorAttachmentLogs);
    ManagedErrorLog log = mock(ManagedErrorLog.class);
    when(log.getId()).thenReturn(UUID.randomUUID());
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(log);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { 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(new ErrorReport());
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(mock(Throwable.class));
    Crashes crashes = Crashes.getInstance();
    crashes.setInstanceListener(listener);
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mock(Channel.class));
    String expectedMessage = "A limit of " + MAX_ATTACHMENT_PER_CRASH + " attachments per error report might be enforced by server.";
    PowerMockito.verifyStatic();
    AppCenterLog.warn(Crashes.LOG_TAG, expectedMessage);
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) Channel(com.microsoft.appcenter.channel.Channel) ArrayList(java.util.ArrayList) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) 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)

Example 29 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesTest method buildErrorReportError.

@Test
public void buildErrorReportError() throws IOException, ClassNotFoundException {
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    File throwableFile = mock(File.class);
    when(throwableFile.length()).thenReturn(1L);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(throwableFile);
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(null);
    Exception classNotFoundException = mock(ClassNotFoundException.class);
    Exception ioException = mock(IOException.class);
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenThrow(classNotFoundException).thenThrow(ioException);
    Crashes crashes = Crashes.getInstance();
    ErrorReport report = crashes.buildErrorReport(mErrorLog);
    assertNull(report);
    report = crashes.buildErrorReport(mErrorLog);
    assertNull(report);
    verifyStatic();
    AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(classNotFoundException));
    verifyStatic();
    AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(ioException));
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) UUID(java.util.UUID) File(java.io.File) JSONException(org.json.JSONException) NativeException(com.microsoft.appcenter.crashes.model.NativeException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with ErrorReport

use of com.microsoft.appcenter.crashes.model.ErrorReport in project AppCenter-SDK-Android by Microsoft.

the class CrashesTest method buildErrorReport.

@Test
public void buildErrorReport() throws IOException, ClassNotFoundException {
    ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    File throwableFile = mock(File.class);
    when(throwableFile.length()).thenReturn(1L);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(throwableFile).thenReturn(null);
    when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport);
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
    Crashes crashes = Crashes.getInstance();
    ErrorReport report = crashes.buildErrorReport(mErrorLog);
    assertErrorEquals(mErrorLog, report);
    mErrorLog.setId(UUIDUtils.randomUUID());
    report = crashes.buildErrorReport(mErrorLog);
    assertNull(report);
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)54 Test (org.junit.Test)40 File (java.io.File)36 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)31 Context (android.content.Context)26 Channel (com.microsoft.appcenter.channel.Channel)25 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)22 UUID (java.util.UUID)21 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)18 Device (com.microsoft.appcenter.ingestion.models.Device)12 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)12 SessionContext (com.microsoft.appcenter.utils.context.SessionContext)12 SessionContext (com.microsoft.appcenter.SessionContext)11 Log (com.microsoft.appcenter.ingestion.models.Log)11 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)10 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)10 JSONException (org.json.JSONException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9