Beispiel #1
0
        public async Task ValidateAsync_WhenModelFieldsIsEmpty_ValidIsFalse()
        {
            //Arrange
            List <FundingPeriod>    fundingPeriods = CreateModel();
            FundingPeriodsJsonModel model          = new FundingPeriodsJsonModel {
                FundingPeriods = fundingPeriods.ToArray()
            };

            model.FundingPeriods[0].Name   = string.Empty;
            model.FundingPeriods[0].Period = string.Empty;

            IValidator <FundingPeriodsJsonModel> validator = CreateValidator();

            //Act
            ValidationResult result = await validator.ValidateAsync(model);

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



            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[0].Name" && x.ErrorMessage == "No funding name was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);

            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[0].Period" && x.ErrorMessage == "No funding period was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);
        }
        public async Task <IActionResult> SaveFundingPeriods(FundingPeriodsJsonModel fundingPeriodsJsonModel)
        {
            if (fundingPeriodsJsonModel == null)
            {
                _logger.Error($"Null or empty json provided for file");
                return(new BadRequestObjectResult($"Invalid json was provided for file"));
            }

            try
            {
                BadRequestObjectResult validationResult = (await _fundingPeriodJsonModelValidator.ValidateAsync(fundingPeriodsJsonModel)).PopulateModelState();

                if (validationResult != null)
                {
                    return(validationResult);
                }
            }
            catch (Exception exception)
            {
                _logger.Error(exception, $"Invalid json was provided for file");
                return(new BadRequestObjectResult($"Invalid json was provided for file"));
            }

            try
            {
                FundingPeriod[] fundingPeriods = fundingPeriodsJsonModel.FundingPeriods;

                if (!fundingPeriods.IsNullOrEmpty())
                {
                    await _policyRepositoryPolicy.ExecuteAsync(() => _policyRepository.SaveFundingPeriods(fundingPeriods));

                    await _cacheProviderPolicy.ExecuteAsync(() => _cacheProvider.RemoveAsync <FundingPeriod[]>(CacheKeys.FundingPeriods));

                    _logger.Information($"Upserted {fundingPeriods.Length} funding periods into cosomos");
                }
            }
            catch (Exception exception)
            {
                string errorMessage = $"Exception occurred writing json file to cosmos db";

                _logger.Error(exception, errorMessage);

                return(new InternalServerErrorResult(errorMessage));
            }

            _logger.Information($"Successfully saved file to cosmos db");

            return(new OkResult());
        }
Beispiel #3
0
        public async Task ValidateAsync_IsValidIsTrue()
        {
            //Arrange
            List <FundingPeriod>    fundingPeriods = CreateModel();
            FundingPeriodsJsonModel model          = new FundingPeriodsJsonModel {
                FundingPeriods = fundingPeriods.ToArray()
            };

            IValidator <FundingPeriodsJsonModel> validator = CreateValidator();

            //Act
            ValidationResult result = await validator.ValidateAsync(model);

            //Assert
            result
            .IsValid
            .Should()
            .BeTrue();
        }
 public async Task <IActionResult> SaveFundingPeriods([FromBody] FundingPeriodsJsonModel fundingPeriodsJsonModel)
 {
     return(await _fundingPeriodService.SaveFundingPeriods(fundingPeriodsJsonModel));
 }
Beispiel #5
0
        public async Task ValidateAsync_WhenModelFieldsIsNull_ValidIsFalse()
        {
            //Arrange

            List <FundingPeriod> fundingPeriods             = CreateModel();
            FundingPeriod        nullStartDatefundingPeriod = new FundingPeriod()
            {
                Id      = "AY2017181",
                Name    = "Academic 2017/18",
                Type    = FundingPeriodType.AY,
                Period  = "1980",
                EndDate = DateTimeOffset.Now.Date
            };
            FundingPeriod nullEndDatefundingPeriod = new FundingPeriod()
            {
                Id        = "AY2017181",
                Name      = "Academic 2017/18",
                Type      = FundingPeriodType.AY,
                Period    = "1980",
                StartDate = DateTimeOffset.Now.Date
            };

            fundingPeriods.Add(nullStartDatefundingPeriod);
            fundingPeriods.Add(nullEndDatefundingPeriod);

            FundingPeriodsJsonModel model = new FundingPeriodsJsonModel {
                FundingPeriods = fundingPeriods.ToArray()
            };

            model.FundingPeriods[0].Name   = null;
            model.FundingPeriods[0].Period = null;
            model.FundingPeriods[1].Type   = null;



            IValidator <FundingPeriodsJsonModel> validator = CreateValidator();

            //Act
            ValidationResult result = await validator.ValidateAsync(model);

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

            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[0].Name" && x.ErrorMessage == "No funding name was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);

            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[0].Period" && x.ErrorMessage == "No funding period was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);

            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[1].Type" && x.ErrorMessage == "Null funding type was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);

            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[2].StartDate" && x.ErrorMessage == "No funding start date was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);

            result
            .Errors.Select(x => x.PropertyName == "FundingPeriods[3].EndDate" && x.ErrorMessage == "No funding end date was provided for the FundingPeriod")
            .Contains(true).Should().Be(true);
        }