Search in sources :

Example 1 with MockCommonSchemaLogFactory

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

the class DatabasePersistenceAndroidTest method upgradeFromVersion5to6.

@Test
public void upgradeFromVersion5to6() throws PersistenceException, JSONException {
    /* Initialize database persistence with old version. */
    ContentValues schema = new ContentValues(SCHEMA);
    DatabaseManager databaseManager = new DatabaseManager(sContext, DatabasePersistence.DATABASE, DatabasePersistence.TABLE, DatabasePersistence.VERSION_TIMESTAMP_COLUMN, schema, CREATE_LOGS_SQL, mock(DatabaseManager.Listener.class));
    /* Init log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory());
    /* Insert old data before upgrade. */
    Log oldLog = AndroidTestUtils.generateMockLog();
    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabasePersistence.COLUMN_GROUP, "test");
        contentValues.put(DatabasePersistence.COLUMN_LOG, logSerializer.serializeLog(oldLog));
        databaseManager.put(contentValues, DatabasePersistence.COLUMN_PRIORITY);
    } finally {
        databaseManager.close();
    }
    /* Upgrade. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    persistence.setLogSerializer(logSerializer);
    /* Prepare a common schema log. */
    MockCommonSchemaLog commonSchemaLog = new MockCommonSchemaLog();
    commonSchemaLog.setName("test");
    commonSchemaLog.setIKey("o:test");
    commonSchemaLog.setTimestamp(new Date());
    commonSchemaLog.setVer("3.0");
    commonSchemaLog.addTransmissionTarget("test-guid");
    /* Check upgrade. */
    try {
        /* Verify old data cleared. */
        assertEquals(0, persistence.countLogs("test"));
        /* Put new data. */
        persistence.putLog(commonSchemaLog, "test/one", NORMAL);
    } finally {
        persistence.close();
    }
    /* Get new data after restart. */
    persistence = new DatabasePersistence(sContext);
    persistence.setLogSerializer(logSerializer);
    try {
        /* Get new data. */
        assertEquals(1, persistence.countLogs("test/one"));
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test/one", Collections.<String>emptyList(), 1, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(commonSchemaLog, outputLogs.get(0));
        /* Verify target token is encrypted. */
        ContentValues values = getContentValues(persistence, "test/one");
        String token = values.getAsString(DatabasePersistence.COLUMN_TARGET_TOKEN);
        assertNotNull(token);
        assertNotEquals("test-guid", token);
        assertEquals("test-guid", CryptoUtils.getInstance(sContext).decrypt(token).getDecryptedData());
        /* Verify target key stored as well. */
        String targetKey = values.getAsString(DatabasePersistence.COLUMN_TARGET_KEY);
        assertEquals(commonSchemaLog.getIKey(), "o:" + targetKey);
        /* Verify priority stored too. */
        assertEquals((Integer) NORMAL, values.getAsInteger(DatabasePersistence.COLUMN_PRIORITY));
    } finally {
        persistence.close();
    }
}
Also used : ContentValues(android.content.ContentValues) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) 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) MockCommonSchemaLogFactory(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory) 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) Date(java.util.Date) MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 2 with MockCommonSchemaLogFactory

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

the class DatabasePersistenceAndroidTest method getLogsFilteringOutPausedTargetKeys.

@Test
public void getLogsFilteringOutPausedTargetKeys() throws PersistenceException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogsPerKey = 10;
        /* Generate and persist some logs with a first iKey. */
        String pausedKey1 = "1";
        generateCsLogsWithIKey(persistence, pausedKey1, numberOfLogsPerKey);
        /* Generate more logs with another iKey to exclude. */
        String pausedKey2 = "2";
        generateCsLogsWithIKey(persistence, pausedKey2, numberOfLogsPerKey);
        /* Generate logs from a third key. */
        String resumedKey = "3";
        generateCsLogsWithIKey(persistence, resumedKey, numberOfLogsPerKey);
        /* Get logs without disabled keys. */
        List<Log> outLogs = new ArrayList<>();
        int limit = numberOfLogsPerKey * 3;
        String batchId = persistence.getLogs("test", Arrays.asList(pausedKey1, pausedKey2), limit, outLogs);
        assertNotNull(batchId);
        /* Verify we get a subset of logs without the disabled keys. */
        assertEquals(numberOfLogsPerKey, outLogs.size());
        assertEquals(limit, persistence.countLogs("test"));
        for (Log log : outLogs) {
            assertTrue(log instanceof CommonSchemaLog);
            assertEquals(resumedKey, ((CommonSchemaLog) log).getIKey());
        }
        /* Calling a second time should return nothing since the batch is in progress. */
        outLogs.clear();
        batchId = persistence.getLogs("test", Arrays.asList(pausedKey1, pausedKey2), limit, outLogs);
        assertNull(batchId);
        assertEquals(0, outLogs.size());
        /* If we try to get a second batch without filtering, we should get all disabled logs. */
        outLogs.clear();
        batchId = persistence.getLogs("test", Collections.<String>emptyList(), limit, outLogs);
        assertNotNull(batchId);
        assertEquals(numberOfLogsPerKey * 2, outLogs.size());
        for (Log log : outLogs) {
            assertTrue(log instanceof CommonSchemaLog);
            assertNotEquals(resumedKey, ((CommonSchemaLog) log).getIKey());
        }
    } finally {
        persistence.close();
    }
}
Also used : 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) MockCommonSchemaLogFactory(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) 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) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

MediumTest (androidx.test.filters.MediumTest)2 Log (com.microsoft.appcenter.ingestion.models.Log)2 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)2 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)2 MockLog (com.microsoft.appcenter.ingestion.models.json.MockLog)2 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)2 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)2 MockCommonSchemaLogFactory (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory)2 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Mockito.anyString (org.mockito.Mockito.anyString)2 SuppressLint (android.annotation.SuppressLint)1 ContentValues (android.content.ContentValues)1 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)1 DatabaseManager (com.microsoft.appcenter.utils.storage.DatabaseManager)1 Date (java.util.Date)1