Search in sources :

Example 6 with LogSerializer

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

the class CrashesTest method processPendingErrorsCorrupted.

@Test
public void processPendingErrorsCorrupted() throws JSONException {
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
    crashes.setLogSerializer(logSerializer);
    CrashesListener listener = mock(CrashesListener.class);
    crashes.setInstanceListener(listener);
    Channel channel = mock(Channel.class);
    crashes.onStarted(mock(Context.class), "", channel);
    verifyZeroInteractions(listener);
    verify(channel, never()).enqueue(any(Log.class), anyString());
}
Also used : Context(android.content.Context) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) 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) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with LogSerializer

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

the class CrashesTest method disabledDuringHandleUserConfirmation.

@Test
public void disabledDuringHandleUserConfirmation() throws IOException, ClassNotFoundException, JSONException {
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mock(Context.class), Thread.currentThread(), new RuntimeException(), Thread.getAllStackTraces(), 0, true);
    ErrorReport errorReport1 = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
    ErrorReport errorReport2 = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, EXCEPTION);
    File errorStorageDirectory = mock(File.class);
    when(errorStorageDirectory.listFiles()).thenReturn(new File[0]);
    CrashesListener listener = mock(CrashesListener.class);
    when(listener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog).thenReturn(errorLog);
    Channel channel = mock(Channel.class);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Crashes.setEnabled(false);
            return null;
        }
    }).when(channel).enqueue(any(Log.class), anyString());
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) });
    when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport1);
    when(ErrorLogHelper.getErrorReportFromErrorLog(errorLog, EXCEPTION)).thenReturn(errorReport2);
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(listener);
    crashes.onStarted(mock(Context.class), "", channel);
    verify(mMockLooper).quit();
    verify(listener, times(2)).shouldProcess(any(ErrorReport.class));
    verify(listener).shouldAwaitUserConfirmation();
    verify(channel).enqueue(any(Log.class), anyString());
    verify(listener).getErrorAttachments(any(ErrorReport.class));
    verifyNoMoreInteractions(listener);
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) 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) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.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 8 with LogSerializer

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

the class CrashesTest method handleUserConfirmationAlwaysSend.

@Test
public void handleUserConfirmationAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
    /* Simulate the method is called from Worker Thread. */
    mockStatic(Looper.class);
    when(Looper.myLooper()).thenReturn(mock(Looper.class));
    when(Looper.getMainLooper()).thenReturn(null);
    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);
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(null);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(any(ErrorReport.class))).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.ALWAYS_SEND);
    verify(mMockLooper).quit();
    verifyStatic();
    StorageHelper.PreferencesStorage.putBoolean(Crashes.PREF_KEY_ALWAYS_SEND, true);
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) Looper(android.os.Looper) 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 9 with LogSerializer

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

the class UncaughtExceptionHandlerTest method testInvalidJsonException.

@Test
public void testInvalidJsonException() throws JSONException {
    mExceptionHandler.register();
    LogSerializer logSerializer = mock(LogSerializer.class);
    final JSONException jsonException = new JSONException("Fake JSON serializing exception");
    when(logSerializer.serializeLog(any(Log.class))).thenThrow(jsonException);
    Whitebox.setInternalState(Crashes.getInstance(), "mLogSerializer", logSerializer);
    final Thread thread = Thread.currentThread();
    final RuntimeException exception = new RuntimeException();
    mExceptionHandler.uncaughtException(thread, exception);
    verifyStatic();
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
    verify(mDefaultExceptionHandler).uncaughtException(thread, exception);
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with LogSerializer

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

the class CrashesTest method noQueueNullLog.

@Test
public void noQueueNullLog() throws JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(null);
    crashes.setLogSerializer(logSerializer);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockChannel, never()).enqueue(any(Log.class), anyString());
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) 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) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)30 Test (org.junit.Test)30 Log (com.microsoft.azure.mobile.ingestion.models.Log)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Context (android.content.Context)14 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)13 File (java.io.File)13 Channel (com.microsoft.azure.mobile.channel.Channel)12 UUID (java.util.UUID)12 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)11 ArrayList (java.util.ArrayList)11 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)9 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)4 Matchers.anyString (org.mockito.Matchers.anyString)4