Search in sources :

Example 11 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer 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 12 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer 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 13 with DefaultLogSerializer

use of com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer 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)

Example 14 with DefaultLogSerializer

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

the class DatabasePersistenceAndroidTest method putLargeLogFailsToRead.

@Test
public void putLargeLogFailsToRead() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLargeLogException", 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);
        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 the file. */
        assertTrue(file.delete());
        /* We won't be able to read the log now but persistence should delete the SQLite log on error. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", 1, outputLogs);
        assertEquals(0, outputLogs.size());
        assertEquals(0, persistence.countLogs("test-p1"));
    } 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 15 with DefaultLogSerializer

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

the class DatabasePersistenceAndroidTest method getLogsException.

@Test
public void getLogsException() throws PersistenceException, IOException, JSONException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = spy(new DefaultLogSerializer());
    /* Throw a JSON exception for the first call. */
    doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).when(logSerializer).deserializeLog(anyString());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogs = 4;
        /* Generate a log and persist. */
        Log[] logs = new Log[numberOfLogs];
        for (int i = 0; i < logs.length; i++) logs[i] = AndroidTestUtils.generateMockLog();
        /* Put. */
        for (Log log : logs) persistence.putLog("test", log);
        /* Get. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test", 10, outputLogs);
        assertEquals(numberOfLogs / 2, outputLogs.size());
        assertEquals(2, persistence.mDatabaseStorage.size());
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)16 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)15 Test (org.junit.Test)15 Log (com.microsoft.appcenter.ingestion.models.Log)11 ArrayList (java.util.ArrayList)11 MediumTest (android.support.test.filters.MediumTest)9 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)8 Date (java.util.Date)6 SuppressLint (android.annotation.SuppressLint)5 Mockito.anyString (org.mockito.Mockito.anyString)5 HashMap (java.util.HashMap)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)3 LogWithProperties (com.microsoft.appcenter.ingestion.models.LogWithProperties)3 UUID (java.util.UUID)3 JSONException (org.json.JSONException)3 File (java.io.File)2 WorkerThread (android.support.annotation.WorkerThread)1 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)1 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)1 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)1