public async Task SegmentControllerPatchLocationReturnsBadRequestWhenModelIsInvalid() { // Arrange var model = new PatchLocationModel(); var controller = BuildSegmentController(); controller.ModelState.AddModelError(string.Empty, "Model is not valid"); // Act var result = await controller.PatchLocation(model, Guid.NewGuid()).ConfigureAwait(false); // Assert var statusResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode); controller.Dispose(); }
public async Task SegmentControllerPatchLocationReturnsSuccessForSuccessfulResponse(HttpStatusCode expectedStatus) { // Arrange var controller = BuildSegmentController(); var model = new PatchLocationModel(); A.CallTo(() => FakeJobProfileSegmentService.PatchLocationAsync(A <PatchLocationModel> .Ignored, A <Guid> .Ignored)).Returns(expectedStatus); // Act var result = await controller.PatchLocation(model, Guid.NewGuid()).ConfigureAwait(false); // Assert A.CallTo(() => FakeJobProfileSegmentService.PatchLocationAsync(A <PatchLocationModel> .Ignored, A <Guid> .Ignored)).MustHaveHappenedOnceExactly(); var statusCodeResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal((int)expectedStatus, statusCodeResult.StatusCode); controller.Dispose(); }
public async Task <HttpStatusCode> PatchLocationAsync(PatchLocationModel patchModel, Guid documentId) { if (patchModel == 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 existingLocation = existingSegmentModel.Data?.Locations?.FirstOrDefault(u => u.Id == patchModel.Id); if (existingLocation is null) { return(patchModel.MessageAction == MessageActionType.Deleted ? HttpStatusCode.AlreadyReported : HttpStatusCode.NotFound); } var existingIndex = existingSegmentModel.Data.Locations.ToList().FindIndex(ai => ai.Id == patchModel.Id); if (patchModel.MessageAction == MessageActionType.Deleted) { existingSegmentModel.Data.Locations.RemoveAt(existingIndex); } else { var updatedLocation = mapper.Map <Location>(patchModel); existingSegmentModel.Data.Locations[existingIndex] = updatedLocation; } existingSegmentModel.SequenceNumber = patchModel.SequenceNumber; return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false)); }
public async Task <IActionResult> PatchLocation([FromBody] PatchLocationModel patchDocument, Guid documentId) { logService.LogInformation($"{PatchLocationActionName} has been called"); if (patchDocument == null) { logService.LogInformation($"{PatchLocationActionName}. No document was passed"); return(BadRequest()); } if (!ModelState.IsValid) { logService.LogInformation($"{PatchLocationActionName}. Model state is invalid"); return(BadRequest(ModelState)); } var statusCode = await jobProfileTasksSegmentService.PatchLocationAsync(patchDocument, documentId).ConfigureAwait(false); return(StatusCode((int)statusCode)); }