Search in sources :

Example 56 with Log

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

the class DatabasePersistenceAndroidTest method getAllLogs.

private void getAllLogs(DatabasePersistence persistence, int numberOfLogs, int sizeForGetLogs) {
    List<Log> outputLogs = new ArrayList<>();
    int expected = 0;
    do {
        numberOfLogs -= expected;
        persistence.getLogs("test", sizeForGetLogs, outputLogs);
        expected = Math.min(Math.max(numberOfLogs, 0), sizeForGetLogs);
        assertEquals(expected, outputLogs.size());
        outputLogs.clear();
    } while (numberOfLogs > 0);
    /* Get should be 0 now. */
    persistence.getLogs("test", sizeForGetLogs, outputLogs);
    assertEquals(0, outputLogs.size());
}
Also used : Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 57 with Log

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

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

Example 59 with Log

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

the class ChannelLogDecorateTest method checkLogAttributes.

@Test
public void checkLogAttributes() throws DeviceInfoHelper.DeviceInfoException {
    mockStatic(DeviceInfoHelper.class);
    Device device = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(device);
    mockStatic(IdHelper.class);
    Channel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mock(Persistence.class), mock(Ingestion.class));
    channel.addGroup("", 0, 0, 0, null);
    /* Test a log that should be decorated. */
    for (int i = 0; i < 3; i++) {
        Log log = mock(Log.class);
        channel.enqueue(log, "");
        verify(log).setDevice(device);
        verify(log).setToffset(anyLong());
    }
    /* Check cache was used, meaning only 1 call to generate a device. */
    verifyStatic();
    DeviceInfoHelper.getDeviceInfo(any(Context.class));
    /* Test a log that is already decorated. */
    Log log2 = mock(Log.class);
    when(log2.getDevice()).thenReturn(device);
    when(log2.getToffset()).thenReturn(123L);
    channel.enqueue(log2, "");
    verify(log2, never()).setDevice(any(Device.class));
    verify(log2, never()).setToffset(anyLong());
    /* Simulate update to wrapper SDK. */
    Device device2 = mock(Device.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(device2);
    channel.invalidateDeviceCache();
    /* Generate some logs to verify device properties have been updated. */
    for (int i = 0; i < 3; i++) {
        Log log3 = mock(Log.class);
        channel.enqueue(log3, "");
        verify(log3).setDevice(device2);
        verify(log3).setToffset(anyLong());
    }
    /* Check only 1 device has been generated after cache invalidate. */
    verifyStatic(times(2));
    DeviceInfoHelper.getDeviceInfo(any(Context.class));
}
Also used : Context(android.content.Context) Persistence(com.microsoft.azure.mobile.persistence.Persistence) Log(com.microsoft.azure.mobile.ingestion.models.Log) Device(com.microsoft.azure.mobile.ingestion.models.Device) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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