Search in sources :

Example 11 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog 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("appcenter.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<>();
    {
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setTimestamp(new Date());
        logs.add(startSessionLog);
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

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

            {
                put("x", "1");
                put("y", "2");
            }
        });
        logs.add(eventLog);
    }
    {
        List<TypedProperty> properties = new ArrayList<>();
        BooleanTypedProperty bp = new BooleanTypedProperty();
        bp.setName("n1");
        bp.setValue(true);
        properties.add(bp);
        DateTimeTypedProperty dtp = new DateTimeTypedProperty();
        dtp.setName("n2");
        dtp.setValue(new Date());
        properties.add(dtp);
        DoubleTypedProperty dp = new DoubleTypedProperty();
        dp.setName("n3");
        dp.setValue(10);
        properties.add(dp);
        LongTypedProperty lp = new LongTypedProperty();
        lp.setName("n4");
        lp.setValue(10000000000L);
        properties.add(lp);
        StringTypedProperty sp = new StringTypedProperty();
        sp.setName("n5");
        sp.setValue("value");
        properties.add(sp);
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        eventLog.setId(UUID.randomUUID());
        eventLog.setName("event");
        eventLog.setTypedProperties(properties);
        logs.add(eventLog);
    }
    UUID sid = UUID.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, null);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) Device(com.microsoft.appcenter.ingestion.models.Device) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Date(java.util.Date) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) StartSessionLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) ArrayList(java.util.ArrayList) List(java.util.List) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) UUID(java.util.UUID) EventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 12 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class SessionTracker method sendStartSession.

/**
 * Generate session id and send start session log.
 */
private void sendStartSession() {
    mSid = UUID.randomUUID();
    /* Update session storage. */
    SessionContext.getInstance().addSession(mSid);
    /* Enqueue a start session log. */
    StartSessionLog startSessionLog = new StartSessionLog();
    startSessionLog.setSid(mSid);
    mChannel.enqueue(startSessionLog, mGroupName, Flags.DEFAULTS);
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)

Example 13 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method setManualSessionTracker.

@Test
public void setManualSessionTracker() {
    /* Set manual session tracker. */
    Analytics.enableManualSessionTracker();
    /* Before start it does not work to change state, it's disabled. */
    Analytics analytics = Analytics.getInstance();
    /* Prepare channel. */
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), channel, "", null, true);
    /* Verify that start session log wasn't sent. */
    ArgumentCaptor<StartSessionLog> log = ArgumentCaptor.forClass(StartSessionLog.class);
    verify(channel, never()).enqueue(log.capture(), eq(analytics.getGroupName()), anyInt());
    /* Verify that start session log was sent. */
    Analytics.startSession();
    verify(channel).enqueue(log.capture(), eq(analytics.getGroupName()), anyInt());
    assertNotNull(log.getValue());
    assertNotNull(log.getValue().getSid());
}
Also used : Context(android.content.Context) UserIdContext(com.microsoft.appcenter.utils.context.UserIdContext) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) Channel(com.microsoft.appcenter.channel.Channel) Test(org.junit.Test)

Example 14 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-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("appcenter.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<>();
    {
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setTimestamp(new Date());
        logs.add(startSessionLog);
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

            {
                put("from", "home_menu");
                put("orientation", "portrait");
            }
        });
        logs.add(pageLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("subscribe");
        logs.add(eventLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        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.appcenter.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) Device(com.microsoft.appcenter.ingestion.models.Device) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Date(java.util.Date) StartSessionLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) EventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 15 with StartSessionLog

use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-SDK-Android by Microsoft.

the class SessionTrackerTest method sdkConfiguredBetweenPauseAndResume.

@Test
public void sdkConfiguredBetweenPauseAndResume() {
    /* Pause application before we saw the first resume event (integration problem). We are handling that gracefully though. */
    mSessionTracker.onActivityPaused();
    /* Application is in background, send a log, verify decoration. */
    UUID expectedSid;
    StartSessionLog expectedStartSessionLog = new StartSessionLog();
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Verify session reused for second log. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* No usage from background for a long time: new session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* App comes to foreground and sends a log, we were in background for a long time but we sent a log recently, still session. */
    {
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* We are in foreground, even after timeout a log is still in session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
}
Also used : StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)22 Test (org.junit.Test)17 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)12 Log (com.microsoft.appcenter.ingestion.models.Log)12 UUID (java.util.UUID)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 StartServiceLog (com.microsoft.appcenter.ingestion.models.StartServiceLog)10 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)6 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)6 Date (java.util.Date)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 Context (android.content.Context)5 Channel (com.microsoft.appcenter.channel.Channel)5 UserIdContext (com.microsoft.appcenter.utils.context.UserIdContext)3 SessionContext (com.microsoft.appcenter.SessionContext)2 EventLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory)2 PageLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory)2 StartSessionLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory)2 Device (com.microsoft.appcenter.ingestion.models.Device)2 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)2