public async Task Then_It_Validates_The_Command(
            CacheReservationStartDateCommand command)
        {
            await _commandHandler.Handle(command, CancellationToken.None);

            _mockValidator.Verify(validator => validator.ValidateAsync(command), Times.Once);
        }
        public async Task Then_Gets_Provider_Cached_Reservation(CacheReservationStartDateCommand command)
        {
            await _commandHandler.Handle(command, CancellationToken.None);

            _mockCacheRepository.Verify(service => service.GetProviderReservation(command.Id, command.UkPrn.Value), Times.Once);
            _mockCacheRepository.Verify(service => service.GetEmployerReservation(It.IsAny <Guid>()), Times.Never);
        }
        public void And_CachedReservation_Not_Found_Then_It_Throws_Exception(
            CacheReservationStartDateCommand command)
        {
            var expectedException = new CachedReservationNotFoundException(command.Id);

            _mockCacheRepository.Setup(r => r.GetProviderReservation(It.IsAny <Guid>(), It.IsAny <uint>()))
            .ThrowsAsync(expectedException);

            var exception = Assert.ThrowsAsync <CachedReservationNotFoundException>(() =>
                                                                                    _commandHandler.Handle(command, CancellationToken.None));

            Assert.AreEqual(expectedException, exception);
        }
Пример #4
0
        public async Task And_All_Fields_Valid_Then_Valid()
        {
            var validator = new CacheReservationStartDateCommandValidator();
            var command   = new CacheReservationStartDateCommand
            {
                Id           = Guid.NewGuid(),
                TrainingDate = new TrainingDateModel {
                    StartDate = DateTime.Now
                }
            };

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
        }
        public void And_Invalid_Then_It_Throws_ValidationException(
            CacheReservationStartDateCommand command,
            ValidationResult validationResult,
            string propertyName)
        {
            validationResult.AddError(propertyName);

            _mockValidator
            .Setup(validator => validator.ValidateAsync(command))
            .ReturnsAsync(validationResult);

            Func <Task> act = async() => { await _commandHandler.Handle(command, CancellationToken.None); };

            act.Should().ThrowExactly <ValidationException>()
            .Which.ValidationResult.MemberNames.First(c => c.StartsWith(propertyName)).Should().NotBeNullOrEmpty();
        }
Пример #6
0
        public async Task And_No_Training_Date_Then_Invalid()
        {
            var validator = new CacheReservationStartDateCommandValidator();
            var command   = new CacheReservationStartDateCommand
            {
                Id = Guid.NewGuid()
            };

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);

            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationStartDateCommand.TrainingDate))
            .WhichValue.Should().Be($"{nameof(CacheReservationStartDateCommand.TrainingDate)} has not been supplied");
        }
        public async Task And_Is_Existing_Cache_Item_Then_Calls_Cache_Service_Using_Same_Key(
            CacheReservationStartDateCommand command)
        {
            _cachedReservation.Id = command.Id;
            var originalCommand = command.Clone();

            await _commandHandler.Handle(command, CancellationToken.None);

            _mockCacheStorageService.Verify(service => service.SaveToCache(originalCommand.Id.ToString(), It.Is <CachedReservation>(reservation =>
                                                                                                                                    reservation.Id == originalCommand.Id &&
                                                                                                                                    reservation.TrainingDate.Equals(originalCommand.TrainingDate) &&
                                                                                                                                    reservation.AccountId == _cachedReservation.AccountId &&
                                                                                                                                    reservation.AccountLegalEntityId == _cachedReservation.AccountLegalEntityId &&
                                                                                                                                    reservation.AccountLegalEntityName == _cachedReservation.AccountLegalEntityName &&
                                                                                                                                    reservation.AccountLegalEntityPublicHashedId == _cachedReservation.AccountLegalEntityPublicHashedId &&
                                                                                                                                    reservation.CourseId == _cachedReservation.CourseId &&
                                                                                                                                    reservation.CourseDescription == _cachedReservation.CourseDescription), 1));
        }
Пример #8
0
        public async Task And_No_Id_Then_Invalid()
        {
            var validator = new CacheReservationStartDateCommandValidator();
            var command   = new CacheReservationStartDateCommand
            {
                TrainingDate = new TrainingDateModel {
                    StartDate = DateTime.Now
                }
            };

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);

            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationStartDateCommand.Id))
            .WhichValue.Should().Be($"{nameof(CacheReservationStartDateCommand.Id)} has not been supplied");
        }
Пример #9
0
        public async Task <IActionResult> PostApprenticeshipTraining(ReservationsRouteModel routeModel, ApprenticeshipTrainingFormModel formModel)
        {
            var isProvider = routeModel.UkPrn != null;
            TrainingDateModel trainingDateModel = null;

            try
            {
                if (!string.IsNullOrWhiteSpace(formModel.StartDate))
                {
                    trainingDateModel = JsonConvert.DeserializeObject <TrainingDateModel>(formModel.StartDate);
                }

                if (!ModelState.IsValid)
                {
                    var model = await BuildApprenticeshipTrainingViewModel(
                        isProvider,
                        formModel.AccountLegalEntityPublicHashedId,
                        formModel.SelectedCourseId,
                        trainingDateModel,
                        formModel.FromReview,
                        formModel.CohortRef,
                        routeModel.UkPrn,
                        routeModel.EmployerAccountId);

                    return(View("ApprenticeshipTraining", model));
                }

                var cachedReservation = await _mediator.Send(new GetCachedReservationQuery { Id = routeModel.Id.GetValueOrDefault() });

                if (isProvider)
                {
                    var courseCommand = new CacheReservationCourseCommand
                    {
                        Id = cachedReservation.Id,
                        SelectedCourseId = formModel.SelectedCourseId,
                        UkPrn            = routeModel.UkPrn
                    };

                    await _mediator.Send(courseCommand);
                }

                var startDateCommand = new CacheReservationStartDateCommand
                {
                    Id           = cachedReservation.Id,
                    TrainingDate = trainingDateModel,
                    UkPrn        = routeModel.UkPrn
                };

                await _mediator.Send(startDateCommand);
            }
            catch (ValidationException e)
            {
                foreach (var member in e.ValidationResult.MemberNames)
                {
                    ModelState.AddModelError(member.Split('|')[0], member.Split('|')[1]);
                }

                var model = await BuildApprenticeshipTrainingViewModel(
                    isProvider,
                    formModel.AccountLegalEntityPublicHashedId,
                    formModel.SelectedCourseId,
                    trainingDateModel,
                    formModel.FromReview,
                    formModel.CohortRef,
                    routeModel.UkPrn,
                    routeModel.EmployerAccountId);

                return(View("ApprenticeshipTraining", model));
            }
            catch (CachedReservationNotFoundException ex)
            {
                _logger.LogWarning(ex, "Expected a cached reservation but did not find one.");
                return(RedirectToRoute(routeModel.UkPrn.HasValue ? RouteNames.ProviderIndex : RouteNames.EmployerIndex, routeModel));
            }

            var reviewRouteName = isProvider ?
                                  RouteNames.ProviderReview :
                                  RouteNames.EmployerReview;

            return(RedirectToRoute(reviewRouteName, routeModel));
        }