public async Task PatchAdviserDetailHttpTrigger_ReturnsStatusCodeNoContent_WhenAdviserDetailDoesNotExist()
        {
            _httpRequestMessageHelper.GetAdviserDetailFromRequest <Models.AdviserDetailPatch>(_request).Returns(Task.FromResult(_addressPatch).Result);

            _patchAdviserDetailHttpTriggerService.GetAdviserDetailByIdAsync(Arg.Any <Guid>()).Returns(Task.FromResult <string>(null).Result);

            // Act
            var result = await RunFunction(ValidAdviserDetailId);

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.NoContent, result.StatusCode);
        }
Ejemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "AdviserDetails/{adviserDetailId}")] HttpRequestMessage req, ILogger log, string adviserDetailId,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchAdviserDetailHttpTriggerService adviserDetailsPatchService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'APIM-TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("Patch Adviser Detail C# HTTP trigger function processed a request. " + touchpointId);

            if (!Guid.TryParse(adviserDetailId, out var adviserDetailGuid))
            {
                return(HttpResponseMessageHelper.BadRequest(adviserDetailGuid));
            }

            Models.AdviserDetailPatch adviserDetailPatchRequest;

            try
            {
                adviserDetailPatchRequest = await httpRequestMessageHelper.GetAdviserDetailFromRequest <Models.AdviserDetailPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

            if (adviserDetailPatchRequest == null)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(req));
            }

            adviserDetailPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(adviserDetailPatchRequest, false);

            if (errors != null && errors.Any())
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(errors));
            }

            var adviserDetail = await adviserDetailsPatchService.GetAdviserDetailByIdAsync(adviserDetailGuid);

            if (adviserDetail == null)
            {
                return(HttpResponseMessageHelper.NoContent(adviserDetailGuid));
            }

            var patchedAdviserDetail = adviserDetailsPatchService.PatchResource(adviserDetail, adviserDetailPatchRequest);

            if (patchedAdviserDetail == null)
            {
                return(HttpResponseMessageHelper.NoContent(adviserDetailGuid));
            }

            var updatedAdviserDetail = await adviserDetailsPatchService.UpdateCosmosAsync(patchedAdviserDetail, adviserDetailGuid);

            return(updatedAdviserDetail == null
                ? HttpResponseMessageHelper.BadRequest(adviserDetailGuid)
                : HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedAdviserDetail)));
        }