Search in sources :

Example 1 with CommonSchemaEventLog

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

the class AnalyticsTransmissionTargetTest method addTicketToLogBeforeStart.

@Test
public void addTicketToLogBeforeStart() {
    /* Simulate not started. */
    Analytics.unsetInstance();
    when(AppCenter.isConfigured()).thenReturn(false);
    /* No actions are prepared without authentication provider. */
    CommonSchemaLog log = new CommonSchemaEventLog();
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
    /* Add authentication provider before start. */
    AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
    assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider).acquireTokenAsync();
    /* Start analytics. */
    when(AppCenter.isConfigured()).thenReturn(true);
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    ArgumentCaptor<Runnable> normalRunnable = ArgumentCaptor.forClass(Runnable.class);
    ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(handler).post(normalRunnable.capture(), disabledRunnable.capture());
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, null, false);
    /* No actions are prepared with no CommonSchemaLog. */
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(mock(Log.class), "test");
    verify(authenticationProvider, never()).checkTokenExpiry();
    /* Call prepare log. */
    final ProtocolExtension protocol = new ProtocolExtension();
    log.setExt(new Extensions() {

        {
            setProtocol(protocol);
        }
    });
    AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
    /* Verify log. */
    assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());
    /* And that we check expiry. */
    verify(authenticationProvider).checkTokenExpiry();
}
Also used : Context(android.content.Context) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) Test(org.junit.Test)

Example 2 with CommonSchemaEventLog

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

the class AnalyticsTransmissionTargetTest method updateAuthProviderAndLog.

@Test
public void updateAuthProviderAndLog() {
    /* When we enqueue app center log from track event. */
    final List<CommonSchemaLog> sentLogs = new ArrayList<>();
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            ProtocolExtension protocol = new ProtocolExtension();
            Extensions ext = new Extensions();
            ext.setProtocol(protocol);
            CommonSchemaLog log = new CommonSchemaEventLog();
            log.setExt(ext);
            sentLogs.add(log);
            /* Call the listener after conversion of common schema for authentication decoration. */
            AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
            return null;
        }
    }).when(mChannel).enqueue(any(Log.class), anyString(), anyInt());
    /* Start analytics and simulate background thread handler (we hold the thread command and run it in the test). */
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
    doNothing().when(handler).post(backgroundRunnable.capture(), any(Runnable.class));
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
    /* Add first authentication provider. */
    AuthenticationProvider.TokenProvider tokenProvider1 = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider1 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider1));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider1);
    /* Check provider updated in background thread only when AppCenter is configured/started. */
    assertNull(AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider1, never()).acquireTokenAsync();
    assertNotNull(backgroundRunnable.getValue());
    /* Run background thread. */
    backgroundRunnable.getValue().run();
    /* Check update. */
    assertEquals(authenticationProvider1, AnalyticsTransmissionTarget.sAuthenticationProvider);
    verify(authenticationProvider1).acquireTokenAsync();
    /* Track an event. */
    Analytics.trackEvent("test1");
    Runnable trackEvent1Command = backgroundRunnable.getValue();
    /* Update authentication provider before the commands run and track a second event. */
    AuthenticationProvider.TokenProvider tokenProvider2 = mock(AuthenticationProvider.TokenProvider.class);
    AuthenticationProvider authenticationProvider2 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key2", tokenProvider2));
    AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider2);
    Runnable addAuthProvider2Command = backgroundRunnable.getValue();
    Analytics.trackEvent("test2");
    Runnable trackEvent2Command = backgroundRunnable.getValue();
    /* Simulate background thread doing everything in a sequence. */
    trackEvent1Command.run();
    addAuthProvider2Command.run();
    trackEvent2Command.run();
    /* Verify first log has first ticket. */
    assertEquals(Collections.singletonList(authenticationProvider1.getTicketKeyHash()), sentLogs.get(0).getExt().getProtocol().getTicketKeys());
    /* And that we checked expiry. */
    verify(authenticationProvider1).checkTokenExpiry();
    /* Verify second log has the second ticket. */
    assertEquals(Collections.singletonList(authenticationProvider2.getTicketKeyHash()), sentLogs.get(1).getExt().getProtocol().getTicketKeys());
    /* And that we checked expiry. */
    verify(authenticationProvider2).checkTokenExpiry();
}
Also used : Context(android.content.Context) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Test(org.junit.Test)

