Search in sources :

Example 21 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log 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 22 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log 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 23 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log 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 24 with Log

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

the class DatabasePersistenceAndroidTest method getLogsException.

@Test
public void getLogsException() throws PersistenceException, IOException, JSONException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = spy(new DefaultLogSerializer());
    /* Throw a JSON exception for the first call. */
    doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).doThrow(new JSONException("JSON exception")).doReturn(AndroidTestUtils.generateMockLog()).when(logSerializer).deserializeLog(anyString());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Test constants. */
        int numberOfLogs = 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. */
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test", 10, outputLogs);
        assertEquals(numberOfLogs / 2, outputLogs.size());
        assertEquals(2, persistence.mDatabaseStorage.size());
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        persistence.close();
    }
}
Also used : Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) 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)

Example 25 with Log

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

the class ErrorModelTest method checkSerialization.

private static void checkSerialization(Log errorLog, LogSerializer serializer) throws JSONException {
    String payload = serializer.serializeLog(errorLog);
    Log deserializedLog = serializer.deserializeLog(payload);
    checkEquals(errorLog, deserializedLog);
}
Also used : Log(com.microsoft.azure.mobile.ingestion.models.Log)

Aggregations

Log (com.microsoft.azure.mobile.ingestion.models.Log)59 Test (org.junit.Test)44 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)37 ArrayList (java.util.ArrayList)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Context (android.content.Context)18 UUID (java.util.UUID)18 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)13 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)13 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)12 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)12 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 Channel (com.microsoft.azure.mobile.channel.Channel)8 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)8 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7