private static IProvidersApiClient CreateProviderApiClient(HttpStatusCode statusCode = HttpStatusCode.NoContent)
        {
            IProvidersApiClient providerApiClient = Substitute.For <IProvidersApiClient>();

            providerApiClient
            .DoesProviderVersionExist(Arg.Any <string>())
            .Returns(statusCode);

            return(providerApiClient);
        }
        private static IProvidersApiClient CreateProviderApiClient(HttpStatusCode statusCode = HttpStatusCode.NotFound)
        {
            IProvidersApiClient providerApiClient = Substitute.For <IProvidersApiClient>();

            providerApiClient
            .DoesProviderVersionExist(ProviderVersionId)
            .Returns(statusCode);

            return(providerApiClient);
        }
示例#3
0
        public AssignSpecificationProviderVersionModelValidator(
            ISpecificationsRepository specificationsRepository,
            IProvidersApiClient providersApiClient,
            ISpecificationsResiliencePolicies resiliencePolicies)
        {
            Guard.ArgumentNotNull(specificationsRepository, nameof(specificationsRepository));
            Guard.ArgumentNotNull(providersApiClient, nameof(providersApiClient));
            Guard.ArgumentNotNull(resiliencePolicies?.ProvidersApiClient, nameof(resiliencePolicies.ProvidersApiClient));

            _specificationsRepository = specificationsRepository;
            _providersApiClient       = providersApiClient;
            _providersApiClientPolicy = resiliencePolicies.ProvidersApiClient;

            RuleFor(model => model.SpecificationId)
            .NotEmpty()
            .WithMessage("Null or Empty SpecificationId provided")
            .CustomAsync(async(name, context, cancellationToken) =>
            {
                AssignSpecificationProviderVersionModel model = context.ParentContext.InstanceToValidate as AssignSpecificationProviderVersionModel;
                if (!string.IsNullOrWhiteSpace(model.SpecificationId))
                {
                    Specification specification = await _specificationsRepository.GetSpecificationById(model.SpecificationId);
                    if (specification == null)
                    {
                        context.AddFailure(nameof(model.SpecificationId), $"Specification not found for SpecificationId - {model.SpecificationId}");
                    }
                    else if (specification.Current.ProviderSource != ProviderSource.FDZ)
                    {
                        context.AddFailure($"Specification ProviderSource is not set to FDZ");
                    }
                }
            });

            RuleFor(model => model.ProviderVersionId)
            .NotEmpty()
            .WithMessage("Null or Empty ProviderVersionId provided")
            .CustomAsync(async(name, context, cancellationToken) =>
            {
                AssignSpecificationProviderVersionModel model = context.ParentContext.InstanceToValidate as AssignSpecificationProviderVersionModel;
                if (!string.IsNullOrWhiteSpace(model.ProviderVersionId))
                {
                    HttpStatusCode providerVersionStatusCode = await _providersApiClientPolicy.ExecuteAsync(() => _providersApiClient.DoesProviderVersionExist(model.ProviderVersionId));
                    if (providerVersionStatusCode == HttpStatusCode.NotFound)
                    {
                        context.AddFailure(nameof(model.ProviderVersionId), $"Provider version id specified does not exist");
                    }
                }
            });
        }
