Esempio n. 1
0
        public async void Given_A_Non_Existing_Job_The_Handler_Should_Return_Null()
        {
            // arrange
            DateTime             date = DateTime.Today;
            string               workshopPlanningId = date.ToString("yyyy-MM-dd");
            Guid                 jobId           = Guid.NewGuid();
            DateTime             actualStartTime = date.AddHours(9);
            DateTime             actualEndTime   = date.AddHours(12);
            FinishMaintenanceJob command         = new FinishMaintenanceJobCommandBuilder()
                                                   .WithJobId(jobId)
                                                   .WithActualStartTime(actualStartTime)
                                                   .WithActualEndTime(actualEndTime)
                                                   .Build();

            Mock <IMessagePublisher> messagePublisherMock = new Mock <IMessagePublisher>();
            Mock <IEventSourceRepository <WorkshopPlanning> > repoMock =
                new Mock <IEventSourceRepository <WorkshopPlanning> >();

            repoMock
            .Setup(m => m.GetByIdAsync(It.Is <string>(p => p == workshopPlanningId)))
            .Returns(Task.FromResult <WorkshopPlanning>(null));

            FinishMaintenanceJobCommandHandler sut =
                new FinishMaintenanceJobCommandHandler(messagePublisherMock.Object, repoMock.Object);

            // act
            var result = await sut.HandleCommandAsync(date, command);

            // assert
            messagePublisherMock.VerifyAll();
            messagePublisherMock.VerifyNoOtherCalls();
            repoMock.VerifyAll();
            repoMock.VerifyNoOtherCalls();
            Assert.Null(result);
        }
Esempio n. 2
0
        public async void FinishMaintenanceJob_Handler_Should_Handle_Command()
        {
            // arrange
            DateTime date               = DateTime.Today;
            Guid     jobId              = Guid.NewGuid();
            DateTime startTime          = date.AddHours(8);
            DateTime endTime            = date.AddHours(11);
            DateTime actualStartTime    = date.AddHours(9);
            DateTime actualEndTime      = date.AddHours(12);
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder()
                .WithDate(date)
                .Build(),
                new MaintenanceJobPlannedEventBuilder()
                .WithStartTime(startTime)
                .WithEndTime(endTime)
                .WithJobId(jobId)
                .Build()
            };
            WorkshopPlanning planning = new WorkshopPlanning(initializingEvents);

            FinishMaintenanceJob command = new FinishMaintenanceJobCommandBuilder()
                                           .WithJobId(jobId)
                                           .WithActualStartTime(actualStartTime)
                                           .WithActualEndTime(actualEndTime)
                                           .Build();

            Mock <IMessagePublisher>           messagePublisherMock = new Mock <IMessagePublisher>(MockBehavior.Strict);
            Mock <IWorkshopPlanningRepository> repoMock             = new Mock <IWorkshopPlanningRepository>(MockBehavior.Strict);

            repoMock
            .Setup(m => m.GetWorkshopPlanningAsync(It.Is <DateTime>(p => p == date)))
            .Returns(Task.FromResult(planning));

            repoMock
            .Setup(m => m.SaveWorkshopPlanningAsync(
                       It.Is <string>(p => p == planning.Id),
                       It.Is <int>(p => p == 2),
                       It.Is <int>(p => p == 3),
                       It.IsAny <IEnumerable <Event> >()
                       ))
            .Returns(Task.CompletedTask);

            messagePublisherMock
            .Setup(m => m.PublishMessageAsync(
                       It.Is <string>(p => p == "MaintenanceJobFinished"),
                       It.IsAny <object>(),
                       ""))
            .Returns(Task.CompletedTask);

            FinishMaintenanceJobCommandHandler sut =
                new FinishMaintenanceJobCommandHandler(messagePublisherMock.Object, repoMock.Object);

            // act
            await sut.HandleCommandAsync(date, command);

            // assert
            messagePublisherMock.VerifyAll();
            repoMock.VerifyAll();
        }
Esempio n. 3
0
        public void Finish_MaintenanceJob_Should_Finish_A_New_MaintenanceJob()
        {
            // arrange
            DateTime date = DateTime.Today;
            string   workshopPlanningId = date.ToString("yyyy-MM-dd");
            Guid     jobId              = Guid.NewGuid();
            DateTime startTime          = date.AddHours(8);
            DateTime endTime            = date.AddHours(11);
            DateTime actualStartTime    = date.AddHours(9);
            DateTime actualEndTime      = date.AddHours(12);
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder()
                .WithDate(date)
                .Build(),
                new MaintenanceJobPlannedEventBuilder()
                .WithStartTime(startTime)
                .WithEndTime(endTime)
                .WithJobId(jobId)
                .Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(date, initializingEvents);

            FinishMaintenanceJob command = new FinishMaintenanceJobCommandBuilder()
                                           .WithJobId(jobId)
                                           .WithActualStartTime(actualStartTime)
                                           .WithActualEndTime(actualEndTime)
                                           .Build();

            // act
            sut.FinishMaintenanceJob(command);
            IEnumerable <Event> events = sut.GetEvents();

            // assert
            Assert.NotNull(sut);
            Assert.NotNull(sut.Id);
            Assert.Equal(workshopPlanningId, sut.Id);
            Assert.Equal(2, sut.OriginalVersion);
            Assert.Equal(3, sut.Version);
            Assert.Collection(sut.Jobs,
                              item0 =>
            {
                Assert.Equal(command.JobId, item0.Id);
                Assert.Equal(startTime, item0.StartTime);
                Assert.Equal(endTime, item0.EndTime);
                Assert.Equal(command.StartTime, item0.ActualStartTime);
                Assert.Equal(command.Notes, item0.Notes);
            }
                              );
            Assert.Collection(events, item0 => Assert.IsAssignableFrom <MaintenanceJobFinished>(item0));
        }
Esempio n. 4
0
        public void Finish_An_Already_Finished_MaintenanceJob_Should_Throw_Exception()
        {
            // arrange
            DateTime date               = DateTime.Today;
            Guid     jobId              = Guid.NewGuid();
            DateTime startTime          = date.AddHours(8);
            DateTime endTime            = date.AddHours(11);
            DateTime actualStartTime    = date.AddHours(9);
            DateTime actualEndTime      = date.AddHours(12);
            string   notes              = "Ok";
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder()
                .WithDate(date)
                .Build(),
                new MaintenanceJobPlannedEventBuilder()
                .WithStartTime(startTime)
                .WithEndTime(endTime)
                .WithJobId(jobId)
                .Build(),
                new MaintenanceJobFinishedEventBuilder()
                .WithJobId(jobId)
                .WithActualStartTime(actualStartTime)
                .WithActualEndTime(actualEndTime)
                .WithNotes(notes)
                .Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(initializingEvents);

            FinishMaintenanceJob command = new FinishMaintenanceJobCommandBuilder()
                                           .WithJobId(jobId)
                                           .WithActualStartTime(actualStartTime)
                                           .WithActualEndTime(actualEndTime)
                                           .WithNotes(notes)
                                           .Build();

            // act
            var thrownException =
                Assert.Throws <BusinessRuleViolationException>(() =>
                                                               sut.FinishMaintenanceJob(command));

            // assert
            Assert.Equal("An already finished job can not be finished.",
                         thrownException.Message);
        }