Beispiel #1
0
        public async Task PatchOutcomesHttpTrigger_ReturnsStatusCodeBadRequest_WhenUnableToUpdateOutcomesRecord()
        {
            _httpRequestMessageHelper.GetOutcomesFromRequest <OutcomesPatch>(_request).Returns(Task.FromResult(_outcomePatch).Result);

            _resourceHelper.DoesCustomerExist(Arg.Any <Guid>()).ReturnsForAnyArgs(true);
            _resourceHelper.DoesInteractionResourceExistAndBelongToCustomer(Arg.Any <Guid>(), Arg.Any <Guid>()).Returns(true);
            _resourceHelper.DoesActionPlanResourceExistAndBelongToCustomer(Arg.Any <Guid>(), Arg.Any <Guid>(), Arg.Any <Guid>()).Returns(true);

            _patchOutcomesHttpTriggerService.GetOutcomesForCustomerAsync(Arg.Any <Guid>(), Arg.Any <Guid>(), Arg.Any <Guid>(), Arg.Any <Guid>()).Returns(Task.FromResult(_outcome.ToString()).Result);

            _patchOutcomesHttpTriggerService.UpdateCosmosAsync(Arg.Any <string>(), Arg.Any <Guid>()).Returns(Task.FromResult <Models.Outcomes>(null).Result);

            var result = await RunFunction(ValidCustomerId, ValidInteractionId, ValidActionPlanId, ValidOutcomeId);

            // Assert
            Assert.IsInstanceOf <HttpResponseMessage>(result);
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Interactions/{interactionId}/actionplans/{actionplanId}/Outcomes/{OutcomeId}")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string actionplanId, string OutcomeId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchOutcomesHttpTriggerService outcomesPatchService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

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

            var ApimURL = httpRequestMessageHelper.GetApimURL(req);

            if (string.IsNullOrEmpty(ApimURL))
            {
                log.LogInformation("Unable to locate 'apimurl' in request header");
                return(HttpResponseMessageHelper.BadRequest());
            }

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

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

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

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

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

            Models.OutcomesPatch outcomesPatchRequest;

            try
            {
                outcomesPatchRequest = await httpRequestMessageHelper.GetOutcomesFromRequest <Models.OutcomesPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            outcomesPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(outcomesPatchRequest);

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

            var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid);

            if (!doesCustomerExist)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            var isCustomerReadOnly = await resourceHelper.IsCustomerReadOnly(customerGuid);

            if (isCustomerReadOnly)
            {
                return(HttpResponseMessageHelper.Forbidden(customerGuid));
            }

            var doesInteractionExist = resourceHelper.DoesInteractionResourceExistAndBelongToCustomer(interactionGuid, customerGuid);

            if (!doesInteractionExist)
            {
                return(HttpResponseMessageHelper.NoContent(interactionGuid));
            }

            var doesActionPlanExist = resourceHelper.DoesActionPlanResourceExistAndBelongToCustomer(actionplanGuid, interactionGuid, customerGuid);

            if (!doesActionPlanExist)
            {
                return(HttpResponseMessageHelper.NoContent(actionplanGuid));
            }

            var outcomeForCustomer = await outcomesPatchService.GetOutcomesForCustomerAsync(customerGuid, interactionGuid, actionplanGuid, outcomesGuid);

            if (outcomeForCustomer == null)
            {
                return(HttpResponseMessageHelper.NoContent(outcomesGuid));
            }

            var patchedOutcome = outcomesPatchService.PatchResource(outcomeForCustomer, outcomesPatchRequest);

            if (patchedOutcome == null)
            {
                return(HttpResponseMessageHelper.NoContent(outcomesGuid));
            }

            var updatedOutcomes = await outcomesPatchService.UpdateCosmosAsync(patchedOutcome, outcomesGuid);

            if (updatedOutcomes != null)
            {
                await outcomesPatchService.SendToServiceBusQueueAsync(updatedOutcomes, customerGuid, ApimURL);
            }

            return(updatedOutcomes == null?
                   HttpResponseMessageHelper.BadRequest(outcomesGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedOutcomes)));
        }