Skip to content

Commit 2213f6f

Browse files
Added Optimizely factory unit tests (#224)
* optimizely factory test base * unit tests * Added unit tests of optimzielyfactory * Nit fixes, and added additional variables * reveting nunittestadapter * added comments * Added addition unit test and did some refactoring * unit test fix * refact Co-authored-by: Sohail Hussain <mirza.sohailhussain@gmail.com>
1 parent 5db689b commit 2213f6f

File tree

5 files changed

+450
-1
lines changed

5 files changed

+450
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
*
3+
* Copyright 2020, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
using System;
18+
using OptimizelySDK.Config;
19+
using OptimizelySDK.Tests.Utils;
20+
21+
namespace OptimizelySDK.Tests.ConfigTest
22+
{
23+
/// <summary>
24+
/// This is a helper class for optimizely factory unit testing and is only to expose private properties of HttpProjectConfigManager and its super classes.
25+
/// </summary>
26+
public class ProjectConfigManagerProps
27+
{
28+
public string LastModified { get; set; }
29+
public string Url { get; set; }
30+
public string DatafileAccessToken { get; set; }
31+
public TimeSpan PollingInterval { get; set; }
32+
public TimeSpan BlockingTimeout { get; set; }
33+
public bool AutoUpdate { get; set; }
34+
35+
public ProjectConfigManagerProps(HttpProjectConfigManager projectConfigManager)
36+
{
37+
LastModified = Reflection.GetFieldValue<string, HttpProjectConfigManager>(projectConfigManager, "LastModifiedSince");
38+
Url = Reflection.GetFieldValue<string, HttpProjectConfigManager>(projectConfigManager, "Url");
39+
DatafileAccessToken = Reflection.GetFieldValue<string, HttpProjectConfigManager>(projectConfigManager, "DatafileAccessToken");
40+
41+
AutoUpdate = Reflection.GetPropertyValue<bool, HttpProjectConfigManager>(projectConfigManager, "AutoUpdate");
42+
PollingInterval = Reflection.GetFieldValue<TimeSpan, HttpProjectConfigManager>(projectConfigManager, "PollingInterval");
43+
BlockingTimeout = Reflection.GetFieldValue<TimeSpan, HttpProjectConfigManager>(projectConfigManager, "BlockingTimeout");
44+
}
45+
46+
/// <summary>
47+
/// To create default instance of expected values.
48+
/// </summary>
49+
public ProjectConfigManagerProps()
50+
{
51+
52+
}
53+
54+
public override bool Equals(object obj)
55+
{
56+
if (obj == null)
57+
return false;
58+
59+
var projectConfigManager = obj as ProjectConfigManagerProps;
60+
if (projectConfigManager == null)
61+
{
62+
return false;
63+
}
64+
65+
if (LastModified != projectConfigManager.LastModified ||
66+
Url != projectConfigManager.Url ||
67+
DatafileAccessToken != projectConfigManager.DatafileAccessToken ||
68+
BlockingTimeout != projectConfigManager.BlockingTimeout ||
69+
AutoUpdate != projectConfigManager.AutoUpdate ||
70+
PollingInterval != projectConfigManager.PollingInterval)
71+
{
72+
return false;
73+
}
74+
75+
return true;
76+
}
77+
}
78+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
*
3+
* Copyright 2020, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
using System;
19+
using OptimizelySDK.Event;
20+
using OptimizelySDK.Tests.Utils;
21+
22+
namespace OptimizelySDK.Tests.EventTest
23+
{
24+
/// <summary>
25+
/// Helper class for optimizely factory unit testing to expose private properties of BatchEventProcessor and its super classes.
26+
/// </summary>
27+
public class EventProcessorProps
28+
{
29+
public int BatchSize { get; set; }
30+
public TimeSpan FlushInterval { get; set; }
31+
public TimeSpan TimeoutInterval { get; set; }
32+
33+
public EventProcessorProps(BatchEventProcessor eventProcessor)
34+
{
35+
var fieldsInfo = Reflection.GetAllFields(eventProcessor.GetType());
36+
BatchSize = Reflection.GetFieldValue<int, BatchEventProcessor>(eventProcessor, "BatchSize", fieldsInfo);
37+
FlushInterval = Reflection.GetFieldValue<TimeSpan, BatchEventProcessor>(eventProcessor, "FlushInterval", fieldsInfo);
38+
TimeoutInterval = Reflection.GetFieldValue<TimeSpan, BatchEventProcessor>(eventProcessor, "TimeoutInterval", fieldsInfo);
39+
}
40+
41+
/// <summary>
42+
/// To create default instance of expected values.
43+
/// </summary>
44+
public EventProcessorProps()
45+
{
46+
47+
}
48+
49+
public override bool Equals(object obj)
50+
{
51+
if (obj == null)
52+
return false;
53+
54+
var eventProcessor = obj as EventProcessorProps;
55+
if (eventProcessor == null)
56+
{
57+
return false;
58+
}
59+
60+
if (BatchSize != eventProcessor.BatchSize ||
61+
FlushInterval.TotalMilliseconds != eventProcessor.FlushInterval.TotalMilliseconds ||
62+
TimeoutInterval.TotalMilliseconds != eventProcessor.TimeoutInterval.TotalMilliseconds)
63+
{
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
}
70+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/**
2+
*
3+
* Copyright 2020, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
using Moq;
19+
using NUnit.Framework;
20+
using OptimizelySDK.Config;
21+
using OptimizelySDK.Event;
22+
using OptimizelySDK.Event.Dispatcher;
23+
using OptimizelySDK.Logger;
24+
using OptimizelySDK.Notifications;
25+
using OptimizelySDK.Tests.ConfigTest;
26+
using OptimizelySDK.Tests.EventTest;
27+
using OptimizelySDK.Tests.Utils;
28+
using System;
29+
namespace OptimizelySDK.Tests
30+
{
31+
[TestFixture]
32+
public class OptimizelyFactoryTest
33+
{
34+
private Mock<ILogger> LoggerMock;
35+
[SetUp]
36+
public void Initialize()
37+
{
38+
LoggerMock = new Mock<ILogger>();
39+
LoggerMock.Setup(i => i.Log(It.IsAny<LogLevel>(), It.IsAny<string>()));
40+
}
41+
42+
[Test]
43+
public void TestOptimizelyInstanceUsingConfigFile()
44+
{
45+
var optimizely = OptimizelyFactory.NewDefaultInstance();
46+
// Check values are loaded from app.config or not.
47+
var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager;
48+
Assert.NotNull(projectConfigManager);
49+
50+
var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager);
51+
var expectedConfigManagerProps = new ProjectConfigManagerProps
52+
{
53+
Url = "www.testurl.com",
54+
LastModified = "",
55+
AutoUpdate = true,
56+
BlockingTimeout = TimeSpan.FromSeconds(10),
57+
PollingInterval = TimeSpan.FromSeconds(2)
58+
};
59+
60+
Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps);
61+
optimizely.Dispose();
62+
}
63+
64+
65+
[Test]
66+
public void TestProjectConfigManagerUsingSDKKey()
67+
{
68+
var optimizely = OptimizelyFactory.NewDefaultInstance("my-sdk-key");
69+
70+
// Check values are loaded from app.config or not.
71+
var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager;
72+
Assert.NotNull(projectConfigManager);
73+
74+
var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager);
75+
var expectedConfigManagerProps = new ProjectConfigManagerProps
76+
{
77+
Url = "https://cdn.optimizely.com/datafiles/my-sdk-key.json",
78+
LastModified = "",
79+
AutoUpdate = true,
80+
BlockingTimeout = TimeSpan.FromSeconds(15),
81+
PollingInterval = TimeSpan.FromMinutes(5)
82+
};
83+
84+
Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps);
85+
optimizely.Dispose();
86+
}
87+
88+
[Test]
89+
public void TestProjectConfigManagerWithDatafileAccessToken()
90+
{
91+
var optimizely = OptimizelyFactory.NewDefaultInstance("my-sdk-key", null, "access-token");
92+
93+
// Check values are loaded from app.config or not.
94+
var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager;
95+
Assert.NotNull(projectConfigManager);
96+
97+
var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager);
98+
var expectedConfigManagerProps = new ProjectConfigManagerProps
99+
{
100+
Url = "https://config.optimizely.com/datafiles/auth/my-sdk-key.json",
101+
LastModified = "",
102+
DatafileAccessToken = "access-token",
103+
AutoUpdate = true,
104+
BlockingTimeout = TimeSpan.FromSeconds(15),
105+
PollingInterval = TimeSpan.FromMinutes(5)
106+
};
107+
108+
Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps);
109+
110+
optimizely.Dispose();
111+
}
112+
113+
[Test]
114+
public void TestProjectConfigManagerWithCustomProjectConfigManager()
115+
{
116+
var projectConfigManager = new HttpProjectConfigManager.Builder()
117+
.WithSdkKey("10192104166")
118+
.WithFormat("https://optimizely.com/json/{0}.json")
119+
.WithPollingInterval(TimeSpan.FromMilliseconds(3000))
120+
.WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(4500))
121+
.WithStartByDefault()
122+
.WithAccessToken("access-token")
123+
.Build(true);
124+
125+
var optimizely = OptimizelyFactory.NewDefaultInstance(projectConfigManager);
126+
var actualProjectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager;
127+
var actualConfigManagerProps = new ProjectConfigManagerProps(actualProjectConfigManager);
128+
var expectedConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager);
129+
Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps);
130+
optimizely.Dispose();
131+
}
132+
133+
[Test]
134+
public void TestEventProcessorWithDefaultEventBatching()
135+
{
136+
var optimizely = OptimizelyFactory.NewDefaultInstance();
137+
138+
var batchEventProcessor = Reflection.GetFieldValue<BatchEventProcessor, Optimizely>(optimizely, "EventProcessor");
139+
var actualEventProcessorProps = new EventProcessorProps(batchEventProcessor);
140+
var expectedEventProcessorProps = new EventProcessorProps
141+
{
142+
BatchSize = 10,
143+
FlushInterval = TimeSpan.FromSeconds(2),
144+
TimeoutInterval = TimeSpan.FromSeconds(10)
145+
};
146+
Assert.AreEqual(actualEventProcessorProps, expectedEventProcessorProps);
147+
optimizely.Dispose();
148+
}
149+
150+
[Test]
151+
public void TestEventProcessorWithEventBatchingBatchSizeAndInterval()
152+
{
153+
OptimizelyFactory.SetBatchSize(2);
154+
OptimizelyFactory.SetFlushInterval(TimeSpan.FromSeconds(4));
155+
156+
var optimizely = OptimizelyFactory.NewDefaultInstance("sdk-Key");
157+
158+
var batchEventProcessor = Reflection.GetFieldValue<BatchEventProcessor, Optimizely>(optimizely, "EventProcessor");
159+
var actualEventProcessorProps = new EventProcessorProps(batchEventProcessor);
160+
var expectedEventProcessorProps = new EventProcessorProps
161+
{
162+
BatchSize = 2,
163+
FlushInterval = TimeSpan.FromSeconds(4),
164+
TimeoutInterval = TimeSpan.FromMinutes(5)
165+
};
166+
Assert.AreEqual(actualEventProcessorProps, expectedEventProcessorProps);
167+
optimizely.Dispose();
168+
}
169+
170+
[Test]
171+
public void TestEventProcessorWithBatchEventProcessorObj()
172+
{
173+
var eventDispatcher = new DefaultEventDispatcher(LoggerMock.Object);
174+
var notificationCenter = new NotificationCenter();
175+
var projectConfigManager = new HttpProjectConfigManager.Builder()
176+
.WithSdkKey("10192104166")
177+
.Build(true);
178+
179+
var batchEventProcessor = new BatchEventProcessor.Builder()
180+
.WithLogger(LoggerMock.Object)
181+
.WithMaxBatchSize(20)
182+
.WithFlushInterval(TimeSpan.FromSeconds(3))
183+
.WithEventDispatcher(eventDispatcher)
184+
.WithNotificationCenter(notificationCenter)
185+
.Build();
186+
187+
var optimizely = OptimizelyFactory.NewDefaultInstance(projectConfigManager, notificationCenter, eventProcessor: batchEventProcessor);
188+
189+
var actualbatchEventProcessor = Reflection.GetFieldValue<BatchEventProcessor, Optimizely>(optimizely, "EventProcessor");
190+
var actualEventProcessorProps = new EventProcessorProps(actualbatchEventProcessor);
191+
var expectedEventProcessorProps = new EventProcessorProps(batchEventProcessor);
192+
Assert.AreEqual(actualEventProcessorProps, expectedEventProcessorProps);
193+
optimizely.Dispose();
194+
}
195+
}
196+
197+
}

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="ConfigTest\FallbackProjectConfigManagerTest.cs" />
7878
<Compile Include="DecisionServiceTest.cs" />
7979
<Compile Include="DefaultErrorHandlerTest.cs" />
80+
<Compile Include="EventTests\EventProcessorProps.cs" />
8081
<Compile Include="OptimizelyJSONTest.cs" />
8182
<Compile Include="EventTests\BatchEventProcessorTest.cs" />
8283
<Compile Include="EventTests\DefaultEventDispatcherTest.cs" />
@@ -106,8 +107,11 @@
106107
<Compile Include="EventTests\UserEventFactoryTest.cs" />
107108
<Compile Include="EventTests\EventFactoryTest.cs" />
108109
<Compile Include="EventTests\CanonicalEvent.cs" />
109-
<Compile Include="Utils\TestHttpProjectConfigManagerUtil.cs" />
110+
<Compile Include="OptimizelyFactoryTest.cs" />
110111
<Compile Include="Utils\TestData.cs" />
112+
<Compile Include="Utils\Reflection.cs" />
113+
<Compile Include="ConfigTest\ProjectConfigProps.cs" />
114+
<Compile Include="Utils\TestHttpProjectConfigManagerUtil.cs" />
111115
</ItemGroup>
112116
<ItemGroup>
113117
<Content Include="App.config" />

0 commit comments

Comments
 (0)