Skip to content
Merged
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: 4 additions & 0 deletions OptimizelySDK.Net40/OptimizelySDK.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.configuration" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -230,6 +231,9 @@
</Compile>
<Compile Include="..\OptimizelySDK\Config\PollingProjectConfigManager.cs">
<Link>Config\PollingProjectConfigManager</Link>
</Compile>
<Compile Include="..\OptimizelySDK\ClientConfigHandler.cs">
<Link>ClientConfigHandler</Link>
</Compile>
<Compile Include="..\OptimizelySDK\Config\HttpProjectConfigManager.cs">
<Link>Config\HttpProjectConfigManager</Link>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down Expand Up @@ -72,6 +72,9 @@
</Compile>
<Compile Include="..\OptimizelySDK\Config\ProjectConfigManager.cs">
<Link>Config\ProjectConfigManager.cs</Link>
</Compile>
<Compile Include="..\OptimizelySDK\ClientConfigHandler.cs">
<Link>Config\ClientConfigHandler.cs</Link>
</Compile>
<Compile Include="..\OptimizelySDK\Entity\Attribute.cs">
<Link>Entity\Attribute.cs</Link>
Expand Down Expand Up @@ -257,6 +260,7 @@
<PackageReference Include="murmurhash-signed" Version="1.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="NJsonSchema" Version="8.30.6304.31883" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\OptimizelySDK\Utils\schema.json">
Expand Down
25 changes: 23 additions & 2 deletions OptimizelySDK.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@
that apply only to the Test project.
-->
<configuration>
<appSettings>

</appSettings>
<configSections>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add notes in the README for this and how it affects unit tests?

<section name="optlySDKConfigSection"
type="OptimizelySDK.OptimizelySDKConfigSection, OptimizelySDK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>

<optlySDKConfigSection>

<HttpProjectConfig sdkKey="43214321"
url="www.testurl.com"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This url is not needed because you have the url format

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not for actual HttpConfigManager testing, it's only to check, if the url what we set is readonly through property or not.

format="https://cdn.optimizely.com/data/{0}.json"
pollingInterval="2000"
blockingTimeOutPeriod="10000"
autoUpdate="true"
defaultStart="true">
</HttpProjectConfig>

<BatchEventProcessor batchSize="10"
flushInterval="2000"
timeoutInterval="10000"
defaultStart="true">
</BatchEventProcessor>

</optlySDKConfigSection>

<connectionStrings>

</connectionStrings>
Expand Down
57 changes: 57 additions & 0 deletions OptimizelySDK.Tests/ClientConfigHandlerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#if !NETSTANDARD1_6 && !NET35

using NUnit.Framework;
using System.Configuration;

namespace OptimizelySDK.Tests
{
[TestFixture]
public class ClientConfigHandlerTest
{

[Test]
public void TestHTTPAppConfigSection()
{
var configSection = ConfigurationManager.GetSection("optlySDKConfigSection") as OptimizelySDKConfigSection;
var httpSetting = configSection.HttpProjectConfig;
Assert.IsNotNull(httpSetting);
Assert.IsTrue(httpSetting.AutoUpdate);
Assert.AreEqual(httpSetting.BlockingTimeOutPeriod, 10000);
Assert.AreEqual(httpSetting.Format, "https://cdn.optimizely.com/data/{0}.json");
Assert.IsTrue(httpSetting.DefaultStart);
Assert.AreEqual(httpSetting.PollingInterval, 2000);
Assert.AreEqual(httpSetting.SDKKey, "43214321");
Assert.AreEqual(httpSetting.Url, "www.testurl.com");
}

[Test]
public void TestBatchEventAppConfigSection()
{
var configSection = ConfigurationManager.GetSection("optlySDKConfigSection") as OptimizelySDKConfigSection;
var batchSetting = configSection.BatchEventProcessor;
Assert.IsNotNull(batchSetting);
Assert.AreEqual(batchSetting.BatchSize, 10);
Assert.AreEqual(batchSetting.FlushInterval, 2000);
Assert.AreEqual(batchSetting.TimeoutInterval, 10000);
Assert.IsTrue(batchSetting.DefaultStart);
}

}
}
#endif
16 changes: 16 additions & 0 deletions OptimizelySDK.Tests/ConfigTest/HttpProjectConfigManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public void TestHttpConfigManagerRetreiveProjectConfigByURL()
Assert.NotNull(httpManager.GetConfig());
}

[Test]
public void TestHttpConfigManagerRetreiveProjectConfigGivenEmptyFormatUseDefaultFormat()
{
HttpProjectConfigManager httpManager = new HttpProjectConfigManager.Builder()
.WithSdkKey("QBw9gFM8oTn7ogY9ANCC1z")
.WithFormat("")
.WithLogger(LoggerMock.Object)
.WithPollingInterval(TimeSpan.FromMilliseconds(1000))
.WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(500))
.WithStartByDefault()
.Build();

