示例#1
0
        public void UpdateClaim(MitchellClaim updater)
        {
            if (string.IsNullOrWhiteSpace(updater.ClaimNumber))
            {
                throw new InvalidApiUsageException(ApiUsageError.ClaimNumberNotSpecified);
            }

            MitchellClaim existingClaim;
            if (_claims.TryGetValue(updater.ClaimNumber, out existingClaim))
            {
                // Before updating the claim let's make sure we will not end up with an invalid claim.
                // We are going to create a clone, update the clone and only if everything looks fine then we'll 
                // persist the clone.
                MitchellClaim newClaim = existingClaim.DeepClone();
                newClaim.Update(updater);

                if (newClaim.ValidateRequiredFields() == false)
                {
                    throw new InvalidApiUsageException(ApiUsageError.RequiredFieldNotSpecified);
                }

                // Now that we know everything is OK, persist the updated claim.
                _claims[updater.ClaimNumber] = newClaim;
            }
            else
            {
                // This method is used incorrectly. The client may attempt to update a claim that does not exist but 
                // that attempt should not propagate to this level. 
                // As far as this method is concerned, this is an invalid call.
                throw new InvalidApiUsageException(ApiUsageError.ItemNotFound);
            }
        }
示例#2
0
        public override bool Equals(object obj)
        {
            MitchellClaim claim = obj as MitchellClaim;
            if ((object)claim == null) return false;

            return this == claim;
        }
示例#3
0
        public void Update(MitchellClaim updater)
        {
            if (updater.ClaimNumber != null && this.ClaimNumber != updater.ClaimNumber)
            {
                throw new InternalErrorException("MitchellClaim.Update can only be invoked with a claim that has the same claim number as the target claim.");
            }

            this.ClaimantFirstName = updater.ClaimantFirstName ?? this.ClaimantFirstName;
            this.ClaimantLastName = updater.ClaimantLastName ?? this.ClaimantLastName;
            this.Status = updater.Status ?? this.Status;
            this.LossDate = updater.LossDate ?? this.LossDate;
            this.AssignedAdjusterId = updater.AssignedAdjusterId ?? this.AssignedAdjusterId;

            if (updater.LossInfo != null)
            {
                this.LossInfo.Update(updater.LossInfo);
            }

            // Update or add vehicles present in the updater.
            if (updater.Vehicles != null)
            {
                foreach (VehicleDetails updaterVehicleDetails in updater.Vehicles)
                {
                    VehicleDetails existingVehicle =
                        this.Vehicles.FirstOrDefault(v => v.Vin == updaterVehicleDetails.Vin);
                    if (existingVehicle != null)
                    {
                        existingVehicle.Update(updaterVehicleDetails);
                    }
                    else
                    {
                        this.Vehicles.Add(updaterVehicleDetails.DeepClone());
                    }
                }

                // Now handle vehicle deletion.
                // Vehicles that are present in the current claim but absent in the updater will be deleted.
                List<VehicleDetails> vehiclesToDelete = new List<VehicleDetails>();
                foreach (VehicleDetails existingVehicleDetails in this.Vehicles)
                {
                    if (updater.Vehicles.FirstOrDefault(v => v.Vin == existingVehicleDetails.Vin) == null)
                    {
                        // A vehicle that was found in this claim is absent from the updater.
                        // This is an indication that the existing vehicle should be deleted.
                        vehiclesToDelete.Add(existingVehicleDetails);
                    }
                }

                foreach (VehicleDetails vehicleToDelete in vehiclesToDelete)
                {
                    this.Vehicles.Remove(vehicleToDelete);
                }
            }
        }
