Search in sources :

Example 11 with LogSerializer

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

the class CrashesTest method crashInLastSession.

@Test
public void crashInLastSession() throws JSONException, IOException, ClassNotFoundException {
    final int tOffset = 10;
    final long appLaunchTOffset = 100L;
    final ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setId(UUIDUtils.randomUUID());
    errorLog.setErrorThreadName(Thread.currentThread().getName());
    errorLog.setToffset(tOffset);
    errorLog.setAppLaunchTOffset(appLaunchTOffset);
    errorLog.setDevice(mock(Device.class));
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(errorLog);
    final Throwable throwable = mock(Throwable.class);
    final ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable);
    /* This callback will be called after Crashes service is initialized. */
    final ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport data) {
            assertNotNull(data);
            assertEquals(errorReport, data);
        }
    };
    mockStatic(ErrorLogHelper.class);
    File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(errorStorageDirectory.newFile());
    when(ErrorLogHelper.getErrorReportFromErrorLog(errorLog, throwable)).thenReturn(errorReport);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            /* Call twice for multiple listeners during initialize. */
            Crashes.getLastSessionCrashReport(callback);
            Crashes.getLastSessionCrashReport(callback);
            return "";
        }
    });
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(throwable);
    Crashes.getInstance().setLogSerializer(logSerializer);
    assertFalse(Crashes.hasCrashedInLastSession());
    /*
         * 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.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
    assertTrue(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport errorReport) {
            assertNotNull(errorReport);
            assertEquals(errorLog.getId().toString(), errorReport.getId());
            assertEquals(errorLog.getErrorThreadName(), errorReport.getThreadName());
            assertEquals(new Date(tOffset - appLaunchTOffset), errorReport.getAppStartTime());
            assertEquals(new Date(tOffset), errorReport.getAppErrorTime());
            assertNotNull(errorReport.getDevice());
            assertEquals(throwable, errorReport.getThrowable());
        }
    });
}
Also used : Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) Device(com.microsoft.azure.mobile.ingestion.models.Device) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with LogSerializer

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

the class CrashesTest method printErrorOnJSONException.

@Test
public void printErrorOnJSONException() throws JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    JSONException jsonException = new JSONException("Fake JSON exception");
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
    crashes.setLogSerializer(logSerializer);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockChannel, never()).enqueue(any(Log.class), anyString());
    verifyStatic();
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with LogSerializer

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

the class ErrorModelTest method managedErrorLog.

@Test
public void managedErrorLog() throws JSONException {
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(ManagedErrorLog.TYPE, ManagedErrorLogFactory.getInstance());
    ManagedErrorLog errorLog1 = new ManagedErrorLog();
    ManagedErrorLog errorLog2 = new ManagedErrorLog();
    compareSelfNullClass(errorLog1);
    checkEquals(errorLog1, errorLog2);
    {
        errorLog1.setId(UUID.randomUUID());
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setId(UUID.randomUUID());
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setId(errorLog1.getId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setProcessId(1);
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setProcessId(2);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setProcessId(errorLog1.getProcessId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setProcessName("1");
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setProcessName("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setProcessName(errorLog1.getProcessName());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setParentProcessId(1);
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setParentProcessId(2);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setParentProcessId(errorLog1.getParentProcessId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setParentProcessName("1");
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setParentProcessName("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setParentProcessName(errorLog1.getParentProcessName());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setErrorThreadId(1L);
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setErrorThreadId(2L);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setErrorThreadId(errorLog1.getErrorThreadId());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setErrorThreadName("1");
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setErrorThreadName("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setErrorThreadName(errorLog1.getErrorThreadName());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setFatal(true);
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setFatal(false);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setFatal(errorLog1.getFatal());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setAppLaunchTOffset(1L);
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setAppLaunchTOffset(2L);
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setAppLaunchTOffset(errorLog1.getAppLaunchTOffset());
        checkEquals(errorLog1, errorLog2);
    }
    {
        errorLog1.setArchitecture("1");
        checkNotEquals(errorLog1, errorLog2);
        checkSerialization(errorLog1, serializer);
        errorLog2.setArchitecture("2");
        checkNotEquals(errorLog1, errorLog2);
        errorLog2.setArchitecture(errorLog1.getArchitecture());
        checkEquals(errorLog1, errorLog2);
    }
    {
        Exception exception1 = new Exception();
        Exception exception2 = new Exception();
        compareSelfNullClass(exception1);
        checkEquals(exception1, exception2);
        {
            exception1.setType("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setType("2");
            checkNotEquals(exception1, exception2);
            exception2.setType(exception1.getType());
            checkEquals(exception1, exception2);
        }
        {
            exception1.setMessage("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setMessage("2");
            checkNotEquals(exception1, exception2);
            exception2.setMessage(exception1.getMessage());
            checkEquals(exception1, exception2);
        }
        {
            exception1.setStackTrace("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setStackTrace("2");
            checkNotEquals(exception1, exception2);
            exception2.setStackTrace(exception1.getStackTrace());
            checkEquals(exception1, exception2);
        }
        {
            errorLog1.setException(exception1);
            errorLog2.setException(exception2);
            StackFrame frame1 = new StackFrame();
            StackFrame frame2 = new StackFrame();
            compareSelfNullClass(frame1);
            checkEquals(frame1, frame2);
            {
                frame1.setClassName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setClassName("2");
                checkNotEquals(frame1, frame2);
                frame2.setClassName(frame1.getClassName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setMethodName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setMethodName("2");
                checkNotEquals(frame1, frame2);
                frame2.setMethodName(frame1.getMethodName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setLineNumber(1);
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setLineNumber(2);
                checkNotEquals(frame1, frame2);
                frame2.setLineNumber(frame1.getLineNumber());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setFileName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, exception1, exception2, frame1, frame2);
                frame2.setFileName("2");
                checkNotEquals(frame1, frame2);
                frame2.setFileName(frame1.getFileName());
                checkEquals(frame1, frame2);
            }
        }
        {
            exception1.setWrapperSdkName("1");
            checkNotEquals(exception1, exception2);
            checkExceptions(serializer, errorLog1, errorLog2, exception1, exception2);
            exception2.setWrapperSdkName("2");
            checkNotEquals(exception1, exception2);
            exception2.setWrapperSdkName(exception1.getWrapperSdkName());
            checkEquals(exception1, exception2);
        }
    }
    {
        Thread thread1 = new Thread();
        Thread thread2 = new Thread();
        compareSelfNullClass(thread1);
        checkEquals(thread1, thread2);
        {
            thread1.setId(1L);
            checkNotEquals(thread1, thread2);
            checkThreads(serializer, errorLog1, errorLog2, thread1, thread2);
            thread2.setId(2L);
            checkNotEquals(thread1, thread2);
            thread2.setId(thread1.getId());
            checkEquals(thread1, thread2);
        }
        {
            thread1.setName("1");
            checkNotEquals(thread1, thread2);
            checkThreads(serializer, errorLog1, errorLog2, thread1, thread2);
            thread2.setName("2");
            checkNotEquals(thread1, thread2);
            thread2.setName(thread1.getName());
            checkEquals(thread1, thread2);
        }
        {
            errorLog1.setThreads(singletonList(thread1));
            errorLog2.setThreads(singletonList(thread2));
            StackFrame frame1 = new StackFrame();
            StackFrame frame2 = new StackFrame();
            compareSelfNullClass(frame1);
            checkEquals(frame1, frame2);
            {
                frame1.setClassName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setClassName("2");
                checkNotEquals(frame1, frame2);
                frame2.setClassName(frame1.getClassName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setMethodName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setMethodName("2");
                checkNotEquals(frame1, frame2);
                frame2.setMethodName(frame1.getMethodName());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setLineNumber(1);
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setLineNumber(2);
                checkNotEquals(frame1, frame2);
                frame2.setLineNumber(frame1.getLineNumber());
                checkEquals(frame1, frame2);
            }
            {
                frame1.setFileName("1");
                checkNotEquals(frame1, frame2);
                checkFrames(serializer, errorLog1, errorLog2, thread1, thread2, frame1, frame2);
                frame2.setFileName("2");
                checkNotEquals(frame1, frame2);
                frame2.setFileName(frame1.getFileName());
                checkEquals(frame1, frame2);
            }
        }
    }
    checkSerialization(errorLog1, serializer);
}
Also used : DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 14 with LogSerializer

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

the class ErrorModelTest method errorAttachmentLog.

@Test
public void errorAttachmentLog() throws JSONException {
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(ErrorAttachmentLog.TYPE, ErrorAttachmentLogFactory.getInstance());
    ErrorAttachmentLog attachmentLog1 = new ErrorAttachmentLog();
    ErrorAttachmentLog attachmentLog2 = new ErrorAttachmentLog();
    compareSelfNullClass(attachmentLog1);
    checkEquals(attachmentLog1, attachmentLog2);
    checkEquals(attachmentLog1.getType(), ErrorAttachmentLog.TYPE);
    {
        attachmentLog1.setId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setId(attachmentLog1.getId());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setErrorId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setErrorId(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setErrorId(attachmentLog1.getErrorId());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setContentType("1");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setContentType("2");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setContentType(attachmentLog1.getContentType());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setFileName("1");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setFileName("2");
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setFileName(attachmentLog1.getFileName());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setData("1".getBytes(CHARSET));
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setData("2".getBytes(CHARSET));
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setData(attachmentLog1.getData());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        attachmentLog1.setSid(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setSid(UUID.randomUUID());
        checkNotEquals(attachmentLog1, attachmentLog2);
        attachmentLog2.setSid(attachmentLog1.getSid());
        checkEquals(attachmentLog1, attachmentLog2);
    }
    {
        /* Check serialization without filename. */
        attachmentLog2.setFileName(null);
    }
    {
        checkSerialization(attachmentLog1, serializer);
        checkSerialization(attachmentLog2, serializer);
    }
}
Also used : DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 15 with LogSerializer

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

the class CrashesTest method saveWrapperSdkErrorLogJSONException.

@Test
public void saveWrapperSdkErrorLogJSONException() throws JSONException {
    mockStatic(MobileCenterLog.class);
    ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setId(UUIDUtils.randomUUID());
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.serializeLog(errorLog)).thenThrow(new JSONException("mock"));
    Crashes.getInstance().setLogSerializer(logSerializer);
    WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
    verifyStatic();
    MobileCenterLog.error(anyString(), anyString(), any(JSONException.class));
}
Also used : ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)30 Test (org.junit.Test)30 Log (com.microsoft.azure.mobile.ingestion.models.Log)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Context (android.content.Context)14 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)13 File (java.io.File)13 Channel (com.microsoft.azure.mobile.channel.Channel)12 UUID (java.util.UUID)12 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)11 ArrayList (java.util.ArrayList)11 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)9 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)4 Matchers.anyString (org.mockito.Matchers.anyString)4