Search in sources :

Example 11 with DefaultLogSerializer

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

the class DatabasePersistenceAndroidTest method getLogs.

@Test
public void getLogs() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogs = 10;
        int sizeForGetLogs = 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. */
        getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
        /* Clear ids, we should be able to get the logs again in the same sequence. */
        persistence.clearPendingLogState();
        getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
        /* Count. */
        assertEquals(10, persistence.countLogs("test"));
        /* Clear. Nothing to get after. */
        persistence.mDatabaseStorage.clear();
        List<Log> outputLogs = new ArrayList<>();
        assertNull(persistence.getLogs("test", sizeForGetLogs, outputLogs));
        assertTrue(outputLogs.isEmpty());
        assertEquals(0, persistence.countLogs("test"));
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)10 Test (org.junit.Test)10 Log (com.microsoft.azure.mobile.ingestion.models.Log)8 ArrayList (java.util.ArrayList)8 MediumTest (android.support.test.filters.MediumTest)6 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 SuppressLint (android.annotation.SuppressLint)3 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)2 UUID (java.util.UUID)2 JSONException (org.json.JSONException)2 Mockito.anyString (org.mockito.Mockito.anyString)2 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)1 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)1 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)1 EventLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.StartSessionLogFactory)1 DefaultChannel (com.microsoft.azure.mobile.channel.DefaultChannel)1 Device (com.microsoft.azure.mobile.ingestion.models.Device)1