public async Task SegmentControllerPatchSkillsMatrixReturnsBadRequestWhenModelIsInvalid() { // Arrange var model = new PatchContextualisedModel(); var controller = BuildSegmentController(); controller.ModelState.AddModelError(string.Empty, "Model is not valid"); // Act var result = await controller.PatchSkillsMatrix(model, Guid.NewGuid()).ConfigureAwait(false); // Assert var statusResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode); controller.Dispose(); }
public async Task SegmentControllerPatchSkillsMatrixReturnsSuccessForSuccessfulResponse(HttpStatusCode expectedStatus) { // Arrange var controller = BuildSegmentController(); var model = new PatchContextualisedModel(); A.CallTo(() => FakeSkillSegmentService.PatchSkillsMatrixAsync(A <PatchContextualisedModel> .Ignored, A <Guid> .Ignored)).Returns(expectedStatus); // Act var result = await controller.PatchSkillsMatrix(model, Guid.NewGuid()).ConfigureAwait(false); // Assert A.CallTo(() => FakeSkillSegmentService.PatchSkillsMatrixAsync(A <PatchContextualisedModel> .Ignored, A <Guid> .Ignored)).MustHaveHappenedOnceExactly(); var statusCodeResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal((int)expectedStatus, statusCodeResult.StatusCode); controller.Dispose(); }
public async Task <HttpStatusCode> PatchSkillsMatrixAsync(PatchContextualisedModel 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 existingSkillMatrix = existingSegmentModel.Data?.Skills?.FirstOrDefault(sm => sm.ContextualisedSkill?.Id == patchModel.Id); if (existingSkillMatrix is null) { return(patchModel.MessageAction == MessageAction.Deleted ? HttpStatusCode.AlreadyReported : HttpStatusCode.NotFound); } var existingIndex = existingSegmentModel.Data.Skills.ToList().FindIndex(ai => ai.ContextualisedSkill.Id == patchModel.Id); if (patchModel.MessageAction == MessageAction.Deleted) { existingSegmentModel.Data.Skills.RemoveAt(existingIndex); } else { existingSegmentModel.Data.Skills[existingIndex] = mapper.Map <Skills>(patchModel); } existingSegmentModel.SequenceNumber = patchModel.SequenceNumber; return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false)); }
public async Task <IActionResult> PatchSkillsMatrix([FromBody] PatchContextualisedModel patchContextualisedModel, Guid documentId) { logService.LogInformation($"{PatchSkillsMatrixActionName} has been called"); if (patchContextualisedModel == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var response = await skillSegmentService.PatchSkillsMatrixAsync(patchContextualisedModel, documentId).ConfigureAwait(false); if (response != HttpStatusCode.OK && response != HttpStatusCode.Created) { logService.LogError($"{PatchSkillsMatrixActionName}: Error while patching Skills Matrix content for Job Profile with Id: {patchContextualisedModel.JobProfileId} for {patchContextualisedModel.Description} "); } return(new StatusCodeResult((int)response)); }