public async Task ThenAnExceptionIsThrownIfReservationExistsAndFailsValidation(bool failValidation)
        {
            _existingApprenticeship.ReservationId = Guid.NewGuid();

            var failures = failValidation ? new[] { new ReservationValidationError("Property", "Reason") } : new ReservationValidationError[0];

            var validationFailures = new ReservationValidationResult(failures);

            _reservationsValidationService
            .Setup(rc => rc.CheckReservation(
                       It.Is <ReservationValidationServiceRequest>(msg => msg.ReservationId == _existingApprenticeship.ReservationId)))
            .ReturnsAsync(validationFailures);

            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            if (failValidation)
            {
                Assert.ThrowsAsync <ValidationException>(() =>
                                                         _handler.Handle(command));
            }
            else
            {
                await _handler.Handle(command);
            }
        }
예제 #2
0
        public async Task ThenTheRepositoryIsCalledToCreateRecordWhenStarted()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            _apprenticeshipRepository.Setup(x => x.GetApprenticeship(It.IsAny <long>()))
            .ReturnsAsync(new Apprenticeship
            {
                EmployerAccountId = 1,
                ProviderId        = 2,
                ULN       = "123",
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1),
                EndDate   = new DateTime(DateTime.Now.Year + 1, 5, 1),
                Id        = 3
            });

            //Act
            await _handler.Handle(command);

            //Assert
            _apprenticeshipUpdateRepository.Verify(x => x.CreateApprenticeshipUpdate(It.IsAny <ApprenticeshipUpdate>(), It.IsAny <Apprenticeship>()), Times.Once);
        }
예제 #3
0
        public async Task ThenTheCommitmentsApiIsCalledToCreateTheUpdate()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate {
                    ApprenticeshipId = 987
                },
                EmployerId       = 321,
                UserId           = "Tester",
                UserEmailAddress = "*****@*****.**",
                UserDisplayName  = "Bob"
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _commitmentsApi.Verify(
                x =>
                x.CreateApprenticeshipUpdate(command.EmployerId, command.ApprenticeshipUpdate.ApprenticeshipId,
                                             It.Is <ApprenticeshipUpdateRequest>(
                                                 y =>
                                                 y.ApprenticeshipUpdate == command.ApprenticeshipUpdate && y.UserId == command.UserId && y.LastUpdatedByInfo.Name == command.UserDisplayName &&
                                                 y.LastUpdatedByInfo.EmailAddress == command.UserEmailAddress)), Times.Once);
        }
        public async Task ThenTheCommitmentsApiIsCalledToCreateTheUpdate()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate {
                    ApprenticeshipId = 123
                },
                ProviderId       = 1,
                UserId           = "Tester",
                UserEmailAddress = "*****@*****.**",
                UserDisplayName  = "Bob"
            };

            //Act
            await _handler.Handle(command, new CancellationToken());

            //Assert
            _commitmentsApi.Verify(
                x =>
                x.CreateApprenticeshipUpdate(command.ProviderId, command.ApprenticeshipUpdate.ApprenticeshipId,
                                             It.Is <ApprenticeshipUpdateRequest>(
                                                 r =>
                                                 r.UserId == command.UserId && r.ApprenticeshipUpdate == command.ApprenticeshipUpdate && r.LastUpdatedByInfo.EmailAddress == command.UserEmailAddress &&
                                                 r.LastUpdatedByInfo.Name == command.UserDisplayName)), Times.Once);
        }
