public async Task JobProfileOverviewSegmentServicePatchWorkingPatternReturnsExceptionWhenPatchmodelIsNull()
        {
            // arrange
            PatchWorkingPatternModel patchModel = null;
            var documentId = Guid.NewGuid();

            // act
            var exceptionResult = await Assert.ThrowsAsync <ArgumentNullException>(async() => await jobProfileOverviewSegmentService.PatchWorkingPatternAsync(patchModel, documentId).ConfigureAwait(false)).ConfigureAwait(false);

            // assert
            Assert.Equal("Value cannot be null. (Parameter 'patchModel')", exceptionResult.Message);
        }
        public async Task SegmentControllerPatchWorkingPatternReturnsBadRequestWhenNullPatchmodel(string mediaTypeName)
        {
            // Arrange
            const HttpStatusCode     expectedResponse = HttpStatusCode.BadRequest;
            PatchWorkingPatternModel patchModel       = null;
            var documentId = Guid.NewGuid();
            var controller = BuildSegmentController(mediaTypeName);

            // Act
            var result = await controller.PatchWorkingPattern(patchModel, documentId).ConfigureAwait(false);

            // Assert
            var statusResult = Assert.IsType <BadRequestResult>(result);

            Assert.Equal((int)expectedResponse, statusResult.StatusCode);

            controller.Dispose();
        }
        public async Task <HttpStatusCode> PatchWorkingPatternAsync(PatchWorkingPatternModel 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 existingWorkingPattern = existingSegmentModel.Data?.WorkingPattern.SingleOrDefault(r => r.Id == patchModel.Id);

            if (existingWorkingPattern is null)
            {
                return(patchModel.MessageAction == MessageAction.Deleted ? HttpStatusCode.AlreadyReported : HttpStatusCode.NotFound);
            }

            var existingIndex = existingSegmentModel.Data.WorkingPattern.ToList().FindIndex(ai => ai.Id == patchModel.Id);

            if (patchModel.MessageAction == MessageAction.Deleted)
            {
                existingSegmentModel.Data.WorkingPattern.RemoveAt(existingIndex);
            }
            else
            {
                var updatedAdditionalInfo = mapper.Map <Data.Models.WorkingPattern>(patchModel);
                existingSegmentModel.Data.WorkingPattern[existingIndex] = updatedAdditionalInfo;
            }

            existingSegmentModel.SequenceNumber = patchModel.SequenceNumber;

            return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false));
        }
        public async Task <IActionResult> PatchWorkingPattern([FromBody] PatchWorkingPatternModel patchWorkingPatternModel, Guid documentId)
        {
            logService.LogInformation($"{PatchWorkingPatternActionName} has been called");

            if (patchWorkingPatternModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var response = await jobProfileOverviewSegmentService.PatchWorkingPatternAsync(patchWorkingPatternModel, documentId).ConfigureAwait(false);

            if (response != HttpStatusCode.OK && response != HttpStatusCode.Created)
            {
                logService.LogError($"{PatchWorkingPatternActionName}: Error while patching Working Pattern content for Job Profile with Id: {patchWorkingPatternModel.JobProfileId} for the {patchWorkingPatternModel.Title} pattern");
            }

            return(new StatusCodeResult((int)response));
        }