public async Task Then_Calls_Cache_Service_To_Save_Reservation(CacheReservationEmployerCommand command)
        {
            GetAccountFundingRulesApiResponse response = new GetAccountFundingRulesApiResponse()
            {
                GlobalRules = new List <GlobalRule>()
            };

            command.UkPrn = null;
            command.EmployerHasSingleLegalEntity = true;

            _mockFundingRulesService.Setup(c => c.GetAccountFundingRules(It.IsAny <long>()))
            .ReturnsAsync(response);

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

            //Assert
            _mockCacheStorageService.Verify(service => service.SaveToCache(
                                                It.IsAny <string>(),
                                                It.Is <CachedReservation>(c => c.Id.Equals(command.Id) &&
                                                                          c.AccountId.Equals(command.AccountId) &&
                                                                          c.AccountLegalEntityId.Equals(command.AccountLegalEntityId) &&
                                                                          c.AccountLegalEntityName.Equals(command.AccountLegalEntityName) &&
                                                                          c.AccountLegalEntityPublicHashedId.Equals(command.AccountLegalEntityPublicHashedId) &&
                                                                          c.AccountName.Equals(command.AccountName) &&
                                                                          c.CohortRef.Equals(command.CohortRef) &&
                                                                          c.UkPrn.Equals(command.UkPrn) &&
                                                                          c.IsEmptyCohortFromSelect.Equals(command.IsEmptyCohortFromSelect) &&
                                                                          c.EmployerHasSingleLegalEntity.Equals(command.EmployerHasSingleLegalEntity)),
                                                1));
        }
        public async Task Then_The_GlobalRules_Are_Checked_And_An_Error_Returned_If_Not_Valid(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IFundingRulesService> rulesService,
            CacheReservationEmployerCommandValidator validator)
        {
            SetupAccountLegalEntityAsNonLevy(mockMediator);
            rulesService.Setup(x => x.GetFundingRules()).ReturnsAsync(new GetFundingRulesApiResponse
            {
                GlobalRules = new List <GlobalRule>
                {
                    new GlobalRule
                    {
                        Restriction = AccountRestriction.NonLevy,
                        RuleType    = GlobalRuleType.FundingPaused,
                        ActiveFrom  = DateTime.UtcNow.AddMonths(-1)
                    }
                }
            });

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.FailedGlobalRuleValidation.Should().BeTrue();
        }
        public void And_If_There_Are_Reservation_Rules_Then_Throws_ReservationLimitReached_Exception(
            CacheReservationEmployerCommand command)
        {
            //Arrange
            _mockValidator
            .Setup(validator => validator.ValidateAsync(command))
            .ReturnsAsync(new ValidationResult {
                FailedRuleValidation = true, ValidationDictionary = new Dictionary <string, string>()
            });

            //Act + Assert
            Assert.ThrowsAsync <ReservationLimitReachedException>(() => _commandHandler.Handle(command, CancellationToken.None));
        }
        public void And_If_The_User_Fails_EOI_Check_Then_Throws_Exception(
            CacheReservationEmployerCommand command)
        {
            //Arrange
            _mockValidator
            .Setup(validator => validator.ValidateAsync(command))
            .ReturnsAsync(new ValidationResult {
                FailedEoiCheck = true, ValidationDictionary = new Dictionary <string, string>()
            });

            //Act + Assert
            Assert.ThrowsAsync <NonEoiUserAccessDeniedException>(() => _commandHandler.Handle(command, CancellationToken.None));
        }
        public async Task And_NonLevy_And_Is_Valid(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IFundingRulesService> rulesService,
            [Frozen] Mock <IMediator> mockMediator,
            CacheReservationEmployerCommandValidator validator)
        {
            ConfigureRulesServiceWithNoGlobalRules(rulesService);
            SetupAccountLegalEntityAsNonLevy(mockMediator);

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
        }
        public async Task And_All_Properties_Ok_Then_Valid(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IFundingRulesService> rulesService,
            CacheReservationEmployerCommandValidator validator)
        {
            ConfigureRulesServiceWithNoGlobalRules(rulesService);
            SetupAccountLegalEntityAsEoi(mockMediator);

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.FailedRuleValidation.Should().BeFalse();
        }
        public async Task Then_It_Validates_The_Command(CacheReservationEmployerCommand command)
        {
            var response = new GetAccountFundingRulesApiResponse()
            {
                GlobalRules = new List <GlobalRule>()
            };

            _mockFundingRulesService.Setup(m => m.GetAccountFundingRules(It.IsAny <long>()))
            .ReturnsAsync(response);

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

            //Assert
            _mockValidator.Verify(validator => validator.ValidateAsync(command), Times.Once);
        }
        public void And_Provider_Is_Not_Trusted_With_Legal_Entity_Then_Throws_Provider_Not_Authorised_Exception(
            CacheReservationEmployerCommand command)
        {
            //Arrange
            _mockValidator
            .Setup(validator => validator.ValidateAsync(command))
            .ReturnsAsync(new ValidationResult {
                FailedAuthorisationValidation = true, ValidationDictionary = new Dictionary <string, string>()
            });

            //Act + Assert
            var exception = Assert.ThrowsAsync <ProviderNotAuthorisedException>(() => _commandHandler.Handle(command, CancellationToken.None));

            Assert.AreEqual(command.AccountId, exception.AccountId);
            Assert.AreEqual(command.UkPrn, exception.UkPrn);
        }
        public async Task And_Provider_Is_Not_Trusted_For_Account_Legal_Entity_Then_Mark_As_Not_Authorised(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IMediator> mediator,
            [Frozen] GetTrustedEmployersResponse response,
            CacheReservationEmployerCommandValidator validator)
        {
            SetupAccountLegalEntityAsNonLevy(mediator);
            command.IsEmptyCohortFromSelect = false;
            mediator.Setup(m => m.Send(It.IsAny <GetTrustedEmployersQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.FailedAuthorisationValidation.Should().BeTrue();
        }
        public async Task And_AccountId_Less_Than_One_Then_Invalid(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IFundingRulesService> rulesService,
            CacheReservationEmployerCommandValidator validator)
        {
            ConfigureRulesServiceWithNoGlobalRules(rulesService);
            command.AccountId = default(long);

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);
            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationEmployerCommand.AccountId))
            .WhichValue.Should().Be($"{nameof(CacheReservationEmployerCommand.AccountId)} has not been supplied");
        }
        public async Task And_Provider_Is_Supplied_For_Empty_Cohort_Then_It_Is_Not_Validated_For_Account_Legal_Entity(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IMediator> mediator,
            [Frozen] GetTrustedEmployersResponse response,
            CacheReservationEmployerCommandValidator validator)
        {
            SetupAccountLegalEntityAsEoi(mediator);
            command.IsEmptyCohortFromSelect = true;
            mediator.Setup(m => m.Send(It.IsAny <GetTrustedEmployersQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.FailedAuthorisationValidation.Should().BeFalse();
            mediator.Verify(x => x.Send(It.IsAny <GetTrustedEmployersQuery>(), It.IsAny <CancellationToken>()), Times.Never);
        }
        public void And_The_Command_Is_Not_Valid_Then_Does_Not_Cache_Reservation(
            CacheReservationEmployerCommand 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);
        }
        public async Task And_No_Id_Then_Invalid(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IFundingRulesService> rulesService,
            CacheReservationEmployerCommandValidator validator)
        {
            SetupAccountLegalEntityAsNonLevy(mockMediator);
            ConfigureRulesServiceWithNoGlobalRules(rulesService);
            command.Id = Guid.Empty;

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeFalse();
            result.ValidationDictionary.Count.Should().Be(1);
            result.ValidationDictionary
            .Should().ContainKey(nameof(CacheReservationEmployerCommand.Id))
            .WhichValue.Should().Be($"{nameof(CacheReservationEmployerCommand.Id)} has not been supplied");
        }
        public void And_The_Command_Is_Not_Valid_Then_Throws_ArgumentException(
            CacheReservationEmployerCommand 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();
        }
        public async Task And_NonLevy_And_Not_Eoi_Then_Not_Valid(
            CacheReservationEmployerCommand command,
            GetLegalEntitiesResponse getLegalEntitiesResponse,
            [Frozen] Mock <IFundingRulesService> rulesService,
            [Frozen] Mock <IMediator> mockMediator,
            CacheReservationEmployerCommandValidator validator)
        {
            ConfigureRulesServiceWithNoGlobalRules(rulesService);
            foreach (var accountLegalEntity in getLegalEntitiesResponse.AccountLegalEntities)
            {
                accountLegalEntity.IsLevy        = false;
                accountLegalEntity.AgreementType = AgreementType.Levy;
            }
            mockMediator
            .Setup(mediator => mediator.Send(It.IsAny <GetLegalEntitiesQuery>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(getLegalEntitiesResponse);

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.FailedEoiCheck.Should().BeTrue();
        }
        public async Task Then_The_Account_Is_Checked_Against_The_AccountRules_And_An_Error_Returned_If_Not_Valid(
            CacheReservationEmployerCommand command,
            [Frozen] Mock <IMediator> mockMediator,
            [Frozen] Mock <IFundingRulesService> rulesService,
            CacheReservationEmployerCommandValidator validator)
        {
            SetupAccountLegalEntityAsNonLevy(mockMediator);
            rulesService.Setup(x => x.GetAccountFundingRules(command.AccountId)).ReturnsAsync(
                new GetAccountFundingRulesApiResponse
            {
                GlobalRules = new List <GlobalRule> {
                    new GlobalRule
                    {
                        Restriction = AccountRestriction.Account,
                        RuleType    = GlobalRuleType.ReservationLimit
                    }
                }
            });

            var result = await validator.ValidateAsync(command);

            result.IsValid().Should().BeTrue();
            result.FailedRuleValidation.Should().BeTrue();
        }