Search in sources :

Example 21 with Channel

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

the class AnalyticsTest method analyticsListener.

@Test
public void analyticsListener() throws IOException, ClassNotFoundException {
    AnalyticsListener listener = mock(AnalyticsListener.class);
    Analytics.setListener(listener);
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    final ArgumentCaptor<Channel.GroupListener> captor = ArgumentCaptor.forClass(Channel.GroupListener.class);
    verify(channel).addGroup(anyString(), anyInt(), anyLong(), anyInt(), captor.capture());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            captor.getValue().onBeforeSending((Log) invocation.getArguments()[0]);
            captor.getValue().onSuccess((Log) invocation.getArguments()[0]);
            captor.getValue().onFailure((Log) invocation.getArguments()[0], new Exception());
            return null;
        }
    }).when(channel).enqueue(any(Log.class), anyString());
    Analytics.trackEvent("name");
    verify(listener).onBeforeSending(notNull(Log.class));
    verify(listener).onSendingSucceeded(notNull(Log.class));
    verify(listener).onSendingFailed(notNull(Log.class), notNull(Exception.class));
}
Also used : Context(android.content.Context) AnalyticsListener(com.microsoft.appcenter.analytics.channel.AnalyticsListener) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) Channel(com.microsoft.appcenter.channel.Channel) IOException(java.io.IOException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with Channel

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

Example 23 with Channel

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

the class CrashesTest method trackExceptionForWrapperSdk.

@Test
public void trackExceptionForWrapperSdk() {
    StackFrame frame = new StackFrame();
    frame.setClassName("1");
    frame.setFileName("2");
    frame.setLineNumber(3);
    frame.setMethodName("4");
    final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
    exception.setType("5");
    exception.setMessage("6");
    exception.setFrames(singletonList(frame));
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    WrapperSdkExceptionManager.trackException(exception);
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    WrapperSdkExceptionManager.trackException(exception);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException());
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 0;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put("valid" + i, "valid");
            }
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 5;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof HandledErrorLog) {
                HandledErrorLog errorLog = (HandledErrorLog) item;
                if (exception.equals((errorLog.getException()))) {
                    if (errorLog.getProperties().size() == 1) {
                        Map.Entry<String, String> entry = errorLog.getProperties().entrySet().iterator().next();
                        String truncatedMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH, '*');
                        return entry.getKey().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH;
                    }
                }
            }
            return false;
        }
    }), 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) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) JSONException(org.json.JSONException) NativeException(com.microsoft.appcenter.crashes.model.NativeException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) ArgumentMatcher(org.mockito.ArgumentMatcher) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with Channel

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

the class CrashesTest method queuePendingCrashesAlwaysSend.

@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    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);
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
    ErrorReport report = new ErrorReport();
    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(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).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())).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) 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) ArgumentMatcher(org.mockito.ArgumentMatcher) 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 25 with Channel

use of com.microsoft.appcenter.channel.Channel in project mobile-center-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)

Aggregations

Channel (com.microsoft.appcenter.channel.Channel)35 Context (android.content.Context)33 Test (org.junit.Test)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 SessionContext (com.microsoft.appcenter.SessionContext)14 Log (com.microsoft.appcenter.ingestion.models.Log)14 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)14 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)13 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)11 File (java.io.File)11 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)10 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)9 ArgumentMatcher (org.mockito.ArgumentMatcher)9 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)8 Matchers.anyString (org.mockito.Matchers.anyString)8 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)7 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)7 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 HashMap (java.util.HashMap)6 UUID (java.util.UUID)6