Example 3 with CommonSchemaEventLog

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

the class PropertyConfiguratorTest method setNonPrefixUserId.

@Test
public void setNonPrefixUserId() {
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(new Extensions());
    log.getExt().setUser(new UserExtension());
    /* Get property configurator and set properties. */
    PropertyConfigurator pc = Analytics.getTransmissionTarget("test").getPropertyConfigurator();
    pc.setUserId("bob");
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    log.addTransmissionTarget("test");
    log.setTag(Analytics.getTransmissionTarget("test"));
    pc.onPreparingLog(log, "groupName");
    /* Check prefix was added. */
    assertEquals("c:bob", log.getExt().getUser().getLocalId());
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) UserExtension(com.microsoft.appcenter.ingestion.models.one.UserExtension) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with CommonSchemaEventLog

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

the class PropertyConfiguratorTest method overridingPartAIsNotRetroactive.

@Test
public void overridingPartAIsNotRetroactive() {
    /* Mock deviceId. */
    mockStatic(Secure.class);
    when(Secure.getString(any(ContentResolver.class), anyString())).thenReturn("mockDeviceId");
    /* Start analytics and simulate background thread handler (we hold the thread command and run it in the test). */
    Analytics analytics = Analytics.getInstance();
    AppCenterHandler handler = mock(AppCenterHandler.class);
    final LinkedList<Runnable> backgroundCommands = new LinkedList<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            backgroundCommands.add((Runnable) invocation.getArguments()[0]);
            return null;
        }
    }).when(handler).post(notNull(Runnable.class), any(Runnable.class));
    analytics.onStarting(handler);
    analytics.onStarted(mock(Context.class), mChannel, null, null, true);
    /* Create a target. */
    AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
    /* Init target background code now. */
    backgroundCommands.removeFirst().run();
    /* Simulate a track event call with no property. */
    CommonSchemaLog logBeforeSetProperty = new CommonSchemaEventLog();
    logBeforeSetProperty.setExt(new Extensions());
    logBeforeSetProperty.getExt().setApp(new AppExtension());
    logBeforeSetProperty.getExt().setUser(new UserExtension());
    logBeforeSetProperty.getExt().setDevice(new DeviceExtension());
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    logBeforeSetProperty.addTransmissionTarget("test");
    logBeforeSetProperty.setTag(target);
    /* Set all common properties that should not be set retroactively before first log persisted. */
    target.getPropertyConfigurator().setAppVersion("appVersion");
    target.getPropertyConfigurator().setAppLocale("appLocale");
    target.getPropertyConfigurator().setAppName("appName");
    target.getPropertyConfigurator().setUserId("c:alice");
    target.getPropertyConfigurator().collectDeviceId();
    /* Run background commands in order: first prepare first log, then all the set property calls. */
    target.getPropertyConfigurator().onPreparingLog(logBeforeSetProperty, "groupName");
    for (Runnable command : backgroundCommands) {
        command.run();
    }
    /* Simulate the second track event call. */
    CommonSchemaLog logAfterSetProperty = new CommonSchemaEventLog();
    logAfterSetProperty.setExt(new Extensions());
    logAfterSetProperty.getExt().setApp(new AppExtension());
    logAfterSetProperty.getExt().setUser(new UserExtension());
    logAfterSetProperty.getExt().setDevice(new DeviceExtension());
    /* Simulate what the pipeline does to convert from App Center to Common Schema. */
    logAfterSetProperty.addTransmissionTarget("test");
    logAfterSetProperty.setTag(target);
    target.getPropertyConfigurator().onPreparingLog(logAfterSetProperty, "groupName");
    /* Check first log has no property. */
    assertNull(logBeforeSetProperty.getExt().getApp().getVer());
    assertNull(logBeforeSetProperty.getExt().getApp().getLocale());
    assertNull(logBeforeSetProperty.getExt().getApp().getName());
    assertNull(logBeforeSetProperty.getExt().getUser().getLocalId());
    assertNull(logBeforeSetProperty.getExt().getDevice().getLocalId());
    /* Check second log has all of them. */
    assertEquals("appVersion", logAfterSetProperty.getExt().getApp().getVer());
    assertEquals("appLocale", logAfterSetProperty.getExt().getApp().getLocale());
    assertEquals("appName", logAfterSetProperty.getExt().getApp().getName());
    assertEquals("c:alice", logAfterSetProperty.getExt().getUser().getLocalId());
    assertEquals("a:mockDeviceId", logAfterSetProperty.getExt().getDevice().getLocalId());
}
Also used : Context(android.content.Context) AppExtension(com.microsoft.appcenter.ingestion.models.one.AppExtension) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) DeviceExtension(com.microsoft.appcenter.ingestion.models.one.DeviceExtension) LinkedList(java.util.LinkedList) ContentResolver(android.content.ContentResolver) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) UserExtension(com.microsoft.appcenter.ingestion.models.one.UserExtension) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with CommonSchemaEventLog

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

