Example #1
0
        public async Task HowToBecomeSegmentServicePatchLinksReturnsExceptionWhenPatchmodelIsNull()
        {
            // arrange
            PatchLinksModel patchModel = null;
            var             documentId = Guid.NewGuid();

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

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

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

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

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

            controller.Dispose();
        }
Example #3
0
        public async Task <HttpStatusCode> PatchLinksAsync(PatchLinksModel 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 existingCommonRoute = existingSegmentModel.GetExistingCommonRoute(patchModel.RouteName);
            var linkToUpdate        = existingCommonRoute?.AdditionalInformation?.FirstOrDefault(ai => ai.Id == patchModel.Id);

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

            var existingIndex = existingCommonRoute.AdditionalInformation.ToList().FindIndex(ai => ai.Id == patchModel.Id);

            if (patchModel.MessageAction == MessageAction.Deleted)
            {
                existingSegmentModel.Data.EntryRoutes.CommonRoutes.First(e => e.RouteName == patchModel.RouteName).AdditionalInformation.RemoveAt(existingIndex);
            }
            else
            {
                var updatedAdditionalInfo = mapper.Map <AdditionalInformation>(patchModel);
                existingSegmentModel.Data.EntryRoutes.CommonRoutes.First(e => e.RouteName == patchModel.RouteName).AdditionalInformation[existingIndex] = updatedAdditionalInfo;
            }

            existingSegmentModel.SequenceNumber = patchModel.SequenceNumber;

            return(await UpsertAndRefreshSegmentModel(existingSegmentModel).ConfigureAwait(false));
        }
        public async Task <IActionResult> PatchLinks([FromBody] PatchLinksModel patchLinksModel, Guid documentId)
        {
            logService.LogInformation($"{PatchLinksActionName} has been called");

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

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

            var response = await howToBecomeSegmentService.PatchLinksAsync(patchLinksModel, documentId).ConfigureAwait(false);

            if (response != HttpStatusCode.OK && response != HttpStatusCode.Created)
            {
                logService.LogError($"{PatchLinksActionName}: Error while patching Link content for Job Profile with Id: {patchLinksModel.JobProfileId} for the {patchLinksModel.RouteName.ToString()} link");
            }

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