示例#4
0
        public void TestPutSimple()
        {
            string newClaimNumber = TestDataGenerator.GenerateUniqueClaimNumber();
            MitchellClaim expectedClaim = TestDataGenerator.GetTestClaim(newClaimNumber);

            // Create a new claim
            HttpResponseMessage response = ClaimsController.Post(expectedClaim);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "A POST of a new claim should succeed.");

            // Prepare a claim "updater".
            MitchellClaim updater = new MitchellClaim();

            updater.ClaimantLastName = "NewLastName";
            expectedClaim.ClaimantLastName = updater.ClaimantLastName;

            updater.LossDate = expectedClaim.LossDate.Value.AddDays(1);
            expectedClaim.LossDate = updater.LossDate;

            // Note:  The updater will have to include both vehicles that are changed and those who are not changed.
            //        The vehicles that are not changed will only have the Vin field set.
            //        This system enables us to delete vehicles with the update request. The tread-off is that when we 
            //        specify a list of vehicles then that list must include vehicles that are not changed.
            updater.Vehicles = new List<VehicleDetails>();

            updater.Vehicles.Add(new VehicleDetails() { Vin = expectedClaim.Vehicles[0].Vin });

            VehicleDetails sourceVehicle = expectedClaim.Vehicles[1];
            VehicleDetails updaterVehicle = new VehicleDetails()
            {
                Vin = sourceVehicle.Vin,
                Mileage = sourceVehicle.Mileage + 100
            };
            updater.Vehicles.Add(updaterVehicle);
            sourceVehicle.Mileage = updaterVehicle.Mileage;

            // Update the claim.
            response = ClaimsController.Put(newClaimNumber, updater);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "A PUT of an existing claim should succeed.");

            // Retrieved the updated claim and compare it with the expected value.
            MitchellClaim retrievedClaim = ClaimsController.Get(newClaimNumber);

            Assert.AreEqual(expectedClaim, retrievedClaim, "The claim that was created, updated and retrieved should have the expected values.");
        }
示例#5
0
        public void TestPutClaimAndAddNewVehicle()
        {
            string newClaimNumber = TestDataGenerator.GenerateUniqueClaimNumber();
            MitchellClaim expectedClaim = TestDataGenerator.GetTestClaim(newClaimNumber);

            // Create a new claim
            HttpResponseMessage response = ClaimsController.Post(expectedClaim);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "A POST of a new claim should succeed.");

            // Prepare a claim "updater".
            MitchellClaim updater = new MitchellClaim();

            updater.ClaimantLastName = "NewLastName";
            expectedClaim.ClaimantLastName = updater.ClaimantLastName;

            // Note:  The updater will have to include both vehicles that are changed and those who are not changed.
            //        The vehicles that are not changed will only have the Vin field set.
            //        This system enables us to delete vehicles with the update request. The tread-off is that when we 
            //        specify a list of vehicles then that list must include vehicles that are not changed.
            updater.Vehicles = new List<VehicleDetails>();

            updater.Vehicles.Add(new VehicleDetails() { Vin = expectedClaim.Vehicles[0].Vin });
            updater.Vehicles.Add(new VehicleDetails() { Vin = expectedClaim.Vehicles[1].Vin });

            // We'll request a new vehicle to be added. However, this vehicle has required parameters that are not specified.
            VehicleDetails newVehicle = new VehicleDetails()
            {
                Vin = TestDataGenerator.GenerateUniqueVinNumber(),
                ModelYear = 2015,
                Mileage = 200
            };
            updater.Vehicles.Add(newVehicle);
            expectedClaim.Vehicles.Add(newVehicle.DeepClone());

            // Update the claim.
            response = ClaimsController.Put(newClaimNumber, updater);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "A PUT of an existing claim should succeed.");

            // Retrieved the updated claim and compare it with the expected value.
            MitchellClaim retrievedClaim = ClaimsController.Get(newClaimNumber);

            Assert.AreEqual(expectedClaim.Vehicles.Count, 3, "Defensive check - making sure that the expected claim was setup correctly.");
            Assert.AreEqual(expectedClaim, retrievedClaim, "The claim that was created, updated and retrieved should have the expected values.");
        }
