Search in sources :

Example 16 with ErrorReport

use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-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.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    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();
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(classNotFoundException));
    verifyStatic();
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(ioException));
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) UUID(java.util.UUID) File(java.io.File) JSONException(org.json.JSONException) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with ErrorReport

use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelper method getErrorReportFromErrorLog.

@NonNull
public static ErrorReport getErrorReportFromErrorLog(@NonNull ManagedErrorLog log, Throwable throwable) {
    ErrorReport report = new ErrorReport();
    report.setId(log.getId().toString());
    report.setThreadName(log.getErrorThreadName());
    report.setThrowable(throwable);
    report.setAppStartTime(new Date(log.getToffset() - log.getAppLaunchTOffset()));
    report.setAppErrorTime(new Date(log.getToffset()));
    report.setDevice(log.getDevice());
    return report;
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Date(java.util.Date) NonNull(android.support.annotation.NonNull)

Example 18 with ErrorReport

use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method handleUserConfirmationDoNotSend.

@Test
public void handleUserConfirmationDoNotSend() throws IOException, ClassNotFoundException, JSONException {
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    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(null);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(true);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mock(Context.class), "", mock(Channel.class));
    Crashes.notifyUserConfirmation(Crashes.DONT_SEND);
    verify(mockListener, never()).getErrorAttachments(any(ErrorReport.class));
    verify(mMockLooper).quit();
    verifyStatic();
    ErrorLogHelper.removeStoredErrorLogFile(mErrorLog.getId());
    verifyStatic();
    ErrorLogHelper.removeStoredThrowableFile(mErrorLog.getId());
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with ErrorReport

use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method crashInLastSessionError.

@Test
public void crashInLastSessionError() throws JSONException, IOException, ClassNotFoundException {
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
    mockStatic(ErrorLogHelper.class);
    File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes.getInstance().setLogSerializer(logSerializer);
    assertFalse(Crashes.hasCrashedInLastSession());
    JSONException jsonException = new JSONException("Fake JSON exception");
    when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
    ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport data) {
            assertNull(data);
        }
    };
    /*
         * Last session error is only fetched upon initialization: enabled and channel ready.
         * Here the service is enabled by default but we are waiting channel to be ready, simulate that.
         */
    assertTrue(Crashes.isEnabled());
    Crashes.getLastSessionCrashReport(callback);
    Crashes.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
    assertFalse(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(callback);
    /*
         * De-serializing fails twice: processing the log from last time as part of the bulk processing.
         * And loading that same file for exposing it in getLastErrorReport.
         */
    verifyStatic(times(2));
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with ErrorReport

use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method getChannelListener.

@Test
public void getChannelListener() throws IOException, ClassNotFoundException {
    ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport);
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
    Crashes.setListener(new AbstractCrashesListener() {

        @Override
        public void onBeforeSending(ErrorReport report) {
            assertErrorEquals(mErrorLog, report);
        }

        @Override
        public void onSendingSucceeded(ErrorReport report) {
            assertErrorEquals(mErrorLog, report);
        }

        @Override
        public void onSendingFailed(ErrorReport report, Exception e) {
            assertErrorEquals(mErrorLog, report);
        }
    });
    Channel.GroupListener listener = Crashes.getInstance().getChannelListener();
    listener.onBeforeSending(mErrorLog);
    listener.onSuccess(mErrorLog);
    listener.onFailure(mErrorLog, EXCEPTION);
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Channel(com.microsoft.azure.mobile.channel.Channel) UUID(java.util.UUID) File(java.io.File) JSONException(org.json.JSONException) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)22 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 File (java.io.File)15 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 Channel (com.microsoft.azure.mobile.channel.Channel)12 UUID (java.util.UUID)12 Context (android.content.Context)10 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)8 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)6 JSONException (org.json.JSONException)6 Log (com.microsoft.azure.mobile.ingestion.models.Log)5 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)4 IOException (java.io.IOException)4 ResultCallback (com.microsoft.azure.mobile.ResultCallback)3 Date (java.util.Date)3 Device (com.microsoft.azure.mobile.ingestion.models.Device)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2