public void ThenIfTheProviderFailsAuthorisationThenAnExceptionIsThrown()
        {
            var command = new AcceptApprenticeshipChangeCommand
            {
                Caller = new Caller(444, CallerType.Provider)
            };

            Func <Task> act = async() => await _sut.Handle(command);

            act.ShouldThrow <UnauthorizedException>();
        }
        public async Task ThenCreatesHistoryIfApproved()
        {
            var testCommitment = new Commitment {
                ProviderId = 1234, Id = 9874
            };
            var expectedOriginalCommitmentState = JsonConvert.SerializeObject(testCommitment);

            _commitment.Setup(x => x.GetCommitmentById(It.IsAny <long>())).ReturnsAsync(testCommitment);

            var expectedOriginalApprenticeshipState = JsonConvert.SerializeObject(_apprenticeship);

            var command = new AcceptApprenticeshipChangeCommand
            {
                ApprenticeshipId = 1234,
                UserId           = "ABC123",
                Caller           = new Caller(555, CallerType.Employer)
            };
            await _sut.Handle(command);

            var expectedNewApprenticeshipState = JsonConvert.SerializeObject(_apprenticeship);

            _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 == _apprenticeship.ProviderId &&
                        y.First().EmployerAccountId == _apprenticeship.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 == _apprenticeship.Id &&
                        y.Last().OriginalState == expectedOriginalApprenticeshipState &&
                        y.Last().UpdatedByRole == command.Caller.CallerType.ToString() &&
                        y.Last().UpdatedState == expectedNewApprenticeshipState &&
                        y.Last().UserId == command.UserId &&
                        y.Last().ProviderId == _apprenticeship.ProviderId &&
                        y.Last().EmployerAccountId == _apprenticeship.EmployerAccountId &&
                        y.Last().UpdatedByName == command.UserName)), Times.Once);
        }
        public void ThenThereMustBeAPendingUpdate()
        {
            var command = new AcceptApprenticeshipChangeCommand
            {
                Caller           = new Caller(666, CallerType.Provider),
                ApprenticeshipId = 5L,
                UserId           = "user123"
            };

            _repository.Setup(m => m.GetPendingApprenticeshipUpdate(5L))
            .ReturnsAsync(null);

            Func <Task> act = async() => await _sut.Handle(command);

            act.ShouldThrow <ValidationException>().WithMessage("No existing apprenticeship update pending for apprenticeship 5");
        }
        public void ThenIfTheRequestIsInvalidThenAValidationFailureExceptionIsThrown()
        {
            _validator.Setup(x => x.Validate(It.IsAny <AcceptApprenticeshipChangeCommand>()))
            .Returns(() =>
                     new ValidationResult(new List <ValidationFailure>
            {
                new ValidationFailure("Error", "Error Message")
            }));

            var command = new AcceptApprenticeshipChangeCommand {
                Caller = new Caller(555, CallerType.Employer)
            };

            Func <Task> act = async() => await _sut.Handle(command);

            act.ShouldThrow <ValidationException>();
        }
        public async Task ThenTheUpdateV2ApprenticeshipUpdatedApprovedEventIsPublished()
        {
            var testCommitment = new Commitment {
                ProviderId = 1234, Id = 9874, EmployerAccountId = 8457
            };

            _commitment.Setup(x => x.GetCommitmentById(It.IsAny <long>())).ReturnsAsync(testCommitment);

            var command = new AcceptApprenticeshipChangeCommand
            {
                ApprenticeshipId = _apprenticeship.Id,
                UserId           = "ABC123",
                Caller           = new Caller(555, CallerType.Employer)
            };
            await _sut.Handle(command);

            _v2EventsPublisher.Verify(
                x =>
                x.PublishApprenticeshipUpdatedApproved(
                    It.Is <Commitment>(m => m.Id == testCommitment.Id), It.Is <Apprenticeship>(m => m.Id == _apprenticeship.Id)), Times.Once);
        }
        public async Task ThenTheUpdateAcceptedEventIsCreated()
        {
            var testCommitment = new Commitment {
                ProviderId = 1234, Id = 9874, EmployerAccountId = 8457
            };

            _commitment.Setup(x => x.GetCommitmentById(It.IsAny <long>())).ReturnsAsync(testCommitment);

            var command = new AcceptApprenticeshipChangeCommand
            {
                ApprenticeshipId = 1234,
                UserId           = "ABC123",
                Caller           = new Caller(555, CallerType.Employer)
            };
            await _sut.Handle(command);

            _messagePublisher.Verify(
                x =>
                x.PublishAsync(
                    It.Is <ApprenticeshipUpdateAccepted>(
                        m => m.AccountId == testCommitment.EmployerAccountId && m.ProviderId == testCommitment.ProviderId.Value && m.ApprenticeshipId == command.ApprenticeshipId)), Times.Once);
        }