Esempio n. 1
0
        public async Task It_should_handle_multiple_notifications()
        {
            const string notificationName         = "multiple-notifications";
            var          registryMock             = new Mock <INotificationRegistry>();
            var          notificationType         = typeof(MultipleNotifications);
            var          notificationHandlerTypes = new List <Type>
            {
                typeof(MultipleNotificationHandler_1),
                typeof(MultipleNotificationHandler_2)
            }.AsEnumerable();
            var outMapping = (notificationType, notificationHandlerTypes);

            registryMock.Setup(registry => registry.TryGetValue(notificationName, out outMapping)).Returns(true);

            var middleware = new NotificationsMiddleware(async _ => { }, registryMock.Object, Mock.Of <ILoggerFactory>());

            var httpContext = await notificationName.InvokeNotificationMiddleware(async ctx =>
            {
                MultipleNotificationHandler_1.ExecutionCounter = 0;
                MultipleNotificationHandler_2.ExecutionCounter = 0;
                await middleware.InvokeAsync(ctx);
            });

            httpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.OK);
            MultipleNotificationHandler_1.ExecutionCounter.Should().Be(1);
            MultipleNotificationHandler_2.ExecutionCounter.Should().Be(1);
        }
Esempio n. 2
0
        public async Task It_should_handle_single_notification()
        {
            const string notificationName = "single-notification";
            var          registryMock = new Mock <INotificationRegistry>();
            var          notificationType = typeof(SingleNotification);
            var          notificationHandlerTypes = new List <Type> {
                typeof(SingleNotificationHandler)
            }.AsEnumerable();
            var outMapping = (notificationType, notificationHandlerTypes);

            registryMock.Setup(registry => registry.TryGetValue(notificationName, out outMapping)).Returns(true);

            var middleware = new NotificationsMiddleware(async _ => { }, registryMock.Object, Mock.Of <ILoggerFactory>());

            var httpContext = await notificationName.InvokeNotificationMiddleware(async ctx =>
            {
                SingleNotificationHandler.ExecutionCounter = 0;
                await middleware.InvokeAsync(ctx);
            });

            httpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.OK);

            var bodyContent = await new StreamReader(httpContext.Response.Body).ReadToEndAsync();

            var jsonBody = JsonDocument.Parse(bodyContent).RootElement;

            jsonBody
            .GetProperty("notificationId").GetGuid()
            .Should().NotBeEmpty();

            SingleNotificationHandler.ExecutionCounter.Should().Be(1);
        }