public void CustomEvents_MultipleEvents_Serialization() { const int countEvents = 2; var customEvents = new CustomEventWireModel[countEvents]; var expectedSerializations = new List <Dictionary <string, object>[]>(); for (var i = 0; i < countEvents; i++) { var timestampVal = DateTime.UtcNow; var typeVal = $"CustomEvent{i}"; var userAttribKey = $"foo{i}"; var userAttribVal = $"bar{i}"; var expectedSerialization = new Dictionary <string, object>[] { new Dictionary <string, object>() { { _attribDefs.CustomEventType.Name, typeVal }, { _attribDefs.Timestamp.Name, timestampVal.ToUnixTimeMilliseconds() } }, new Dictionary <string, object>() { { userAttribKey, userAttribVal } }, new Dictionary <string, object>() }; var attribVals = new AttributeValueCollection(AttributeDestinations.CustomEvent); _attribDefs.Timestamp.TrySetValue(attribVals, timestampVal); _attribDefs.CustomEventType.TrySetValue(attribVals, typeVal); _attribDefs.GetCustomAttributeForCustomEvent(userAttribKey).TrySetValue(attribVals, userAttribVal); var customEvent = new CustomEventWireModel(.5f, attribVals); customEvents[i] = customEvent; expectedSerializations.Add(expectedSerialization); } var serialized = JsonConvert.SerializeObject(customEvents); Assert.That(serialized, Is.Not.Null); var deserialized = JsonConvert.DeserializeObject <List <Dictionary <string, object>[]> >(serialized); Assert.That(deserialized, Is.Not.Null); Assert.AreEqual(customEvents.Length, deserialized.Count); AttributeComparer.CompareDictionaries(expectedSerializations[0], deserialized[0]); AttributeComparer.CompareDictionaries(expectedSerializations[1], deserialized[1]); }
public void SetUp() { _lastPublishedCustomEvent = null; _configurationService = Mock.Create <IConfigurationService>(); Mock.Arrange(() => _configurationService.Configuration.CustomEventsEnabled).Returns(true); Mock.Arrange(() => _configurationService.Configuration.CustomEventsAttributesEnabled).Returns(true); _customEventAggregator = Mock.Create <ICustomEventAggregator>(); Mock.Arrange(() => _customEventAggregator.Collect(Arg.IsAny <CustomEventWireModel>())) .DoInstead <CustomEventWireModel>(customEvent => _lastPublishedCustomEvent = customEvent); _attribDefSvc = new AttributeDefinitionService((f) => new AttributeDefinitions(f)); _customEventTransformer = new CustomEventTransformer(_configurationService, _customEventAggregator, _attribDefSvc); }
public void Transform(string eventType, IEnumerable <KeyValuePair <string, object> > attributes, float priority) { if (!_configurationService.Configuration.CustomEventsEnabled) { return; } eventType = eventType?.Trim(); var eventTypeSize = eventType.SizeBytes(); if (eventTypeSize > CustomEventTypeMaxLengthBytes) { Log.Debug($"Custom Event could not be added - Event Type was larger than {CustomEventTypeMaxLengthBytes} bytes."); return; } if (eventTypeSize == 0) { Log.Debug($"Custom Event could not be added - Event Type was null/empty"); return; } if (!EventTypeRegex.Match(eventType).Success) { Log.Debug($"Custom Event could not be added - Event Type did not conform to the following regex: {EventTypeRegexText}"); return; } var attribValues = new AttributeValueCollection(AttributeDestinations.CustomEvent); _attribDefs.CustomEventType.TrySetValue(attribValues, eventType); _attribDefs.Timestamp.TrySetDefault(attribValues); if (_configurationService.Configuration.CustomEventsAttributesEnabled) { foreach (var customAttrib in attributes) { _attribDefs.GetCustomAttributeForCustomEvent(customAttrib.Key).TrySetValue(attribValues, customAttrib.Value); } } var customEvent = new CustomEventWireModel(priority, attribValues); _customEventAggregator.Collect(customEvent); }
public static IDictionary <string, object> GetAttributes(this CustomEventWireModel customEvent, AttributeClassification attributeClassification) { return(customEvent.AttributeValues.ToDictionary(attributeClassification)); }
public static void DoesNotHaveAttributes(IEnumerable <string> unexpectedAttributes, AttributeClassification attributeClassification, CustomEventWireModel customEvent) { var succeeded = true; var builder = new StringBuilder(); var actualAttributes = customEvent.GetAttributes(attributeClassification); foreach (var unexpectedAttribute in unexpectedAttributes) { if (actualAttributes.ContainsKey(unexpectedAttribute)) { builder.AppendFormat("Attribute named {0} was found in the transaction event but it should not have been.", unexpectedAttribute); builder.AppendLine(); succeeded = false; } } Assert.True(succeeded, builder.ToString()); }
public static void HasAttributes(IEnumerable <ExpectedAttribute> expectedAttributes, AttributeClassification attributeClassification, CustomEventWireModel customEvent) { var succeeded = true; var builder = new StringBuilder(); var actualAttributes = customEvent.GetAttributes(attributeClassification); foreach (var expectedAttribute in expectedAttributes) { if (!actualAttributes.ContainsKey(expectedAttribute.Key)) { builder.AppendFormat("Attribute named {0} was not found in the transaction event.", expectedAttribute); builder.AppendLine(); succeeded = false; continue; } var expectedValue = expectedAttribute.Value; var actualValue = actualAttributes[expectedAttribute.Key] as string; if (expectedValue != null && actualValue != expectedAttribute.Value as string) { builder.AppendFormat("Attribute named {0} in the transaction event had an unexpected value. Expected: {1}, Actual: {2}", expectedAttribute.Key, expectedAttribute.Value, actualValue); builder.AppendLine(); succeeded = false; continue; } } Assert.True(succeeded, builder.ToString()); }