示例#6
0
        public void AddClaim(MitchellClaim claim)
        {
            if (claim.ValidateRequiredFields() == false)
            {
                throw new InvalidApiUsageException(ApiUsageError.RequiredFieldNotSpecified);
            }

            if (_claims.ContainsKey(claim.ClaimNumber))
            {
                throw new InvalidApiUsageException(ApiUsageError.ItemAlreadyExists);
            }

            if (claim.Vehicles.GroupBy(v => v.Vin).Any(g => g.Count() > 1))
            {
                throw new InvalidApiUsageException(ApiUsageError.DuplicateVehicles);
            }

            _claims.Add(claim.ClaimNumber, claim);
        }
示例#7
0
        public MitchellClaim DeepClone()
        {
            MitchellClaim newClaim = new MitchellClaim()
            {
                ClaimNumber = this.ClaimNumber,
                ClaimantFirstName = this.ClaimantFirstName,
                ClaimantLastName = this.ClaimantLastName,
                Status = this.Status,
                LossDate = this.LossDate,
                LossInfo = this.LossInfo.DeepClone(),
                AssignedAdjusterId = this.AssignedAdjusterId,
            };

            if (this.Vehicles != null)
            {
                newClaim.Vehicles = new List<VehicleDetails>();
                for (int i = 0; i < this.Vehicles.Count; i++)
                {
                    newClaim.Vehicles.Add(this.Vehicles[i].DeepClone());
                }
            }

            return newClaim;
        }
示例#8
0
 public bool TryGetClaim(string claimNumber, out MitchellClaim claim)
 {
     throw new System.NotImplementedException();
 }
示例#9
0
        public void TestPutClaimWithEmptyVehicleList()
        {
            string newClaimNumber = TestDataGenerator.GenerateUniqueClaimNumber();
            MitchellClaim expectedClaim = TestDataGenerator.GetTestClaim(newClaimNumber);

            // Create a new claim
            HttpResponseMessage response = ClaimsController.Post(expectedClaim);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "A POST of a new claim should succeed.");

            // Prepare a claim "updater".
            MitchellClaim updater = new MitchellClaim();

            updater.ClaimantLastName = "NewLastName";
            expectedClaim.ClaimantLastName = updater.ClaimantLastName;

            updater.Vehicles = new List<VehicleDetails>();

            // Update the claim.
            response = ClaimsController.Put(newClaimNumber, updater);
            Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode, "An update request where the updater has an empty vehicle list is not legal.");

            // Retrieved the claim we attempted to update and make sure it did not change.
            MitchellClaim retrievedClaim = ClaimsController.Get(newClaimNumber);

            Assert.AreEqual(expectedClaim, retrievedClaim, "The claim that was subject to a failed update should not have changed.");
        }
示例#10
0
        public void TestPutWithRequiredParametersNotSpecified()
        {
            string newClaimNumber = TestDataGenerator.GenerateUniqueClaimNumber();
            MitchellClaim expectedClaim = TestDataGenerator.GetTestClaim(newClaimNumber);

            // Create a new claim
            HttpResponseMessage response = ClaimsController.Post(expectedClaim);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "A POST of a new claim should succeed.");

            // Prepare a claim "updater".
            MitchellClaim updater = new MitchellClaim();

            updater.ClaimantLastName = "NewLastName";

            updater.Vehicles = new List<VehicleDetails>();

            // We'll request a new vehicle to be added. However, this vehicle has required parameters that are not specified.
            updater.Vehicles.Add(new VehicleDetails() { Vin = TestDataGenerator.GenerateUniqueVinNumber(), Mileage = 100 });

            // Update the claim. 
            response = ClaimsController.Put(newClaimNumber, updater);
            Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode, "An update of a claim that would results in required fields that are not specified should fail with a specific status.");

            // Retrieved the claim we attempted to update and make sure it did not change.
            MitchellClaim retrievedClaim = ClaimsController.Get(newClaimNumber);

            Assert.AreEqual(expectedClaim, retrievedClaim, "The claim that was subject to a failed update should not have changed.");
        }
示例#11
0
 public void UpdateClaim(MitchellClaim updater)
 {
     throw new System.NotImplementedException();
 }
