Search in sources :

Example 1 with LogSerializer

use of com.microsoft.appcenter.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("appcenter.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<>();
    {
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setTimestamp(new Date());
        logs.add(startSessionLog);
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

            {
                put("from", "home_menu");
                put("orientation", "portrait");
            }
        });
        logs.add(pageLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("subscribe");
        logs.add(eventLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        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);
}
Also used : PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) Device(com.microsoft.appcenter.ingestion.models.Device) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Date(java.util.Date) StartSessionLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) EventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 2 with LogSerializer

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

the class ErrorModelTest method handledErrorLog.

@Test
public void handledErrorLog() throws JSONException {
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(HandledErrorLog.TYPE, HandledErrorLogFactory.getInstance());
    HandledErrorLog errorLog1 = new HandledErrorLog();
    compareSelfNullClass(errorLog1);
    Date timestamp = new Date();
    errorLog1.setTimestamp(timestamp);
    HandledErrorLog errorLog2 = new HandledErrorLog();
    checkNotEquals(errorLog1, errorLog2);
    errorLog2.setTimestamp(timestamp);
    checkEquals(errorLog1, errorLog2);
    {
        errorLog1.setId(UUID.randomUUID());
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setId(UUID.randomUUID());
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setId(errorLog1.getId());
        checkEquals(errorLog1, errorLog2);
    }
    checkSerialization(errorLog1, serializer);
    {
        Exception exception1 = new Exception();
        exception1.setMessage("1");
        Exception exception2 = new Exception();
        exception2.setMessage("2");
        errorLog1.setException(exception1);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setException(exception2);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setException(exception1);
        checkEquals(errorLog1, errorLog2);
    }
    checkSerialization(errorLog1, serializer);
}
Also used : DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Date(java.util.Date) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 3 with LogSerializer

use of com.microsoft.appcenter.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.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(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.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mock(Channel.class));
    Crashes.notifyUserConfirmation(Crashes.DONT_SEND);
    verify(mockListener, never()).getErrorAttachments(any(ErrorReport.class));
    verifyStatic();
    ErrorLogHelper.removeStoredErrorLogFile(mErrorLog.getId());
    verifyStatic();
    ErrorLogHelper.removeStoredThrowableFile(mErrorLog.getId());
}
Also used : ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.appcenter.channel.Channel) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with LogSerializer

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

the class CrashesTest method queuePendingCrashesShouldProcess.

@Test
public void queuePendingCrashesShouldProcess() throws IOException, ClassNotFoundException, JSONException {
    /* Setup mock. */
    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.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());
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    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);
    ErrorAttachmentLog mockEmptyAttachment = mock(ErrorAttachmentLog.class);
    final int skipAttachmentLogsCount = 2;
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment, mockEmptyAttachment, null);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, "", mockChannel);
    /* Test. */
    verify(mockListener).shouldProcess(report);
    verify(mockListener).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() - skipAttachmentLogsCount)).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 5 with LogSerializer

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

the class CrashesTest method noQueueNullLog.

@Test
public void noQueueNullLog() throws JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(null);
    crashes.setLogSerializer(logSerializer);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockChannel, never()).enqueue(any(Log.class), anyString());
}
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) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)44 Test (org.junit.Test)43 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 Log (com.microsoft.appcenter.ingestion.models.Log)26 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)23 File (java.io.File)22 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)19 Context (android.content.Context)16 ArrayList (java.util.ArrayList)16 JSONException (org.json.JSONException)16 UUID (java.util.UUID)15 SessionContext (com.microsoft.appcenter.SessionContext)14 Channel (com.microsoft.appcenter.channel.Channel)14 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)14 MediumTest (android.support.test.filters.MediumTest)10 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)10 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)10 IOException (java.io.IOException)9 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)8 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)8