use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer 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.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.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.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method saveWrapperSdkErrorLogIOException.
@Test
public void saveWrapperSdkErrorLogIOException() throws IOException, JSONException {
mockStatic(MobileCenterLog.class);
ManagedErrorLog errorLog = new ManagedErrorLog();
errorLog.setId(UUIDUtils.randomUUID());
mockStatic(StorageHelper.InternalStorage.class);
doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
StorageHelper.InternalStorage.write(any(File.class), anyString());
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.serializeLog(errorLog)).thenReturn("mock");
Crashes.getInstance().setLogSerializer(logSerializer);
WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
verifyStatic();
MobileCenterLog.error(anyString(), anyString(), any(IOException.class));
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class IngestionHttpTest method sendAsync.
@Test
public void sendAsync() throws Exception {
/* Build some payload. */
LogContainer container = new LogContainer();
Log log = mock(Log.class);
long logAbsoluteTime = 123L;
when(log.getToffset()).thenReturn(logAbsoluteTime);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
/* Stable time. */
mockStatic(System.class);
long now = 456L;
when(System.currentTimeMillis()).thenReturn(now);
/* Configure mock HTTP. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
/* Test calling code. */
IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
ingestionHttp.setLogUrl("http://mock");
String appSecret = UUIDUtils.randomUUID().toString();
UUID installId = UUIDUtils.randomUUID();
ServiceCallback serviceCallback = mock(ServiceCallback.class);
assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
/* Verify call to http client. */
HashMap<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.put(APP_SECRET, appSecret);
expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
verify(httpClient).callAsync(eq("http://mock" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
assertNotNull(callTemplate.get());
assertEquals("mockPayload", callTemplate.get().buildRequestBody());
/* Verify toffset manipulation. */
verify(log).setToffset(now - logAbsoluteTime);
verify(log).setToffset(logAbsoluteTime);
/* Verify close. */
ingestionHttp.close();
verify(httpClient).close();
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method handleUserConfirmationDoNotSend.
@Test
public void handleUserConfirmationDoNotSend() throws IOException, ClassNotFoundException, JSONException {
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(new ErrorReport());
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(null);
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
when(mockListener.shouldAwaitUserConfirmation()).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.DONT_SEND);
verify(mockListener, never()).getErrorAttachments(any(ErrorReport.class));
verify(mMockLooper).quit();
verifyStatic();
ErrorLogHelper.removeStoredErrorLogFile(mErrorLog.getId());
verifyStatic();
ErrorLogHelper.removeStoredThrowableFile(mErrorLog.getId());
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method crashInLastSessionError.
@Test
public void crashInLastSessionError() throws JSONException, IOException, ClassNotFoundException {
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
mockStatic(ErrorLogHelper.class);
File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
Crashes.getInstance().setLogSerializer(logSerializer);
assertFalse(Crashes.hasCrashedInLastSession());
JSONException jsonException = new JSONException("Fake JSON exception");
when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport data) {
assertNull(data);
}
};
/*
* 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.getLastSessionCrashReport(callback);
Crashes.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
assertFalse(Crashes.hasCrashedInLastSession());
Crashes.getLastSessionCrashReport(callback);
/*
* De-serializing fails twice: processing the log from last time as part of the bulk processing.
* And loading that same file for exposing it in getLastErrorReport.
*/
verifyStatic(times(2));
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Aggregations