示例#4
0
        public SpecificationCreateModelValidator(ISpecificationsRepository specificationsRepository,
                                                 IProvidersApiClient providersApiClient,
                                                 IPoliciesApiClient policiesApiClient,
                                                 ISpecificationsResiliencePolicies resiliencePolicies)
        {
            Guard.ArgumentNotNull(specificationsRepository, nameof(specificationsRepository));
            Guard.ArgumentNotNull(providersApiClient, nameof(providersApiClient));
            Guard.ArgumentNotNull(policiesApiClient, nameof(policiesApiClient));
            Guard.ArgumentNotNull(resiliencePolicies?.PoliciesApiClient, nameof(resiliencePolicies.PoliciesApiClient));

            _specificationsRepository = specificationsRepository;
            _providersApiClient       = providersApiClient;
            _policiesApiClient        = policiesApiClient;
            _policiesApiClientPolicy  = resiliencePolicies.PoliciesApiClient;

            RuleFor(model => model.FundingPeriodId)
            .NotEmpty()
            .WithMessage("Null or empty academic year id provided")
            .Custom((name, context) =>
            {
                SpecificationCreateModel specModel = context.ParentContext.InstanceToValidate as SpecificationCreateModel;
                if (!string.IsNullOrWhiteSpace(specModel.FundingPeriodId))
                {
                    ApiResponse <PolicyModels.FundingPeriod> fundingPeriodResponse =
                        _policiesApiClientPolicy.ExecuteAsync(() => _policiesApiClient.GetFundingPeriodById(specModel.FundingPeriodId)).GetAwaiter().GetResult();
                    if (fundingPeriodResponse?.StatusCode != HttpStatusCode.OK || fundingPeriodResponse?.Content == null)
                    {
                        context.AddFailure("Funding period not found");
                    }
                }
            });

            RuleFor(model => model.ProviderVersionId)
            .Custom((name, context) => {
                SpecificationCreateModel specModel = context.ParentContext.InstanceToValidate as SpecificationCreateModel;
                ApiResponse <PolicyModels.FundingConfig.FundingConfiguration> fundingConfigResponse =
                    _policiesApiClientPolicy.ExecuteAsync(() => _policiesApiClient.GetFundingConfiguration(specModel.FundingStreamIds.FirstOrDefault(), specModel.FundingPeriodId)).GetAwaiter().GetResult();

                if (fundingConfigResponse?.StatusCode != HttpStatusCode.OK || fundingConfigResponse?.Content == null)
                {
                    context.AddFailure("Funding config not found");
                    return;
                }

                switch (fundingConfigResponse.Content.ProviderSource)
                {
                case ProviderSource.CFS:
                    {
                        if (string.IsNullOrWhiteSpace(specModel.ProviderVersionId))
                        {
                            context.AddFailure($"Null or empty provider version id");
                        }

                        if (_providersApiClient.DoesProviderVersionExist(specModel.ProviderVersionId).Result == System.Net.HttpStatusCode.NotFound)
                        {
                            context.AddFailure($"Provider version id selected does not exist");
                        }

                        if (specModel.CoreProviderVersionUpdates != CoreProviderVersionUpdates.Manual)
                        {
                            context.AddFailure($"CoreProviderVersionUpdates - {specModel.CoreProviderVersionUpdates} is not valid for provider source - {fundingConfigResponse.Content.ProviderSource}");
                        }

                        break;
                    }

                case ProviderSource.FDZ:
                    {
                        if (!specModel.ProviderSnapshotId.HasValue)
                        {
                            context.AddFailure($"Null or empty provider snapshot id");
                        }

                        break;
                    }
                }
            });

            RuleFor(model => model.FundingStreamIds)
            .NotNull()
            .NotEmpty()
            .WithMessage("You must select at least one funding stream")
            .Custom((name, context) => {
                SpecificationCreateModel specModel = context.ParentContext.InstanceToValidate as SpecificationCreateModel;
                foreach (string fundingStreamId in specModel.FundingStreamIds)
                {
                    if (string.IsNullOrWhiteSpace(fundingStreamId))
                    {
                        context.AddFailure($"A null or empty string funding stream ID was provided");
                    }
                }
            });

            RuleFor(model => model.Name)
            .NotEmpty()
            .WithMessage("You must give a unique specification name")
            .Custom((name, context) => {
                SpecificationCreateModel specModel = context.ParentContext.InstanceToValidate as SpecificationCreateModel;
                Specification specification        = _specificationsRepository.GetSpecificationByQuery(m => m.Content.Name.ToLower() == specModel.Name.Trim().ToLower()).Result;
                if (specification != null)
                {
                    context.AddFailure($"You must give a unique specification name");
                }
            });
        }