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
3 changes: 3 additions & 0 deletions OptimizelySDK.Net35/OptimizelySDK.Net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
</Compile>
<Compile Include="..\OptimizelySDK\Logger\NoOpLogger.cs">
<Link>Logger\NoOpLogger.cs</Link>
</Compile>
<Compile Include="..\OptimizelySDK\Notifications\NotificationCenter.cs">
<Link>Notifications\NotificationCenter.cs</Link>
</Compile>
<Compile Include="..\OptimizelySDK\Optimizely.cs">
<Link>Optimizely.cs</Link>
Expand Down
3 changes: 3 additions & 0 deletions OptimizelySDK.Net40/OptimizelySDK.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
</Compile>
<Compile Include="..\OptimizelySDK\Logger\NoOpLogger.cs">
<Link>Logger\NoOpLogger.cs</Link>
</Compile>
<Compile Include="..\OptimizelySDK\Notifications\NotificationCenter.cs">
<Link>Notifications\NotificationCenter.cs</Link>
</Compile>
<Compile Include="..\OptimizelySDK\Optimizely.cs">
<Link>Optimizely.cs</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Compile Include="..\OptimizelySDK\Logger\DefaultLogger.cs" />
<Compile Include="..\OptimizelySDK\Logger\ILogger.cs" />
<Compile Include="..\OptimizelySDK\Logger\NoOpLogger.cs" />
<Compile Include="..\OptimizelySDK\Notifications\NotificationCenter.cs" />
<Compile Include="..\OptimizelySDK\Optimizely.cs" />
<Compile Include="..\OptimizelySDK\ProjectConfig.cs" />
<Compile Include="..\OptimizelySDK\Utils\ConditionEvaluator.cs" />
Expand Down
4 changes: 2 additions & 2 deletions OptimizelySDK.Tests/DecisionServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ public void TestGetVariationSavesBucketedVariationIntoUserProfile()
}

