Search in sources :

Example 11 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method queuePendingCrashesShouldProcess.

@Test
public void queuePendingCrashesShouldProcess() throws IOException, ClassNotFoundException, JSONException {
    /* Setup mock. */
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorReport report = new ErrorReport();
    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(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    ErrorAttachmentLog mockEmptyAttachment = mock(ErrorAttachmentLog.class);
    final int skipAttachmentLogsCount = 2;
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment, mockEmptyAttachment, null);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mockContext, "", mockChannel);
    /* Test. */
    verify(mockListener).shouldProcess(report);
    verify(mockListener).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()));
    verify(mockChannel, times(errorAttachmentLogList.size() - skipAttachmentLogsCount)).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) 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) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Channel

use of com.microsoft.azure.mobile.channel.Channel 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 13 with Channel

use of com.microsoft.azure.mobile.channel.Channel 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 14 with Channel

use of com.microsoft.azure.mobile.channel.Channel 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)

Example 15 with Channel

use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method crashInLastSession.

@Test
public void crashInLastSession() throws JSONException, IOException, ClassNotFoundException {
    final int tOffset = 10;
    final long appLaunchTOffset = 100L;
    final ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setId(UUIDUtils.randomUUID());
    errorLog.setErrorThreadName(Thread.currentThread().getName());
    errorLog.setToffset(tOffset);
    errorLog.setAppLaunchTOffset(appLaunchTOffset);
    errorLog.setDevice(mock(Device.class));
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(errorLog);
    final Throwable throwable = mock(Throwable.class);
    final ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
    /* This callback will be called after Crashes service is initialized. */
    final ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport data) {
            assertNotNull(data);
            assertEquals(errorReport, data);
        }
    };
    mockStatic(ErrorLogHelper.class);
    File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(errorStorageDirectory.newFile());
    when(ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable)).thenReturn(errorReport);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            /* Call twice for multiple listeners during initialize. */
            Crashes.getLastSessionCrashReport(callback);
            Crashes.getLastSessionCrashReport(callback);
            return "";
        }
    });
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(throwable);
    Crashes.getInstance().setLogSerializer(logSerializer);
    assertFalse(Crashes.hasCrashedInLastSession());
    /*
         * 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.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
    assertTrue(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport errorReport) {
            assertNotNull(errorReport);
            assertEquals(errorLog.getId().toString(), errorReport.getId());
            assertEquals(errorLog.getErrorThreadName(), errorReport.getThreadName());
            assertEquals(new Date(tOffset - appLaunchTOffset), errorReport.getAppStartTime());
            assertEquals(new Date(tOffset), errorReport.getAppErrorTime());
            assertNotNull(errorReport.getDevice());
            assertEquals(throwable, errorReport.getThrowable());
        }
    });
}
Also used : Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) Device(com.microsoft.azure.mobile.ingestion.models.Device) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) 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)

Aggregations

Channel (com.microsoft.azure.mobile.channel.Channel)36 Test (org.junit.Test)35 Context (android.content.Context)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Log (com.microsoft.azure.mobile.ingestion.models.Log)14 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)14 File (java.io.File)11 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)10 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)10 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)10 ArgumentMatcher (org.mockito.ArgumentMatcher)10 Matchers.anyString (org.mockito.Matchers.anyString)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)7 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)7 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)6 UUID (java.util.UUID)6 Answer (org.mockito.stubbing.Answer)4 TestUtils.generateString (com.microsoft.azure.mobile.test.TestUtils.generateString)3