Esempio n. 1
0
        public async Task Then_It_Validates_The_Command(CacheReservationCourseCommand command)
        {
            //Act
            await _commandHandler.Handle(command, CancellationToken.None);

            //Assert
            _mockValidator.Verify(validator => validator.ValidateAsync(command), Times.Once);
        }
Esempio n. 2
0
        public async Task Then_Gets_Selected_Course(CacheReservationCourseCommand command)
        {
            //Act
            await _commandHandler.Handle(command, CancellationToken.None);

            //Assert
            _mockCourseService.Verify(cs => cs.GetCourse(command.SelectedCourseId), Times.Once);
        }
Esempio n. 3
0
        public async Task Then_Gets_Provider_Cached_Reservation(CacheReservationCourseCommand command)
        {
            //Act
            await _commandHandler.Handle(command, CancellationToken.None);

            //Assert
            _mockCacheRepository.Verify(service => service.GetProviderReservation(command.Id, command.UkPrn.Value), Times.Once);
            _mockCacheRepository.Verify(service => service.GetEmployerReservation(It.IsAny <Guid>()), Times.Never);
        }
Esempio n. 4
0
        public void Then_Throws_Exception_If_Reservation_Not_Found_In_Cache(CacheReservationCourseCommand 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);
        }
Esempio n. 5
0
        public async Task And_All_Fields_Valid_Then_Valid()
        {
            var command = new  CacheReservationCourseCommand
            {
                Id = Guid.NewGuid(),
                SelectedCourseId = "1"
            };

            var result = await _validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.ValidationDictionary.Count.Should().Be(0);
        }
Esempio n. 6
0
        public async Task Then_Caches_Course_Choice_If_Not_Selected(CacheReservationCourseCommand command)
        {
            //Assign
            command.SelectedCourseId = null;

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

            //Assert
            _mockCourseService.Verify(s => s.GetCourse(It.IsAny <string>()), Times.Never);
            _mockCacheStorageService.Verify(service => service.SaveToCache(
                                                It.IsAny <string>(),
                                                It.Is <CachedReservation>(c => c.CourseId == null && c.CourseDescription == "Unknown"),
                                                1));
        }
Esempio n. 7
0
        public async Task Then_Calls_Cache_Service_To_Save_Reservation(CacheReservationCourseCommand command)
        {
            //Assign
            command.SelectedCourseId = _expectedCourse.Id;

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

            //Assert
            _mockCacheStorageService.Verify(service => service.SaveToCache(
                                                It.IsAny <string>(),
                                                It.Is <CachedReservation>(c => c.CourseId.Equals(_expectedCourse.Id) &&
                                                                          c.CourseDescription.Equals(_expectedCourse.CourseDescription)),
                                                1));
        }
Esempio n. 8
0
        public async Task And_All_Fields_Invalid_Then_Returns_All_Errors()
        {
            _courseService.Setup(s => s.CourseExists(It.IsAny <string>())).ReturnsAsync(false);

            var command = new  CacheReservationCourseCommand {
                SelectedCourseId = "INVALID"
            };

            var result = await _validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(2);
            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationCourseCommand.Id))
            .And.ContainKey(nameof(CacheReservationCourseCommand.SelectedCourseId));
        }
Esempio n. 9
0
        public async Task Then_If_ReservationId_Is_Invalid_Then_Fail()
        {
            var command = new  CacheReservationCourseCommand
            {
                Id = Guid.Empty,
                SelectedCourseId = "1"
            };

            var result = await _validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);
            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationCourseCommand.Id))
            .WhichValue.Should().Be($"{nameof( CacheReservationCourseCommand.Id)} has not been supplied");
        }
Esempio n. 10
0
        public void And_The_Command_Is_Not_Valid_Then_Does_Not_Cache_Reservation(
            CacheReservationCourseCommand command,
            ValidationResult validationResult,
            string propertyName)
        {
            //Assign
            validationResult.AddError(propertyName);

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

            //Act
            Assert.ThrowsAsync <ValidationException>(() => _commandHandler.Handle(command, CancellationToken.None));

            //Assert
            _mockCacheStorageService.Verify(s => s.SaveToCache(It.IsAny <string>(), It.IsAny <CachedReservation>(), It.IsAny <int>()), Times.Never);
        }
Esempio n. 11
0
        public async Task Then_If_CourseId_Is_Invalid_Then_Fail()
        {
            _courseService.Setup(s => s.CourseExists(It.IsAny <string>())).ReturnsAsync(false);

            var command = new  CacheReservationCourseCommand
            {
                Id = Guid.NewGuid(),
                SelectedCourseId = "123"
            };

            var result = await _validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);
            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationCourseCommand.SelectedCourseId))
            .WhichValue.Should().Be("Selected course does not exist");
        }
Esempio n. 12
0
        public async Task Then_If_CourseId_Not_Set_Then_Fail()
        {
            _courseService.Setup(s => s.CourseExists(It.IsAny <string>())).ReturnsAsync(false);

            var command = new  CacheReservationCourseCommand
            {
                Id = Guid.NewGuid(),
                SelectedCourseId = ""
            };

            var result = await _validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);
            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationCourseCommand.SelectedCourseId))
            .WhichValue.Should().Be("Select which apprenticeship training your apprentice will take");
        }
Esempio n. 13
0
        public void And_The_Command_Is_Not_Valid_Then_Does_Not_Get_Cached_Reservation(
            CacheReservationCourseCommand command,
            ValidationResult validationResult,
            string propertyName)
        {
            //Assign
            validationResult.AddError(propertyName);

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

            //Act
            Assert.ThrowsAsync <ValidationException>(() => _commandHandler.Handle(command, CancellationToken.None));

            //Assert
            _mockCacheRepository.Verify(service => service.GetEmployerReservation(It.IsAny <Guid>()), Times.Never);
            _mockCacheRepository.Verify(service => service.GetProviderReservation(It.IsAny <Guid>(), It.IsAny <uint>()), Times.Never);
        }
Esempio n. 14
0
        public void And_The_Command_Is_Not_Valid_Then_Throws_ArgumentException(
            CacheReservationCourseCommand command,
            ValidationResult validationResult,
            string propertyName)
        {
            //Assign
            validationResult.AddError(propertyName);

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

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

            //Assert
            act.Should().ThrowExactly <ValidationException>()
            .Which.ValidationResult.MemberNames.First(c => c.StartsWith(propertyName)).Should().NotBeNullOrEmpty();
        }
Esempio n. 15
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));
        }