FoghornServiceTests.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // <copyright file="FoghornServiceTests.cs" company="Objective Advantage Europe Ltd.">
  2. //
  3. // Copyright (c) Objective Advantage Europe Ltd. 2011 - 2013 All Right Reserved.
  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. // </copyright>
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using Foghorn.Core;
  23. using Foghorn.WcfService;
  24. using NLog;
  25. using Xunit;
  26. namespace Foghorn.Test
  27. {
  28. public class FoghornServiceTests
  29. {
  30. private const string ApplicationTestName = "TestApplication";
  31. private const int NumNotificationTypes = 3;
  32. private const string NotificationName = "TestNotification";
  33. private const string FailureMessage = "Failed while testing.";
  34. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  35. private int _testApplicationId;
  36. [Fact]
  37. public void TestRegisterSendingApplication_3NotificationTypes_ApplicationAndNotificationsInDatabase()
  38. {
  39. var dataContext = new FoghornEntities();
  40. DeleteTestApplications(dataContext);
  41. var service = new FoghornService();
  42. RegisterTestApplication(service);
  43. Assert.True(_testApplicationId > 0);
  44. var application =
  45. dataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == ApplicationTestName);
  46. Assert.NotNull(application);
  47. Assert.Equal(NumNotificationTypes, application.NotificationTypes.Count);
  48. Assert.True(application.NotificationTypes.First().NotificationTypeName.StartsWith(NotificationName));
  49. Assert.Equal(_testApplicationId, application.SendingApplicationId);
  50. }
  51. [Fact]
  52. public void TestRegisterSubscription_ApplicationExists_SubscriberRegistered()
  53. {
  54. var service = new FoghornService();
  55. if (_testApplicationId <= 0)
  56. {
  57. RegisterTestApplication(service);
  58. }
  59. var subscriberDto = new SubscriberDto
  60. {
  61. HostName = "localhost",
  62. Password = "elmo123!",
  63. SubscriberName = ApplicationTestName + "TestSubscriber",
  64. };
  65. var subscriberId = service.RegisterSubscription(subscriberDto, ApplicationTestName);
  66. var dataContext = new FoghornEntities();
  67. var subscriber = dataContext.Subscribers.FirstOrDefault(x => x.SubscriberId == subscriberId);
  68. Assert.NotNull(subscriber);
  69. }
  70. [Fact]
  71. public void TestNotify_SetUpConfigurationAndCallNotify_NotificationSentAndLogged()
  72. {
  73. TestRegisterSubscription_ApplicationExists_SubscriberRegistered();
  74. var service = new FoghornService();
  75. var notificationDto = new NotificationDto
  76. {
  77. NotificationMessage = "This is the message of the test notification.",
  78. NotificationTypeName = NotificationName + "1",
  79. NotificationTitle = "Notification Title",
  80. Sticky = true,
  81. Priority = 2
  82. };
  83. var notificationDtoReturned = service.Notify(notificationDto, ApplicationTestName);
  84. var notificationId = notificationDtoReturned.NotificationId;
  85. var dataContext = new FoghornEntities();
  86. var checkNotification = dataContext.Notifications.FirstOrDefault(x => x.NotificationId == notificationId);
  87. Assert.NotNull(checkNotification);
  88. }
  89. [Fact]
  90. public void TestNotify_ApplicationNotRegistered_ReturnsErrorLogsFailure()
  91. {
  92. var service = new FoghornService();
  93. var notificationDto = new NotificationDto
  94. {
  95. NotificationMessage = "This is the message of the test notification.",
  96. NotificationTypeName = NotificationName + "1",
  97. NotificationTitle = "Notification Title"
  98. };
  99. Assert.Throws<ArgumentException>(delegate { service.Notify(notificationDto, "IncorrectApplication"); });
  100. }
  101. private void RegisterTestApplication(IFoghornService service)
  102. {
  103. var notificationTypeDtos = new List<NotificationTypeDto>();
  104. for (var i = 0; i < NumNotificationTypes; i++)
  105. {
  106. var notificationTypeDto = new NotificationTypeDto
  107. {
  108. NotificationTypeName = NotificationName + i,
  109. NotificationTypeDisplayName = "Test Notfication " + i
  110. };
  111. try
  112. {
  113. var iconFilePath = string.Format(@"..\..\NotificationIcons\{0}{1}.png", NotificationName, i);
  114. var iconFile = File.OpenRead(iconFilePath);
  115. var iconBuffer = new byte[iconFile.Length];
  116. iconFile.Read(iconBuffer, 0, (int) iconFile.Length);
  117. notificationTypeDto.NotificationTypeIcon = iconBuffer;
  118. }
  119. catch (Exception exception)
  120. {
  121. Logger.LogException(LogLevel.Error, FailureMessage, exception);
  122. throw;
  123. }
  124. notificationTypeDtos.Add(notificationTypeDto);
  125. }
  126. var sendingApplicationDto = new SendingApplicationDto {SendingApplicationName = ApplicationTestName};
  127. try
  128. {
  129. var iconFilePath = string.Format(@"..\..\NotificationIcons\Icon.png");
  130. var iconFile = File.OpenRead(iconFilePath);
  131. var iconBuffer = new byte[iconFile.Length];
  132. iconFile.Read(iconBuffer, 0, (int) iconFile.Length);
  133. sendingApplicationDto.SendingApplicationIcon = iconBuffer;
  134. }
  135. catch (Exception exception)
  136. {
  137. Logger.LogException(LogLevel.Error, FailureMessage, exception);
  138. throw;
  139. }
  140. _testApplicationId = service.RegisterSendingApplication(sendingApplicationDto, notificationTypeDtos);
  141. }
  142. private void DeleteTestApplications(FoghornEntities dataContext)
  143. {
  144. var testApplications =
  145. dataContext.SendingApplications.Where(x => x.SendingApplicationName.StartsWith(ApplicationTestName));
  146. foreach (var sendingApplication in testApplications)
  147. {
  148. var subscribersToRemove = sendingApplication.Subscribers;
  149. foreach (var subscriber in subscribersToRemove)
  150. {
  151. var notifications = subscriber.NotificationsSent.ToList();
  152. foreach (var notification in notifications)
  153. {
  154. dataContext.Notifications.Remove(notification);
  155. }
  156. }
  157. dataContext.SendingApplications.Remove(sendingApplication);
  158. }
  159. dataContext.SaveChanges();
  160. _testApplicationId = 0;
  161. }
  162. }
  163. }