예제 #5
0
        public void ThenIfApprenticeHasStarted_And_DataLockExists_ValidationFailureExceptionIsThrown_IfCostIsChanged()
        {
            _mockCurrentDateTime.SetupGet(x => x.Now).Returns(new DateTime(2017, 7, 13));

            _apprenticeshipRepository.Setup(x => x.GetApprenticeship(It.IsAny <long>()))
            .ReturnsAsync(new Apprenticeship
            {
                EmployerAccountId = 1,
                ProviderId        = 2,
                ULN                   = " 123",
                StartDate             = new DateTime(2017, 6, 1),
                EndDate               = new DateTime(2017, 5, 1),
                Id                    = 3,
                HasHadDataLockSuccess = true
            });

            var request = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate =
                    new ApprenticeshipUpdate
                {
                    Id = 5,
                    ApprenticeshipId = 42,
                    Cost             = 1234
                }
            };

            //Act && Assert
            Func <Task> act = async() => await _handler.Handle(request);

            act.ShouldThrow <ValidationException>();
        }
예제 #6
0
        public void ThenIfApprenticeHasStarted_And_DataLockSuccessExist_ValidationFailureExceptionIsThrown_IfUpdated_TrainingCode()
        {
            _apprenticeshipRepository.Setup(x => x.GetApprenticeship(It.IsAny <long>()))
            .ReturnsAsync(new Apprenticeship
            {
                EmployerAccountId = 1,
                ProviderId        = 2,
                ULN                   = " 123",
                StartDate             = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1),
                EndDate               = new DateTime(DateTime.Now.Year + 1, 5, 1),
                Id                    = 3,
                HasHadDataLockSuccess = true
            });

            var request = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate =
                    new ApprenticeshipUpdate
                {
                    Id = 5,
                    ApprenticeshipId = 42,
                    TrainingCode     = "abc-123"
                },
                Caller = new Caller(2, CallerType.Provider)
            };

            //Act && Assert
            Func <Task> act = async() => await _handler.Handle(request);

            act.ShouldThrow <ValidationException>();
        }
        public async Task ThenIfTheCommandChangesTheULNThenAnEventIsPublishedAsItIsUpdatedImmediately()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    ULN = "NewValue",
                    ApprenticeshipId  = 3,
                    EffectiveFromDate = DateTime.Now
                }
            };

            var apprenticeship = new Apprenticeship
            {
                EmployerAccountId = 1,
                ProviderId        = 2,
                ULN       = "OldValue",
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1),
                EndDate   = new DateTime(DateTime.Now.Year + 1, 5, 1),
            };

            _apprenticeshipRepository.Setup(x => x.GetApprenticeship(It.IsAny <long>())).ReturnsAsync(apprenticeship);

            await _handler.Handle(command);

            _apprenticeshipEventsList.Verify(x => x.Add(It.IsAny <Commitment>(), It.Is <Apprenticeship>(p => p.ULN == "NewValue"), "APPRENTICESHIP-UPDATED",
                                                        It.Is <DateTime?>(p => p == _mockCurrentDateTime.Object.Now), null));
            _apprenticeshipEventsPublisher.Verify(x => x.Publish(_apprenticeshipEventsList.Object));
        }
