use of com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog 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());
}
use of com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method trackException.
@Test
public void trackException() {
/* Track exception test. */
Crashes crashes = Crashes.getInstance();
Channel mockChannel = mock(Channel.class);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), "", mockChannel);
Crashes.trackException(EXCEPTION);
verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
return item instanceof HandledErrorLog && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage());
}
}), eq(crashes.getGroupName()));
reset(mockChannel);
Crashes.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.getMessage().equals(((HandledErrorLog) item).getException().getMessage()) && ((HandledErrorLog) item).getProperties().size() == 0;
}
}), eq(crashes.getGroupName()));
reset(mockChannel);
Crashes.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.getMessage().equals(((HandledErrorLog) item).getException().getMessage()) && ((HandledErrorLog) item).getProperties().size() == 5;
}
}), eq(crashes.getGroupName()));
reset(mockChannel);
final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
Crashes.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.getMessage().equals((errorLog.getException().getMessage()))) {
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()));
HandledErrorLog mockLog = mock(HandledErrorLog.class);
CrashesListener mockListener = mock(CrashesListener.class);
crashes.setInstanceListener(mockListener);
/* Crashes callback test for trackException. */
crashes.getChannelListener().onBeforeSending(mockLog);
verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
crashes.getChannelListener().onSuccess(mockLog);
verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
crashes.getChannelListener().onFailure(mockLog, EXCEPTION);
verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
crashes.getChannelListener().onBeforeSending(attachmentLog);
verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
crashes.getChannelListener().onSuccess(attachmentLog);
verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
crashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
use of com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method sendMoreThan2ErrorAttachments.
@Test
public void sendMoreThan2ErrorAttachments() throws IOException, ClassNotFoundException, JSONException {
int MAX_ATTACHMENT_PER_CRASH = 2;
int numOfAttachments = MAX_ATTACHMENT_PER_CRASH + 1;
ArrayList<ErrorAttachmentLog> errorAttachmentLogs = new ArrayList<>(3);
for (int i = 0; i < numOfAttachments; ++i) {
ErrorAttachmentLog log = mock(ErrorAttachmentLog.class);
when(log.isValid()).thenReturn(true);
errorAttachmentLogs.add(log);
}
CrashesListener listener = mock(CrashesListener.class);
when(listener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
when(listener.getErrorAttachments(any(ErrorReport.class))).thenReturn(errorAttachmentLogs);
ManagedErrorLog log = mock(ManagedErrorLog.class);
when(log.getId()).thenReturn(UUID.randomUUID());
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(log);
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(new ErrorReport());
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(mock(Throwable.class));
Crashes crashes = Crashes.getInstance();
crashes.setInstanceListener(listener);
crashes.setLogSerializer(logSerializer);
crashes.onStarting(mAppCenterHandler);
crashes.onStarted(mock(Context.class), "", mock(Channel.class));
String expectedMessage = "A limit of " + MAX_ATTACHMENT_PER_CRASH + " attachments per error report might be enforced by server.";
PowerMockito.verifyStatic();
AppCenterLog.warn(Crashes.LOG_TAG, expectedMessage);
}
Aggregations