Example #1
0
        public static Notification Notify(NotificationDto notificationDto, string sendingApplicationName)
        {
            var dataContext = new FoghornEntities();

            var sendingApplication =
                dataContext.SendingApplications.FirstOrDefault(x => x.SendingApplicationName == sendingApplicationName);

            if (sendingApplication == null)
            {
                var exception =
                    new ArgumentException("sendingApplicationName must match a previously registered SendingApplication",
                                          "sendingApplicationName");
                Logger.ErrorException(FailureMessage, exception);
                throw exception;
            }

            if (notificationDto == null)
            {
                var exception = new ArgumentException("notification must not be null", "notificationDto");
                Logger.ErrorException(FailureMessage, exception);
                throw exception;
            }

            if (notificationDto.Priority > 2) notificationDto.Priority = 2;
            if (notificationDto.Priority < -2) notificationDto.Priority = -2;

            var growlNotification = new Growl.Connector.Notification(sendingApplication.SendingApplicationName,
                                                                     notificationDto.NotificationTypeName,
                                                                     notificationDto.NotificationId.ToString(
                                                                         CultureInfo.InvariantCulture),
                                                                     notificationDto.NotificationTitle,
                                                                     notificationDto.NotificationMessage)
                {
                    Sticky = notificationDto.Sticky,
                    Priority = (Priority) notificationDto.Priority
                };

            var notification = notificationDto.ToEntity();
            foreach (var subscriber in sendingApplication.Subscribers)
            {
                var port = subscriber.Port.HasValue ? subscriber.Port.Value : Settings.Default.GrowlDefaultPort;
                var growlConnector = new GrowlConnector(subscriber.Password, subscriber.HostName, port);
                growlConnector.Notify(growlNotification);
                subscriber.NotificationsSent.Add(notification);
            }

            notification.SentDateTime = DateTime.UtcNow;
            notification.NotificationType =
                dataContext.NotificationTypes.FirstOrDefault(
                    x => x.NotificationTypeName == notificationDto.NotificationTypeName);
            dataContext.Notifications.Add(notification);
            dataContext.SaveChanges();
            return notification;
        }
 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);
 }
        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);
        }
 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;
 }
 public void TestRegisterSubscription_ApplicationExists_SubscriberRegistered()
 {
     var service = new FoghornService();
     if (_testApplicationId <= 0)
     {
         RegisterTestApplication(service);
     }
     var subscriberDto = new SubscriberDto
         {
             HostName = "localhost",
             Password = "******",
             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);
 }