public async Task ValidateFundingTemplate_GivenTemplateWIthValidSchemaVersionButTemplateDoesNotValidateAgainstTheSchema_ReturnsValidationResultWithErrors1()
        {
            //Arrange
            const string schemaVersion = "1.0";

            string fundingTemplate = CreateJsonFile("CalculateFunding.Services.Policy.Resources.LogicalModelTemplate.json");
            string fundingSchema   = CreateJsonFile("CalculateFunding.Services.Policy.Resources.LogicalModel.json");

            string blobName = $"{fundingSchemaFolder}/{schemaVersion}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(true);
            fundingSchemaRepository
            .GetFundingSchemaVersion(Arg.Is(blobName))
            .Returns(fundingSchema);

            FundingTemplateValidationService fundingTemplateValidationService = CreateFundingTemplateValidationService(fundingSchemaRepository: fundingSchemaRepository);

            //Act
            FundingTemplateValidationResult result = await fundingTemplateValidationService.ValidateFundingTemplate(fundingTemplate, "fsid", "fpid", "1.0");

            //Assert
            result
            .Errors
            .Should()
            .HaveCount(8);

            result
            .IsValid
            .Should()
            .BeFalse();
        }
コード例 #2
0
        public async Task GetFundingSchemaByVersion_WhenSchemaRetrivedFromBlobStorgae_ReturnsOkResult()
        {
            //arrange
            const string schema = "schema";

            const string version = "0.6";

            string blobName = $"{fundingSchemaFolder}/{version}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(true);

            fundingSchemaRepository
            .GetFundingSchemaVersion(Arg.Is(blobName))
            .Returns(schema);

            ILogger logger = CreateLogger();

            FundingSchemaService fundingSchemaService = CreateFundingSchemaService(logger, fundingSchemaRepository: fundingSchemaRepository);

            //Act
            IActionResult result = await fundingSchemaService.GetFundingSchemaByVersion(version);

            //Assert
            result
            .Should()
            .BeAssignableTo <OkObjectResult>()
            .Which
            .Value
            .Should()
            .Be(schema);
        }
        public async Task ValidateFundingTemplate_GivenTemplateWithValidFundingPeriodIdButNoFundingPeriodExists_ReturnsValidationResultWithErrors()
        {
            //Arrange
            const string schemaVersion = "1.0";

            FundingStream fundingStream = new FundingStream();

            JsonSchema schema = JsonSchema.FromType <TestTemplate_schema_1_0>();

            TestTemplate_schema_1_0 testClassWithFunding = new TestTemplate_schema_1_0
            {
                SchemaVersion = schemaVersion,
                Funding       = new { templateVersion = "2.1", fundingStream = new { code = "PES" }, fundingPeriod = new { id = "AY-2020" } }
            };

            string fundingTemplate = JsonConvert.SerializeObject(testClassWithFunding);

            string fundingSchema = schema.AsJson();

            string blobName = $"{fundingSchemaFolder}/{schemaVersion}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(true);
            fundingSchemaRepository
            .GetFundingSchemaVersion(Arg.Is(blobName))
            .Returns(fundingSchema);

            IPolicyRepository policyRepository = CreatePolicyRepository();

            policyRepository
            .GetFundingStreamById(Arg.Is("PES"))
            .Returns(fundingStream);

            FundingTemplateValidationService fundingTemplateValidationService = CreateFundingTemplateValidationService(
                fundingSchemaRepository: fundingSchemaRepository,
                policyRepository: policyRepository);

            //Act
            FundingTemplateValidationResult result = await fundingTemplateValidationService.ValidateFundingTemplate(fundingTemplate, "PES", "AY-2020", "2.1");

            //Assert
            result
            .IsValid
            .Should()
            .BeFalse();

            result
            .Errors[0]
            .ErrorMessage
            .Should()
            .Be("A funding period could not be found for funding period id 'AY-2020'");
        }
        private async Task <bool> CheckIfFundingSchemaVersionExists(string blobName)
        {
            try
            {
                return(await _fundingSchemaRepositoryPolicy.ExecuteAsync(() => _fundingSchemaRepository.SchemaVersionExists(blobName)));
            }
            catch (Exception ex)
            {
                _logger.Error($"Failed to check if funding schema version: '{blobName}' exists");

                throw new NonRetriableException($"Failed to check if funding schema version: '{blobName}' exists", ex);
            }
        }
