Search in sources :

Example 6 with LogWithProperties

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

the class DatabasePersistenceAndroidTest method putNormalLogCloseToMaxSizeClearsEverything.

@Test
public void putNormalLogCloseToMaxSizeClearsEverything() throws PersistenceException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    assertTrue(persistence.setMaxStorageSize(MAX_STORAGE_SIZE_IN_BYTES));
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate some logs of both priority that will be evicted. */
        int someLogCount = 3;
        for (int i = 0; i < someLogCount; i++) {
            persistence.putLog(AndroidTestUtils.generateMockLog(), "test-p1", NORMAL);
        }
        assertEquals(someLogCount, persistence.countLogs("test-p1"));
        /*
             * Generate a log that is so large that will empty all the database and
             * eventually fails because close to the limit we check and the overhead of columns/index
             * is larger than max size.
             */
        LogWithProperties log = AndroidTestUtils.generateMockLog();
        int size = 30 * 1024;
        Map<String, String> properties = new HashMap<>();
        properties.put("key", generateString(size, 'x'));
        log.setProperties(properties);
        try {
            persistence.putLog(log, "test-p1", NORMAL);
            fail("Expected persistence exception");
        } catch (PersistenceException ignore) {
        }
        /* Verify the behavior: not inserted and database now empty. */
        assertEquals(0, persistence.countLogs("test-p1"));
    } finally {
        persistence.close();
    }
}
Also used : LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) HashMap(java.util.HashMap) PersistenceException(com.microsoft.appcenter.persistence.Persistence.PersistenceException) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 7 with LogWithProperties

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

the class DatabasePersistenceAndroidTest method putLargeLogFails.

@Test
public void putLargeLogFails() {
    /* Initialize database persistence. */
    String path = Constants.FILES_PATH;
    Constants.FILES_PATH = null;
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    /* 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);
        persistence.putLog(log, "test-p1", NORMAL);
        fail("putLog was expected to fail");
    } catch (Persistence.PersistenceException e) {
        assertTrue(e.getCause() instanceof IOException);
        /* Make sure database entry has been removed. */
        assertEquals(0, persistence.countLogs("test-p1"));
    } finally {
        persistence.close();
        /* Restore path. */
        Constants.FILES_PATH = path;
    }
}
Also used : LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) HashMap(java.util.HashMap) Mockito.anyString(org.mockito.Mockito.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) PersistenceException(com.microsoft.appcenter.persistence.Persistence.PersistenceException) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 8 with LogWithProperties

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

the class SasquatchAnalyticsListener method onSendingSucceeded.

@Override
public void onSendingSucceeded(com.microsoft.appcenter.ingestion.models.Log log) {
    String message = null;
    if (log instanceof EventLog) {
        message = String.format("%s\nName: %s", mContext.getString(R.string.event_sent_succeeded), ((EventLog) log).getName());
    } else if (log instanceof PageLog) {
        message = String.format("%s\nName: %s", mContext.getString(R.string.page_sent_succeeded), ((PageLog) log).getName());
    } else if (log instanceof CommonSchemaLog) {
        CommonSchemaLog commonSchemaLog = (CommonSchemaLog) log;
        message = String.format("%s\nName: %s", mContext.getString(R.string.event_sent_succeeded), commonSchemaLog.getName());
        if (commonSchemaLog.getData() != null) {
            message += String.format("\nProperties: %s", commonSchemaLog.getData().getProperties().toString());
        }
    }
    if (log instanceof LogWithProperties) {
        if (((LogWithProperties) log).getProperties() != null) {
            message += String.format("\nProperties: %s", new JSONObject(((LogWithProperties) log).getProperties()).toString());
        }
    }
    if (message != null) {
        notifySendingCompletion(message);
    }
    analyticsIdlingResource.decrement();
}
Also used : LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) JSONObject(org.json.JSONObject) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)

Example 9 with LogWithProperties

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

the class DatabasePersistenceAndroidTest method putLargeLogFailsToRead.

@Test
public void putLargeLogFailsToRead() throws PersistenceException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    /* 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(log, "test-p1", NORMAL);
        assertEquals(1, persistence.countLogs("test-p1"));
        /* Verify large file. */
        File file = persistence.getLargePayloadFile(persistence.getLargePayloadGroupDirectory("test-p1"), id);
        assertNotNull(file);
        String fileLog = FileManager.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", Collections.<String>emptyList(), 1, outputLogs);
        assertEquals(0, outputLogs.size());
        assertEquals(0, persistence.countLogs("test-p1"));
    } finally {
        persistence.close();
    }
}
Also used : LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) HashMap(java.util.HashMap) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) MockLog(com.microsoft.appcenter.ingestion.models.json.MockLog) 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) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) 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(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 10 with LogWithProperties

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

the class DatabasePersistenceAndroidTest method putLargeLogAndDeleteAll.

@Test
public void putLargeLogAndDeleteAll() throws PersistenceException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    /* 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(log, "test-p1", NORMAL);
        /* Count logs. */
        assertEquals(1, persistence.countLogs("test-p1"));
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", Collections.<String>emptyList(), 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 = FileManager.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());
        assertNotNull(file.getParentFile());
        assertFalse(file.getParentFile().exists());
    } finally {
        persistence.close();
    }
}
Also used : LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) HashMap(java.util.HashMap) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) MockLog(com.microsoft.appcenter.ingestion.models.json.MockLog) 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) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) 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(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

LogWithProperties (com.microsoft.appcenter.ingestion.models.LogWithProperties)12 SuppressLint (android.annotation.SuppressLint)10 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)10 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)10 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)10 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 Mockito.anyString (org.mockito.Mockito.anyString)10 MediumTest (androidx.test.filters.MediumTest)7 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)7 PersistenceException (com.microsoft.appcenter.persistence.Persistence.PersistenceException)6 Log (com.microsoft.appcenter.ingestion.models.Log)5 ArrayList (java.util.ArrayList)5 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)4 File (java.io.File)4 MediumTest (android.support.test.filters.MediumTest)3 MockLog (com.microsoft.appcenter.ingestion.models.json.MockLog)3 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)3 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)2 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)2