Skip to content

Commit 79d6a9a

Browse files
committed
TestBucketLogsCorrectlyWhenUserProfileFailsToSave fixed.
Added one more test case for FeatureRollout Notification type. Removed ConfigParser method which was not being used.
1 parent 857b934 commit 79d6a9a

File tree

6 files changed

+134
-23
lines changed

6 files changed

+134
-23
lines changed

OptimizelySDK.Tests/DecisionServiceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ public void TestGetVariationSavesBucketedVariationIntoUserProfile()
277277
}
278278

279279
[Test]
280-
[ExpectedException]
281280
public void TestBucketLogsCorrectlyWhenUserProfileFailsToSave()
282281
{
283282
Experiment experiment = NoAudienceProjectConfig.Experiments[0];
@@ -300,8 +299,9 @@ public void TestBucketLogsCorrectlyWhenUserProfileFailsToSave()
300299
decisionService.SaveVariation(experiment, variation, saveUserProfile);
301300

302301
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, string.Format
303-
("Failed to save variation \"{0}\" of experiment \"{1}\" for user \"{2}\".", UserProfileId, variation.Id, experiment.Id))
302+
("Failed to save variation \"{0}\" of experiment \"{1}\" for user \"{2}\".", variation.Id, experiment.Id, UserProfileId))
304303
, Times.Once);
304+
ErrorHandlerMock.Verify(er => er.HandleError(It.IsAny<OptimizelySDK.Exceptions.OptimizelyRuntimeException>()), Times.Once);
305305
}
306306

307307
[Test]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
*
3+
* Copyright 2017, 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 System.Collections.Generic;
19+
using Moq;
20+
using OptimizelySDK.Logger;
21+
using OptimizelySDK.ErrorHandler;
22+
using OptimizelySDK.Entity;
23+
using NUnit.Framework;
24+
using OptimizelySDK.Bucketing;
25+
using OptimizelySDK.Exceptions;
26+
27+
namespace OptimizelySDK.Tests
28+
{
29+
public class DefaultErrorHandlerTest
30+
{
31+
private DefaultErrorHandler DefaultErrorHandler;
32+
private Mock<ILogger> LoggerMock;
33+
34+
[SetUp]
35+
public void Setup()
36+
{
37+
LoggerMock = new Mock<ILogger>();
38+
}
39+
40+
[Test]
41+
public void TestErrorHandlerMessage()
42+
{
43+
DefaultErrorHandler = new DefaultErrorHandler(LoggerMock.Object, false);
44+
string testingException = "Testing exception";
45+
try
46+
{
47+
throw new OptimizelyException("Testing exception");
48+
}
49+
catch(OptimizelyException ex)
50+
{
51+
DefaultErrorHandler.HandleError(ex);
52+
}
53+
54+
LoggerMock.Verify(log => log.Log(LogLevel.ERROR, testingException), Times.Once);
55+
}
56+
57+
[Test]
58+
[ExpectedException]
59+
public void TestErrorHandlerMessageWithThrowException()
60+
{
61+
DefaultErrorHandler = new DefaultErrorHandler(LoggerMock.Object, true);
62+
string testingException = "Testing and throwing exception";
63+
try
64+
{
65+
throw new OptimizelyException("Testing exception");
66+
}
67+
catch (OptimizelyException ex)
68+
{
69+
//have to throw exception.
70+
DefaultErrorHandler.HandleError(ex);
71+
}
72+
73+
LoggerMock.Verify(log => log.Log(LogLevel.ERROR, testingException), Times.Once);
74+
}
75+
76+
}
77+
}

OptimizelySDK.Tests/NotificationTests/NotificationCenterTests.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using OptimizelySDK.Event;
2222
using OptimizelySDK.Logger;
2323
using OptimizelySDK.Notifications;
24+
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;
2425