[Test]
[ExpectedException]
public void TestBucketLogsCorrectlyWhenUserProfileFailsToSave()
{
Experiment experiment = NoAudienceProjectConfig.Experiments[0];
Expand All @@ -300,8 +299,9 @@ public void TestBucketLogsCorrectlyWhenUserProfileFailsToSave()
decisionService.SaveVariation(experiment, variation, saveUserProfile);

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

[Test]
Expand Down
77 changes: 77 additions & 0 deletions OptimizelySDK.Tests/DefaultErrorHandlerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
*
* Copyright 2017, Optimizely and contributors
*
* 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;
using System.Collections.Generic;
using Moq;
using OptimizelySDK.Logger;
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Entity;
using NUnit.Framework;
using OptimizelySDK.Bucketing;
using OptimizelySDK.Exceptions;

namespace OptimizelySDK.Tests
{
public class DefaultErrorHandlerTest
{
private DefaultErrorHandler DefaultErrorHandler;
private Mock<ILogger> LoggerMock;

[SetUp]
public void Setup()
{
LoggerMock = new Mock<ILogger>();
}

[Test]
public void TestErrorHandlerMessage()
{
DefaultErrorHandler = new DefaultErrorHandler(LoggerMock.Object, false);
string testingException = "Testing exception";
try
{
throw new OptimizelyException("Testing exception");
}
catch(OptimizelyException ex)
{
DefaultErrorHandler.HandleError(ex);
}

LoggerMock.Verify(log => log.Log(LogLevel.ERROR, testingException), Times.Once);
}

[Test]
[ExpectedException]
public void TestErrorHandlerMessageWithThrowException()
{
DefaultErrorHandler = new DefaultErrorHandler(LoggerMock.Object, true);
string testingException = "Testing and throwing exception";
try
{
throw new OptimizelyException("Testing exception");
}
catch (OptimizelyException ex)
{
//have to throw exception.
DefaultErrorHandler.HandleError(ex);
}

LoggerMock.Verify(log => log.Log(LogLevel.ERROR, testingException), Times.Once);
}

}
}
241 changes: 241 additions & 0 deletions OptimizelySDK.Tests/NotificationTests/NotificationCenterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/*
* Copyright 2017, 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 Moq;
using NUnit.Framework;
using OptimizelySDK.Entity;
using OptimizelySDK.ErrorHandler;
using OptimizelySDK.Event;
using OptimizelySDK.Logger;
using OptimizelySDK.Notifications;
using System.Collections.Generic;
using NotificationType = OptimizelySDK.Notifications.NotificationCenter.NotificationType;

namespace OptimizelySDK.Tests.NotificationTests
{
public class NotificationCenterTests
{
private Mock<ILogger> LoggerMock;
private NotificationCenter NotificationCenter;
private TestNotificationCallbacks TestNotificationCallbacks;

private NotificationType NotificationTypeActivate = NotificationType.Activate;
private NotificationType NotificationTypeTrack = NotificationType.Track;

[SetUp]
public void Setup()
{
LoggerMock = new Mock<ILogger>();
LoggerMock.Setup(i => i.Log(It.IsAny<LogLevel>(), It.IsAny<string>()));

NotificationCenter = new NotificationCenter(LoggerMock.Object);
TestNotificationCallbacks = new TestNotificationCallbacks();
}

[Test]
public void TestAddAndRemoveNotificationListener()
{
// Verify that callback added successfully.
Assert.AreEqual(1, NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback));
Assert.AreEqual(1, NotificationCenter.NotificationsCount);

// Verify that callback removed successfully.
Assert.AreEqual(true, NotificationCenter.RemoveNotification(1));
Assert.AreEqual(0, NotificationCenter.NotificationsCount);

//Verify return false with invalid ID.
Assert.AreEqual(false, NotificationCenter.RemoveNotification(1));

// Verify that callback added successfully and return right notification ID.
Assert.AreEqual(NotificationCenter.NotificationId, NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback));
Assert.AreEqual(1, NotificationCenter.NotificationsCount);
}

[Test]
public void TestAddMultipleNotificationListeners()
{
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback);
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestAnotherActivateCallback);

// Verify that multiple notifications will be added for same notification type.
Assert.AreEqual(2, NotificationCenter.NotificationsCount);

// Verify that notifications of other types will also gets added successfully.
NotificationCenter.AddNotification(NotificationTypeTrack, TestNotificationCallbacks.TestTrackCallback);
Assert.AreEqual(3, NotificationCenter.NotificationsCount);
}

[Test]
public void TestAddSameNotificationListenerMultipleTimes()
{
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback);

// Verify that adding same callback multiple times will gets failed.
Assert.AreEqual(-1, NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback));
Assert.AreEqual(1, NotificationCenter.NotificationsCount);
LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "The notification callback already exists."), Times.Once);
}

[Test]
public void TestAddInvalidNotificationListeners()
{
// Verify that AddNotification gets failed on adding invalid notification listeners.
Assert.AreEqual(0, NotificationCenter.AddNotification(NotificationTypeTrack,
TestNotificationCallbacks.TestActivateCallback));


LoggerMock.Verify(l => l.Log(LogLevel.ERROR, $@"Invalid notification type provided for ""{NotificationTypeActivate}"" callback."),
Times.Once);

// Verify that no notifion has been added.
Assert.AreEqual(0, NotificationCenter.NotificationsCount);
}

[Test]
public void TestClearNotifications()
{
// Add decision notifications.
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback);
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestAnotherActivateCallback);

// Add track notification.
NotificationCenter.AddNotification(NotificationTypeTrack, TestNotificationCallbacks.TestTrackCallback);

// Verify that callbacks added successfully.
Assert.AreEqual(3, NotificationCenter.NotificationsCount);

// Verify that only decision callbacks are removed.
NotificationCenter.ClearNotifications(NotificationTypeActivate);
Assert.AreEqual(1, NotificationCenter.NotificationsCount);

// Verify that ClearNotifications does not break on calling twice for same type.
NotificationCenter.ClearNotifications(NotificationTypeActivate);
NotificationCenter.ClearNotifications(NotificationTypeActivate);

// Verify that ClearNotifications does not break after calling ClearAllNotifications.
NotificationCenter.ClearAllNotifications();
NotificationCenter.ClearNotifications(NotificationTypeTrack);
}

[Test]
public void TestClearAllNotifications()
{
// Add decision notifications.
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestActivateCallback);
NotificationCenter.AddNotification(NotificationTypeActivate, TestNotificationCallbacks.TestAnotherActivateCallback);

// Add track notification.
NotificationCenter.AddNotification(NotificationTypeTrack, TestNotificationCallbacks.TestTrackCallback);

// Verify that callbacks added successfully.
Assert.AreEqual(3, NotificationCenter.NotificationsCount);

// Verify that ClearAllNotifications remove all the callbacks.
NotificationCenter.ClearAllNotifications();
Assert.AreEqual(0, NotificationCenter.NotificationsCount);

// Verify that ClearAllNotifications does not break on calling twice or after ClearNotifications.
NotificationCenter.ClearNotifications(NotificationTypeActivate);
NotificationCenter.ClearAllNotifications();
NotificationCenter.ClearAllNotifications();
}

[Test]
public void TestSendNotifications()
{
var config = ProjectConfig.Create(TestData.Datafile, LoggerMock.Object, new NoOpErrorHandler());
var logEventMocker = new Mock<LogEvent>("http://mockedurl", new Dictionary<string, object>(), "POST", new Dictionary<string, string>());
// Mocking notification callbacks.
var notificationCallbackMock = new Mock<TestNotificationCallbacks>();

notificationCallbackMock.Setup(nc => nc.TestActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()));

notificationCallbackMock.Setup(nc => nc.TestAnotherActivateCallback(It.IsAny<Experiment>(),
It.IsAny<string>(), It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()));


// Adding decision notifications.
NotificationCenter.AddNotification(NotificationTypeActivate, notificationCallbackMock.Object.TestActivateCallback);
NotificationCenter.AddNotification(NotificationTypeActivate, notificationCallbackMock.Object.TestAnotherActivateCallback);


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

// Fire decision type notifications.
NotificationCenter.SendNotifications(NotificationTypeActivate, config.GetExperimentFromKey("test_experiment"),

Choose a reason for hiding this comment

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

The LogEvent would never be null. So maybe mock it?

Copy link
Contributor

Choose a reason for hiding this comment

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

mocked it. Please check. 0e80f49

"testUser", new UserAttributes(), config.GetVariationFromId("test_experiment", "7722370027"), logEventMocker.Object);

// Verify that only the registered notifications of decision type are called.
notificationCallbackMock.Verify(nc => nc.TestActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Once);

notificationCallbackMock.Verify(nc => nc.TestAnotherActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Once);

notificationCallbackMock.Verify(nc => nc.TestTrackCallback(It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<EventTags>(), It.IsAny<LogEvent>()), Times.Never);




// Verify that after clearing notifications, SendNotification should not call any notification
// which were previously registered.
NotificationCenter.ClearAllNotifications();
notificationCallbackMock.ResetCalls();

NotificationCenter.SendNotifications(NotificationTypeActivate, config.GetExperimentFromKey("test_experiment"),
"testUser", new UserAttributes(), config.GetVariationFromId("test_experiment", "7722370027"), null);


// Again verify notifications which were registered are not called.
notificationCallbackMock.Verify(nc => nc.TestActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Never);

notificationCallbackMock.Verify(nc => nc.TestAnotherActivateCallback(It.IsAny<Experiment>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<Variation>(), It.IsAny<LogEvent>()), Times.Never);

notificationCallbackMock.Verify(nc => nc.TestTrackCallback(It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<UserAttributes>(), It.IsAny<EventTags>(), It.IsAny<LogEvent>()), Times.Never);
}
}

#region Test Notification callbacks class.

/// <summary>
/// Test class containing dummy notification callbacks.
/// </summary>
public class TestNotificationCallbacks
{
public virtual void TestActivateCallback(Experiment experiment, string userId, UserAttributes userAttributes,
Variation variation, LogEvent logEvent) {
}

public virtual void TestAnotherActivateCallback(Experiment experiment, string userId, UserAttributes userAttributes,
Variation variation, LogEvent logEvent) {
}

public virtual void TestTrackCallback(string eventKey, string userId, UserAttributes userAttributes,
EventTags eventTags, LogEvent logEvent) {
}

public virtual void TestAnotherTrackCallback(string eventKey, string userId, UserAttributes userAttributes,
EventTags eventTags, LogEvent logEvent) {
}
}
#endregion // Test Notification callbacks class.
}
2 changes: 2 additions & 0 deletions OptimizelySDK.Tests/OptimizelySDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DecisionServiceTest.cs" />
<Compile Include="DefaultErrorHandlerTest.cs" />
<Compile Include="EventTests\DefaultEventDispatcherTest.cs" />
<Compile Include="EventTests\EventBuilderTest.cs" />
<Compile Include="EventTests\LogEventTest.cs" />
<Compile Include="InvalidEventDispatcher.cs" />
<Compile Include="NotificationTests\NotificationCenterTests.cs" />
<Compile Include="OptimizelyTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestBucketer.cs" />
Expand Down
Loading