예제 #8
0
        public async Task ThenIfTheUpdateContainsDataForImmediateEffectTheHistoryRecordsAreCreated()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    ULN = "Test",
                    ApprenticeshipId = 3
                }
            };

            var testCommitment = new Commitment {
                Id = 7643
            };
            var expectedOriginalCommitmentState = JsonConvert.SerializeObject(testCommitment);

            _commitmentRepository.Setup(x => x.GetCommitmentById(_existingApprenticeship.CommitmentId)).ReturnsAsync(testCommitment);

            var expectedOriginalApprenticeshipState = JsonConvert.SerializeObject(_existingApprenticeship);

            //Act
            await _handler.Handle(command);

            var expectedNewApprenticeshipState = JsonConvert.SerializeObject(_existingApprenticeship);

            //Assert
            _historyRepository.Verify(
                x =>
                x.InsertHistory(
                    It.Is <IEnumerable <HistoryItem> >(
                        y =>
                        y.First().ChangeType == CommitmentChangeType.EditedApprenticeship.ToString() &&
                        y.First().CommitmentId == testCommitment.Id &&
                        y.First().ApprenticeshipId == null &&
                        y.First().OriginalState == expectedOriginalCommitmentState &&
                        y.First().UpdatedByRole == command.Caller.CallerType.ToString() &&
                        y.First().UpdatedState == expectedOriginalCommitmentState &&
                        y.First().UserId == command.UserId &&
                        y.First().ProviderId == _existingApprenticeship.ProviderId &&
                        y.First().EmployerAccountId == _existingApprenticeship.EmployerAccountId &&
                        y.First().UpdatedByName == command.UserName)), Times.Once);

            _historyRepository.Verify(
                x =>
                x.InsertHistory(
                    It.Is <IEnumerable <HistoryItem> >(
                        y =>
                        y.Last().ChangeType == ApprenticeshipChangeType.Updated.ToString() &&
                        y.Last().CommitmentId == null &&
                        y.Last().ApprenticeshipId == _existingApprenticeship.Id &&
                        y.Last().OriginalState == expectedOriginalApprenticeshipState &&
                        y.Last().UpdatedByRole == command.Caller.CallerType.ToString() &&
                        y.Last().UpdatedState == expectedNewApprenticeshipState &&
                        y.Last().UserId == command.UserId &&
                        y.Last().ProviderId == _existingApprenticeship.ProviderId &&
                        y.Last().EmployerAccountId == _existingApprenticeship.EmployerAccountId &&
                        y.Last().UpdatedByName == command.UserName)), Times.Once);
        }
        public async Task ThenIfAIfAPendingUpdateIsCreatedThenAnApprentieceshipUpdateCreatedEventIsCreated()
        {
            var apprenticeship = new Apprenticeship
            {
                EmployerAccountId = 1,
                ProviderId        = 2,
                LegalEntityId     = "12345",
                ULN       = " 123",
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1),
                EndDate   = new DateTime(DateTime.Now.Year + 1, 5, 1),
                Id        = 42
            };

            _apprenticeshipRepository.Setup(x => x.GetApprenticeship(It.IsAny <long>())).ReturnsAsync(apprenticeship);


            var trainingCode = "abc-123";
            var request      = new CreateApprenticeshipUpdateCommand {
                ApprenticeshipUpdate = new ApprenticeshipUpdate {
                    Id = 5, ApprenticeshipId = 42, TrainingCode = trainingCode
                }, Caller = new Caller(1, CallerType.Employer)
            };

            await _handler.Handle(request);

            _messagePublisher.Verify(
                x =>
                x.PublishAsync(
                    It.Is <ApprenticeshipUpdateCreated>(
                        y => y.ApprenticeshipId == apprenticeship.Id && y.AccountId == apprenticeship.EmployerAccountId && y.ProviderId == apprenticeship.ProviderId)), Times.Once);
        }
예제 #10
0
        public async Task ThenTheCOmmandIsValidated()
        {
            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate(),
                EmployerId           = 321,
                UserId = "Tester"
            };

            await _handler.Handle(command);

            _validator.Verify(m => m.Validate(It.IsAny <CreateApprenticeshipUpdateCommand>()), Times.Once());
        }
예제 #11
0
        public void ThenIfTheEmployerFailsAuthorisationThenAnExceptionIsThrown()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(666, CallerType.Employer),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            //Act && Assert
            Func <Task> act = async() => await _handler.Handle(command);

            act.ShouldThrow <UnauthorizedException>();
        }
        public void Arrange()
        {
            _validator = new CreateApprenticeshipUpdateValidator();

            _command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(1, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    ApprenticeshipId = 1,
                    Originator       = Originator.Provider
                }
            };
        }
