Esempio n. 1
0
        public async Task SegmentControllerPatchRestrictionReturnsBadRequestWhenModelIsInvalid()
        {
            // Arrange
            var model      = new PatchRestrictionModel();
            var controller = BuildSegmentController();

            controller.ModelState.AddModelError(string.Empty, "Model is not valid");

            // Act
            var result = await controller.PatchRestriction(model, Guid.NewGuid()).ConfigureAwait(false);

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

            Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode);

            controller.Dispose();
        }
Esempio n. 2
0
        public async Task SegmentControllerPatchRestrictionReturnsSuccessForSuccessfulResponse(HttpStatusCode expectedStatus)
        {
            // Arrange
            var controller = BuildSegmentController();
            var model      = new PatchRestrictionModel();

            A.CallTo(() => FakeSkillSegmentService.PatchRestrictionAsync(A <PatchRestrictionModel> .Ignored, A <Guid> .Ignored)).Returns(expectedStatus);

            // Act
            var result = await controller.PatchRestriction(model, Guid.NewGuid()).ConfigureAwait(false);

            // Assert
            A.CallTo(() => FakeSkillSegmentService.PatchRestrictionAsync(A <PatchRestrictionModel> .Ignored, A <Guid> .Ignored)).MustHaveHappenedOnceExactly();
            var statusCodeResult = Assert.IsType <StatusCodeResult>(result);

            Assert.Equal((int)expectedStatus, statusCodeResult.StatusCode);

            controller.Dispose();
        }
Esempio n. 3
0
        public async Task <HttpStatusCode> PatchRestrictionAsync(PatchRestrictionModel 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?.Restrictions?.FirstOrDefault(sm => sm.Id == patchModel.Id);

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

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

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

            existingSegmentModel.SequenceNumber = patchModel.SequenceNumber;

            return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false));
        }
        public async Task <IActionResult> PatchRestriction([FromBody] PatchRestrictionModel patchRestrictionModel, Guid documentId)
        {
            logService.LogInformation($"{PatchRestrictionActionName} has been called");

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

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

            var response = await skillSegmentService.PatchRestrictionAsync(patchRestrictionModel, documentId).ConfigureAwait(false);

            if (response != HttpStatusCode.OK && response != HttpStatusCode.Created)
            {
                logService.LogError($"{PatchRestrictionActionName}: Error while patching Skills Matrix content for Job Profile with Id: {patchRestrictionModel.JobProfileId} for {patchRestrictionModel.Title} ");
            }

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