Search in sources :

Example 51 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 52 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 53 with Log

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

the class Crashes method getChannelListener.

@Override
protected Channel.GroupListener getChannelListener() {
    return new Channel.GroupListener() {

        /** Process callback (template method) */
        private void processCallback(Log log, final CallbackProcessor callbackProcessor) {
            if (log instanceof ManagedErrorLog) {
                ManagedErrorLog errorLog = (ManagedErrorLog) log;
                if (errorLog.getFatal()) {
                    final ErrorReport report = buildErrorReport(errorLog);
                    UUID id = errorLog.getId();
                    if (report != null) {
                        /* Clean up before calling callbacks if requested. */
                        if (callbackProcessor.shouldDeleteThrowable()) {
                            removeStoredThrowable(id);
                        }
                        /* Call back. */
                        HandlerUtils.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                callbackProcessor.onCallBack(report);
                            }
                        });
                    } else
                        MobileCenterLog.warn(LOG_TAG, "Cannot find crash report for the error log: " + id);
                }
            } else {
                if (!(log instanceof ErrorAttachmentLog))
                    MobileCenterLog.warn(LOG_TAG, "A different type of log comes to crashes: " + log.getClass().getName());
            }
        }

        @Override
        public void onBeforeSending(Log log) {
            processCallback(log, new CallbackProcessor() {

                @Override
                public boolean shouldDeleteThrowable() {
                    return false;
                }

                @Override
                public void onCallBack(ErrorReport report) {
                    mCrashesListener.onBeforeSending(report);
                }
            });
        }

        @Override
        public void onSuccess(Log log) {
            processCallback(log, new CallbackProcessor() {

                @Override
                public boolean shouldDeleteThrowable() {
                    return true;
                }

                @Override
                public void onCallBack(ErrorReport report) {
                    mCrashesListener.onSendingSucceeded(report);
                }
            });
        }

        @Override
        public void onFailure(Log log, final Exception e) {
            processCallback(log, new CallbackProcessor() {

                @Override
                public boolean shouldDeleteThrowable() {
                    return true;
                }

                @Override
                public void onCallBack(ErrorReport report) {
                    mCrashesListener.onSendingFailed(report, e);
                }
            });
        }
    };
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 54 with Log

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

the class SessionTrackerTest method pastSessions.

@Test
public void pastSessions() {
    /* Get a current session. */
    UUID firstSid, currentSid;
    long firstSessionTime = mMockTime;
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertNotNull(log.getSid());
        currentSid = firstSid = log.getSid();
    }
    /* Verify session reused for second log. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertEquals(currentSid, log.getSid());
    }
    /* Past log: correlation will fail and use current session. */
    {
        Log log = newEvent();
        log.setToffset(123L);
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertEquals(currentSid, log.getSid());
    }
    /* No usage from background for a long time, should produce a new session but we'll correlate, correlation does not trigger a new session. */
    {
        spendTime(30000);
        Log log = newEvent();
        log.setToffset(firstSessionTime + 1);
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertEquals(currentSid, log.getSid());
        Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
        assertNotNull(sessions);
        assertEquals(1, sessions.size());
    }
    /* Trigger a second session. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertNotEquals(currentSid, log.getSid());
        Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
        assertNotNull(sessions);
        assertEquals(2, sessions.size());
    }
    /* Correlate log to previous. */
    {
        spendTime(30000);
        Log log = newEvent();
        log.setToffset(firstSessionTime + 1);
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertEquals(firstSid, log.getSid());
        Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
        assertNotNull(sessions);
        assertEquals(2, sessions.size());
    }
    /* Re-test with persistence now, no current session but same correlation will work and no session will be triggered on the new instance. */
    mSessionTracker = new SessionTracker(mChannel, TEST_GROUP);
    {
        Log log = newEvent();
        log.setToffset(firstSessionTime + 1);
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertEquals(firstSid, log.getSid());
        Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
        assertNotNull(sessions);
        assertEquals(2, sessions.size());
    }
    /* Failed correlation without an active session will start a new session. */
    {
        Log log = newEvent();
        log.setToffset(1);
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        assertNotNull(log.getSid());
        Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
        assertNotNull(sessions);
        assertEquals(3, sessions.size());
    }
    /* Clear sessions. */
    mSessionTracker.clearSessions();
    verifyStatic();
    StorageHelper.PreferencesStorage.remove("sessions");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 55 with Log

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

the class AnalyticsSerializerTest method someBatch.

@Test
public void someBatch() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    Device device = new Device();
    device.setSdkName("mobilecenter.android");
    device.setSdkVersion("1.2.3");
    device.setModel("S5");
    device.setOemName("HTC");
    device.setOsName("Android");
    device.setOsVersion("4.0.3");
    device.setOsBuild("LMY47X");
    device.setOsApiLevel(15);
    device.setLocale("en_US");
    device.setTimeZoneOffset(120);
    device.setScreenSize("800x600");
    device.setAppVersion("3.2.1");
    device.setAppBuild("42");
    List<Log> logs = new ArrayList<>();
    {
        logs.add(new StartSessionLog());
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

            {
                put("from", "home_menu");
                put("orientation", "portrait");
            }
        });
        logs.add(pageLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("subscribe");
        logs.add(eventLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("click");
        eventLog.setProperties(new HashMap<String, String>() {

            {
                put("x", "1");
                put("y", "2");
            }
        });
        logs.add(eventLog);
    }
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
        log.setDevice(device);
    }
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
    serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
    serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    android.util.Log.v(TAG, payload);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) HashMap(java.util.HashMap) Device(com.microsoft.azure.mobile.ingestion.models.Device) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.azure.mobile.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) StartSessionLogFactory(com.microsoft.azure.mobile.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) EventLogFactory(com.microsoft.azure.mobile.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

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