public void ValidatePolicy_ShouldFailIfMoreThan2ClaimsForDriver()
        {
            // Setup dependencies
            var policy = new InsurancePolicy
            {
                StartDate = DateTime.UtcNow.AddMonths(1),
                Drivers   = new List <InsurancePolicyDriver>
                {
                    new InsurancePolicyDriver
                    {
                        FirstName   = "Test",
                        Surname     = "Driver",
                        DateOfBirth = DateTime.UtcNow.AddYears(-30),
                        Claims      = new List <DriverClaim>
                        {
                            new DriverClaim(),
                            new DriverClaim(),
                            new DriverClaim(),
                            new DriverClaim()
                        }
                    }
                }
            };

            // Call Validate
            var result = policy.Validate();

            // Verify result
            Assert.IsFalse(result.Succeeded);
            Assert.AreEqual("Driver has more than 2 claims: Test Driver", result.Error);
        }
        public void ValidatePolicy_ShouldSucceed()
        {
            // Setup dependencies
            var policy = new InsurancePolicy
            {
                StartDate = DateTime.UtcNow,
                Drivers   = new List <InsurancePolicyDriver>
                {
                    new InsurancePolicyDriver
                    {
                        FirstName   = "Test",
                        Surname     = "Driver",
                        DateOfBirth = DateTime.UtcNow.AddYears(-30),
                        Claims      = new List <DriverClaim>
                        {
                            new DriverClaim(),
                        }
                    }
                }
            };

            // Call Validate
            var result = policy.Validate();

            // Verify result
            Assert.IsTrue(result.Succeeded);
        }
        public void ValidatePolicy_ShouldFailIfStartDateInThePast()
        {
            // Setup dependencies
            var policy = new InsurancePolicy
            {
                StartDate = DateTime.UtcNow.AddDays(-3)
            };

            // Call Validate
            var result = policy.Validate();

            // Verify result
            Assert.IsFalse(result.Succeeded);
            Assert.AreEqual("Start Date of Policy", result.Error);
        }
        public ActionResult Apply(PolicyApplicationViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestResult(ModelState));
            }

            var newPolicy = new InsurancePolicy
            {
                UserId          = Convert.ToInt32(_authenticationManager.User.Identity.GetUserId()),
                ApplicationDate = DateTime.UtcNow,
                StartDate       = model.StartDate,
                Drivers         = model.Drivers.Select(d => new InsurancePolicyDriver
                {
                    DateOfBirth  = d.DateOfBirth,
                    FirstName    = d.FirstName.Trim(),
                    Surname      = d.Surname.Trim(),
                    OccupationId = d.OccupationId,
                    Claims       = d.Claims.Select(c => new DriverClaim
                    {
                        DateOfClaim = c.DateOfClaim
                    }).ToList()
                }).ToList()
            };

            var policyValidationResult = newPolicy.Validate();

            if (!policyValidationResult.Succeeded)
            {
                return(new BadRequestResult(policyValidationResult.Error));
            }

            TransactionHelper.InvokeTransaction(() =>
            {
                newPolicy         = _policyService.AddInsurancePolicyApplication(newPolicy);
                newPolicy.Premium = _policyService.CalculatePremium(newPolicy, 500);
                _policyService.UpdateInsurancePolicy(newPolicy);
            });

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }
        public void ValidatePolicy_ShouldFailIfOldestDriverGeaterThan75()
        {
            // Setup dependencies
            var policy = new InsurancePolicy
            {
                StartDate = DateTime.UtcNow,
                Drivers   = new List <InsurancePolicyDriver>
                {
                    new InsurancePolicyDriver
                    {
                        FirstName   = "Test",
                        Surname     = "Driver",
                        DateOfBirth = DateTime.UtcNow.AddYears(-77)
                    }
                }
            };

            // Call Validate
            var result = policy.Validate();

            // Verify result
            Assert.IsFalse(result.Succeeded);
            Assert.AreEqual("Age of Oldest Driver: Test Driver", result.Error);
        }