public async Task Given_A_Non_Existing_Command_Handler_Should_Return_NullAsync() { // arrange var date = DateTime.Today; var jobId = Guid.NewGuid(); var actualStartTime = date.AddHours(9); var actualEndTime = date.AddHours(12); var description = "Job has been updated"; var command = new UpdateMaintenanceJobCommandBuilder() .WithChangedDescription(description) .Build(); var messagePublisherMock = new Mock <IMessagePublisher>(); var repoMock = new Mock <IWorkshopPlanningRepository>(); repoMock .Setup(m => m.GetWorkshopPlanningAsync(It.Is <DateTime>(p => p == date))) .Returns(Task.FromResult <WorkshopPlanning>(null)); var sut = new UpdateMaintenanceJobCommandHandler(messagePublisherMock.Object, repoMock.Object); // act var result = await sut.HandleCommandAsync(date, command); // assert messagePublisherMock.VerifyAll(); messagePublisherMock.VerifyNoOtherCalls(); repoMock.VerifyAll(); repoMock.VerifyNoOtherCalls(); Assert.Null(result); }
public void Updating_A_Job_That_Conflicts_With_MaintenanceJobs_In_Parallel_Should_Throw_Exception() { // arrange var date = DateTime.Today; var jobId = Guid.NewGuid(); var startTime = date.AddHours(12); var endTime = date.AddHours(13); var createdEvent = new WorkshopPlanningCreatedEventBuilder() .WithDate(date) .Build(); var plannedEvent1 = new MaintenanceJobPlannedEventBuilder() .Build(); var plannedEvent2 = new MaintenanceJobPlannedEventBuilder() .Build(); var plannedEvent3 = new MaintenanceJobPlannedEventBuilder() .Build(); var plannedEvent4 = new MaintenanceJobPlannedEventBuilder() .WithStartTime(startTime) .WithEndTime(endTime) .WithJobId(jobId) .Build(); var initializingEvents = new Event[] { createdEvent, plannedEvent1, plannedEvent2, plannedEvent3, plannedEvent4 }; var sut = new WorkshopPlanning(date, initializingEvents); var command = new UpdateMaintenanceJobCommandBuilder() .WithEvent(plannedEvent4) .WithChangedStartTime(plannedEvent1.StartTime) .WithChangedEndTime(plannedEvent1.EndTime) .Build(); // act var thrownException = Assert.Throws <BusinessRuleViolationException>(() => { sut.UpdateMaintenanceJob(command); // 4th job is updated to conflict with the 1st job }); // assert Assert.Equal("Maintenancejob overlaps with more than 3 other jobs.", thrownException.Message); }
public void Update_MaintenanceJob_That_Spans_Two_Days_Should_Throw_Exception() { // arrange var date = DateTime.Today; var jobId = Guid.NewGuid(); var startTime = date.AddHours(8); var endTime = date.AddHours(11); var createdEvent = new WorkshopPlanningCreatedEventBuilder() .WithDate(date) .Build(); var plannedEvent = new MaintenanceJobPlannedEventBuilder() .WithStartTime(startTime) .WithEndTime(endTime) .WithJobId(jobId) .Build(); var initializingEvents = new Event[] { createdEvent, plannedEvent }; var sut = new WorkshopPlanning(date, initializingEvents); var command = new UpdateMaintenanceJobCommandBuilder() .WithEvent(plannedEvent) .WithChangedStartTime(startTime.AddDays(-1)) .Build(); // act var thrownException = Assert.Throws <BusinessRuleViolationException>(() => sut.UpdateMaintenanceJob(command)); // assert Assert.Equal("Start-time and end-time of a Maintenance Job must be within a 1 day.", thrownException.Message); }
public async void UpdateMaintenanceJob_Handler_Should_Handle_Command() { var date = DateTime.Today; var jobId = Guid.NewGuid(); var startTime = date.AddHours(8); var endTime = date.AddHours(11); var description = "Job has been updated"; var createdEvent = new WorkshopPlanningCreatedEventBuilder() .WithDate(date) .Build(); var plannedEvent = new MaintenanceJobPlannedEventBuilder() .WithStartTime(startTime) .WithEndTime(endTime) .WithJobId(jobId) .Build(); var initializingEvents = new Event[] { createdEvent, plannedEvent }; var planning = new WorkshopPlanning(date, initializingEvents); var command = new UpdateMaintenanceJobCommandBuilder() .WithEvent(plannedEvent) .WithChangedDescription(description) .Build(); var messagePublisherMock = new Mock <IMessagePublisher>(); var repositoryMock = new Mock <IWorkshopPlanningRepository>(); repositoryMock .Setup(m => m.GetWorkshopPlanningAsync(It.Is <DateTime>(p => p == date))) .Returns(Task.FromResult(planning)); repositoryMock .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 <List <Event> >() )) .Returns(Task.CompletedTask); messagePublisherMock .Setup(m => m.PublishMessageAsync( It.Is <string>(p => p == "MaintenanceJobUpdated"), It.IsAny <object>(), "")) .Returns(Task.CompletedTask); var sut = new UpdateMaintenanceJobCommandHandler(messagePublisherMock.Object, repositoryMock.Object); // act var result = await sut.HandleCommandAsync(date, command); // assert messagePublisherMock.VerifyAll(); messagePublisherMock.VerifyNoOtherCalls(); repositoryMock.VerifyAll(); repositoryMock.VerifyNoOtherCalls(); Assert.IsAssignableFrom <WorkshopPlanning>(result); Assert.NotEmpty(result.Jobs.Where(j => j.Description == description)); }
public void Updating_A_MaintenanceJobs_For_The_Same_Vehicle_with_Time_Conflict_Should_Throw_Exception() { // arrange var date = DateTime.Today; var jobId1 = Guid.NewGuid(); var jobId2 = Guid.NewGuid(); var jobId3 = Guid.NewGuid(); var startTime1 = date.AddHours(12); var endTime1 = date.AddHours(13); var startTime2 = date.AddHours(14); var endTime2 = date.AddHours(15); var startTime3 = date.AddHours(8); var endTime3 = date.AddHours(11); var vehicleBuilder = new VehicleBuilder(); var createdEvent = new WorkshopPlanningCreatedEventBuilder() .WithDate(date) .Build(); var plannedEvent1 = new MaintenanceJobPlannedEventBuilder() .WithStartTime(startTime1) .WithEndTime(endTime1) .WithJobId(jobId1) .WithVehicleBuilder(vehicleBuilder) .Build(); var plannedEvent2 = new MaintenanceJobPlannedEventBuilder() .WithStartTime(startTime2) .WithEndTime(endTime2) .WithJobId(jobId2) .WithVehicleBuilder(vehicleBuilder) .Build(); var plannedEvent3 = new MaintenanceJobPlannedEventBuilder() .WithStartTime(startTime3) .WithEndTime(endTime3) .WithJobId(jobId3) .WithVehicleBuilder(vehicleBuilder) .Build(); var initializingEvents = new Event[] { createdEvent, plannedEvent1, plannedEvent2, plannedEvent3 }; var sut = new WorkshopPlanning(date, initializingEvents); var command = new UpdateMaintenanceJobCommandBuilder() .WithEvent(plannedEvent3) .WithChangedStartTime(plannedEvent1.StartTime) .WithChangedEndTime(plannedEvent1.EndTime) .Build(); // act var thrownException = Assert.Throws <BusinessRuleViolationException>(() => { sut.UpdateMaintenanceJob(command); // 4th job is updated to conflict with the 1st job }); // assert Assert.Equal("Only 1 maintenance job can be executed on a vehicle during a certain time-slot.", thrownException.Message); }