httpManager.OnReady().Wait(System.Threading.Timeout.Infinite);
Assert.NotNull(httpManager.GetConfig());
}

[Test]
public void TestHttpConfigManagerRetreiveProjectConfigBySDKKey()
{
Expand Down
1 change: 1 addition & 0 deletions OptimizelySDK.Tests/OptimizelySDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="EventTests\TestForwardingEventDispatcher.cs" />
<Compile Include="InvalidEventDispatcher.cs" />
<Compile Include="NotificationTests\NotificationCenterTests.cs" />
<Compile Include="ClientConfigHandlerTest.cs" />
<Compile Include="OptimizelyTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestBucketer.cs" />
Expand Down
108 changes: 108 additions & 0 deletions OptimizelySDK/ClientConfigHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Configuration;

namespace OptimizelySDK
{
public class HttpProjectConfigElement : ConfigurationElement
{
[ConfigurationProperty("sdkKey", IsRequired = true, IsKey = true)]
public string SDKKey
{
get { return (string)base["sdkKey"]; }
}

[ConfigurationProperty("url")]
public string Url
{
get { return (string)base["url"]; }
}

[ConfigurationProperty("format")]
public string Format
{
get { return (string)base["format"]; }
}

[ConfigurationProperty("pollingInterval")]
public int PollingInterval
{
get { return base["pollingInterval"] is int ? (int)base["pollingInterval"] : 0; }
}

[ConfigurationProperty("blockingTimeOutPeriod")]
public int BlockingTimeOutPeriod
{
get { return base["blockingTimeOutPeriod"] is int ? (int)base["blockingTimeOutPeriod"] : 0; }
}

[ConfigurationProperty("autoUpdate")]
public bool AutoUpdate
{
get { return (bool)base["autoUpdate"]; }
}

[ConfigurationProperty("defaultStart")]
public bool DefaultStart
{
get { return (bool)base["defaultStart"]; }
}
}

public class BatchEventProcessorElement : ConfigurationElement
{
[ConfigurationProperty("batchSize")]
public int BatchSize
{
get { return (int)base["batchSize"]; }
}

[ConfigurationProperty("flushInterval")]
public int FlushInterval
{
get { return base["flushInterval"] is int ? (int)base["flushInterval"] : 0; }
}

[ConfigurationProperty("timeoutInterval")]
public int TimeoutInterval
{
get { return base["timeoutInterval"] is int ? (int)base["timeoutInterval"] : 0; }
}

[ConfigurationProperty("defaultStart")]
public bool DefaultStart
{
get { return (bool)base["defaultStart"]; }
}
}

public class OptimizelySDKConfigSection : ConfigurationSection
{
[ConfigurationProperty("HttpProjectConfig")]
public HttpProjectConfigElement HttpProjectConfig
{
get { return (HttpProjectConfigElement)base["HttpProjectConfig"]; }
set { base["HttpProjectConfig"] = value; }
}

[ConfigurationProperty("BatchEventProcessor")]
public BatchEventProcessorElement BatchEventProcessor {
get { return (BatchEventProcessorElement)(base["BatchEventProcessor"]); }
set { base["BatchEventProcessor"] = value; }
}
}
}
7 changes: 6 additions & 1 deletion OptimizelySDK/Config/HttpProjectConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ public class Builder
private const long MAX_MILLISECONDS_LIMIT = 4294967294;
private readonly TimeSpan DEFAULT_PERIOD = TimeSpan.FromMinutes(5);
private readonly TimeSpan DEFAULT_BLOCKINGOUT_PERIOD = TimeSpan.FromSeconds(15);
private readonly string DEFAULT_FORMAT = "https://cdn.optimizely.com/datafiles/{0}.json";

private string Datafile;
private string SdkKey;
private string Url;
private string Format = "https://cdn.optimizely.com/datafiles/{0}.json";
private string Format;
private ILogger Logger;
private IErrorHandler ErrorHandler;
private TimeSpan Period;
Expand Down Expand Up @@ -247,6 +248,10 @@ public HttpProjectConfigManager Build(bool defer)
if (ErrorHandler == null)
ErrorHandler = new DefaultErrorHandler();

if (string.IsNullOrEmpty(Format)) {
Format = DEFAULT_FORMAT;
}

if (string.IsNullOrEmpty(Url) && string.IsNullOrEmpty(SdkKey))
{
ErrorHandler.HandleError(new Exception("SdkKey cannot be null"));
Expand Down
Loading