/EnhancedReminders/EnhancedReminders/Code/EnhancedReminders/UnitTests/DispatcherServiceUnitTests/CompositeMessageSenderTest.cs

# · C# · 212 lines · 105 code · 32 blank · 75 comment · 0 complexity · cc7f7c1bc07cb0ae2f9866f37797ef8b MD5 · raw file

  1. //++
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Module Name:
  6. //
  7. // CompositeMessageSenderTest.cs
  8. //
  9. // Abstract:
  10. //
  11. // This class contains unit tests for CompositeMessageSender.
  12. //
  13. //--
  14. using System.Collections.Generic;
  15. using System.Threading;
  16. using Microsoft.VisualStudio.TestTools.UnitTesting;
  17. using Microsoft.OfficeCommunicationsServer.Applications.Common;
  18. namespace Microsoft.EnhancedReminders.Dispatcher.UnitTests
  19. {
  20. /// <summary>
  21. ///This is a test class for CompositeMessageSenderTest and is intended
  22. ///to contain all CompositeMessageSenderTest Unit Tests
  23. ///</summary>
  24. [TestClass()]
  25. public class CompositeMessageSenderTest
  26. {
  27. private TestContext testContextInstance;
  28. /// <summary>
  29. ///Gets or sets the test context which provides
  30. ///information about and functionality for the current test run.
  31. ///</summary>
  32. public TestContext TestContext
  33. {
  34. get
  35. {
  36. return testContextInstance;
  37. }
  38. set
  39. {
  40. testContextInstance = value;
  41. }
  42. }
  43. #region Additional test attributes
  44. //
  45. //You can use the following additional attributes as you write your tests:
  46. //
  47. //Use ClassInitialize to run code before running the first test in the class
  48. //[ClassInitialize()]
  49. //public static void MyClassInitialize(TestContext testContext)
  50. //{
  51. //}
  52. //
  53. //Use ClassCleanup to run code after all tests in a class have run
  54. //[ClassCleanup()]
  55. //public static void MyClassCleanup()
  56. //{
  57. //}
  58. //
  59. //[TestInitialize()]
  60. //public void MyTestInitialize()
  61. //{
  62. //}
  63. //
  64. //
  65. //[TestCleanup()]
  66. //public void MyTestCleanup()
  67. //{
  68. //}
  69. //
  70. #endregion
  71. /// <summary>
  72. /// Test a CompositeMessage sender that contains only one channel and
  73. /// successfully delivers the notification.
  74. /// </summary>
  75. [TestMethod()]
  76. public void TestCompositeWithOneChannelSuccess()
  77. {
  78. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Communicator);
  79. IList<IMessageSender> channels = new List<IMessageSender>();
  80. channels.Add(new MockMessageSender(expectedResult));
  81. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  82. }
  83. /// <summary>
  84. /// Test a CompositeMessage sender that contains only one channel and
  85. /// fails to delivers the notification.
  86. /// </summary>
  87. [TestMethod()]
  88. public void TestCompositeWithOneChannelFailure()
  89. {
  90. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator);
  91. IList<IMessageSender> channels = new List<IMessageSender>();
  92. channels.Add(new MockMessageSender(expectedResult));
  93. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  94. }
  95. /// <summary>
  96. /// Test a CompositeMessage sender that contains two channels and
  97. /// successfully delivers the notification on the first channel.
  98. /// </summary>
  99. [TestMethod()]
  100. public void TestCompositeWithTwoChannelsFirstSucceeds()
  101. {
  102. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Communicator);
  103. IList<IMessageSender> channels = new List<IMessageSender>();
  104. channels.Add(new MockMessageSender(expectedResult));
  105. channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Phone)));
  106. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  107. }
  108. /// <summary>
  109. /// Test a CompositeMessage sender that contains two channels and
  110. /// successfully delivers the notification on the second channel.
  111. /// </summary>
  112. [TestMethod()]
  113. public void TestCompositeWithTwoChannelsSecondSucceeds()
  114. {
  115. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Phone);
  116. IList<IMessageSender> channels = new List<IMessageSender>();
  117. channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator)));
  118. channels.Add(new MockMessageSender(expectedResult));
  119. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  120. }
  121. /// <summary>
  122. /// Test a CompositeMessage sender that contains two channels and
  123. /// fails to deliver a notification on either channel.
  124. /// </summary>
  125. [TestMethod()]
  126. public void TestCompositeWithTwoChannelsNoneSucceed()
  127. {
  128. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Phone);
  129. IList<IMessageSender> channels = new List<IMessageSender>();
  130. channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator)));
  131. channels.Add(new MockMessageSender(expectedResult));
  132. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  133. }
  134. /// <summary>
  135. /// Test a CompositeMessage sender that contains three channels and
  136. /// successfully delivers the notification on the second channel.
  137. /// </summary>
  138. [TestMethod()]
  139. public void TestCompositeWithThreeChannelsMiddleSucceeds()
  140. {
  141. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Phone);
  142. IList<IMessageSender> channels = new List<IMessageSender>();
  143. channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Communicator)));
  144. channels.Add(new MockMessageSender(expectedResult));
  145. channels.Add(new MockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Phone)));
  146. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  147. }
  148. /// <summary>
  149. /// Test a CompositeMessage sender that contains two channels, where the first succeeds, but is not a stop
  150. /// point, meaning that the second should execute.
  151. /// </summary>
  152. [TestMethod()]
  153. public void TestCompositeWithFalseStopPointSuccess()
  154. {
  155. NotificationResult expectedResult = new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Unreachable, NotificationChannelType.Phone);
  156. IList<IMessageSender> channels = new List<IMessageSender>();
  157. channels.Add(new NotStopPointMockMessageSender(new NotificationResult(Context.Global, new NotificationId(), NotificationResultStatus.Success, NotificationChannelType.Communicator)));
  158. channels.Add(new MockMessageSender(expectedResult));
  159. ExecuteCompositeMessageSenderTest(expectedResult, channels);
  160. }
  161. private static void ExecuteCompositeMessageSenderTest(NotificationResult expectedResult, IList<IMessageSender> channels)
  162. {
  163. using (AutoResetEvent waitEvent = new AutoResetEvent(false))
  164. {
  165. CompositeMessageSender messageSender = new CompositeMessageSender(channels);
  166. NotificationResult actualResult = null;
  167. messageSender.DeliverMessageCompleted += delegate(object sender, DeliveryCompletedEventArgs e)
  168. {
  169. actualResult = e.Result;
  170. waitEvent.Set();
  171. };
  172. messageSender.DeliverMessageAsync();
  173. waitEvent.WaitOne(1000, false);
  174. Assert.IsNotNull(actualResult);
  175. Assert.AreEqual(expectedResult.HowContacted, actualResult.HowContacted);
  176. Assert.AreEqual(expectedResult.Status, actualResult.Status);
  177. }
  178. }
  179. }
  180. }