Search in sources :

Example 1 with PersistenceException

use of com.microsoft.appcenter.persistence.Persistence.PersistenceException 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 2 with PersistenceException

use of com.microsoft.appcenter.persistence.Persistence.PersistenceException in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method putLargeLogNotSupportedOnCommonSchema.

@Test
public void putLargeLogNotSupportedOnCommonSchema() throws JSONException {
    /* 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. */
        CommonSchemaLog log = new MockCommonSchemaLog();
        int size = 2 * 1024 * 1024;
        StringBuilder largeValue = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            largeValue.append("x");
        }
        log.setVer("3.0");
        log.setName("test");
        log.setTimestamp(new Date());
        log.addTransmissionTarget("token");
        Data data = new Data();
        log.setData(data);
        data.getProperties().put("key", largeValue.toString());
        /* Persisting that log should fail. */
        try {
            persistence.putLog(log, "test-p1", NORMAL);
            fail("Inserting large common schema log is not supposed to work");
        } catch (PersistenceException e) {
            /* Count logs is still 0 */
            e.printStackTrace();
            assertEquals(0, persistence.countLogs("test-p1"));
        }
    } finally {
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) PersistenceException(com.microsoft.appcenter.persistence.Persistence.PersistenceException) Data(com.microsoft.appcenter.ingestion.models.one.Data) 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) Date(java.util.Date) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with PersistenceException

use of com.microsoft.appcenter.persistence.Persistence.PersistenceException in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method putCriticalLogCloseToMaxSizeClearsEverything.

@Test
public void putCriticalLogCloseToMaxSizeClearsEverything() 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);
        }
        for (int i = 0; i < someLogCount; i++) {
            persistence.putLog(AndroidTestUtils.generateMockLog(), "test-p1", CRITICAL);
        }
        assertEquals(someLogCount * 2, 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", CRITICAL);
            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 4 with PersistenceException

use of com.microsoft.appcenter.persistence.Persistence.PersistenceException 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 5 with PersistenceException

use of com.microsoft.appcenter.persistence.Persistence.PersistenceException in project mobile-center-sdk-android by Microsoft.

the class DatabasePersistenceAndroidTest method putNormalLogFailsIfFullOfCritical.

@Test
public void putNormalLogFailsIfFullOfCritical() 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 {
        /* Fill storage with critical logs. */
        List<Log> expectedLogs = new ArrayList<>();
        int someLogCount = 12;
        for (int i = 0; i < someLogCount; i++) {
            MockLog log = AndroidTestUtils.generateMockLog();
            persistence.putLog(log, "test-p1", CRITICAL);
            expectedLogs.add(log);
        }
        assertEquals(someLogCount, persistence.countLogs("test-p1"));
        /* Try to insert a normal log: that will fail and no log deleted. */
        try {
            persistence.putLog(AndroidTestUtils.generateMockLog(), "test-p1", NORMAL);
            fail("Put log was supposed to fail.");
        } catch (PersistenceException ignore) {
        }
        /* Get logs from persistence: critical were kept. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", Collections.<String>emptyList(), expectedLogs.size() + 1, outputLogs);
        assertTrue(expectedLogs.size() >= persistence.countLogs("test-p1"));
        assertThat(expectedLogs, hasItems(outputLogs.toArray(new Log[0])));
    } finally {
        persistence.close();
    }
}
Also used : MockLog(com.microsoft.appcenter.ingestion.models.json.MockLog) MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) 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) PersistenceException(com.microsoft.appcenter.persistence.Persistence.PersistenceException) 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(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

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