예제 #13
0
        public async Task ThenTheRepositoryIsCalledToCreateRecord()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _apprenticeshipUpdateRepository.Verify(x => x.CreateApprenticeshipUpdate(It.IsAny <ApprenticeshipUpdate>(), It.IsAny <Apprenticeship>()), Times.Once);
        }
        public void ThenProviderIdIsMandatory()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

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

            //Assert
            Assert.IsFalse(result.IsValid());
            Assert.IsTrue(result.ValidationDictionary.Any(x => x.Key.Contains(nameof(CreateApprenticeshipUpdateCommand.EmployerId))));
        }
예제 #15
0
        public async Task ThenTheRequestIsValidated()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(1, CallerType.Employer),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _validator.Verify(x => x.Validate(It.IsAny <CreateApprenticeshipUpdateCommand>()), Times.Once);
        }
예제 #16
0
        public void ThenApprenticeshipIdUpdateIsMandatory()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

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

            //Assert
            Assert.IsFalse(result.IsValid);
            Assert.IsTrue(result.Errors.Any(x => x.PropertyName.Contains(nameof(ApprenticeshipUpdate.ApprenticeshipId))));
        }
        public async Task ThenTheCommandIsValidated()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate(),
                ProviderId           = 1,
                UserId = "Tester"
            };

            //Act
            await _handler.Handle(command, new CancellationToken());

            //Assert
            _validator.Verify(x => x.Validate(It.IsAny <CreateApprenticeshipUpdateCommand>()), Times.Once);
        }
        public void ThenShouldThrowExceptionIfAStartDateIsNotAvailable()
        {
            _existingApprenticeship.ReservationId = Guid.NewGuid();

            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    StartDate = null
                }
            };

            _existingApprenticeship.StartDate = null;

            Assert.ThrowsAsync <InvalidOperationException>(() => _handler.Handle(command));
        }
예제 #19
0
        public void ThenIfTheRequestIsInvalidThenAValidationFailureExceptionIsThrown()
        {
            //Arrange
            _validator.Setup(x => x.Validate(It.IsAny <CreateApprenticeshipUpdateCommand>()))
            .Returns(() =>
                     new ValidationResult(new List <ValidationFailure>
            {
                new ValidationFailure("Error", "Error Message")
            }));

            var request = new CreateApprenticeshipUpdateCommand();

            //Act && Assert
            Func <Task> act = async() => await _handler.Handle(request);

            act.ShouldThrow <ValidationException>();
        }
예제 #20
0
        public async Task ThenTheMediatorIsCalledToCheckOverlappingApprenticeships()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    ULN = "123"
                }
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _mediator.Verify(x => x.SendAsync(It.IsAny <GetOverlappingApprenticeshipsRequest>()), Times.Once);
        }
예제 #21
0
        public async Task ThenIfTheUpdateContainsNoDataForImmediateEffectTheNoHistoryIsCreated()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    FirstName = "Test",
                    LastName  = "Tester"
                }
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _historyRepository.Verify(x => x.InsertHistory(It.IsAny <IEnumerable <HistoryItem> >()), Times.Never);
        }
        public async Task ThenUpdatedDateIsUsedToValidateReservationIfDateUpdated()
        {
            _existingApprenticeship.ReservationId = Guid.NewGuid();

            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    StartDate = DateTime.Now.AddMonths(3)
                }
            };

            _existingApprenticeship.StartDate = null;

            await _handler.Handle(command);

            _reservationsValidationService.Verify(rvs => rvs.CheckReservation(It.Is <ReservationValidationServiceRequest>(request => request.StartDate == command.ApprenticeshipUpdate.StartDate.Value)), Times.Once);
        }
