-
Notifications
You must be signed in to change notification settings - Fork 20
Feature Notification Center #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thomaszurkan-optimizely
merged 8 commits into
optimizely:master
from
msohailhussain:feature_notification_center
Nov 27, 2017
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93b4056
Merge remote-tracking branch 'Optimizely_Remote/master'
mfahadahmed d1a949f
Feature Notification Center (#25)
mfahadahmed e86590e
Added FeatureExperiment and FeatureRollout notification types.
mfahadahmed 857b934
Fixed code review defects.
mfahadahmed 79d6a9a
TestBucketLogsCorrectlyWhenUserProfileFailsToSave fixed.
msohailh 3478d57
remvoed feature experiment and featurue rollout notifications.
msohailh 4b096d1
removed feature experiment and feature rollback callbacks.
msohailhussain 0e80f49
fixed unit test issue.
msohailh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
241
OptimizelySDK.Tests/NotificationTests/NotificationCenterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
"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. | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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