Ejemplo n.º 1
0
        public async Task <IActionResult> PostNotifications(string applicationName, [FromBody] WebNotificationRequestItemsContainer webNotificationRequestsContainer, [FromServices] INotificationsChannel notificationsChannel)
        {
            IActionResult result = null;

            using (this.logger.BeginScope($"RootActivityId: {this.RootActivityId}"))
            {
                this.logger.LogInformation($"Started {nameof(this.PostNotifications)} method of {nameof(NotificationsController)}.");
                if (string.IsNullOrWhiteSpace(applicationName))
                {
                    throw new ArgumentException("Application name is mandatory.", nameof(applicationName));
                }

                if (webNotificationRequestsContainer is null)
                {
                    throw new ArgumentNullException(nameof(webNotificationRequestsContainer));
                }

                if (notificationsChannel == null)
                {
                    throw new ArgumentNullException(nameof(notificationsChannel));
                }

                IEnumerable <WebNotification> notifications = await this.notificationsManager.ProcessNotificationsAsync(applicationName, webNotificationRequestsContainer.Notifications).ConfigureAwait(false);

                foreach (var notification in notifications.ToList())
                {
                    _ = notificationsChannel.AddNotificationAsync(notification);
                }

                result = this.Accepted();
                this.logger.LogInformation($"Finished {nameof(this.PostNotifications)} method of {nameof(NotificationsController)}.");
            }

            return(result);
        }
        public void PostNotificationsTests_NullChannel()
        {
            WebNotificationRequestItemsContainer webNotificationRequestItemsContainer = new WebNotificationRequestItemsContainer();
            var ex = Assert.ThrowsAsync <ArgumentNullException>(async() => await this.NotificationsControllerObject.PostNotifications(this.ApplicationName, webNotificationRequestItemsContainer, null).ConfigureAwait(false));

            Assert.IsTrue(ex.ParamName.Equals("notificationsChannel", StringComparison.Ordinal));
        }
        public void PostNotificationsTests_ValidInputs()
        {
            string notificationId = Guid.NewGuid().ToString();
            WebNotificationRequestItemsContainer webNotificationRequestItemsContainer = new WebNotificationRequestItemsContainer
            {
                Notifications = new List <WebNotificationRequestItem>
                {
                    new WebNotificationRequestItem
                    {
                        Title          = "Test Title",
                        Body           = "Test Body",
                        NotificationId = notificationId,
                    },
                },
            };

            List <WebNotification> webNotifications = new List <WebNotification>
            {
                new WebNotification
                {
                    NotificationId = notificationId,
                    Title          = "Test Title",
                    Body           = "Test Body",
                },
            };

            _ = this.NotificationsManagerMock.Setup(nm => nm.ProcessNotificationsAsync(this.ApplicationName, webNotificationRequestItemsContainer.Notifications)).Returns(Task.FromResult(webNotifications.AsEnumerable()));
            _ = this.NotificationsChannelMock.Setup(ncc => ncc.AddNotificationAsync(It.IsAny <WebNotification>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(true));
            var result = this.NotificationsControllerObject.PostNotifications(this.ApplicationName, webNotificationRequestItemsContainer, this.NotificationsChannelMock.Object);

            Assert.IsTrue(result.Result.GetType().FullName.Equals(typeof(AcceptedResult).FullName, StringComparison.Ordinal));
            this.NotificationsChannelMock.Verify(ncc => ncc.AddNotificationAsync(It.IsAny <WebNotification>(), It.IsAny <CancellationToken>()), Times.Once);
        }
        public void PostNotificationsTests_InvalidApplicationName(string applicationName)
        {
            WebNotificationRequestItemsContainer webNotificationRequestItemsContainer = new WebNotificationRequestItemsContainer();
            var ex = Assert.ThrowsAsync <ArgumentException>(async() => await this.NotificationsControllerObject.PostNotifications(applicationName, webNotificationRequestItemsContainer, this.NotificationsChannelMock.Object).ConfigureAwait(false));

            Assert.IsTrue(ex.Message.StartsWith("Application name is mandatory.", StringComparison.Ordinal));
        }