Skip to content

[FME-4192] Implement property validator. #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: feature/FME-4191-update-dto
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Splitio/Domain/EventValidatorResult.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Splitio.Domain
{
public class EventValidatorResult
public class PropertiesValidatorResult
{
public bool Success { get; set; }
public object Value { get; set; }
public long EventSize { get; set; }
public long Size { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Splitio/Services/Client/Classes/SplitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class SplitClient : ISplitClient
protected readonly IKeyValidator _keyValidator;
protected readonly ISplitNameValidator _splitNameValidator;
protected readonly IEventTypeValidator _eventTypeValidator;
protected readonly IEventPropertiesValidator _eventPropertiesValidator;
protected readonly IPropertiesValidator _eventPropertiesValidator;
protected readonly IWrapperAdapter _wrapperAdapter;
protected readonly IConfigService _configService;
protected readonly IFlagSetsValidator _flagSetsValidator;
Expand Down Expand Up @@ -71,7 +71,7 @@ public SplitClient(string apikey)
_keyValidator = new KeyValidator();
_splitNameValidator = new SplitNameValidator();
_eventTypeValidator = new EventTypeValidator();
_eventPropertiesValidator = new EventPropertiesValidator();
_eventPropertiesValidator = new PropertiesValidator();
_factoryInstantiationsService = FactoryInstantiationsService.Instance();
_flagSetsValidator = new FlagSetsValidator();
_configService = new ConfigService(_wrapperAdapter, _flagSetsValidator, new SdkMetadataValidator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

namespace Splitio.Services.InputValidation.Classes
{
public class EventPropertiesValidator : IEventPropertiesValidator
public class PropertiesValidator : IPropertiesValidator
{
private const int MAX_PROPERTIES_LENGTH_BYTES = 32 * 1024;
protected readonly ISplitLogger _log;

public EventPropertiesValidator(ISplitLogger log = null)
public PropertiesValidator(ISplitLogger log = null)
{
_log = log ?? WrapperAdapter.Instance().GetLogger(typeof(EventPropertiesValidator));
_log = log ?? WrapperAdapter.Instance().GetLogger(typeof(PropertiesValidator));
}

public EventValidatorResult IsValid(Dictionary<string, object> properties)
public PropertiesValidatorResult IsValid(Dictionary<string, object> properties)
{
if (properties == null) return new EventValidatorResult { Success = true };
if (properties == null) return new PropertiesValidatorResult { Success = true };

var propertiesResult = new Dictionary<string, object>();
var size = 1024L;
Expand Down Expand Up @@ -48,21 +48,21 @@ public EventValidatorResult IsValid(Dictionary<string, object> properties)
if (size > MAX_PROPERTIES_LENGTH_BYTES)
{
_log.Error($"The maximum size allowed for the properties is 32768 bytes. Current one is {size} bytes. Event not queued");
return new EventValidatorResult { Success = false };
return new PropertiesValidatorResult { Success = false };
}

propertiesResult.Add(entry.Key, value);
}

return new EventValidatorResult
return new PropertiesValidatorResult
{
Success = true,
Value = propertiesResult,
EventSize = size
Size = size
};
}

private bool IsNumeric(object value)
private static bool IsNumeric(object value)
{
if (value == null) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Splitio.Services.InputValidation.Interfaces
{
public interface IEventPropertiesValidator
public interface IPropertiesValidator
{
EventValidatorResult IsValid(Dictionary<string, object> properties);
PropertiesValidatorResult IsValid(Dictionary<string, object> properties);
}
}
12 changes: 6 additions & 6 deletions src/Splitio/Services/Shared/Classes/ClientExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ClientExtensionService : IClientExtensionService
private readonly ISplitNameValidator _splitNameValidator;
private readonly ITelemetryEvaluationProducer _telemetryEvaluationProducer;
private readonly IEventTypeValidator _eventTypeValidator;
private readonly IEventPropertiesValidator _eventPropertiesValidator;
private readonly IPropertiesValidator _eventPropertiesValidator;
private readonly ITrafficTypeValidator _trafficTypeValidator;
private readonly IFlagSetsValidator _flagSetsValidator;
private readonly IFlagSetsFilter _flagSetsFilter;
Expand All @@ -32,7 +32,7 @@ public ClientExtensionService(IBlockUntilReadyService blockUntilReadyService,
ISplitNameValidator splitNameValidator,
ITelemetryEvaluationProducer telemetryEvaluationProducer,
IEventTypeValidator eventTypeValidator,
IEventPropertiesValidator eventPropertiesValidator,
IPropertiesValidator eventPropertiesValidator,
ITrafficTypeValidator trafficTypeValidator,
IFlagSetsValidator flagSetsValidator,
IFlagSetsFilter flagSetsFilter)
Expand All @@ -55,23 +55,23 @@ public bool TrackValidations(string key, string trafficType, string eventType, d

var keyResult = _keyValidator.IsValid(new Key(key, null), API.Track);
var eventTypeResult = _eventTypeValidator.IsValid(eventType, nameof(eventType));
var eventPropertiesResult = _eventPropertiesValidator.IsValid(properties);
var propertiesResult = _eventPropertiesValidator.IsValid(properties);
var trafficTypeResult = _trafficTypeValidator.IsValid(trafficType, API.Track);

if (!keyResult || !trafficTypeResult.Success || !eventTypeResult || !eventPropertiesResult.Success)
if (!keyResult || !trafficTypeResult.Success || !eventTypeResult || !propertiesResult.Success)
return false;

wrappedEvent = new WrappedEvent
{
Size = eventPropertiesResult.EventSize,
Size = propertiesResult.Size,
Event = new Event
{
key = key,
trafficTypeName = trafficTypeResult.Value,
eventTypeId = eventType,
value = value,
timestamp = CurrentTimeHelper.CurrentTimeMillis(),
properties = (Dictionary<string, object>)eventPropertiesResult.Value
properties = (Dictionary<string, object>)propertiesResult.Value
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Splitio_Tests.Unit_Tests.InputValidation
public class EventPropertiesValidatorTests
{
private Mock<ISplitLogger> _log;
private EventPropertiesValidator eventPropertiesValidator;
private PropertiesValidator eventPropertiesValidator;

[TestInitialize]
public void Initialize()
{
_log = new Mock<ISplitLogger>();

eventPropertiesValidator = new EventPropertiesValidator(_log.Object);
eventPropertiesValidator = new PropertiesValidator(_log.Object);
}

[TestMethod]
Expand All @@ -31,7 +31,7 @@ public void IsValid_WhenPropertiesIsNull_ReturnsTrue()
// Assert.
Assert.IsTrue(result.Success);
Assert.IsNull(result.Value);
Assert.IsTrue(result.EventSize == default(long));
Assert.IsTrue(result.Size == default(long));
}

[TestMethod]
Expand Down Expand Up @@ -95,7 +95,7 @@ public void IsValid_WhenPropertiesIsNotNull_ReturnsTrue()
Assert.AreEqual(ushortValue, dicResult["property_11"]);
Assert.AreEqual(uintValue, dicResult["property_12"]);
Assert.AreEqual(ulongValue, dicResult["property_13"]);
Assert.IsTrue(result.EventSize == sizeExpected);
Assert.IsTrue(result.Size == sizeExpected);

_log.Verify(mock => mock.Warn($"Property Splitio.Domain.ParsedSplit is of invalid type. Setting value to null"), Times.Once);
_log.Verify(mock => mock.Warn(It.IsAny<string>()), Times.Exactly(1));
Expand Down Expand Up @@ -128,7 +128,7 @@ public void IsValid_WhenPropertiesCountIsBiggerThan300_ReturnsTrue()
// Assert.
Assert.IsTrue(result.Success);
Assert.IsNotNull(result.Value);
Assert.IsTrue(result.EventSize == sizeExpected);
Assert.IsTrue(result.Size == sizeExpected);

_log.Verify(mock => mock.Warn("Event has more than 300 properties. Some of them will be trimmed when processed"), Times.Once);
_log.Verify(mock => mock.Warn(It.IsAny<string>()), Times.Exactly(1));
Expand All @@ -153,7 +153,7 @@ public void IsValid_WhenPropertiesBytesIsBiggerThanWeSupport_ReturnsFalse()
// Assert.
Assert.IsFalse(result.Success);
Assert.IsNull(result.Value);
Assert.IsTrue(result.EventSize == default(long));
Assert.IsTrue(result.Size == default(long));

var size = 1024L;
foreach (var item in properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ClientExtensionServiceTests
private readonly ISplitNameValidator _splitNameValidator;
private readonly IKeyValidator _keyValidator;
private readonly IEventTypeValidator _eventTypeValidator;
private readonly IEventPropertiesValidator _eventPropertiesValidator;
private readonly IPropertiesValidator _eventPropertiesValidator;
private readonly ITrafficTypeValidator _trafficTypeValidator;
private readonly IFlagSetsValidator _flagSetsValidator;
private readonly IFlagSetsFilter _flagSetsFilter;
Expand All @@ -42,7 +42,7 @@ public ClientExtensionServiceTests()
_splitNameValidator = new SplitNameValidator();
_keyValidator = new KeyValidator(_logger.Object);
_eventTypeValidator = new EventTypeValidator(_logger.Object);
_eventPropertiesValidator = new EventPropertiesValidator(_logger.Object);
_eventPropertiesValidator = new PropertiesValidator(_logger.Object);
_trafficTypeValidator = new TrafficTypeValidator(_featureFlagCacheConsumer.Object, _blockUntilReadyService.Object, _logger.Object);
_flagSetsValidator = new FlagSetsValidator();
_flagSetsFilter = new FlagSetsFilter(new HashSet<string>());
Expand Down