Beispiel #1
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenPageRegionDoesNotMatchRoute()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;
            var responseModel = new Regions.Models.Region()
            {
                DocumentId     = Guid.NewGuid(),
                Path           = path,
                PageRegion     = PageRegions.SidebarRight,
                RegionEndpoint = ValidEndpointValue,
                OfflineHtml    = ValidHtmlFragment
            };

            _httpRequestHelper.GetResourceFromRequest <Regions.Models.Region>(_request).Returns(Task.FromResult(responseModel).Result);

            _httpResponseMessageHelper.BadRequest().Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #2
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeOk_ForUpdatedRegion_WithPlaceHolder()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.OK;
            var responseModel = new Regions.Models.Region()
            {
                DocumentId     = Guid.NewGuid(),
                Path           = path,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValueWithPlaceHolder,
                OfflineHtml    = ValidHtmlFragment
            };

            _httpRequestHelper.GetResourceFromRequest <Regions.Models.Region>(_request).Returns(Task.FromResult(responseModel).Result);

            _regionService.ReplaceAsync(Arg.Any <Regions.Models.Region>()).Returns(Task.FromResult(responseModel).Result);

            _httpResponseMessageHelper.Ok(Arg.Any <string>()).Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #3
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeOk_ForUpdatedRegion_WithPlaceHolder()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.OK;
            var regionModel = new Regions.Models.Region()
            {
                DocumentId     = Guid.NewGuid(),
                Path           = path,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValueWithPlaceHolder,
                OfflineHtml    = ValidHtmlFragment
            };
            var regionService = serviceProvider.GetService <Services.IRegionService>();

            regionModel = await regionService.CreateAsync(regionModel);

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion, regionModel);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #4
0
        public async Task PatchRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenMissingBody()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Patch";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;
            var responseModel = new Regions.Models.Region()
            {
                DocumentId     = Guid.NewGuid(),
                Path           = "a-path",
                PageRegion     = PageRegions.Body,
                RegionEndpoint = ValidEndpointValueWithPlaceHolder,
            };

            _httpRequestHelper.GetResourceFromRequest <JsonPatchDocument <Region> >(_request).Returns(Task.FromResult(default(JsonPatchDocument <Region>)).Result);
            _regionService.GetAsync(Arg.Any <string>(), Arg.Any <PageRegions>()).Returns(Task.FromResult(responseModel).Result);
            _regionService.ReplaceAsync(Arg.Any <Regions.Models.Region>()).Returns(Task.FromResult(responseModel).Result);

            _httpResponseMessageHelper.BadRequest().Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #5
0
        public async Task GetRegionHttpTrigger_ReturnsStatusCodeOk_WhenRegionExists()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Get";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.OK;
            var regionModel = new Region()
            {
                Path           = path,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValueWithPlaceHolder,
            };
            var regionService = serviceProvider.GetService <Services.IRegionService>();

            _ = await regionService.CreateAsync(regionModel);

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
            var content = await result.Content.ReadAsStringAsync();

            var responseItem = JsonConvert.DeserializeObject <Region>(content);

            responseItem.Path.Should().Be(path);
            responseItem.PageRegion.Should().Be(pageRegion);
        }
Beispiel #6
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenBadPathUrlInBody()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;
            var responseModel = new Regions.Models.Region()
            {
                DocumentId     = Guid.NewGuid(),
                Path           = NullPathValue,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValue
            };

            _httpRequestHelper.GetResourceFromRequest <Regions.Models.Region>(_request).Returns(Task.FromResult(responseModel).Result);

            _regionService.ReplaceAsync(Arg.Any <Regions.Models.Region>()).Returns(Task.FromResult(responseModel).Result);

            _httpResponseMessageHelper.BadRequest().Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #7
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "paths/{path}/regions/{pageRegion}")]
            HttpRequest req,
            ILogger log,
            string path,
            int pageRegion,
            [Inject] ILoggerHelper loggerHelper,
            [Inject] IHttpRequestHelper httpRequestHelper,
            [Inject] IHttpResponseMessageHelper httpResponseMessageHelper,
            [Inject] IJsonHelper jsonHelper,
            [Inject] IRegionService regionService
            )
        {
            loggerHelper.LogMethodEnter(log);

            // validate the parameters are present
            var dssCorrelationId = httpRequestHelper.GetDssCorrelationId(req);

            if (string.IsNullOrEmpty(dssCorrelationId))
            {
                log.LogInformation($"Unable to locate '{nameof(dssCorrelationId)}' in request header");
            }

            if (!Guid.TryParse(dssCorrelationId, out var correlationGuid))
            {
                log.LogInformation($"Unable to parse '{nameof(dssCorrelationId)}' to a Guid");
                correlationGuid = Guid.NewGuid();
            }

            if (string.IsNullOrEmpty(path))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing value in request for '{nameof(path)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (pageRegion == 0 || !Enum.IsDefined(typeof(PageRegions), pageRegion))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing/invalid value in request for '{nameof(pageRegion)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            PageRegions pageRegionValue = (PageRegions)pageRegion;

            loggerHelper.LogInformationMessage(log, correlationGuid, $"Attempting to get Region {pageRegionValue} for Path {path}");

            var regionModel = await regionService.GetAsync(path, pageRegionValue);

            if (regionModel == null)
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Region does not exist for {pageRegionValue} for Path {path}");
                return(httpResponseMessageHelper.NoContent());
            }

            loggerHelper.LogMethodExit(log);

            return(regionModel != null
                ? httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(regionModel, "id", nameof(Models.Region.DocumentId)))
                : httpResponseMessageHelper.NoContent());
        }
