Search in sources :

Example 11 with LogSerializer

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrash.

@Test
public void saveWrapperSdkCrash() throws JSONException, IOException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    byte[] data = new byte[] { 'd' };
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(data));
    /* We can't do it twice in the same process. */
    data = new byte[] { 'e' };
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic(never());
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(data));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with LogSerializer

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashWithJavaThrowable.

@Test
public void saveWrapperSdkCrashWithJavaThrowable() throws JSONException, IOException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    byte[] data = new byte[] { 'd' };
    Throwable throwable = new Throwable();
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), data);
    verifyStatic();
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(data));
    verifyStatic();
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(throwable));
    /* We can't do it twice in the same process. */
    data = new byte[] { 'e' };
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), data);
    verifyStatic(never());
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(data));
    verifyStatic();
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(throwable));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with LogSerializer

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithIOExceptionAfterLog.

@Test
public void saveWrapperSdkCrashFailsWithIOExceptionAfterLog() throws IOException, JSONException {
    byte[] data = { 'd' };
    doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(data));
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
    /* Second call is ignored. */
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[] { 'e' });
    /* No more error. */
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) StorageHelper(com.microsoft.appcenter.utils.storage.StorageHelper) IOException(java.io.IOException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with LogSerializer

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashWithOnlyJavaThrowable.

@Test
public void saveWrapperSdkCrashWithOnlyJavaThrowable() throws JSONException, IOException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    Throwable throwable = new Throwable();
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null);
    verifyStatic(never());
    StorageHelper.InternalStorage.writeObject(any(File.class), isNull(byte[].class));
    verifyStatic();
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(throwable));
    /* We can't do it twice in the same process. */
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null);
    verifyStatic();
    StorageHelper.InternalStorage.writeObject(any(File.class), eq(throwable));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with LogSerializer

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

the class PushSerializerTest method serialize.

@Test
public void serialize() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    List<Log> logs = new ArrayList<>();
    {
        PushInstallationLog log = new PushInstallationLog();
        log.setTimestamp(new Date());
        log.setPushToken("TEST");
        logs.add(log);
    }
    expectedContainer.setLogs(logs);
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
    }
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : PushInstallationLog(com.microsoft.appcenter.push.ingestion.models.PushInstallationLog) Log(com.microsoft.appcenter.ingestion.models.Log) PushInstallationLog(com.microsoft.appcenter.push.ingestion.models.PushInstallationLog) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) UUID(java.util.UUID) PushInstallationLogFactory(com.microsoft.appcenter.push.ingestion.models.json.PushInstallationLogFactory) Date(java.util.Date) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) 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