예제 #1
0
        public static async Task Run(
            [QueueTrigger(AzureStorageNames.EmailInputDataQueue, Connection = "AzureWebJobsStorage")] string myQueueItem,
            [Queue(AzureStorageNames.EmailOutputDataQueue), StorageAccount("AzureWebJobsStorage")] ICollector <EmailInfo> storage,
            ILogger log)
        {
            ModelSerializer modelMappers = new ModelSerializer();

            log.LogInformation($"C# Queue trigger function processed: ");
            try
            {
                var modelMapper = new ModelMapper();
                if (!string.IsNullOrEmpty(myQueueItem))
                {
                    var handler = new NotifyPartiesQueueHandler(new EmailBuilderService(new TemplateDataService()));
                    var email   = await handler.Handle(myQueueItem, false);

                    if (email != null)
                    {
                        var output = JsonConvert.SerializeObject(email);
                        storage.Add(email);
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Something went wrong with the EmailQueueTrigger {myQueueItem}");
                throw;
            }
        }
        public async Task ServiceHappyPathTestWithInMemoryRepoAndQueuedNotifications()
        {
            var services = new ServiceCollection();

            services.AddTransient <ISecuritySignInService, SecuritySignInMockService>();
            services.AddScoped <IUserAuthorizationService, UserAuthorizationMockService>();
            services.AddSingleton <IUserAuthorizationService, UserAuthorizationService>();
            services.AddSingleton <IPropertyRepository, PropertyRepositoryInMemory>();
            services.AddTransient <ITemplateDataService, TemplateDataService>();
            services.AddSingleton <IEmailService, EmailServiceMock>();
            services.AddSingleton <IQueueClient, QueueClientMock>();
            services.AddTransient <IEmailBuilderService, EmailBuilderService>();
            services.AddTransient <INotifyPartiesService, NotifyPartiesQueueClient>();
            services.AddTransient <NotifyPartiesService> ();
            services.AddSingleton <IPropertyService, PropertyService>();

            var serviceProvider = services.BuildServiceProvider();

            var propertyService = serviceProvider.GetService <IPropertyService>();
            var authService     = serviceProvider.GetService <IUserAuthorizationService>();

            var repo = serviceProvider.GetService <IPropertyRepository>();

            await SharedTests.TestHappyPath(repo, propertyService, authService);

            var queueClient = serviceProvider.GetService <IQueueClient>();
            var messages    = ((QueueClientMock)queueClient).Messages;

            Assert.AreEqual(13, messages.Count);

            var builder = serviceProvider.GetService <IEmailBuilderService>();

            var handler = new NotifyPartiesQueueHandler(builder);
            int count   = 0;

            foreach (var m in messages)
            {
                var email = await handler.Handle(m);

                if (email != null)
                {
                    count++;
                    Assert.IsNotNull(email.Body);
                    Assert.IsNotNull(email.RecipientEmail);
                    Assert.IsNotNull(email.SenderEmail);
                    Assert.IsNotNull(email.Subject);
                }
            }


            Assert.AreEqual(10, count);
        }