Search in sources :

Example 11 with HandledErrorLog

use of com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog in project mobile-center-sdk-android by Microsoft.

the class HandledErrorTest method trackError.

@Test
public void trackError() {
    startCrashes();
    Crashes.trackError(EXCEPTION);
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && EXCEPTION.getMessage() != null && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage());
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    reset(mChannel);
    Crashes.trackError(EXCEPTION, new HashMap<String, String>() {

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

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && EXCEPTION.getMessage() != null && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage()) && ((HandledErrorLog) item).getProperties().size() == 0;
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    reset(mChannel);
    Crashes.trackError(EXCEPTION, new HashMap<String, String>() {

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

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && EXCEPTION.getMessage() != null && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage()) && ((HandledErrorLog) item).getProperties().size() == 20;
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    reset(mChannel);
    final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    Crashes.trackError(EXCEPTION, new HashMap<String, String>() {

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

        @Override
        public boolean matches(Object item) {
            if (item instanceof HandledErrorLog) {
                HandledErrorLog errorLog = (HandledErrorLog) item;
                if (EXCEPTION.getMessage() != null && EXCEPTION.getMessage().equals((errorLog.getException().getMessage()))) {
                    if (errorLog.getProperties().size() == 1) {
                        Map.Entry<String, String> entry = errorLog.getProperties().entrySet().iterator().next();
                        return entry.getKey().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH;
                    }
                }
            }
            return false;
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    HandledErrorLog mockLog = mock(HandledErrorLog.class);
    CrashesListener mockListener = mock(CrashesListener.class);
    mCrashes.setInstanceListener(mockListener);
    /* mCrashes callback test for trackError. */
    mCrashes.getChannelListener().onBeforeSending(mockLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    mCrashes.getChannelListener().onSuccess(mockLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    mCrashes.getChannelListener().onFailure(mockLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
    ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
    mCrashes.getChannelListener().onBeforeSending(attachmentLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    mCrashes.getChannelListener().onSuccess(attachmentLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    mCrashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)11 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)10 Test (org.junit.Test)10 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)9 Matchers.anyString (org.mockito.Matchers.anyString)9 HashMap (java.util.HashMap)8 Log (com.microsoft.appcenter.ingestion.models.Log)7 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)7 Map (java.util.Map)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 Context (android.content.Context)5 Channel (com.microsoft.appcenter.channel.Channel)5 SessionContext (com.microsoft.appcenter.SessionContext)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)3 StackFrame (com.microsoft.appcenter.crashes.ingestion.models.StackFrame)3 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)3 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)2 NativeException (com.microsoft.appcenter.crashes.model.NativeException)2 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)2