Exemplo n.º 1
0
        public async Task PostAdviserDetailHttpTrigger_ReturnsStatusCodeUnprocessableEntity_WhenAdviserDetailHasFailedValidation()
        {
            _httpRequestMessageHelper.GetAdviserDetailFromRequest <Models.AdviserDetail>(_request).Returns(Task.FromResult(_address).Result);

            var validationResults = new List <ValidationResult> {
                new ValidationResult("adviser detail Id is Required")
            };

            _validate.ValidateResource(Arg.Any <Models.AdviserDetail>(), true).Returns(validationResults);

            var result = await RunFunction();

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual((HttpStatusCode)422, result.StatusCode);
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "AdviserDetails")] HttpRequestMessage req, ILogger log,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPostAdviserDetailHttpTriggerService adviserDetailsPostService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

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

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

            Models.AdviserDetail adviserDetailRequest;

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

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

            adviserDetailRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(adviserDetailRequest, true);

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

            var adviserDetail = await adviserDetailsPostService.CreateAsync(adviserDetailRequest);

            return(adviserDetail == null
                ? HttpResponseMessageHelper.BadRequest()
                : HttpResponseMessageHelper.Created(JsonHelper.SerializeObject(adviserDetail)));
        }
Exemplo n.º 3
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)));
        }