Example #1
0
        public void Planning_Too_Much_MaintenanceJobs_In_Parallel_Should_Throw_Exception()
        {
            // arrange
            var initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder().WithDate(DateTime.Today).Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(initializingEvents);

            VehicleBuilder vehicleBuilder = new VehicleBuilder();
            PlanMaintenanceJobCommandBuilder commandBuilder = new PlanMaintenanceJobCommandBuilder()
                                                              .WithVehicleBuilder(vehicleBuilder);

            PlanMaintenanceJob command1 = commandBuilder.Build();

            vehicleBuilder.WithLicenseNumber(Guid.NewGuid().ToString());
            PlanMaintenanceJob command2 = commandBuilder.Build();

            vehicleBuilder.WithLicenseNumber(Guid.NewGuid().ToString());
            PlanMaintenanceJob command3 = commandBuilder.Build();

            vehicleBuilder.WithLicenseNumber(Guid.NewGuid().ToString());
            PlanMaintenanceJob command4 = commandBuilder.Build();

            // act
            sut.PlanMaintenanceJob(command1);
            sut.PlanMaintenanceJob(command2);
            sut.PlanMaintenanceJob(command3);
            var thrownException = Assert.Throws <BusinessRuleViolationException>(() => {
                sut.PlanMaintenanceJob(command4); // 4th parallel job
            });

            // assert
            Assert.Equal("Maintenancejob overlaps with more than 3 other jobs.",
                         thrownException.Message);
        }
        public async Task <WorkshopPlanning> HandleCommandAsync(DateTime planningDate, PlanMaintenanceJob command)
        {
            List <Event> events = new List <Event>();

            // get or create workshop-planning
            WorkshopPlanning planning = await _planningRepo.GetWorkshopPlanningAsync(planningDate);

            if (planning == null)
            {
                events.AddRange(WorkshopPlanning.Create(planningDate, out planning));
            }

            // handle command
            events.AddRange(planning.PlanMaintenanceJob(command));

            // persist
            await _planningRepo.SaveWorkshopPlanningAsync(
                planning.Id, planning.OriginalVersion, planning.Version, events);

            // publish event
            foreach (var e in events)
            {
                await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");
            }

            // return result
            return(planning);
        }
Example #3
0
        public void Plan_MaintenanceJob_That_Spans_Two_Days_Should_Throw_Exception()
        {
            // arrange
            DateTime date = DateTime.Today;
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder().WithDate(date).Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(date, initializingEvents);

            MaintenanceJobBuilder maintenanceJobBuilder = new MaintenanceJobBuilder();

            maintenanceJobBuilder
            .WithStartTime(DateTime.Today.AddHours(-2));     // to make the job span 2 days
            PlanMaintenanceJob command = new PlanMaintenanceJobCommandBuilder()
                                         .WithMaintenanceJobBuilder(maintenanceJobBuilder)
                                         .Build();

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

            // assert
            Assert.Equal("Start-time and end-time of a Maintenance Job must be within a 1 day.",
                         thrownException.Message);
        }
Example #4
0
        public async Task <WorkshopPlanning> HandleCommandAsync(DateTime planningDate, PlanMaintenanceJob command)
        {
            // get planning
            WorkshopPlanning planning = await _planningRepo.GetWorkshopPlanningAsync(planningDate);

            if (planning == null)
            {
                return(null);
            }

            // handle command
            IEnumerable <Event> events = planning.PlanMaintenanceJob(command);

            // persist
            await _planningRepo.SaveWorkshopPlanningAsync(planning, events);

            // publish event
            foreach (var e in events)
            {
                await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");
            }

            // return result
            return(planning);
        }
