Skip to content
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
21 changes: 21 additions & 0 deletions ProviderTests/ProviderOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Splitio.OpenFeature.Provider;

namespace ProviderTests
{
[TestClass]
public class ProviderOptionsTests
{
[TestMethod]
public void ProviderOptionsConstructorThrowsWhenSdkKeyIsNull()
{
// arrange, act + assert
Assert.ThrowsException<ArgumentNullException>(() =>
{
new ProviderOptions(
null);
});
}
}
}
47 changes: 45 additions & 2 deletions ProviderTests/ProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ public ProviderTests()

[TestMethod]
[ExpectedException(typeof(ArgumentException), "Missing SplitClient instance or SDK ApiKey")]
public async Task InitializeWithNullTest()
public async Task InitializeWithNullDictionaryTest()
{
await Api.Instance.SetProviderAsync(new Provider(null));
await Api.Instance.SetProviderAsync(new Provider(null as Dictionary<string, object>));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException), "Missing SplitClient instance or SDK ApiKey")]
public async Task InitializeWithNullProviderOptionsTest()
{
await Api.Instance.SetProviderAsync(new Provider(null as ProviderOptions));
}

[TestMethod]
Expand Down Expand Up @@ -468,6 +475,42 @@ public async Task TestTrackWithInvalidArguments()
catch (Exception) { }
Assert.AreEqual(0, splitClient.Invocations.Count);
}

[TestMethod]
public async Task ProviderConstructorWithOptionsIncludesConfiguration()
{
// arrange
var configuration = new ConfigurationOptions();
var options = new ProviderOptions(
"test-sdk-key",
configuration,
1234);

// act
await Api.Instance.SetProviderAsync(new Provider(options));
var featureClient = Api.Instance.GetClient();

// assert
Assert.IsNotNull(featureClient);
}

[TestMethod]
public async Task ProviderConstructorWithOptionsWithoutConfigurationUsesDefaults()
{
// arrange
var options = new ProviderOptions(
"test-sdk-key",
null,
10000);

// act
await Api.Instance.SetProviderAsync(new Provider(options));
var featureClient = OpenFeature.Api.Instance.GetClient();

// assert
Assert.IsNotNull(featureClient);
}


private static bool StructuresMatch(Structure s1, Structure s2)
{
Expand Down
2 changes: 1 addition & 1 deletion Splitio.OpenFeature.Provider/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static class Constants
public const string ConfigKey = "ConfigOptions";
public const string TrafficType = "trafficType";
public const string ReadyBlockTime = "ReadyBlockTime";

public const int DefaultReadyBlockTime = 10000;
}
}
23 changes: 23 additions & 0 deletions Splitio.OpenFeature.Provider/Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public Provider(Dictionary<string, object> initialContext)
_splitWrapper = CreateSplitWrapper(initialContext);
_log = WrapperAdapter.Instance().GetLogger(typeof(Provider));
}

public Provider(ProviderOptions options)
: this(ToInitialContext(options))
{
}

public override Metadata GetMetadata() => _metadata;

Expand Down Expand Up @@ -315,5 +320,23 @@ private bool ValidateTrackDetails(string trackingEventName, EvaluationContext ev

return true;
}

private static Dictionary<string, object> ToInitialContext(ProviderOptions options)
{
var initialContext = new Dictionary<string, object>();

if (options != null)
{
initialContext[Constants.SdkApiKey] = options.SdkKey;
initialContext[Constants.ReadyBlockTime] = options.ReadyBlockTime;
}

if (options?.Configuration != null)
{
initialContext[Constants.ConfigKey] = options.Configuration;
}

return initialContext;
}
}
}
22 changes: 22 additions & 0 deletions Splitio.OpenFeature.Provider/ProviderOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Splitio.Services.Client.Classes;

namespace Splitio.OpenFeature.Provider
{
public sealed class ProviderOptions
{
public string SdkKey { get; }
public ConfigurationOptions Configuration { get; }
public int ReadyBlockTime { get; }

public ProviderOptions(
string sdkKey,
ConfigurationOptions configOptions = null,
int readyBlockTime = Constants.DefaultReadyBlockTime)
{
SdkKey = sdkKey ?? throw new ArgumentNullException(nameof(sdkKey));
Configuration = configOptions;
ReadyBlockTime = readyBlockTime;
}
}
}
18 changes: 9 additions & 9 deletions Splitio.OpenFeature.Provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ Below is a simple example that describes the instantiation of the Split Provider
using OpenFeature;
using Splitio.OpenFeature;

Dictionary<string, object> initialContext = new Dictionary<string, object>();
var config = new ConfigurationOptions
{
Logger = new CustomLogger()
};
initialContext.Add("ConfigOptions", config);
initialContext.Add("SdkKey", "SPLIT SDK API KEY");
initialContext.Add("ReadyBlockTime", 5000);
var options = new ProviderOptions(
sdkKey: "SPLIT SDK API KEY",
configuration: new ConfigurationOptions
{
Logger = new CustomLogger()
},
readyBlockTime: 5000
);

Api api = OpenFeature.Api.Instance;

api.setProviderAsync(new Provider(initialContext));
api.setProviderAsync(new Provider(options));
```

If you are more familiar with Split or want access to other initialization options, you can provide a `Split Client` to the constructor. See the [Split .NET Documentation](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK) for more information.
Expand Down
2 changes: 1 addition & 1 deletion Splitio.OpenFeature.Provider/SplitWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SplitWrapper(ISplitClient splitClient)
this.splitClient = splitClient;
}

public SplitWrapper(string SdkKey, ConfigurationOptions Configs, int ReadyBlockTime=10000)
public SplitWrapper(string SdkKey, ConfigurationOptions Configs, int ReadyBlockTime=Constants.DefaultReadyBlockTime)
{
var factory = new SplitFactory(SdkKey, Configs);
_log = WrapperAdapter.Instance().GetLogger(typeof(SplitWrapper));
Expand Down