public async Task WhenSettingDisabledOrUnsupportedThenExecutionSkipped()
        {
            var settings = new UserSettings
            {
                Settings = new List <NotificationSetting> {
                    new NotificationSetting(NotificationType.Push, NotificationEvent.ArticleCreated, false)
                }
            };

            var pushSender = new Mock <IPushNotificationSender>();
            var factory    = new Mock <IMessageFactory <PushNotificationMessage> >();

            var sender = new PushNotificationSender(pushSender.Object, factory.Object, Provider, _loggerFactory);
            var result = await sender.SendAsync(new NotificationMessage
            {
                Event      = NotificationEvent.ArticleCreated,
                Parameters = new Dictionary <string, object>()
            }, settings);

            Assert.NotNull(result);
            Assert.Equal(NotificationSendingStatus.Skipped, result.Status);

            result = await sender.SendAsync(new NotificationMessage
            {
                Event      = NotificationEvent.PackageArrived,
                Parameters = new Dictionary <string, object>()
            }, settings);

            Assert.NotNull(result);
            Assert.Equal(NotificationSendingStatus.Skipped, result.Status);

            pushSender.Verify(x => x.SendAsync(It.IsAny <PushNotificationMessage>(), It.IsAny <string>()), Times.Never);
        }
        public async Task WhenValidationFailedThenExecutionFailed()
        {
            var settings = new UserSettings
            {
                Settings = new List <NotificationSetting> {
                    new NotificationSetting(NotificationType.Push, NotificationEvent.ArticleCreated, true)
                }
            };

            var message = new NotificationMessage
            {
                Event      = NotificationEvent.ArticleCreated,
                Parameters = new Dictionary <string, object>()
            };

            var pushSender = new Mock <IPushNotificationSender>();
            var factory    = new Mock <IMessageFactory <PushNotificationMessage> >();

            factory.Setup(x => x.Create(It.Is <NotificationMessage>(notificationMessage => notificationMessage == message),
                                        It.Is <UserSettings>(userSettings => userSettings == settings)))
            .Returns(new ArticleCreatedPushMessage());

            var sender = new PushNotificationSender(pushSender.Object, factory.Object, Provider, _loggerFactory);
            var result = await sender.SendAsync(message, settings);

            Assert.NotNull(result);
            Assert.Equal(NotificationSendingStatus.Failed, result.Status);
            Assert.True(result.Errors.Any());

            pushSender.Verify(x => x.SendAsync(It.IsAny <PushNotificationMessage>(), It.IsAny <string>()), Times.Never);
        }
        public async Task MessageSentSuccessfully()
        {
            var settings = new UserSettings
            {
                Settings = new List <NotificationSetting> {
                    new NotificationSetting(NotificationType.Push, NotificationEvent.ArticleCreated, true)
                }
            };

            var message = new NotificationMessage
            {
                Event      = NotificationEvent.ArticleCreated,
                Parameters = new Dictionary <string, object>()
            };

            var factory = new Mock <IMessageFactory <PushNotificationMessage> >();

            factory.Setup(x => x.Create(It.Is <NotificationMessage>(notificationMessage => notificationMessage == message),
                                        It.Is <UserSettings>(userSettings => userSettings == settings)))
            .Returns(new ArticleCreatedPushMessage {
                ArticleId = Guid.NewGuid()
            });

            var pushSender = new Mock <IPushNotificationSender>();

            pushSender.Setup(x => x.SendAsync(It.IsAny <PushNotificationMessage>(), It.IsAny <string>()))
            .ReturnsAsync(true);

            var sender = new PushNotificationSender(pushSender.Object, factory.Object, Provider, _loggerFactory);
            var result = await sender.SendAsync(message, settings);

            Assert.NotNull(result);
            Assert.Equal(NotificationSendingStatus.Success, result.Status);
            Assert.False(result.Errors.Any());
        }
Example #4
0
        public static async Task ProcessQueueMessage([ServiceBusTrigger("dsbforsinketqueue")] BrokeredMessage message, TextWriter log)
        {
            log.WriteLine($"Got message: {message}");
            var isDebugMode = Convert.ToBoolean(ConfigurationManager.AppSettings["APP_DEBUG_MODE"]);

            log.WriteLine($"DEBUG MODE: {isDebugMode}");

            string stationId  = (string)message.Properties["station"];
            string tag        = (string)message.Properties["tag"];
            bool   isTestSend = (bool)message.Properties["istest"];
            var    service    = new DSBLabsStationService(new Uri(BaseUrl));

            log.WriteLine($"stationId: {stationId} tag: {tag} isTestSend: {isTestSend}");
            log.WriteLine("Preparing the query.");

            var delayedDeparturesQuery =
                from departure in service.Queue
                where (departure.StationUic == stationId) &&
                (departure.Cancelled == true || departure.DepartureDelay > 0 || isDebugMode)
                select departure;

            log.WriteLine("Executing the query.");
            var delayedDepartures = delayedDeparturesQuery.ToList();

            log.WriteLine("Query executed.");
            log.WriteLine($"Delayed departures: {delayedDepartures.Count}.");

            if (delayedDepartures.Any() || isTestSend || isDebugMode)
            {
                log.WriteLine($"Sending push message to tag: {tag}.");

                delayedDepartures = delayedDepartures.OrderBy(d => d.ScheduledDeparture).ToList();

                Dictionary <string, string> messageData = new Dictionary <string, string>
                {
                    ["delayedCount"] = delayedDepartures.Count.ToString(CultureInfo.InvariantCulture)
                };

                foreach (var departure in delayedDepartures.Take(5).Select((data, index) => new { index, data }))
                {
                    var destinationName = string.IsNullOrWhiteSpace(departure.data.Line)
                                                ?  departure.data.DestinationName
                                                : $"{departure.data.Line} <i>{departure.data.DestinationName}</i>";
                    messageData[$"departureName{departure.index}"] = destinationName;
                    messageData[$"departureTime{departure.index}"] = departure.data.ScheduledDeparture.HasValue
                                                                        ? departure.data.ScheduledDeparture.Value.ToString("HH:mm", CultureInfo.InvariantCulture)
                                                                        : string.Empty;
                    long delayInMinutes = (departure.data.DepartureDelay / 60) ?? 0;
                    messageData[$"departureDelay{departure.index}"] = Convert.ToString(delayInMinutes);
                }

                var notificationSender = new PushNotificationSender(log.WriteLine);

                var notificationOutcome = await notificationSender.SendAsync(messageData, tag);

                log.WriteLine($"State: {notificationOutcome.State}");
                log.WriteLine($"Success: {notificationOutcome.Success}");
                log.WriteLine($"Failure: {notificationOutcome.Failure}");
                log.WriteLine($"NotificationId: {notificationOutcome.NotificationId}");
                log.WriteLine($"TrackingId: {notificationOutcome.TrackingId}");
            }
        }