示例#12
0
        public void TestPutClaimWithNoVehicleList()
        {
            string newClaimNumber = TestDataGenerator.GenerateUniqueClaimNumber();
            MitchellClaim expectedClaim = TestDataGenerator.GetTestClaim(newClaimNumber);

            // Create a new claim
            HttpResponseMessage response = ClaimsController.Post(expectedClaim);
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "A POST of a new claim should succeed.");

            // Prepare a claim "updater".
            MitchellClaim updater = new MitchellClaim();

            updater.ClaimantLastName = "NewLastName";
            expectedClaim.ClaimantLastName = updater.ClaimantLastName;

            // Update the claim.
            response = ClaimsController.Put(newClaimNumber, updater);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "A PUT of an existing claim should succeed.");

            // Retrieved the updated claim and compare it with the expected value.
            MitchellClaim retrievedClaim = ClaimsController.Get(newClaimNumber);

            Assert.AreEqual(expectedClaim.Vehicles.Count, 2, "Defensive check - making sure that the expected claim was setup correctly.");
            Assert.AreEqual(expectedClaim, retrievedClaim, "The claim that was created, updated and retrieved should have the expected values.");
        }
示例#13
0
        private HttpResponseMessage UpdateClaim(MitchellClaim updater)
        {
            HttpResponseMessage response;

            try
            {
                _repository.UpdateClaim(updater);
                response = new HttpResponseMessage(HttpStatusCode.OK);
            }
            catch (InvalidApiUsageException ex) when (ex.UsageError == ApiUsageError.RequiredFieldNotSpecified)
            {
                response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "A required field is missing." };
            }
            catch (InvalidApiUsageException ex) when (ex.UsageError == ApiUsageError.DuplicateVehicles)
            {
                response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "Duplicate vehicles are not allowed." };
            }

            return response;
        }
示例#14
0
        private HttpResponseMessage AddClaim(MitchellClaim claim)
        {
            HttpResponseMessage response;

            try
            {
                _repository.AddClaim(claim);

                response = new HttpResponseMessage(HttpStatusCode.Created);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { claimNumber = claim.ClaimNumber }));
            }
            catch (InvalidApiUsageException ex) when (ex.UsageError == ApiUsageError.ItemAlreadyExists)
            {
                response = new HttpResponseMessage(HttpStatusCode.Conflict) { ReasonPhrase = "Resource already exists." };
            }
            catch (InvalidApiUsageException ex) when (ex.UsageError == ApiUsageError.RequiredFieldNotSpecified)
            {
                response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "A required field is missing." };
            }
            catch (InvalidApiUsageException ex) when (ex.UsageError == ApiUsageError.DuplicateVehicles)
            {
                response = new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = "Duplicate vehicles are not allowed." };
            }
            catch (InvalidApiUsageException)
            {
                response = new HttpResponseMessage(HttpStatusCode.Forbidden);
            }

            return response;
        }
示例#15
0
        /// <summary>
        /// Updates the given claim changing a few fields. 
        /// At this point, those fields are not parameterizable.
        /// </summary>
        /// <remarks>
        /// Note that UpdateClaim and GetTestClaimUpdateInXmlFormat must be kept in sync.
        /// </remarks>
        public static void UpdateClaim(MitchellClaim claim)
        {
            claim.ClaimantLastName = "NewLastName";
            claim.Status = ClaimStatus.Open;
            claim.LossDate = new DateTime(2016, 1, 2, 13, 14, 15, 700, DateTimeKind.Utc);

            claim.LossInfo.CauseOfLoss = CauseOfLoss.Collision;

            VehicleDetails vehicleDetails = claim.Vehicles.Single(v => v.Vin == "1M8GDM9AXKP000001");
            vehicleDetails.ModelYear = 2014;
            vehicleDetails.LicPlateExpDate = new DateTime(2017, 1, 3, 0, 0, 0, DateTimeKind.Utc);
        }
示例#16
0
 public void AddClaim(MitchellClaim claim)
 {
     throw new System.NotImplementedException();
 }
示例#17
0
 public bool TryGetClaim(string claimNumber, out MitchellClaim claim)
 {
     return _claims.TryGetValue(claimNumber, out claim);
 }