public async Task SegmentControllerPatchEnvironmentReturnsBadRequestWhenModelIsInvalid()
        {
            // Arrange
            var model      = new PatchEnvironmentsModel();
            var controller = BuildSegmentController();

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

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

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

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

            controller.Dispose();
        }
        public async Task SegmentControllerPatchEnvironmentReturnsSuccessForSuccessfulResponse(HttpStatusCode expectedStatus)
        {
            // Arrange
            var controller = BuildSegmentController();
            var model      = new PatchEnvironmentsModel();

            A.CallTo(() => FakeJobProfileSegmentService.PatchEnvironmentAsync(A <PatchEnvironmentsModel> .Ignored, A <Guid> .Ignored)).Returns(expectedStatus);

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

            // Assert
            A.CallTo(() => FakeJobProfileSegmentService.PatchEnvironmentAsync(A <PatchEnvironmentsModel> .Ignored, A <Guid> .Ignored)).MustHaveHappenedOnceExactly();
            var statusCodeResult = Assert.IsType <StatusCodeResult>(result);

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

            controller.Dispose();
        }
        public async Task <HttpStatusCode> PatchEnvironmentAsync(PatchEnvironmentsModel 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 existingEnvironment = existingSegmentModel.Data?.Environments?.FirstOrDefault(u => u.Id == patchModel.Id);

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

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

            if (patchModel.MessageAction == MessageActionType.Deleted)
            {
                existingSegmentModel.Data.Environments.RemoveAt(existingIndex);
            }
            else
            {
                var updatedEnvironment = mapper.Map <Environment>(patchModel);
                existingSegmentModel.Data.Environments[existingIndex] = updatedEnvironment;
            }

            existingSegmentModel.SequenceNumber = patchModel.SequenceNumber;

            return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false));
        }
Example #4
0
        public async Task <IActionResult> PatchEnvironment([FromBody] PatchEnvironmentsModel patchDocument, Guid documentId)
        {
            logService.LogInformation($"{PatchEnvironmentActionName} has been called");

            if (patchDocument == null)
            {
                logService.LogInformation($"{PatchEnvironmentActionName}. No document was passed");
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                logService.LogInformation($"{PatchEnvironmentActionName}. Model state is invalid");
                return(BadRequest(ModelState));
            }

            var statusCode = await jobProfileTasksSegmentService.PatchEnvironmentAsync(patchDocument, documentId).ConfigureAwait(false);

            return(StatusCode((int)statusCode));
        }