use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class AnalyticsSerializerTest method someBatch.
@Test
public void someBatch() throws JSONException {
LogContainer expectedContainer = new LogContainer();
Device device = new Device();
device.setSdkName("mobilecenter.android");
device.setSdkVersion("1.2.3");
device.setModel("S5");
device.setOemName("HTC");
device.setOsName("Android");
device.setOsVersion("4.0.3");
device.setOsBuild("LMY47X");
device.setOsApiLevel(15);
device.setLocale("en_US");
device.setTimeZoneOffset(120);
device.setScreenSize("800x600");
device.setAppVersion("3.2.1");
device.setAppBuild("42");
List<Log> logs = new ArrayList<>();
{
logs.add(new StartSessionLog());
}
expectedContainer.setLogs(logs);
{
PageLog pageLog = new PageLog();
pageLog.setName("home");
logs.add(pageLog);
}
{
PageLog pageLog = new PageLog();
pageLog.setName("settings");
pageLog.setProperties(new HashMap<String, String>() {
{
put("from", "home_menu");
put("orientation", "portrait");
}
});
logs.add(pageLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("subscribe");
logs.add(eventLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("click");
eventLog.setProperties(new HashMap<String, String>() {
{
put("x", "1");
put("y", "2");
}
});
logs.add(eventLog);
}
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
log.setDevice(device);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
android.util.Log.v(TAG, payload);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
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));
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method queuePendingCrashesShouldNotProcess.
@Test
public void queuePendingCrashesShouldNotProcess() throws IOException, ClassNotFoundException, JSONException {
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()).thenReturn(new byte[] {});
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(report)).thenReturn(false);
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, never()).getErrorAttachments(report);
verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method disabledDuringProcessPendingErrors.
@Test
public void disabledDuringProcessPendingErrors() throws IOException, ClassNotFoundException, JSONException {
ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
File errorStorageDirectory = mock(File.class);
when(errorStorageDirectory.listFiles()).thenReturn(new File[0]);
CrashesListener listener = mock(CrashesListener.class);
when(listener.shouldProcess(errorReport)).thenReturn(true);
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
Channel channel = mock(Channel.class);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class), mock(File.class) }).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory);
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport);
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
when(StorageHelper.InternalStorage.read(any(File.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Crashes.setEnabled(false);
return "";
}
});
/* Disabled while Crashes service is processing pending errors. */
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(listener);
crashes.onStarted(mock(Context.class), "", channel);
verify(channel, never()).enqueue(any(Log.class), anyString());
verify(listener).shouldProcess(errorReport);
verifyNoMoreInteractions(listener);
/* Disabled right before handling user confirmation. */
Crashes.unsetInstance();
Crashes.setEnabled(true);
crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(listener);
crashes.onStarted(mock(Context.class), "", channel);
verify(channel, never()).enqueue(any(Log.class), anyString());
verify(listener, times(2)).shouldProcess(errorReport);
verifyNoMoreInteractions(listener);
}
Aggregations