the class PropertyConfiguratorTest method inheritCommonSchemaPropertiesFromGrandparent.

@Test
public void inheritCommonSchemaPropertiesFromGrandparent() {
    CommonSchemaLog log = new CommonSchemaEventLog();
    log.setExt(new Extensions());
    log.getExt().setApp(new AppExtension());
    log.getExt().setUser(new UserExtension());
    /* Set properties on parent to override unset properties on child */
    AnalyticsTransmissionTarget grandparent = Analytics.getTransmissionTarget("grandparent");
    grandparent.getPropertyConfigurator().setAppVersion("appVersion");
    grandparent.getPropertyConfigurator().setAppName("appName");
    grandparent.getPropertyConfigurator().setAppLocale("appLocale");
    grandparent.getPropertyConfigurator().setUserId("c:alice");
    /* Set up hierarchy. */
    AnalyticsTransmissionTarget parent = grandparent.getTransmissionTarget("parent");
    AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child");
    /* Simulate channel callbacks. */
    log.addTransmissionTarget("child");
    log.setTag(child);
    grandparent.getPropertyConfigurator().onPreparingLog(log, "groupName");
    parent.getPropertyConfigurator().onPreparingLog(log, "groupName");
    child.getPropertyConfigurator().onPreparingLog(log, "groupName");
    /* Assert properties set on common schema. */
    assertEquals("appVersion", log.getExt().getApp().getVer());
    assertEquals("appName", log.getExt().getApp().getName());
    assertEquals("appLocale", log.getExt().getApp().getLocale());
    assertEquals("c:alice", log.getExt().getUser().getLocalId());
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) AppExtension(com.microsoft.appcenter.ingestion.models.one.AppExtension) UserExtension(com.microsoft.appcenter.ingestion.models.one.UserExtension) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)17 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)16 Test (org.junit.Test)16 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 UserExtension (com.microsoft.appcenter.ingestion.models.one.UserExtension)10 AppExtension (com.microsoft.appcenter.ingestion.models.one.AppExtension)9 Context (android.content.Context)5 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)4 ContentResolver (android.content.ContentResolver)3 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)3 DeviceExtension (com.microsoft.appcenter.ingestion.models.one.DeviceExtension)3 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)3 Log (com.microsoft.appcenter.ingestion.models.Log)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)2 LinkedList (java.util.LinkedList)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ArrayList (java.util.ArrayList)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1