コード例 #5
0
        public async Task GetFundingSchemaByVersion_GivenASchemaVersionThatDoesNotRxistInBlobStorage_ReturnsNotFound()
        {
            //Arrange
            const string version = "0.6";

            string blobName = $"{fundingSchemaFolder}/{version}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(false);

            FundingSchemaService fundingSchemaService = CreateFundingSchemaService(fundingSchemaRepository: fundingSchemaRepository);

            //Act
            IActionResult result = await fundingSchemaService.GetFundingSchemaByVersion(version);

            //Assert
            result
            .Should()
            .BeAssignableTo <NotFoundResult>();
        }
コード例 #6
0
        public async Task GetFundingSchemaByVersion_WhenEmptyStringRetrivedFromBlobStorgae_ReturnsInternalServerError()
        {
            //arrange
            const string version = "0.6";

            string blobName = $"{fundingSchemaFolder}/{version}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(true);

            fundingSchemaRepository
            .GetFundingSchemaVersion(Arg.Is(blobName))
            .Returns(string.Empty);

            ILogger logger = CreateLogger();

            FundingSchemaService fundingSchemaService = CreateFundingSchemaService(logger, fundingSchemaRepository: fundingSchemaRepository);

            //Act
            IActionResult result = await fundingSchemaService.GetFundingSchemaByVersion(version);

            //Assert
            result
            .Should()
            .BeAssignableTo <InternalServerErrorResult>()
            .Which
            .Value
            .Should()
            .Be($"Failed to retrive blob contents for funding schema version '{version}'");

            logger
            .Received(1)
            .Error(Arg.Is($"Empty schema returned from blob storage for blob name '{blobName}'"));
        }