2526
namespace OptimizelySDK.Tests.NotificationTests
2627
{
@@ -29,9 +30,11 @@ public class NotificationCenterTests
2930
private Mock<ILogger> LoggerMock;
3031
private NotificationCenter NotificationCenter;
3132
private TestNotificationCallbacks TestNotificationCallbacks;
32-
private NotificationCenter.NotificationType NotificationTypeActivate = NotificationCenter.NotificationType.Activate;
33-
private NotificationCenter.NotificationType NotificationTypeTrack = NotificationCenter.NotificationType.Track;
34-
private NotificationCenter.NotificationType NotificationTypeFeatureExperiment = NotificationCenter.NotificationType.FeatureExperiment;
33+
34+
private NotificationType NotificationTypeActivate = NotificationType.Activate;
35+
private NotificationType NotificationTypeTrack = NotificationType.Track;
36+
private NotificationType NotificationTypeFeatureExperiment = NotificationType.FeatureExperiment;
37+
private NotificationType NotificationTypeFeatureRollout = NotificationType.FeatureRollout;
3538

3639
[SetUp]
3740
public void Setup()
@@ -53,6 +56,13 @@ public void TestAddAndRemoveNotificationListener()
5356
// Verify that callback removed successfully.
5457
Assert.AreEqual(true, NotificationCenter.RemoveNotification(1));
5558
Assert.AreEqual(0, NotificationCenter.NotificationsCount);
59+
60+
//Verify return false with invalid ID.
61+
Assert.AreEqual(false, NotificationCenter.RemoveNotification(1));
62+
63+
// Verify that callback added successfully and return right notification ID.
64+
Assert.AreEqual(NotificationCenter.NotificationId, NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback));
65+
Assert.AreEqual(1, NotificationCenter.NotificationsCount);
5666
}
5767

5868
[Test]
@@ -158,34 +168,69 @@ public void TestSendNotifications()
158168

159169
// Mocking notification callbacks.
160170
var notificationCallbackMock = new Mock<TestNotificationCallbacks>();
171+
161172
notificationCallbackMock.Setup(nc => nc.TestActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
162173
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()));
174+
163175
notificationCallbackMock.Setup(nc => nc.TestAnotherActivateCallback(It.IsAny<Experiment>(),
164176
It.IsAny<string>(), It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()));
165177

178+
notificationCallbackMock.Setup(nc => nc.TestFeatureRolloutCallback(It.IsAny<string>(), It.IsAny<string>(),
179+
It.IsAny<UserAttributes>(), It.IsAny<Audience[]>()));
180+
166181
// Adding decision notifications.
167182
NotificationCenter.AddNotification(NotificationTypeActivate, notificationCallbackMock.Object.TestActivateCallback);
168183
NotificationCenter.AddNotification(NotificationTypeActivate, notificationCallbackMock.Object.TestAnotherActivateCallback);
184+
NotificationCenter.AddNotification(NotificationTypeFeatureRollout, notificationCallbackMock.Object.TestFeatureRolloutCallback);
185+
169186

170187
// Adding track notifications.
171188
NotificationCenter.AddNotification(NotificationTypeTrack, notificationCallbackMock.Object.TestTrackCallback);
172189

173-
// Firing decision type notifications.
190+
// Fire decision type notifications.
174191
NotificationCenter.SendNotifications(NotificationTypeActivate, config.GetExperimentFromKey("test_experiment"),
175192
"testUser", new UserAttributes(), config.GetVariationFromId("test_experiment", "7722370027"), null);
176193

177194
// Verify that only the registered notifications of decision type are called.
178195
notificationCallbackMock.Verify(nc => nc.TestActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
179-
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Exactly(1));
196+
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Once);
197+
180198
notificationCallbackMock.Verify(nc => nc.TestAnotherActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
181-
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Exactly(1));
199+
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Once);
200+
182201
notificationCallbackMock.Verify(nc => nc.TestTrackCallback(It.IsAny<string>(), It.IsAny<string>(),
183202
It.IsAny<UserAttributes>(), It.IsAny<EventTags>(), It.IsAny<LogEvent>()), Times.Never);
184203

