Search in sources :

Example 36 with LogSerializer

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

the class DistributeSerializerTest method serialize.

@Test
public void serialize() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    List<Log> logs = new ArrayList<>();
    {
        DistributionStartSessionLog log = new DistributionStartSessionLog();
        log.setTimestamp(new Date());
        logs.add(log);
    }
    expectedContainer.setLogs(logs);
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
    }
    /* Serialize and deserialize logs container. */
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(DistributionStartSessionLog.TYPE, new DistributionStartSessionLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    /* Verify that logs container successfully deserialized. */
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : DistributionStartSessionLogFactory(com.microsoft.appcenter.distribute.ingestion.models.json.DistributionStartSessionLogFactory) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) Log(com.microsoft.appcenter.ingestion.models.Log) DistributionStartSessionLog(com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog) 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) Date(java.util.Date) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 37 with LogSerializer

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

the class DatabasePersistenceAndroidTest method putLargeLogAndDeleteAll.

@Test
public void putLargeLogAndDeleteAll() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLargeLog", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Initial count is 0. */
        assertEquals(0, persistence.countLogs("test-p1"));
        /* Generate a large log and persist. */
        LogWithProperties log = AndroidTestUtils.generateMockLog();
        int size = 2 * 1024 * 1024;
        StringBuilder largeValue = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            largeValue.append("x");
        }
        Map<String, String> properties = new HashMap<>();
        properties.put("key", largeValue.toString());
        log.setProperties(properties);
        long id = persistence.putLog("test-p1", log);
        /* Count logs. */
        assertEquals(1, persistence.countLogs("test-p1"));
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", 1, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(log, outputLogs.get(0));
        assertEquals(1, persistence.countLogs("test-p1"));
        /* Verify large file. */
        File file = persistence.getLargePayloadFile(persistence.getLargePayloadGroupDirectory("test-p1"), id);
        assertNotNull(file);
        String fileLog = StorageHelper.InternalStorage.read(file);
        assertNotNull(fileLog);
        assertTrue(fileLog.length() >= size);
        /* Delete entire group. */
        persistence.deleteLogs("test-p1");
        assertEquals(0, persistence.countLogs("test-p1"));
        /* Verify file delete and also parent directory since we used group deletion. */
        assertFalse(file.exists());
        assertFalse(file.getParentFile().exists());
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) HashMap(java.util.HashMap) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) SuppressLint(android.annotation.SuppressLint) MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) File(java.io.File) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 38 with LogSerializer

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

the class DatabasePersistenceAndroidTest method deleteLogs.

@Test
public void deleteLogs() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate a log and persist. */
        Log log1 = AndroidTestUtils.generateMockLog();
        Log log2 = AndroidTestUtils.generateMockLog();
        Log log3 = AndroidTestUtils.generateMockLog();
        Log log4 = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log1);
        persistence.putLog("test-p1", log2);
        persistence.putLog("test-p2", log3);
        persistence.putLog("test-p3", log4);
        assertEquals(2, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(1, persistence.countLogs("test-p3"));
        /* Get a log from persistence. */
        List<Log> outputLogs1 = new ArrayList<>();
        List<Log> outputLogs2 = new ArrayList<>();
        List<Log> outputLogs3 = new ArrayList<>();
        String id = persistence.getLogs("test-p1", 5, outputLogs1);
        persistence.getLogs("test-p2", 5, outputLogs2);
        persistence.getLogs("test-p3", 5, outputLogs3);
        /* Verify. */
        assertNotNull(id);
        assertNotEquals("", id);
        assertEquals(2, outputLogs1.size());
        assertEquals(1, outputLogs2.size());
        assertEquals(1, outputLogs3.size());
        /* Delete. */
        persistence.deleteLogs("", id);
        /* Access DatabaseStorage directly to verify the deletions. */
        DatabaseScanner scanner1 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
        DatabaseScanner scanner2 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p2");
        DatabaseScanner scanner3 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p3");
        // noinspection TryFinallyCanBeTryWithResources
        try {
            /* Verify. */
            assertEquals(2, getIteratorSize(scanner1.iterator()));
            assertEquals(1, getIteratorSize(scanner2.iterator()));
            assertEquals(1, getIteratorSize(scanner3.iterator()));
        } finally {
            /* Close. */
            scanner1.close();
            scanner2.close();
            scanner3.close();
        }
        /* Delete. */
        persistence.deleteLogs("test-p1", id);
        /* Access DatabaseStorage directly to verify the deletions. */
        DatabaseScanner scanner4 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
        // noinspection TryFinallyCanBeTryWithResources
        try {
            /* Verify. */
            assertEquals(0, getIteratorSize(scanner4.iterator()));
        } finally {
            /* Close. */
            scanner4.close();
        }
        /* Count logs after delete. */
        assertEquals(0, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(1, persistence.countLogs("test-p3"));
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) Log(com.microsoft.appcenter.ingestion.models.Log) DatabaseScanner(com.microsoft.appcenter.utils.storage.StorageHelper.DatabaseStorage.DatabaseScanner) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 39 with LogSerializer

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

the class DatabasePersistenceAndroidTest method putLogException.

@Test(expected = PersistenceException.class)
public void putLogException() throws PersistenceException, IOException, JSONException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLogException", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = mock(LogSerializer.class);
    doThrow(new JSONException("JSON exception")).when(logSerializer).serializeLog(any(Log.class));
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate a log and persist. */
        Log log = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log);
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : Log(com.microsoft.appcenter.ingestion.models.Log) JSONException(org.json.JSONException) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 40 with LogSerializer

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

the class DatabasePersistenceAndroidTest method putLog.

@Test
public void putLog() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLog", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Initial count is 0. */
        assertEquals(0, persistence.countLogs("test-p1"));
        /* Generate a log and persist. */
        Log log = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log);
        /* Count logs. */
        assertEquals(1, persistence.countLogs("test-p1"));
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", 1, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(log, outputLogs.get(0));
        assertEquals(1, persistence.countLogs("test-p1"));
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) 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