コード例 #7
0
        public async Task GetFundingSchemaByVersion_WhenGettingSchemaThrowsException_ReturnsInternalServerError()
        {
            //arrange
            const string version = "0.6";

            string blobName = $"{fundingSchemaFolder}/{version}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(true);

            fundingSchemaRepository
            .When(x => x.GetFundingSchemaVersion(Arg.Is(blobName)))
            .Do(x => { throw new Exception(); });

            ILogger logger = CreateLogger();

            FundingSchemaService fundingSchemaService = CreateFundingSchemaService(logger, fundingSchemaRepository: fundingSchemaRepository);

            //Act
            IActionResult result = await fundingSchemaService.GetFundingSchemaByVersion(version);

            //Assert
            result
            .Should()
            .BeAssignableTo <InternalServerErrorResult>()
            .Which
            .Value
            .Should()
            .Be($"Error occurred fetching funding schema verion '{version}'");

            logger
            .Received(1)
            .Error(Arg.Any <Exception>(), Arg.Is($"Failed to fetch funding schema '{blobName}' from blob storage"));
        }
        public async Task ValidateFundingTemplate_GivenTemplateWIthValidSchemaVersionButSchemaDoesNotExist_ReturnsValidationResultWithErrors()
        {
            //Arrange
            const string schemaVersion   = "1.0";
            string       fundingTemplate = $"{{ \"schemaVersion\" : \"{schemaVersion}\"}}";

            string blobName = $"{fundingSchemaFolder}/{schemaVersion}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(false);

            FundingTemplateValidationService fundingTemplateValidationService = CreateFundingTemplateValidationService(fundingSchemaRepository: fundingSchemaRepository);

            //Act
            FundingTemplateValidationResult result = await fundingTemplateValidationService.ValidateFundingTemplate(fundingTemplate, "fsid", "fpid", "1.0");

            //Assert
            result
            .Errors
            .Should()
            .HaveCount(1);

            result
            .Errors[0]
            .ErrorMessage
            .Should()
            .Be($"A valid schema could not be found for schema version '{schemaVersion}'.");

            result
            .IsValid
            .Should()
            .BeFalse();
        }
        public async Task ValidateFundingTemplate_Schema_1_1_GivenTemplateIsValidAndValuesExtracted_ReturnsValidationResultWithNoErrors()
        {
            //Arrange
            const string schemaVersion = "1.1";

            FundingStream fundingStream = new FundingStream();

            JsonSchema schema = JsonSchema.FromType <TestTemplate_schema_1_1>();

            var template = new TestTemplate_schema_1_1
            {
                SchemaVersion         = schemaVersion,
                FundingStreamTemplate = new { templateVersion = "56.4", fundingStream = new { code = "XXX" }, fundingPeriod = new { id = "AY-2020" } }
            };

            string fundingTemplate = JsonConvert.SerializeObject(template);

            string fundingSchema = schema.AsJson();

            string blobName = $"{fundingSchemaFolder}/{schemaVersion}.json";

            IFundingSchemaRepository fundingSchemaRepository = CreateFundingSchemaRepository();

            fundingSchemaRepository
            .SchemaVersionExists(Arg.Is(blobName))
            .Returns(true);
            fundingSchemaRepository
            .GetFundingSchemaVersion(Arg.Is(blobName))
            .Returns(fundingSchema);

            IPolicyRepository policyRepository = CreatePolicyRepository();

            policyRepository
            .GetFundingStreamById(Arg.Is("XXX"))
            .Returns(fundingStream);
            policyRepository
            .GetFundingPeriodById(Arg.Is("AY-2020"))
            .Returns(new FundingPeriod());

            FundingTemplateValidationService fundingTemplateValidationService = CreateFundingTemplateValidationService(
                fundingSchemaRepository: fundingSchemaRepository,
                policyRepository: policyRepository);

            //Act
            FundingTemplateValidationResult result = await fundingTemplateValidationService.ValidateFundingTemplate(fundingTemplate, "XXX", "AY-2020", "56.4");

            //Assert
            result
            .Errors
            .Should()
            .BeEmpty();

            result
            .TemplateVersion
            .Should()
            .Be("56.4");

            result
            .SchemaVersion
            .Should()
            .Be("1.1");

            result
            .FundingStreamId
            .Should()
            .Be("XXX");

            result
            .FundingPeriodId
            .Should()
            .Be("AY-2020");

            result
            .IsValid
            .Should()
            .BeTrue();
        }
        public async Task <FundingTemplateValidationResult> ValidateFundingTemplate(string fundingTemplate, string fundingStreamId, string fundingPeriodId, string templateVersion = null)
        {
            Guard.IsNullOrWhiteSpace(fundingTemplate, nameof(fundingTemplate));

            FundingTemplateValidationResult fundingTemplateValidationResult = new FundingTemplateValidationResult()
            {
                FundingStreamId = fundingStreamId,
                FundingPeriodId = fundingPeriodId,
                TemplateVersion = templateVersion
            };

            JObject parsedFundingTemplate;

            try
            {
                parsedFundingTemplate = JObject.Parse(fundingTemplate);
            }
            catch (JsonReaderException jre)
            {
                fundingTemplateValidationResult.Errors.Add(new ValidationFailure("", jre.Message));

                return(fundingTemplateValidationResult);
            }

            if (parsedFundingTemplate["schemaVersion"] == null ||
                string.IsNullOrWhiteSpace(parsedFundingTemplate["schemaVersion"].Value <string>()))
            {
                fundingTemplateValidationResult.Errors.Add(new ValidationFailure("", "Missing schema version from funding template."));

                return(fundingTemplateValidationResult);
            }

            string schemaVersion = parsedFundingTemplate["schemaVersion"].Value <string>();

            fundingTemplateValidationResult.SchemaVersion = schemaVersion;

            string blobName = $"{fundingSchemaFolder}/{schemaVersion}.json";

            bool schemaExists = await _fundingSchemaRepositoryPolicy.ExecuteAsync(() => _fundingSchemaRepository.SchemaVersionExists(blobName));

            if (!schemaExists)
            {
                fundingTemplateValidationResult.Errors.Add(new ValidationFailure("", $"A valid schema could not be found for schema version '{schemaVersion}'."));

                return(fundingTemplateValidationResult);
            }

            await ValidateAgainstSchema(blobName, parsedFundingTemplate, fundingTemplateValidationResult);

            if (!fundingTemplateValidationResult.IsValid)
            {
                return(fundingTemplateValidationResult);
            }

            await ValidateFundingStream(fundingTemplateValidationResult);
            await ValidateFundingPeriod(fundingTemplateValidationResult);

            return(fundingTemplateValidationResult);
        }