Search in sources :

Example 1 with TypedProperty

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

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

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

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

the class EventLogTest method compareDevices.

@Test
public void compareDevices() {
    /* Empty objects. */
    EventLog a = new EventLog();
    EventLog b = new EventLog();
    checkEquals(a, b);
    /* Properties. */
    Map<String, String> p1 = new HashMap<>();
    p1.put("a", "b");
    Map<String, String> p2 = new HashMap<>();
    p1.put("c", "d");
    a.setProperties(p1);
    checkNotEquals(a, b);
    b.setProperties(p2);
    checkNotEquals(a, b);
    b.setProperties(p1);
    checkEquals(a, b);
    /* Name. */
    a.setName("a");
    checkNotEquals(a, b);
    b.setName("b");
    checkNotEquals(a, b);
    b.setName("a");
    checkEquals(a, b);
    /* Id */
    UUID sid1 = UUID.randomUUID();
    UUID sid2 = UUID.randomUUID();
    a.setId(sid1);
    checkNotEquals(a, b);
    b.setId(sid2);
    checkNotEquals(a, b);
    b.setId(sid1);
    checkEquals(a, b);
    /* Event properties. */
    a.setTypedProperties(new ArrayList<TypedProperty>());
    checkNotEquals(a, b);
    a.setTypedProperties(null);
    b.setTypedProperties(new ArrayList<TypedProperty>());
    checkNotEquals(a, b);
    a.setTypedProperties(new ArrayList<TypedProperty>());
    checkEquals(a, b);
}
Also used : HashMap(java.util.HashMap) UUID(java.util.UUID) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) Test(org.junit.Test)

Example 5 with TypedProperty

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

the class PropertyConfiguratorTest method eventPropertiesCascadingWithTypes.

@Test
public void eventPropertiesCascadingWithTypes() {
    /* Create transmission target hierarchy. */
    AnalyticsTransmissionTarget grandParent = Analytics.getTransmissionTarget("grandParent");
    AnalyticsTransmissionTarget parent = grandParent.getTransmissionTarget("parent");
    AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child");
    /* Set common properties across hierarchy with some overrides. */
    grandParent.getPropertyConfigurator().setEventProperty("a", "1");
    grandParent.getPropertyConfigurator().setEventProperty("b", 2.0);
    grandParent.getPropertyConfigurator().setEventProperty("c", "3");
    /* Override some. */
    parent.getPropertyConfigurator().setEventProperty("a", 11);
    parent.getPropertyConfigurator().setEventProperty("b", "22");
    /* And new ones. */
    parent.getPropertyConfigurator().setEventProperty("d", 44);
    /* Just to show we still get value from grandParent if we remove an override. */
    parent.getPropertyConfigurator().setEventProperty("c", "33");
    parent.getPropertyConfigurator().removeEventProperty("c");
    /* Overrides in child. */
    child.getPropertyConfigurator().setEventProperty("d", true);
    /* New in child. */
    child.getPropertyConfigurator().setEventProperty("e", 55.5);
    child.getPropertyConfigurator().setEventProperty("f", "666");
    /* Track event in child. Override properties in trackEvent. */
    EventProperties properties = new EventProperties();
    properties.set("f", new Date(6666));
    properties.set("g", "7777");
    child.trackEvent("eventName", properties);
    /* Verify log that was sent. */
    ArgumentCaptor<EventLog> logArgumentCaptor = ArgumentCaptor.forClass(EventLog.class);
    verify(mChannel).enqueue(logArgumentCaptor.capture(), anyString(), eq(DEFAULTS));
    EventLog log = logArgumentCaptor.getValue();
    assertNotNull(log);
    assertEquals("eventName", log.getName());
    assertEquals(1, log.getTransmissionTargetTokens().size());
    assertTrue(log.getTransmissionTargetTokens().contains("child"));
    /* Verify properties. */
    assertNull(log.getProperties());
    List<TypedProperty> typedProperties = new ArrayList<>();
    typedProperties.add(typedProperty("a", 11));
    typedProperties.add(typedProperty("b", "22"));
    typedProperties.add(typedProperty("c", "3"));
    typedProperties.add(typedProperty("d", true));
    typedProperties.add(typedProperty("e", 55.5));
    typedProperties.add(typedProperty("f", new Date(6666)));
    typedProperties.add(typedProperty("g", "7777"));
    assertUnorderedListEquals(typedProperties, log.getTypedProperties());
}
Also used : EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Date(java.util.Date) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

TypedProperty (com.microsoft.appcenter.ingestion.models.properties.TypedProperty)37 StringTypedProperty (com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)35 BooleanTypedProperty (com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty)31 DateTimeTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)31 DoubleTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty)31 LongTypedProperty (com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty)31 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 JSONObject (org.json.JSONObject)14 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)6 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)5 Date (java.util.Date)4 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Map (java.util.Map)2 JSONException (org.json.JSONException)2 Context (android.content.Context)1 EventProperties (com.microsoft.appcenter.analytics.EventProperties)1