Beispiel #8
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeUnprocessableEntity_WhenMissingBody()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.UnprocessableEntity;

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion, null);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task DeleteRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenPageRegionIsNone()
        {
            // arrange
            const string         path                   = ValidPathValue;
            const PageRegions    pageRegion             = PageRegions.None;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task DeleteRegionHttpTrigger_ReturnsStatusCodeNoContent_WhenRegionDoesNotExist()
        {
            // arrange
            const string         path                   = ValidPathNoContentValue;
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.NoContent;

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task PatchRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenPathIsNull()
        {
            // arrange
            const string         path                   = NullPathValue;
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;
            var regionPatchModel = new JsonPatchDocument <Region>();

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion, regionPatchModel);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task PatchRegionHttpTrigger_ReturnsStatusCodeNoContent_WhenRegionDoesNotExist()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Patch";
            const PageRegions    pageRegion             = PageRegions.SidebarLeft;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.NoContent;
            var regionPatchModel = new JsonPatchDocument <Region>();

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion, regionPatchModel);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task DeleteRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenPathIsNull()
        {
            // arrange
            const string         path                   = NullPathValue;
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;

            _httpResponseMessageHelper.BadRequest().Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task DeleteRegionHttpTrigger_ReturnsStatusCodeNoContent_WhenRegionDoesNotExist()
        {
            // arrange
            const string         path                   = ValidPathNoContentValue;
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.NoContent;

            _regionService.GetAsync(Arg.Any <string>(), Arg.Any <PageRegions>()).Returns(Task.FromResult <Regions.Models.Region>(null).Result);

            _httpResponseMessageHelper.NoContent().Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #15
0
        public async Task GetRegionHttpTrigger_ReturnsStatusCodeOk_WhenRegionExists()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Get";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.OK;
            var responseModel = new Regions.Models.Region();

            _regionService.GetAsync(Arg.Any <string>(), Arg.Any <PageRegions>()).Returns(Task.FromResult(responseModel).Result);

            _httpResponseMessageHelper.Ok(Arg.Any <string>()).Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #16
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeUnprocessableEntity_WhenMissingBody()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.UnprocessableEntity;

            _httpRequestHelper.GetResourceFromRequest <Regions.Models.Region>(_request).Throws(new JsonException());

            _regionService.ReplaceAsync(Arg.Any <Regions.Models.Region>()).Returns(Task.FromResult <Regions.Models.Region>(null).Result);

            _httpResponseMessageHelper.UnprocessableEntity(Arg.Any <JsonException>()).Returns(x => new HttpResponseMessage(expectedHttpStatusCode));

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task GetAsyncTest()
        {
            // arrange
            const string      path       = ValidPathValue + "_Get";
            const PageRegions pageRegion = PageRegions.Body;
            var regionModel = new Region()
            {
                Path       = path,
                PageRegion = pageRegion
            };

            _documentDbProvider.GetRegionForPathAsync(Arg.Any <string>(), Arg.Any <PageRegions>()).Returns(Task.FromResult(regionModel).Result);

            // act
            var result = await _regionService.GetAsync(path, pageRegion);

            // assert
            result.Should().NotBeNull();
            result.Path.Should().Be(path);
            result.PageRegion.Should().Be(pageRegion);
        }
Beispiel #18
0
        public async Task PutRegionHttpTrigger_ReturnsStatusCodeBadRequest_WhenDocumemntIdIsMissing()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Put";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.BadRequest;
            var regionModel = new Regions.Models.Region()
            {
                Path           = path,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValue,
                OfflineHtml    = ValidHtmlFragment
            };

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion, regionModel);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
        public async Task DeleteAsyncTest()
        {
            // arrange
            const string      path       = ValidPathValue + "_Delete";
            const PageRegions pageRegion = PageRegions.Body;
            var regionModel = new Region()
            {
                DocumentId = Guid.NewGuid(),
                Path       = path,
                PageRegion = pageRegion
            };

            var resourceResponse = MockResourceResponse(HttpStatusCode.NoContent);

            _documentDbProvider.DeleteRegionAsync(Arg.Any <Guid>()).Returns(Task.FromResult(resourceResponse).Result);

            // act
            var result = await _regionService.DeleteAsync(regionModel.DocumentId.Value);

            // assert
            result.Should().BeTrue();
        }
Beispiel #20
0
        public async Task CreateAsyncTest()
        {
            // arrange
            const string      path       = ValidPathValue + "_Create";
            const PageRegions pageRegion = PageRegions.Body;
            var regionModel = new Region()
            {
                Path           = path,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValueWithPlaceHolder,
            };
            var resourceResponse = MockResourceResponse(HttpStatusCode.Created);

            _documentDbProvider.CreateRegionAsync(Arg.Any <Region>()).Returns(Task.FromResult(resourceResponse).Result);

            // act
            var result = await _regionService.CreateAsync(regionModel);

            // assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <Models.Region>(result);
        }
        public async Task GetByIdAsyncTest()
        {
            // arrange
            const string      path       = ValidPathValue + "_GetById";
            const PageRegions pageRegion = PageRegions.Body;
            var regionModel = new Region()
            {
                DocumentId = Guid.NewGuid(),
                Path       = path,
                PageRegion = pageRegion
            };

            _documentDbProvider.GetRegionByIdAsync(Arg.Any <Guid>()).Returns(Task.FromResult(regionModel).Result);

            // act
            var result = await _regionService.GetByIdAsync(regionModel.DocumentId.Value);

            // assert
            result.Should().NotBeNull();
            result.Path.Should().Be(path);
            result.PageRegion.Should().Be(pageRegion);
        }
        public async Task DeleteRegionHttpTrigger_ReturnsStatusCodeOk_WhenRegionExists()
        {
            // arrange
            const string         path                   = ValidPathValue + "_Delete";
            const PageRegions    pageRegion             = PageRegions.Body;
            const HttpStatusCode expectedHttpStatusCode = HttpStatusCode.OK;
            var regionModel = new Region()
            {
                Path           = path,
                PageRegion     = pageRegion,
                RegionEndpoint = ValidEndpointValueWithPlaceHolder,
            };
            var regionService = serviceProvider.GetService <Services.IRegionService>();

            _ = await regionService.CreateAsync(regionModel);

            // act
            var result = await RunFunctionAsync(path, (int)pageRegion);

            // assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(expectedHttpStatusCode, result.StatusCode);
        }
Beispiel #23
0
        public async Task ReplaceAsyncTest()
        {
            // arrange
            const string      path       = ValidPathValue + "_Replace";
            const PageRegions pageRegion = PageRegions.Body;
            var regionModel = new Region()
            {
                DocumentId = Guid.NewGuid(),
                Path       = path,
                PageRegion = pageRegion
            };

            var resourceResponse = MockResourceResponse(HttpStatusCode.OK);

            _documentDbProvider.UpdateRegionAsync(Arg.Any <Region>()).Returns(Task.FromResult(resourceResponse).Result);

            // act
            var result = await _regionService.ReplaceAsync(regionModel);

            // assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <Models.Region>(result);
        }
Beispiel #24
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "paths/{path}/regions/{pageRegion}")]
            HttpRequest req,
            ILogger log,
            string path,
            int pageRegion,
            [Inject] ILoggerHelper loggerHelper,
            [Inject] IHttpRequestHelper httpRequestHelper,
            [Inject] IHttpResponseMessageHelper httpResponseMessageHelper,
            [Inject] IJsonHelper jsonHelper,
            [Inject] IRegionService regionService
            )
        {
            loggerHelper.LogMethodEnter(log);

            // validate the parameters are present
            var correlationId = httpRequestHelper.GetOrCreateDssCorrelationId(req);

            if (string.IsNullOrEmpty(path))
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Missing value in request for '{nameof(path)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (pageRegion == 0 || !Enum.IsDefined(typeof(PageRegions), pageRegion))
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Missing/invalid value in request for '{nameof(pageRegion)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            PageRegions pageRegionValue = (PageRegions)pageRegion;

            JsonPatchDocument <Region> regionPatch;

            try
            {
                regionPatch = await httpRequestHelper.GetResourceFromRequest <JsonPatchDocument <Region> >(req);

                if (regionPatch == null)
                {
                    loggerHelper.LogException(log, correlationId, "Request body is empty", null);
                    return(httpResponseMessageHelper.BadRequest());
                }
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, ex);
                return(httpResponseMessageHelper.BadRequest());
            }

            loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to get Region {pageRegionValue} for Path {path}");

            var currentRegion = await regionService.GetAsync(path, pageRegionValue);

            if (currentRegion == null)
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Region does not exist for {pageRegionValue} for Path {path}");
                return(httpResponseMessageHelper.NoContent());
            }

            try
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to apply patch to {path} region {pageRegionValue}");
                regionPatch?.ApplyTo(currentRegion);
                var validationResults = currentRegion.Validate(new ValidationContext(currentRegion));

                if (validationResults.Any())
                {
                    loggerHelper.LogInformationMessage(log, correlationId, "Validation Failed");
                    return(httpResponseMessageHelper.UnprocessableEntity(validationResults.ToList()));
                }
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, ex);
                return(httpResponseMessageHelper.BadRequest());
            }

            try
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to update path {path}");
                var patchedRegion = await regionService.ReplaceAsync(currentRegion);

                loggerHelper.LogMethodExit(log);
                return(httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(patchedRegion, "id", nameof(Models.Region.DocumentId))));
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, ex);
                return(httpResponseMessageHelper.UnprocessableEntity(new JsonException(ex.Message, ex)));
            }
        }
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "paths/{path}/regions/{pageRegion}")]
            HttpRequest req,
            ILogger log,
            string path,
            int pageRegion,
            [Inject] ILoggerHelper loggerHelper,
            [Inject] IHttpRequestHelper httpRequestHelper,
            [Inject] IHttpResponseMessageHelper httpResponseMessageHelper,
            [Inject] IJsonHelper jsonHelper,
            [Inject] IRegionService regionService
            )
        {
            loggerHelper.LogMethodEnter(log);

            // validate the parameters are present
            var dssCorrelationId = httpRequestHelper.GetDssCorrelationId(req);

            if (string.IsNullOrEmpty(dssCorrelationId))
            {
                log.LogInformation($"Unable to locate '{nameof(dssCorrelationId)}' in request header");
            }

            if (!Guid.TryParse(dssCorrelationId, out var correlationGuid))
            {
                log.LogInformation($"Unable to parse '{nameof(dssCorrelationId)}' to a Guid");
                correlationGuid = Guid.NewGuid();
            }

            if (string.IsNullOrEmpty(path))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing value in request for '{nameof(path)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            var pathRegex = new Regex(@"^[A-Za-z0-9.,-_]*$");

            if (path.Length > 100 || !pathRegex.IsMatch(path))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Invalid value in request for '{nameof(path)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (pageRegion == 0 || !Enum.IsDefined(typeof(PageRegions), pageRegion))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing/invalid value in request for '{nameof(pageRegion)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            PageRegions pageRegionValue = (PageRegions)pageRegion;

            Models.Region regionRequest;

            try
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, "Attempt to get resource from body of the request");
                regionRequest = await httpRequestHelper.GetResourceFromRequest <Models.Region>(req);

                if (regionRequest == null)
                {
                    loggerHelper.LogInformationMessage(log, correlationGuid, "Missing body in req");
                    return(httpResponseMessageHelper.UnprocessableEntity());
                }
            }
            catch (JsonException ex)
            {
                loggerHelper.LogError(log, correlationGuid, "Unable to retrieve body from req", ex);
                return(httpResponseMessageHelper.UnprocessableEntity(ex));
            }

            if (!regionRequest.DocumentId.HasValue)
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.DocumentId)}' is missing");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (path != regionRequest.Path)
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.Path)}' does not match resource path value");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (string.IsNullOrEmpty(regionRequest.RegionEndpoint))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing value in request for '{nameof(regionRequest.RegionEndpoint)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            const string PlaceMarkerStub = "{0}";
            string       regionEndpoint  = regionRequest.RegionEndpoint;

            if (regionEndpoint.Contains(PlaceMarkerStub))
            {
                // this is allowable, so replace with a valid string to permit the Uri.IsWellFormedUriString to check the resulting string
                regionEndpoint = regionEndpoint.Replace(PlaceMarkerStub, "valid");
            }

            if (!Uri.IsWellFormedUriString(regionEndpoint, UriKind.Absolute))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.RegionEndpoint)}' is not a valid absolute Uri");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (pageRegionValue != regionRequest.PageRegion)
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.PageRegion)}' does not match resource path value");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (!string.IsNullOrEmpty(regionRequest.OfflineHtml))
            {
                var htmlDoc = new HtmlDocument();

                htmlDoc.LoadHtml(regionRequest.OfflineHtml);

                if (htmlDoc.ParseErrors.Any())
                {
                    loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.OfflineHtml)}' contains malformed HTML");
                    return(httpResponseMessageHelper.BadRequest());
                }
            }

            loggerHelper.LogInformationMessage(log, correlationGuid, string.Format("Attempting to update region {0}", regionRequest.DocumentId));
            var replacedRegion = await regionService.ReplaceAsync(regionRequest);

            loggerHelper.LogMethodExit(log);

            return(replacedRegion != null
                ? httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(replacedRegion, "id", nameof(Models.Region.DocumentId)))
                : httpResponseMessageHelper.NoContent());
        }