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); } }
public override bool Equals(object obj) { MitchellClaim claim = obj as MitchellClaim; if ((object)claim == null) return false; return this == claim; }
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); } } }
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."); }
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."); }
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); }
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; }
public bool TryGetClaim(string claimNumber, out MitchellClaim claim) { throw new System.NotImplementedException(); }
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."); }
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."); }
public void UpdateClaim(MitchellClaim updater) { throw new System.NotImplementedException(); }
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."); }
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; }
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; }
/// <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); }
public void AddClaim(MitchellClaim claim) { throw new System.NotImplementedException(); }
public bool TryGetClaim(string claimNumber, out MitchellClaim claim) { return _claims.TryGetValue(claimNumber, out claim); }