Example #5
0
        public void Plan_Two_MaintenanceJobs_In_Parallel_For_The_Same_Vehicle_Should_Throw_Exception()
        {
            // arrange
            var initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder().Build()
            };
            WorkshopPlanning   sut     = new WorkshopPlanning(initializingEvents);
            PlanMaintenanceJob command = new PlanMaintenanceJobCommandBuilder()
                                         .Build();

            // act
            sut.PlanMaintenanceJob(command);
            var thrownException = Assert.Throws <BusinessRuleViolationException>(() => {
                sut.PlanMaintenanceJob(command); // parallel job for same vehicle
            });

            // assert
            Assert.Equal("Only 1 maintenance job can be executed on a vehicle during a certain time-slot.",
                         thrownException.Message);
        }
        public async Task <IActionResult> PlanMaintenanceJobAsync(DateTime date, [FromBody] PlanMaintenanceJob command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // get planning
                    WorkshopPlanning planning = await _planningRepo.GetWorkshopPlanningAsync(date);

                    if (planning == null)
                    {
                        return(NotFound());
                    }

                    // handle command
                    try
                    {
                        IEnumerable <Event> events = planning.PlanMaintenanceJob(command);

                        // persist
                        await _planningRepo.SaveWorkshopPlanningAsync(planning, events);

                        // publish event
                        foreach (var e in events)
                        {
                            await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");
                        }

                        // return result
                        return(CreatedAtRoute("GetByDate", new { date = planning.Date }, planning));
                    }
                    catch (BusinessRuleViolationException ex)
                    {
                        return(StatusCode(StatusCodes.Status409Conflict, new BusinessRuleViolation {
                            ErrorMessage = ex.Message
                        }));
                    }
                }
                return(BadRequest());
            }
            catch (ConcurrencyException)
            {
                ModelState.AddModelError("ErrorMessage", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Example #7
0
        public void Plan_MaintenanceJob_Should_Add_A_New_MaintenanceJob()
        {
            // arrange
            DateTime date = DateTime.Today;
            string   workshopPlanningId = date.ToString("yyyy-MM-dd");
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder().WithDate(date).Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(date, initializingEvents);

            PlanMaintenanceJob command = new PlanMaintenanceJobCommandBuilder()
                                         .Build();

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

            // assert
            Assert.NotNull(sut);
            Assert.NotNull(sut.Id);
            Assert.Equal(workshopPlanningId, sut.Id);
            Assert.Equal(1, sut.OriginalVersion);
            Assert.Equal(2, sut.Version);
            Assert.Collection(sut.Jobs,
                              item0 =>
            {
                Assert.Equal(command.JobId, item0.Id);
                Assert.Equal(command.StartTime, item0.StartTime);
                Assert.Equal(command.EndTime, item0.EndTime);
                Assert.Equal(command.CustomerInfo.Id, item0.Customer.Id);
                Assert.Equal(command.CustomerInfo.Name, item0.Customer.Name);
                Assert.Equal(command.CustomerInfo.TelephoneNumber, item0.Customer.TelephoneNumber);
                Assert.Equal(command.VehicleInfo.LicenseNumber, item0.Vehicle.Id);
                Assert.Equal(command.VehicleInfo.Brand, item0.Vehicle.Brand);
                Assert.Equal(command.VehicleInfo.Type, item0.Vehicle.Type);
                Assert.Equal(command.CustomerInfo.Id, item0.Vehicle.OwnerId);
                Assert.Equal(command.Description, item0.Description);
            }
                              );
            Assert.Collection(events, item0 => Assert.IsAssignableFrom <MaintenanceJobPlanned>(item0));
        }
Example #8
0
        public void Plan_MaintenanceJob_Should_Add_A_New_MaintenanceJob()
        {
            // arrange
            DateTime date = DateTime.Today;
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreated(Guid.NewGuid(), date)
            };
            WorkshopPlanning sut = new WorkshopPlanning(initializingEvents);

            PlanMaintenanceJob command = new PlanMaintenanceJobCommandBuilder()
                                         .Build();

            // act
            List <Event> events = new List <Event>(sut.PlanMaintenanceJob(command));

            // assert
            Assert.NotNull(sut);
            Assert.NotNull(sut.Id);
            Assert.Equal(date, sut.Date);
            Assert.Equal(1, sut.OriginalVersion);
            Assert.Equal(2, sut.Version);
            Assert.Collection(sut.Jobs,
                              item0 => {
                Assert.Equal(command.JobId, item0.Id);
                Assert.Equal(command.StartTime, item0.StartTime);
                Assert.Equal(command.EndTime, item0.EndTime);
                Assert.Equal(command.CustomerInfo.Id, item0.Customer.CustomerId);
                Assert.Equal(command.CustomerInfo.Name, item0.Customer.Name);
                Assert.Equal(command.CustomerInfo.TelephoneNumber, item0.Customer.TelephoneNumber);
                Assert.Equal(command.VehicleInfo.LicenseNumber, item0.Vehicle.LicenseNumber);
                Assert.Equal(command.VehicleInfo.Brand, item0.Vehicle.Brand);
                Assert.Equal(command.VehicleInfo.Type, item0.Vehicle.Type);
                Assert.Equal(command.CustomerInfo.Id, item0.Vehicle.OwnerId);
                Assert.Equal(command.Description, item0.Description);
            }
                              );
            Assert.Collection(events, item0 => Assert.IsAssignableFrom <MaintenanceJobPlanned>(item0));
        }