Search in sources :

Example 1 with EventLog

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

the class AnalyticsAndroidTest method testAnalyticsListener.

@Test
public void testAnalyticsListener() {
    AnalyticsListener analyticsListener = mock(AnalyticsListener.class);
    Analytics.setListener(analyticsListener);
    Channel channel = mock(Channel.class);
    Analytics.getInstance().onStarted(sContext, "", channel);
    Analytics.trackEvent("event");
    /* First process: enqueue log but network is down... */
    final EventLog log = new EventLog();
    log.setId(randomUUID());
    log.setName("name");
    Analytics.unsetInstance();
    Analytics.setListener(analyticsListener);
    verify(channel).enqueue(any(Log.class), anyString());
    verifyNoMoreInteractions(analyticsListener);
    /* Second process: sending succeeds. */
    final AtomicReference<Channel.GroupListener> groupListener = new AtomicReference<>();
    channel = mock(Channel.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Channel.GroupListener listener = (Channel.GroupListener) invocationOnMock.getArguments()[4];
            groupListener.set(listener);
            listener.onBeforeSending(log);
            return null;
        }
    }).when(channel).addGroup(anyString(), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
    Analytics.unsetInstance();
    Analytics.setListener(analyticsListener);
    Analytics.getInstance().onStarted(sContext, "", channel);
    assertNotNull(groupListener.get());
    groupListener.get().onSuccess(log);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    verify(analyticsListener).onBeforeSending(any(EventLog.class));
    verify(analyticsListener).onSendingSucceeded(any(EventLog.class));
    verifyNoMoreInteractions(analyticsListener);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Channel(com.microsoft.azure.mobile.channel.Channel) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with EventLog

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

the class AnalyticsTest method testTrackEvent.

@Test
public void testTrackEvent() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.trackEvent(null, null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent("", null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(" ", null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(generateString(257, '*'), null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(generateString(256, '*'), null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(65, '*'), null);
            put("1", null);
            put("2", generateString(65, '*'));
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getProperties().size() == 0;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String validMapItem = "valid";
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put(validMapItem + i, validMapItem);
            }
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getProperties().size() == 5;
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.azure.mobile.test.TestUtils.generateString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with EventLog

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

the class AnalyticsTest method trackEvent.

@Test
public void trackEvent() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    final String name = "testEvent";
    final HashMap<String, String> properties = new HashMap<>();
    properties.put("a", "b");
    Analytics.trackEvent(name, properties);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return name.equals(eventLog.getName()) && properties.equals(eventLog.getProperties()) && eventLog.getId() != null;
            }
            return false;
        }
    }), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.azure.mobile.test.TestUtils.generateString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with EventLog

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

the class Analytics method queueEvent.

/**
     * Send an event.
     *
     * @param name       event name.
     * @param properties optional properties.
     */
private synchronized void queueEvent(String name, Map<String, String> properties) {
    if (isInactive())
        return;
    EventLog eventLog = new EventLog();
    eventLog.setId(UUIDUtils.randomUUID());
    eventLog.setName(name);
    eventLog.setProperties(properties);
    mChannel.enqueue(eventLog, ANALYTICS_GROUP);
}
Also used : EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)

Example 5 with EventLog

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

the class AnalyticsTest method testGetChannelListener.

@Test
public void testGetChannelListener() throws IOException, ClassNotFoundException {
    final EventLog testEventLog = new EventLog();
    testEventLog.setId(UUID.randomUUID());
    testEventLog.setName("name");
    final Exception testException = new Exception("test exception message");
    Analytics.setListener(new AnalyticsListener() {

        @Override
        public void onBeforeSending(Log log) {
            assertEquals(log, testEventLog);
        }

        @Override
        public void onSendingSucceeded(Log log) {
            assertEquals(log, testEventLog);
        }

        @Override
        public void onSendingFailed(Log log, Exception e) {
            assertEquals(log, testEventLog);
            assertEquals(e, testException);
        }
    });
    Channel.GroupListener listener = Analytics.getInstance().getChannelListener();
    listener.onBeforeSending(testEventLog);
    listener.onSuccess(testEventLog);
    listener.onFailure(testEventLog, testException);
}
Also used : AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Channel(com.microsoft.azure.mobile.channel.Channel) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)9 Test (org.junit.Test)6 Channel (com.microsoft.azure.mobile.channel.Channel)5 AnalyticsListener (com.microsoft.azure.mobile.analytics.channel.AnalyticsListener)4 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)4 Log (com.microsoft.azure.mobile.ingestion.models.Log)4 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)3 Context (android.content.Context)2 TestUtils.generateString (com.microsoft.azure.mobile.test.TestUtils.generateString)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 Matchers.anyString (org.mockito.Matchers.anyString)2 NonNull (android.support.annotation.NonNull)1 Log (android.util.Log)1 EventLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.azure.mobile.analytics.ingestion.models.json.StartSessionLogFactory)1