Search in sources :

Example 1 with MockLogFactory

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

the class DatabasePersistenceAndroidTest method deleteLogsForGroup.

@Test
public void deleteLogsForGroup() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogsForGroup", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate a log and persist. */
        Log log1 = AndroidTestUtils.generateMockLog();
        Log log2 = AndroidTestUtils.generateMockLog();
        Log log3 = AndroidTestUtils.generateMockLog();
        Log log4 = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log1);
        persistence.putLog("test-p1", log2);
        persistence.putLog("test-p2", log3);
        persistence.putLog("test-p3", log4);
        /* Get a log from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        String id1 = persistence.getLogs("test-p1", 5, outputLogs);
        String id2 = persistence.getLogs("test-p2", 5, outputLogs);
        assertNotNull(id1);
        assertNotNull(id2);
        /* Delete. */
        persistence.deleteLogs("test-p1");
        persistence.deleteLogs("test-p3");
        /* Try another get for verification. */
        outputLogs.clear();
        persistence.getLogs("test-p3", 5, outputLogs);
        /* Verify. */
        Map<String, List<Long>> pendingGroups = persistence.mPendingDbIdentifiersGroups;
        assertNull(pendingGroups.get("test-p1" + id1));
        assertEquals(1, pendingGroups.get("test-p2" + id2).size());
        assertEquals(1, pendingGroups.size());
        assertEquals(0, outputLogs.size());
        assertEquals(1, persistence.mDatabaseStorage.size());
        /* Verify one log still persists in the database. */
        persistence.clearPendingLogState();
        outputLogs.clear();
        persistence.getLogs("test-p2", 5, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(log3, outputLogs.get(0));
        /* Count for groups. */
        assertEquals(0, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(0, persistence.countLogs("test-p3"));
    } 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) ArrayList(java.util.ArrayList) List(java.util.List) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 2 with MockLogFactory

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

the class DatabasePersistenceAndroidTest method deleteLogs.

@Test
public void deleteLogs() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate a log and persist. */
        Log log1 = AndroidTestUtils.generateMockLog();
        Log log2 = AndroidTestUtils.generateMockLog();
        Log log3 = AndroidTestUtils.generateMockLog();
        Log log4 = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log1);
        persistence.putLog("test-p1", log2);
        persistence.putLog("test-p2", log3);
        persistence.putLog("test-p3", log4);
        assertEquals(2, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(1, persistence.countLogs("test-p3"));
        /* Get a log from persistence. */
        List<Log> outputLogs1 = new ArrayList<>();
        List<Log> outputLogs2 = new ArrayList<>();
        List<Log> outputLogs3 = new ArrayList<>();
        String id = persistence.getLogs("test-p1", 5, outputLogs1);
        persistence.getLogs("test-p2", 5, outputLogs2);
        persistence.getLogs("test-p3", 5, outputLogs3);
        /* Verify. */
        assertNotNull(id);
        assertNotEquals("", id);
        assertEquals(2, outputLogs1.size());
        assertEquals(1, outputLogs2.size());
        assertEquals(1, outputLogs3.size());
        /* Delete. */
        persistence.deleteLogs("", id);
        /* Access DatabaseStorage directly to verify the deletions. */
        DatabaseScanner scanner1 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
        DatabaseScanner scanner2 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p2");
        DatabaseScanner scanner3 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p3");
        //noinspection TryFinallyCanBeTryWithResources
        try {
            /* Verify. */
            assertEquals(2, getIteratorSize(scanner1.iterator()));
            assertEquals(1, getIteratorSize(scanner2.iterator()));
            assertEquals(1, getIteratorSize(scanner3.iterator()));
        } finally {
            /* Close. */
            scanner1.close();
            scanner2.close();
            scanner3.close();
        }
        /* Delete. */
        persistence.deleteLogs("test-p1", id);
        /* Access DatabaseStorage directly to verify the deletions. */
        DatabaseScanner scanner4 = persistence.mDatabaseStorage.getScanner(DatabasePersistence.COLUMN_GROUP, "test-p1");
        //noinspection TryFinallyCanBeTryWithResources
        try {
            /* Verify. */
            assertEquals(0, getIteratorSize(scanner4.iterator()));
        } finally {
            /* Close. */
            scanner4.close();
        }
        /* Count logs after delete. */
        assertEquals(0, persistence.countLogs("test-p1"));
        assertEquals(1, persistence.countLogs("test-p2"));
        assertEquals(1, persistence.countLogs("test-p3"));
    } 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) DatabaseScanner(com.microsoft.azure.mobile.utils.storage.StorageHelper.DatabaseStorage.DatabaseScanner) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with MockLogFactory

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

the class DatabasePersistenceAndroidTest method putLog.

@Test
public void putLog() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLog", 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 log and persist. */
        Log log = AndroidTestUtils.generateMockLog();
        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"));
    } 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) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with MockLogFactory

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

the class DatabasePersistenceAndroidTest method putTooManyLogs.

@Test
public void putTooManyLogs() throws PersistenceException, IOException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putTooManyLogs", 1, 2);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Generate too many logs and persist. */
        Log log1 = AndroidTestUtils.generateMockLog();
        Log log2 = AndroidTestUtils.generateMockLog();
        Log log3 = AndroidTestUtils.generateMockLog();
        Log log4 = AndroidTestUtils.generateMockLog();
        persistence.putLog("test-p1", log1);
        persistence.putLog("test-p1", log2);
        persistence.putLog("test-p1", log3);
        persistence.putLog("test-p1", log4);
        /* Get logs from persistence. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test-p1", 4, outputLogs);
        assertEquals(2, outputLogs.size());
        assertEquals(log3, outputLogs.get(0));
        assertEquals(log4, outputLogs.get(1));
        assertEquals(2, persistence.countLogs("test-p1"));
    } 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) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 5 with MockLogFactory

use of com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory 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

MediumTest (android.support.test.filters.MediumTest)5 Log (com.microsoft.azure.mobile.ingestion.models.Log)5 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)5 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)5 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Mockito.anyString (org.mockito.Mockito.anyString)2 SuppressLint (android.annotation.SuppressLint)1 DatabaseScanner (com.microsoft.azure.mobile.utils.storage.StorageHelper.DatabaseStorage.DatabaseScanner)1 List (java.util.List)1