123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
-
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using Foghorn.Core;
- using Foghorn.WcfService;
- using NLog;
- using Xunit;
- namespace Foghorn.Test
- {
- public class FoghornServiceTests
- {
- private const string ApplicationTestName = "TestApplication";
- private const int NumNotificationTypes = 3;
- private const string NotificationName = "TestNotification";
- private const string FailureMessage = "Failed while testing.";
- private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
- private int _testApplicationId;
- [Fact]
- public void TestRegisterSendingApplication_3NotificationTypes_ApplicationAndNotificationsInDatabase()
- {
- var dataContext = new FoghornEntities();
- DeleteTestApplications(dataContext);
- var service = new FoghornService();
- RegisterTestApplication(service);
- Assert.True(_testApplicationId > 0);
- var application =
- dataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == ApplicationTestName);
- Assert.NotNull(application);
- Assert.Equal(NumNotificationTypes, application.NotificationTypes.Count);
- Assert.True(application.NotificationTypes.First().NotificationTypeName.StartsWith(NotificationName));
- Assert.Equal(_testApplicationId, application.SendingApplicationId);
- }
- [Fact]
- public void TestRegisterSubscription_ApplicationExists_SubscriberRegistered()
- {
- var service = new FoghornService();
- if (_testApplicationId <= 0)
- {
- RegisterTestApplication(service);
- }
- var subscriberDto = new SubscriberDto
- {
- HostName = "localhost",
- Password = "elmo123!",
- SubscriberName = ApplicationTestName + "TestSubscriber",
- };
- var subscriberId = service.RegisterSubscription(subscriberDto, ApplicationTestName);
- var dataContext = new FoghornEntities();
- var subscriber = dataContext.Subscribers.FirstOrDefault(x => x.SubscriberId == subscriberId);
- Assert.NotNull(subscriber);
- }
- [Fact]
- public void TestNotify_SetUpConfigurationAndCallNotify_NotificationSentAndLogged()
- {
- TestRegisterSubscription_ApplicationExists_SubscriberRegistered();
- var service = new FoghornService();
- var notificationDto = new NotificationDto
- {
- NotificationMessage = "This is the message of the test notification.",
- NotificationTypeName = NotificationName + "1",
- NotificationTitle = "Notification Title",
- Sticky = true,
- Priority = 2
- };
- var notificationDtoReturned = service.Notify(notificationDto, ApplicationTestName);
- var notificationId = notificationDtoReturned.NotificationId;
- var dataContext = new FoghornEntities();
- var checkNotification = dataContext.Notifications.FirstOrDefault(x => x.NotificationId == notificationId);
- Assert.NotNull(checkNotification);
- }
- [Fact]
- public void TestNotify_ApplicationNotRegistered_ReturnsErrorLogsFailure()
- {
- var service = new FoghornService();
- var notificationDto = new NotificationDto
- {
- NotificationMessage = "This is the message of the test notification.",
- NotificationTypeName = NotificationName + "1",
- NotificationTitle = "Notification Title"
- };
- Assert.Throws<ArgumentException>(delegate { service.Notify(notificationDto, "IncorrectApplication"); });
- }
- private void RegisterTestApplication(IFoghornService service)
- {
- var notificationTypeDtos = new List<NotificationTypeDto>();
- for (var i = 0; i < NumNotificationTypes; i++)
- {
- var notificationTypeDto = new NotificationTypeDto
- {
- NotificationTypeName = NotificationName + i,
- NotificationTypeDisplayName = "Test Notfication " + i
- };
- try
- {
- var iconFilePath = string.Format(@"..\..\NotificationIcons\{0}{1}.png", NotificationName, i);
- var iconFile = File.OpenRead(iconFilePath);
- var iconBuffer = new byte[iconFile.Length];
- iconFile.Read(iconBuffer, 0, (int) iconFile.Length);
- notificationTypeDto.NotificationTypeIcon = iconBuffer;
- }
- catch (Exception exception)
- {
- Logger.LogException(LogLevel.Error, FailureMessage, exception);
- throw;
- }
- notificationTypeDtos.Add(notificationTypeDto);
- }
- var sendingApplicationDto = new SendingApplicationDto {SendingApplicationName = ApplicationTestName};
- try
- {
- var iconFilePath = string.Format(@"..\..\NotificationIcons\Icon.png");
- var iconFile = File.OpenRead(iconFilePath);
- var iconBuffer = new byte[iconFile.Length];
- iconFile.Read(iconBuffer, 0, (int) iconFile.Length);
- sendingApplicationDto.SendingApplicationIcon = iconBuffer;
- }
- catch (Exception exception)
- {
- Logger.LogException(LogLevel.Error, FailureMessage, exception);
- throw;
- }
- _testApplicationId = service.RegisterSendingApplication(sendingApplicationDto, notificationTypeDtos);
- }
- private void DeleteTestApplications(FoghornEntities dataContext)
- {
- var testApplications =
- dataContext.SendingApplications.Where(x => x.SendingApplicationName.StartsWith(ApplicationTestName));
- foreach (var sendingApplication in testApplications)
- {
- var subscribersToRemove = sendingApplication.Subscribers;
- foreach (var subscriber in subscribersToRemove)
- {
- var notifications = subscriber.NotificationsSent.ToList();
- foreach (var notification in notifications)
- {
- dataContext.Notifications.Remove(notification);
- }
- }
- dataContext.SendingApplications.Remove(sendingApplication);
- }
- dataContext.SaveChanges();
- _testApplicationId = 0;
- }
- }
- }
|