예제 #23
0
        public void ThenIfTheApprenticeshipAlreadyHasAPendingUpdateThenAnExceptionIsThrown()
        {
            //Arrange
            _apprenticeshipUpdateRepository.Setup(x => x.GetPendingApprenticeshipUpdate(It.IsAny <long>()))
            .ReturnsAsync(new ApprenticeshipUpdate());

            var command = new CreateApprenticeshipUpdateCommand
            {
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            //Act && Assert
            Func <Task> act = async() => await _handler.Handle(command);

            act.ShouldThrow <ValidationException>();

            //Assert
            _apprenticeshipUpdateRepository.Verify(x => x.CreateApprenticeshipUpdate(It.IsAny <ApprenticeshipUpdate>(), It.IsAny <Apprenticeship>()), Times.Never);
        }
예제 #24
0
        public void ThenIfAllMandatoryPropertiesAreProvidedThenIsValid()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                UserId               = "User1",
                ProviderId           = 2,
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    ApprenticeshipId = 3,
                    Originator       = Originator.Employer
                }
            };

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

            //Assert
            Assert.IsTrue(result.IsValid);
        }
        public void TheOriginatorAndTheCallerMustBeEquivalent(CallerType callerType, Originator originator, bool isValid)
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(1, callerType),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
                {
                    ApprenticeshipId = 1,
                    Originator       = originator,
                    FirstName        = "Test"
                }
            };

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

            //Assert
            Assert.AreEqual(isValid, result.IsValid);
        }
        public void ThenTheEmployerCannotModifyTheProviderRef()
        {
            //Arrange
            _command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(1, CallerType.Employer),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
                {
                    ApprenticeshipId = 1,
                    Originator       = Originator.Employer,
                    ProviderRef      = "123"
                }
            };

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

            //Assert
            Assert.IsFalse(result.IsValid);
        }
예제 #27
0
        public async Task ThenIfTheUpdateContainsNoDataForApprovalThenTheUpdateIsNotCreated()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                }
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _apprenticeshipUpdateRepository.Verify(x => x.CreateApprenticeshipUpdate(
                                                       It.Is <ApprenticeshipUpdate>(y => y == null),
                                                       It.Is <Apprenticeship>(y => y != null)),
                                                   Times.Never);
        }
        public async Task ThenTheReservationIsValidatedIfItHasAValue(bool setReservationId)
        {
            _existingApprenticeship.ReservationId = setReservationId ? Guid.NewGuid() : (Guid?)null;

            _reservationsValidationService
            .Setup(rc => rc.CheckReservation(
                       It.Is <ReservationValidationServiceRequest>(msg => msg.ReservationId == _existingApprenticeship.ReservationId)))
            .ReturnsAsync(new ReservationValidationResult());

            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate()
            };

            await _handler.Handle(command);

            _reservationsValidationService
            .Verify(rc => rc.CheckReservation(
                        It.Is <ReservationValidationServiceRequest>(msg => msg.ReservationId == _existingApprenticeship.ReservationId)),
                    Times.Once());
        }
예제 #29
0
        public async Task ThenIfTheApprenticeshipIsWaitingToStartThenTheChangeWillBeEffectiveFromTheApprenticeshipStartDate()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    ULN  = "123",
                    Cost = 100
                }
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _apprenticeshipUpdateRepository.Verify(
                x => x.CreateApprenticeshipUpdate(
                    It.Is <ApprenticeshipUpdate>(u => u.EffectiveFromDate == _existingApprenticeship.StartDate),
                    It.IsAny <Apprenticeship>()));
        }
예제 #30
0
        public async Task ThenIfTheUpdateContainsNoDataForImmediateEffectThenTheApprenticeshipIsNotUpdated()
        {
            //Arrange
            var command = new CreateApprenticeshipUpdateCommand
            {
                Caller = new Caller(2, CallerType.Provider),
                ApprenticeshipUpdate = new ApprenticeshipUpdate
                {
                    FirstName = "Test",
                    LastName  = "Tester"
                }
            };

            //Act
            await _handler.Handle(command);

            //Assert
            _apprenticeshipUpdateRepository.Verify(x => x.CreateApprenticeshipUpdate(
                                                       It.Is <ApprenticeshipUpdate>(y => y != null),
                                                       It.Is <Apprenticeship>(y => y == null)),
                                                   Times.Once);
        }