public async Task HowToBecomeSegmentServicePatchRequirementsReturnsExceptionWhenPatchmodelIsNull() { // arrange PatchRequirementsModel patchModel = null; var documentId = Guid.NewGuid(); // act var exceptionResult = await Assert.ThrowsAsync <ArgumentNullException>(async() => await howToBecomeSegmentService.PatchRequirementsAsync(patchModel, documentId).ConfigureAwait(false)).ConfigureAwait(false); // assert Assert.Equal("Value cannot be null. (Parameter 'patchModel')", exceptionResult.Message); }
public async Task SegmentControllerPatchRequirementsReturnsBadRequestWhenNullPatchmodel(string mediaTypeName) { // Arrange const HttpStatusCode expectedResponse = HttpStatusCode.BadRequest; PatchRequirementsModel patchModel = null; var documentId = Guid.NewGuid(); var controller = BuildSegmentController(mediaTypeName); // Act var result = await controller.PatchRequirements(patchModel, documentId).ConfigureAwait(false); // Assert var statusResult = Assert.IsType <BadRequestResult>(result); Assert.Equal((int)expectedResponse, statusResult.StatusCode); controller.Dispose(); }
public async Task <HttpStatusCode> PatchRequirementsAsync(PatchRequirementsModel patchModel, Guid documentId) { if (patchModel is null) { throw new ArgumentNullException(nameof(patchModel)); } var existingSegmentModel = await GetByIdAsync(documentId).ConfigureAwait(false); if (existingSegmentModel is null) { return(HttpStatusCode.NotFound); } if (patchModel.SequenceNumber <= existingSegmentModel.SequenceNumber) { return(HttpStatusCode.AlreadyReported); } var existingCommonRoute = existingSegmentModel.GetExistingCommonRoute(patchModel.RouteName); var existingRequirement = existingCommonRoute?.EntryRequirements.FirstOrDefault(r => r.Id == patchModel.Id); if (existingRequirement is null) { return(patchModel.MessageAction == MessageAction.Deleted ? HttpStatusCode.AlreadyReported : HttpStatusCode.NotFound); } var existingIndex = existingCommonRoute.EntryRequirements.ToList().FindIndex(ai => ai.Id == patchModel.Id); if (patchModel.MessageAction == MessageAction.Deleted) { existingSegmentModel.Data.EntryRoutes.CommonRoutes.First(e => e.RouteName == patchModel.RouteName).EntryRequirements.RemoveAt(existingIndex); } else { var updatedEntryRequirements = mapper.Map <EntryRequirement>(patchModel); existingSegmentModel.Data.EntryRoutes.CommonRoutes.First(e => e.RouteName == patchModel.RouteName).EntryRequirements[existingIndex] = updatedEntryRequirements; } existingSegmentModel.SequenceNumber = patchModel.SequenceNumber; return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false)); }
public async Task <IActionResult> PatchRequirements([FromBody] PatchRequirementsModel patchRequirementsModel, Guid documentId) { logService.LogInformation($"{PatchRequirementsActionName} has been called"); if (patchRequirementsModel == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var response = await howToBecomeSegmentService.PatchRequirementsAsync(patchRequirementsModel, documentId).ConfigureAwait(false); if (response != HttpStatusCode.OK && response != HttpStatusCode.Created) { logService.LogError($"{PatchRequirementsActionName}: Error while patching Requirement content for Job Profile with Id: {patchRequirementsModel.JobProfileId} for the {patchRequirementsModel.RouteName.ToString()} link"); } return(new StatusCodeResult((int)response)); }