public async Task ShouldValidate()
        {
            var viewModel   = new ApprenticeshipViewModel();
            var updateModel = new CreateApprenticeshipUpdateViewModel();

            _mockMapper
            .Setup(m => m.MapApprenticeship(It.IsAny <ApprenticeshipViewModel>()))
            .ReturnsAsync(new Apprenticeship {
                ReservationId = Guid.NewGuid(), StartDate = DateTime.Today
            });

            _mockMediator
            .Setup(m => m.Send(It.IsAny <GetOverlappingApprenticeshipsQueryRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetOverlappingApprenticeshipsQueryResponse {
                Overlaps = new List <ApprenticeshipOverlapValidationResult>()
            });

            _mockMediator
            .Setup(m => m.Send(It.IsAny <GetReservationValidationRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetReservationValidationResponse {
                Data = new ReservationValidationResult(new ReservationValidationError[0])
            });

            await _orchestrator.ValidateEditApprenticeship(viewModel, updateModel);

            _mockMediator.Verify(m => m.Send(It.IsAny <GetOverlappingApprenticeshipsQueryRequest>(), It.IsAny <CancellationToken>()), Times.Once, failMessage: "Should call");
            _mockMediator.Verify(m => m.Send(It.IsAny <GetReservationValidationRequest>(), It.IsAny <CancellationToken>()), Times.Once, failMessage: "Should call");
            _mockValidator.Verify(m => m.MapOverlappingErrors(It.IsAny <GetOverlappingApprenticeshipsQueryResponse>()), Times.Once, failMessage: "Should verify overlapping apprenticeship");
            _mockValidator.Verify(m => m.ValidateToDictionary(It.IsAny <ApprenticeshipViewModel>()), Times.Once, failMessage: "Should validate apprenticeship");
            _mockValidator.Verify(m => m.ValidateAcademicYear(It.IsAny <CreateApprenticeshipUpdateViewModel>()), Times.Once, failMessage: "Should validate academic year");
        }
 private bool NeedReapproval(CreateApprenticeshipUpdateViewModel model)
 {
     return
         (!string.IsNullOrEmpty(model.FirstName) ||
          !string.IsNullOrEmpty(model.LastName) ||
          model.DateOfBirth?.DateTime != null ||
          !string.IsNullOrEmpty(model.CourseCode) ||
          model.StartDate?.DateTime != null ||
          model.EndDate?.DateTime != null ||
          !string.IsNullOrEmpty(model.Cost)
         );
 }
        public async Task ShouldAppendErrorsTogether()
        {
            var viewModel   = new ApprenticeshipViewModel();
            var updateModel = new CreateApprenticeshipUpdateViewModel();

            _mockMapper
            .Setup(m => m.MapApprenticeship(It.IsAny <ApprenticeshipViewModel>()))
            .ReturnsAsync(new Apprenticeship {
                ReservationId = Guid.NewGuid(), StartDate = DateTime.Today
            });

            _mockMediator
            .Setup(m => m.Send(It.IsAny <GetOverlappingApprenticeshipsQueryRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetOverlappingApprenticeshipsQueryResponse {
                Overlaps = new List <ApprenticeshipOverlapValidationResult>()
            });

            _mockValidator
            .Setup(v => v.ValidateToDictionary(viewModel))
            .Returns(BuildDictionary("VTD1=error1", "VTD2=error2"));

            _mockValidator
            .Setup(v => v.ValidateAcademicYear(updateModel))
            .Returns(BuildDictionary("VAY1=error3", "VAY2=error4"));

            _mockValidator
            .Setup(v => v.MapOverlappingErrors(It.IsAny <GetOverlappingApprenticeshipsQueryResponse>()))
            .Returns(BuildDictionary("OLE1=error7", "OLE2=error8"));

            _mockMediator
            .Setup(m => m.Send(It.IsAny <GetReservationValidationRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new GetReservationValidationResponse
            {
                Data = new ReservationValidationResult(new[]
                {
                    new ReservationValidationError("RVE1", "error9"),
                    new ReservationValidationError("RVE2", "error10")
                })
            });

            var errors = await _orchestrator.ValidateEditApprenticeship(viewModel, updateModel);

            TickoffError(errors, "VTD1", "error1");
            TickoffError(errors, "VTD2", "error2");
            TickoffError(errors, "VAY1", "error3");
            TickoffError(errors, "VAY2", "error4");
            TickoffError(errors, "OLE1", "error7");
            TickoffError(errors, "OLE2", "error8");
            TickoffError(errors, "RVE1", "error9");
            TickoffError(errors, "RVE2", "error10");

            Assert.AreEqual(0, errors.Count, "Additional unexpected errors were returned by the validator");
        }
Ejemplo n.º 4
0
        public Dictionary <string, string> ValidateAcademicYear(CreateApprenticeshipUpdateViewModel model)
        {
            var dict = new Dictionary <string, string>();

            if (model.StartDate?.DateTime != null &&
                _academicYearValidator.Validate(model.StartDate.DateTime.Value) == AcademicYearValidationResult.NotWithinFundingPeriod)
            {
                dict.Add($"{nameof(model.StartDate)}", ValidationText.AcademicYearStartDate01.Text);
            }

            return(dict);
        }
Ejemplo n.º 5
0
        public void ThenAnOptionMustBeSelected()
        {
            //Arrange
            var viewModel = new CreateApprenticeshipUpdateViewModel();

            //Act
            var result = _validator.Validate(viewModel);

            //Assert
            Assert.IsFalse(result.IsValid);
            Assert.IsTrue(result.Errors.Any(x => x.PropertyName == nameof(CreateApprenticeshipUpdateViewModel.ChangesConfirmed)));
        }
        public async Task <CreateApprenticeshipUpdateViewModel> CompareAndMapToCreateUpdateApprenticeshipViewModel(Apprenticeship original, ApprenticeshipViewModel edited)
        {
            string ChangedOrNull(string a, string edit) => a?.Trim() == edit?.Trim() ? null : edit;

            var model = new CreateApprenticeshipUpdateViewModel
            {
                HashedApprenticeshipId = _hashingService.HashValue(original.Id),
                ULN         = ChangedOrNull(original.ULN, edited.ULN),
                FirstName   = ChangedOrNull(original.FirstName, edited.FirstName),
                LastName    = ChangedOrNull(original.LastName, edited.LastName),
                DateOfBirth = original.DateOfBirth == edited.DateOfBirth.DateTime
                    ? null
                    : edited.DateOfBirth,
                Cost      = original.Cost == edited.Cost.AsNullableDecimal() ? null : edited.Cost,
                StartDate = original.StartDate == edited.StartDate.DateTime
                  ? null
                  : edited.StartDate,
                EndDate = original.EndDate == edited.EndDate.DateTime
                    ? null
                    : edited.EndDate,
                ProviderRef = original.ProviderRef?.Trim() == edited.ProviderRef?.Trim() ||
                              (string.IsNullOrEmpty(original.ProviderRef) && string.IsNullOrEmpty(edited.ProviderRef))
                    ? null
                    : edited.ProviderRef ?? "",
                OriginalApprenticeship = original,
                ProviderName           = original.ProviderName,
                LegalEntityName        = original.LegalEntityName,
                ReservationId          = original.ReservationId
            };

            if (!string.IsNullOrWhiteSpace(edited.CourseCode) && original.TrainingCode != edited.CourseCode)
            {
                var training = await GetTrainingProgramme(edited.CourseCode);

                if (training != null)
                {
                    model.CourseType = int.TryParse(training.CourseCode, out _) ? TrainingType.Standard : TrainingType.Framework;
                    model.CourseCode = edited.CourseCode;
                    model.CourseName = training.Name;
                }
                else
                {
                    model.CourseType = edited.CourseType == CommitmentTrainingType.Standard ? TrainingType.Standard : TrainingType.Framework;
                    model.CourseCode = edited.CourseCode;
                    model.CourseName = edited.CourseName;

                    _logger.Warn($"Apprentice training course has expired. TrainingName: {edited.CourseName}, TrainingCode: {edited.CourseCode}, Employer Ref: {edited.EmployerRef}, Apprenticeship ULN: {edited.ULN}");
                }
            }

            return(model);
        }
 private bool AnyChanges(CreateApprenticeshipUpdateViewModel data)
 {
     return
         (!string.IsNullOrWhiteSpace(data.ULN) ||
          !string.IsNullOrWhiteSpace(data.FirstName) ||
          !string.IsNullOrWhiteSpace(data.LastName) ||
          data.DateOfBirth != null ||
          !string.IsNullOrWhiteSpace(data.CourseName) ||
          data.StartDate != null ||
          data.EndDate != null ||
          data.Cost != null ||
          data.ProviderRef != null);
 }
Ejemplo n.º 8
0
        public async Task ShouldCallMediatorToCreate()
        {
            var    providerId             = 123;
            string userId                 = "ABC";
            var    expectedApprenticeship = new ApprenticeshipUpdate();
            var    viewModel              = new CreateApprenticeshipUpdateViewModel();
            var    signedInUser           = new SignInUserModel()
            {
                DisplayName = "Bob", Email = "*****@*****.**"
            };

            _mockApprenticeshipMapper.Setup(x => x.MapApprenticeshipUpdate(viewModel)).Returns(expectedApprenticeship);

            await _orchestrator.CreateApprenticeshipUpdate(viewModel, providerId, userId, signedInUser);

            _mockMediator.Verify(
                x =>
                x.Send(
                    It.Is <CreateApprenticeshipUpdateCommand>(
                        c =>
                        c.ProviderId == providerId && c.UserId == userId && c.ApprenticeshipUpdate == expectedApprenticeship && c.UserDisplayName == signedInUser.DisplayName &&
                        c.UserEmailAddress == signedInUser.Email), It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task <ActionResult> SubmitChanges(long providerId, string hashedApprenticeshipId, CreateApprenticeshipUpdateViewModel updateApprenticeship)
        {
            var originalApp = await _orchestrator.GetApprenticeship(providerId, hashedApprenticeshipId);

            updateApprenticeship.OriginalApprenticeship = originalApp;

            if (!ModelState.IsValid)
            {
                return(View("ConfirmChanges", updateApprenticeship));
            }

            if (updateApprenticeship.ChangesConfirmed != null && !updateApprenticeship.ChangesConfirmed.Value)
            {
                return(RedirectToAction("Details", new { providerId, hashedApprenticeshipId }));
            }

            await _orchestrator.CreateApprenticeshipUpdate(updateApprenticeship, providerId, CurrentUserId, GetSignedInUser());

            var message = NeedReapproval(updateApprenticeship)
                ? "Suggested changes sent to employer for approval, where needed."
                : "Apprentice updated";

            SetInfoMessage(message, FlashMessageSeverityLevel.Okay);

            return(RedirectToAction("Details", new { providerId, hashedApprenticeshipId }));
        }