Search in sources :

Example 1 with StringTypedProperty

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

the class AnalyticsTransmissionTargetTest method testTrackEventWithTransmissionTarget.

private void testTrackEventWithTransmissionTarget(final String defaultToken, boolean startFromApp) {
    /* Overwrite setup for this test. */
    Analytics.unsetInstance();
    Analytics analytics = Analytics.getInstance();
    mChannel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), mChannel, null, defaultToken, startFromApp);
    final AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("token");
    assertNotNull(target);
    /* Getting a reference to the same target a second time actually returns the same. */
    assertSame(target, Analytics.getTransmissionTarget("token"));
    /* Track event with default transmission target. */
    Analytics.trackEvent("name");
    if (startFromApp) {
        verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

            @Override
            public boolean matches(Object item) {
                if (item instanceof EventLog) {
                    EventLog eventLog = (EventLog) item;
                    boolean nameAndPropertiesMatch = eventLog.getName().equals("name") && eventLog.getTypedProperties() == null;
                    boolean tokenMatch;
                    boolean tagMatch;
                    if (defaultToken != null) {
                        tokenMatch = eventLog.getTransmissionTargetTokens().size() == 1 && eventLog.getTransmissionTargetTokens().contains(defaultToken);
                        tagMatch = Analytics.getInstance().mDefaultTransmissionTarget.equals(eventLog.getTag());
                    } else {
                        tokenMatch = eventLog.getTransmissionTargetTokens().isEmpty();
                        tagMatch = eventLog.getTag() == null;
                    }
                    return nameAndPropertiesMatch && tokenMatch && tagMatch;
                }
                return false;
            }
        }), anyString(), eq(DEFAULTS));
    } else {
        verify(mChannel, never()).enqueue(isA(EventLog.class), anyString(), anyInt());
    }
    reset(mChannel);
    /* Track event via transmission target method. */
    target.trackEvent("name");
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                boolean nameAndPropertiesMatch = eventLog.getName().equals("name") && eventLog.getTypedProperties() == null;
                boolean tokenMatch = eventLog.getTransmissionTargetTokens().size() == 1 && eventLog.getTransmissionTargetTokens().contains("token");
                boolean tagMatch = target.equals(eventLog.getTag());
                return nameAndPropertiesMatch && tokenMatch && tagMatch;
            }
            return false;
        }
    }), anyString(), eq(DEFAULTS));
    reset(mChannel);
    /* Track event via another transmission target method with properties. */
    Analytics.getTransmissionTarget("token2").trackEvent("name", new HashMap<String, String>() {

        {
            put("valid", "valid");
        }
    });
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                boolean nameMatches = eventLog.getName().equals("name");
                List<TypedProperty> typedProperties = new ArrayList<>();
                StringTypedProperty stringTypedProperty = new StringTypedProperty();
                stringTypedProperty.setName("valid");
                stringTypedProperty.setValue("valid");
                typedProperties.add(stringTypedProperty);
                boolean tokenMatch = eventLog.getTransmissionTargetTokens().size() == 1 && eventLog.getTransmissionTargetTokens().contains("token2");
                boolean tagMatch = Analytics.getTransmissionTarget("token2").equals(eventLog.getTag());
                return nameMatches && tokenMatch && tagMatch && typedProperties.equals(eventLog.getTypedProperties());
            }
            return false;
        }
    }), anyString(), eq(DEFAULTS));
    reset(mChannel);
    /* Create a child transmission target and track event. */
    final AnalyticsTransmissionTarget childTarget = target.getTransmissionTarget("token3");
    childTarget.trackEvent("name");
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                boolean nameAndPropertiesMatch = eventLog.getName().equals("name") && eventLog.getTypedProperties() == null;
                boolean tokenMatch = eventLog.getTransmissionTargetTokens().size() == 1 && eventLog.getTransmissionTargetTokens().contains("token3");
                boolean tagMatch = childTarget.equals(eventLog.getTag());
                return nameAndPropertiesMatch && tokenMatch && tagMatch;
            }
            return false;
        }
    }), anyString(), eq(DEFAULTS));
    reset(mChannel);
    /* Another child transmission target with the same token should be the same instance. */
    assertSame(childTarget, target.getTransmissionTarget("token3"));
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) ArgumentMatcher(org.mockito.ArgumentMatcher) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty)

Example 2 with StringTypedProperty

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

the class AnalyticsValidatorForEventLogTest method shouldFilterTooLongStringTypedPropertyKeyAndValue.

