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