Search in sources :

Example 1 with LogWithProperties

use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project AppCenter-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());
    }
    if (message != null) {
        if (((LogWithProperties) log).getProperties() != null) {
            message += String.format("\nProperties: %s", new JSONObject(((LogWithProperties) log).getProperties()).toString());
        }
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }
    analyticsIdlingResource.decrement();
}
Also used : JSONObject(org.json.JSONObject) LogWithProperties(com.microsoft.appcenter.ingestion.models.LogWithProperties) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog)

Example 2 with LogWithProperties

use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project AppCenter-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 3 with LogWithProperties

use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project AppCenter-SDK-Android by Microsoft.

the class DatabasePersistenceAndroidTest method putLargeLogFails.

@Test
public void putLargeLogFails() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    String path = Constants.FILES_PATH;
    Constants.FILES_PATH = null;
    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);
        persistence.putLog("test-p1", log);
        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 {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        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) 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(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with LogWithProperties

use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project AppCenter-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 5 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)

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