@Test
public void shouldFilterTooLongStringTypedPropertyKeyAndValue() {
    String validEventName = "eventName";
    mEventLog.setName(validEventName);
    String longerPropertyItem = generateString(MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    StringTypedProperty originalProperty = new StringTypedProperty();
    originalProperty.setName(longerPropertyItem);
    originalProperty.setValue(longerPropertyItem);
    List<TypedProperty> typedProperties = new ArrayList<>();
    typedProperties.add(originalProperty);
    mEventLog.setTypedProperties(typedProperties);
    assertFalse(mAnalyticsValidator.shouldFilter(mEventLog));
    assertEquals(validEventName, mEventLog.getName());
    assertEquals(1, mEventLog.getTypedProperties().size());
    StringTypedProperty stringProperty = (StringTypedProperty) mEventLog.getTypedProperties().iterator().next();
    assertEquals(MAX_PROPERTY_ITEM_LENGTH, stringProperty.getName().length());
    assertEquals(MAX_PROPERTY_ITEM_LENGTH, stringProperty.getValue().length());
    /* Verify original property value references were not modified. */
    assertSame(longerPropertyItem, originalProperty.getName());
    assertSame(longerPropertyItem, originalProperty.getValue());
}
Also used : ArrayList(java.util.ArrayList) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) Test(org.junit.Test)

Example 3 with StringTypedProperty

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

the class AnalyticsValidatorForEventLogTest method shouldFilterTooLongPropertyKeysOfAnyType.

@Test
public void shouldFilterTooLongPropertyKeysOfAnyType() {
    /* Set event name. */
    String validEventName = "eventName";
    mEventLog.setName(validEventName);
    /* Use a long key for every property type. */
    String longKey = generateString(MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    StringTypedProperty originalStringProperty = new StringTypedProperty();
    originalStringProperty.setName(longKey);
    originalStringProperty.setValue("does not matter");
    BooleanTypedProperty originalBoolProperty = new BooleanTypedProperty();
    originalBoolProperty.setName(longKey);
    originalBoolProperty.setValue(true);
    DoubleTypedProperty originalDoubleProperty = new DoubleTypedProperty();
    originalDoubleProperty.setName(longKey);
    originalDoubleProperty.setValue(3.14);
    LongTypedProperty originalLongProperty = new LongTypedProperty();
    originalLongProperty.setName(longKey);
    originalLongProperty.setValue(-123);
    DateTimeTypedProperty originalDateProperty = new DateTimeTypedProperty();
    originalDateProperty.setName(longKey);
    originalDateProperty.setValue(new Date());
    /* Submit property list to validation, should pass with truncate. */
    List<TypedProperty> typedProperties = new ArrayList<>();
    typedProperties.add(originalStringProperty);
    typedProperties.add(originalBoolProperty);
    typedProperties.add(originalDoubleProperty);
    typedProperties.add(originalLongProperty);
    typedProperties.add(originalDateProperty);
    mEventLog.setTypedProperties(typedProperties);
    assertFalse(mAnalyticsValidator.shouldFilter(mEventLog));
    /* Check name and number of property not modified. */
    assertEquals(validEventName, mEventLog.getName());
    assertEquals(5, mEventLog.getTypedProperties().size());
    /* Verify all property names truncated. */
    for (TypedProperty property : mEventLog.getTypedProperties()) {
        assertEquals(MAX_PROPERTY_ITEM_LENGTH, property.getName().length());
    }
    /* Check all property changes were made by copy. */
    assertSame(longKey, originalStringProperty.getName());
    assertSame(longKey, originalBoolProperty.getName());
    assertSame(longKey, originalDoubleProperty.getName());
    assertSame(longKey, originalLongProperty.getName());
    assertSame(longKey, originalDateProperty.getName());
}
Also used : DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) ArrayList(java.util.ArrayList) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) Date(java.util.Date) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) Test(org.junit.Test)

Example 4 with StringTypedProperty

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

the class PropertyConfiguratorTest method typedProperty.

private static StringTypedProperty typedProperty(String name, String value) {
    StringTypedProperty typedProperty = new StringTypedProperty();
    typedProperty.setName(name);
    typedProperty.setValue(value);
    return typedProperty;
}
Also used : StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)

Example 5 with StringTypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty 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)

Aggregations

StringTypedProperty (com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)25 TypedProperty (com.microsoft.appcenter.ingestion.models.properties.TypedProperty)17 Test (org.junit.Test)17 BooleanTypedProperty (com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty)16 DateTimeTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)16 DoubleTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty)16 LongTypedProperty (com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty)16 ArrayList (java.util.ArrayList)16 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)5 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)5 JSONObject (org.json.JSONObject)5 Context (android.content.Context)3 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)3 Channel (com.microsoft.appcenter.channel.Channel)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 UserIdContext (com.microsoft.appcenter.utils.context.UserIdContext)2 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)1