public void CreatingAPolicyWithDefaultValuesMustFail() { string name = null; string description = null; Dictionary <InsuranceCoverage, float> coverages = null; DateTime coverageStartDate = DateTime.Now; int coverageLengthInMonths = 0; float premiumPrice = 0; RiskLevel riskLevel = RiskLevel.None; repoMock .Setup(mock => mock.StorePolicyAsync(It.IsAny <InsurancePolicy>())) .Returns(Task.CompletedTask); Check .ThatAsyncCode(CreatePolicyAsync) .Throws <ArgumentNullException>(); name = "Test policy"; Check .ThatAsyncCode(CreatePolicyAsync) .Throws <ArgumentNullException>(); description = "This is a test"; Check .ThatAsyncCode(CreatePolicyAsync) .Throws <ArgumentNullException>(); coverages = new Dictionary <InsuranceCoverage, float> { { InsuranceCoverage.Earthquake, 10 } }; Check .ThatAsyncCode(CreatePolicyAsync) .Throws <ArgumentOutOfRangeException>(); coverageLengthInMonths = 10; Check .ThatAsyncCode(CreatePolicyAsync) .Throws <ArgumentOutOfRangeException>(); premiumPrice = 50; Check .ThatAsyncCode(CreatePolicyAsync) .Throws <ArgumentNullException>(); riskLevel = RiskLevel.Low; Check .ThatAsyncCode(CreatePolicyAsync) .DoesNotThrow(); async Task <InsurancePolicy> CreatePolicyAsync() { return(await service.CreatePolicyAsync(name, description, coverages, coverageStartDate, coverageLengthInMonths, premiumPrice, riskLevel)); } }
public async Task <IActionResult> CreatePolicy([FromBody] PolicyCreationRequest request) { if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Description) || string.IsNullOrEmpty(request.CoverageStartDate) || request.CoverageLength <= 0 || request.PremiumPrice <= 0 || !Enum.IsDefined(typeof(RiskLevel), request.RiskLevelId)) { return(BadRequest()); } var coverages = new Dictionary <InsuranceCoverage, float>(); if (request.EarthquakeCoverage > 0) { coverages.Add(InsuranceCoverage.Earthquake, request.EarthquakeCoverage); } if (request.FireCoverage > 0) { coverages.Add(InsuranceCoverage.Fire, request.FireCoverage); } if (request.TheftCoverage > 0) { coverages.Add(InsuranceCoverage.Theft, request.TheftCoverage); } if (request.LossCoverage > 0) { coverages.Add(InsuranceCoverage.Loss, request.LossCoverage); } if (!coverages.Any()) { return(BadRequest()); } DateTime coverageStartDate; try { coverageStartDate = DateTime.Parse(request.CoverageStartDate); } catch (FormatException) { return(BadRequest()); } var riskLevel = (RiskLevel)request.RiskLevelId; var policy = await insuranceService.CreatePolicyAsync( request.Name, request.Description, coverages, coverageStartDate, request.CoverageLength, request.PremiumPrice, riskLevel); var response = new PolicyResponse(policy); return(Ok(response)); }