185-
// Verify that SendNotifications does not break when no notification exists.
204+
205+
// Fire feature rollout notification
206+
NotificationCenter.SendNotifications(NotificationTypeFeatureRollout, "featureKey", "testUser", new UserAttributes(), null);
207+
208+
notificationCallbackMock.Verify(nc => nc.TestFeatureRolloutCallback(It.IsAny<string>(), It.IsAny<string>(),
209+
It.IsAny<UserAttributes>(), It.IsAny<Audience[]>()), Times.Once);
210+
211+
212+
// Verify that after clearing notifications, SendNotification should not call any notification
213+
// which were previously registered.
186214
NotificationCenter.ClearAllNotifications();
215+
notificationCallbackMock.ResetCalls();
216+
187217
NotificationCenter.SendNotifications(NotificationTypeActivate, config.GetExperimentFromKey("test_experiment"),
188218
"testUser", new UserAttributes(), config.GetVariationFromId("test_experiment", "7722370027"), null);
219+
220+
221+
// Again verify notifications which were registered are not called.
222+
notificationCallbackMock.Verify(nc => nc.TestActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
223+
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Never);
224+
225+
notificationCallbackMock.Verify(nc => nc.TestAnotherActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
226+
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Never);
227+
228+
notificationCallbackMock.Verify(nc => nc.TestTrackCallback(It.IsAny<string>(), It.IsAny<string>(),
229+
It.IsAny<UserAttributes>(), It.IsAny<EventTags>(), It.IsAny<LogEvent>()), Times.Never);
230+
231+
notificationCallbackMock.Verify(nc => nc.TestFeatureRolloutCallback(It.IsAny<string>(), It.IsAny<string>(),
232+
It.IsAny<UserAttributes>(), It.IsAny<Audience[]>()), Times.Never);
233+
189234
}
190235
}
191236

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="DecisionServiceTest.cs" />
74+
<Compile Include="DefaultErrorHandlerTest.cs" />
7475
<Compile Include="EventTests\DefaultEventDispatcherTest.cs" />
7576
<Compile Include="EventTests\EventBuilderTest.cs" />
7677
<Compile Include="EventTests\LogEventTest.cs" />

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ public void SaveVariation(Experiment experiment, Variation variation, UserProfil
241241
}
242242
catch (Exception exception)
243243
{
244-
Logger.Log(LogLevel.ERROR, string.Format("Failed to save variation \"{0}\" of experiment \"{1}\" for user \"{2}\": {3}.",
245-
variation.Id, experiment.Id, userProfile.UserId, exception.Message));
244+
Logger.Log(LogLevel.ERROR, string.Format("Failed to save variation \"{0}\" of experiment \"{1}\" for user \"{2}\".",
245+
variation.Id, experiment.Id, userProfile.UserId));
246246
ErrorHandler.HandleError(new Exceptions.OptimizelyRuntimeException(exception.Message));
247247
}
248248
}

OptimizelySDK/Utils/ConfigParser.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,5 @@ public static Dictionary<string, T> GenerateMap(IEnumerable<T> entities, Func<T,
3232
{
3333
return entities.ToDictionary(e => getKey(e), e => clone ? (T)e.Clone() : e);
3434
}
35-
36-
/// <summary>
37-
/// Creates an array of entities from the entity
38-
/// (not sure this is really needed)
39-
/// </summary>
40-
/// <param name="entities">Original Entities</param>
41-
/// <param name="clone">Whether or not to clone the original entity</param>
42-
/// <returns>array of entities</returns>
43-
public static T[] GenerateMap(IEnumerable<T> entities, bool clone)
44-
{
45-
return entities.Select(e => clone ? (T)e.Clone() : e).ToArray();
46-
}
4735
}
4836
}

0 commit comments

Comments
 (0)