Exemple #1
0
        public async Task After_command_is_executed__if_email_service_encounters_error__aggregate_contains_EmailSendingFailed_event()
        {
            // Arrange
            var enrollmentId = EnrollmentAggregate.EnrollmentId.New;
            var event1       = new DomainEvent <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId, RecruitmentFormSubmitted>(
                new RecruitmentFormSubmitted(
                    SystemClock.Instance.GetCurrentInstant(),
                    "Andrzej", "Strzelba",
                    EmailAddress.Parse("*****@*****.**"), Consts.FakePhoneNumber,
                    "ala ma kota", 1, "Wolne Miasto Gdańsk", new[] { "Wadowice" }, new[] { 1 }, true),
                new Metadata(), DateTimeOffset.Now, enrollmentId, 1
                );
            var event2 = new DomainEvent <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId, CandidateAcceptedTrainingInvitation>(
                new CandidateAcceptedTrainingInvitation(Guid.NewGuid(), CommunicationChannel.OutgoingEmail, 1, string.Empty),
                new Metadata(), DateTimeOffset.Now, enrollmentId, 2
                );
            var enrollment = new EnrollmentAggregate(enrollmentId);

            enrollment.ApplyEvents(new IDomainEvent[] { event1, event2 });

            var aggregateStore = new MockAggregateStore <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId>(enrollment);
            var emailService   = new FailingEmailService();

            var options = Mock.Of <IOptions <Config> >(mock =>
                                                       mock.Value == new Config()
            {
                TrainingReminderEmail = new Config.EmailMessageConfig()
                {
                    Subject = "przypomnienie o szkoleniu", Body = "przypominamy o szkoleniu"
                }
            });


            // Act
            var training     = CreateTrainingWithIdAndOffset(1, Duration.FromHours(12));
            var trainingRepo = Mock.Of <ITrainingRepository>(repo => repo.GetById(1) == Task.FromResult(Maybe <Training> .From(training)));

            var handler = new SendTrainingReminderHandler(SystemClock.Instance, aggregateStore, trainingRepo, emailService, options, new FluidTemplateRenderer());

            var command = new SendTrainingReminder.Command()
            {
                EnrollmentId = enrollmentId.GetGuid(), TrainingId = 1
            };
            var result = await handler.Handle(command, CancellationToken.None);


            // Assert
            result.IsSuccess.Should().BeTrue();
            enrollment.UncommittedEvents.Should().ContainSingle().Which.AggregateEvent.Should().BeOfType <EmailSendingFailed>();
            var newEvent = enrollment.UncommittedEvents.Single().AggregateEvent as EmailSendingFailed;

            newEvent.Subject.Should().Be("przypomnienie o szkoleniu");
            newEvent.Body.Should().Be("przypominamy o szkoleniu");
            emailService.FailedMessages.Should().ContainSingle();
            var emailMessage = emailService.FailedMessages.Single();

            emailMessage.Subject.Should().Be("przypomnienie o szkoleniu");
            emailMessage.Body.Should().Be("przypominamy o szkoleniu");
            emailMessage.To.Should().ContainSingle().Which.Should().Be(EmailAddress.Parse("*****@*****.**"));
        }
Exemple #2
0
        public async Task After_command_is_executed__email_is_sent_and_aggregate_contains_EmailSent_event()
        {
            // Arrange
            var enrollmentId = EnrollmentAggregate.EnrollmentId.New;
            var event1       = new DomainEvent <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId, RecruitmentFormSubmitted>(
                new RecruitmentFormSubmitted(
                    SystemClock.Instance.GetCurrentInstant(),
                    "Andrzej", "Strzelba",
                    EmailAddress.Parse("*****@*****.**"), Consts.FakePhoneNumber,
                    "ala ma kota", 1, "Wolne Miasto Gdańsk", new[] { "Wadowice" }, new[] { 1 }, true),
                new Metadata(), DateTimeOffset.Now, enrollmentId, 1
                );
            var event2 = new DomainEvent <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId, CandidateAcceptedTrainingInvitation>(
                new CandidateAcceptedTrainingInvitation(Guid.NewGuid(), CommunicationChannel.OutgoingEmail, 1, string.Empty),
                new Metadata(), DateTimeOffset.Now, enrollmentId, 2
                );
            var enrollment = new EnrollmentAggregate(enrollmentId);

            enrollment.ApplyEvents(new IDomainEvent[] { event1, event2 });

            var aggregateStore = new MockAggregateStore <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId>(enrollment);
            var training       = CreateTrainingWithIdAndOffset(1, Duration.FromHours(12));
            var trainingRepo   = Mock.Of <ITrainingRepository>(repo => repo.GetById(1) == Task.FromResult(Maybe <Training> .From(training)));
            var emailService   = new SucceedingEmailService();

            var options = Mock.Of <IOptions <Config> >(mock =>
                                                       mock.Value == new Config()
            {
                TrainingReminderEmail = new Config.EmailMessageConfig()
                {
                    Subject = "przypomnienie o szkoleniu",
                    Body    = "{{ Candidate.FullName }} przypominamy o szkoleniu w {{ Training.City }} w dniu {{ Training.StartDate }}" +
                              " o godzinie {{ Training.StartTime }} trwającym {{ Training.Duration }} godzin!"
                }
            });

            // Act
            var handler = new SendTrainingReminderHandler(SystemClock.Instance, aggregateStore, trainingRepo, emailService, options, new FluidTemplateRenderer());

            var command = new SendTrainingReminder.Command()
            {
                EnrollmentId = enrollmentId.GetGuid(), TrainingId = 1
            };
            var result = await handler.Handle(command, CancellationToken.None);


            // Assert
            result.IsSuccess.Should().BeTrue();
            enrollment.UncommittedEvents.Should().ContainSingle();
            enrollment.UncommittedEvents.Single().AggregateEvent.Should().BeOfType <EmailSent>();
            emailService.SentMessages.Should().ContainSingle();
            emailService.SentMessages.Single().Should().BeEquivalentTo(new {
                Subject = "przypomnienie o szkoleniu",
                Body    = $"Andrzej Strzelba przypominamy o szkoleniu w Wadowice w dniu {training.StartDateTime.Date}" +
                          $" o godzinie {training.StartDateTime.TimeOfDay} trwającym {training.Duration.ToString("HH':'mm", null)} godzin!"
            });
        }