public async Task ReferenceController_Send_NullReferenceTest()
        {
            var request = new SendReferenceRequest
            {
                Delivery = new Delivery
                {
                    Method = Constants.DeliveryMethodBO
                }
            };

            var _controller = new ReferenceController(new Repository(), new PrintServerBridge(), new EventHub())
            {
                Configuration = Helper.CreateHttpConfiguration(),
                Request       = new HttpRequestMessage()
            };

            var response = await(await _controller.Send("0", request)).ExecuteAsync(new CancellationToken());

            Assert.AreEqual(response.StatusCode, HttpStatusCode.NotFound);

            AdditionalInformation result = null;

            response.TryGetContentValue(out result);

            Assert.IsNotNull(result);
            Assert.NotNull(result.Errors);
            Assert.IsTrue(result.Errors.Where(e => e.Code == "150").Any());
        }
        public async Task ReferenceController_Send_BadEHTest()
        {
            var request = new SendReferenceRequest
            {
                Delivery = new Delivery
                {
                    Method = Constants.DeliveryMethodEmail,
                    Email  = "TEST"
                }
            };

            var repository = new Repository();

            repository.References.Add("0", new Reference {
                Number = "0", Status = Constants.ReferenceStatusPdfOk
            });

            var _controller = new ReferenceController(repository, new PrintServerBridge(), new BadEventHub())
            {
                Configuration = Helper.CreateHttpConfiguration(),
                Request       = new HttpRequestMessage()
            };

            var response = await(await _controller.Send("0", request)).ExecuteAsync(new CancellationToken());

            Assert.AreEqual(response.StatusCode, HttpStatusCode.InternalServerError);

            AdditionalInformation result = null;

            response.TryGetContentValue(out result);

            Assert.IsNotNull(result);
            Assert.NotNull(result.Errors);
            Assert.IsTrue(result.Errors.Where(e => e.Code == "101").Any());
        }
        public async Task ReferenceController_SendTest()
        {
            var request = new SendReferenceRequest
            {
                Delivery = new Delivery
                {
                    Method = Constants.DeliveryMethodBO
                }
            };

            var repository = new Repository();

            repository.References.Add("0", new Reference {
                Number = "0", Status = Constants.ReferenceStatusPdfOk
            });

            var _controller = new ReferenceController(repository, new PrintServerBridge(), new EventHub())
            {
                Configuration = Helper.CreateHttpConfiguration(),
                Request       = new HttpRequestMessage()
            };

            var response = await(await _controller.Send("0", request)).ExecuteAsync(new CancellationToken());

            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
            Assert.AreEqual(repository.References["0"].Status, Constants.ReferenceStatusDelivered);
        }
        public async Task <IHttpActionResult> Send(string refNumber, [FromBody] SendReferenceRequest request)
        {
            if (string.IsNullOrEmpty(refNumber))
            {
                return(NotFound());
            }

            if (request == null)
            {
                return(Error(HttpStatusCode.BadRequest, new[]
                {
                    new Error
                    {
                        Code = "1000",
                        InternalMessage = Resources.Strings.ErrorRequestRequired
                    }
                }));
            }

            var errors = Validate(request, SendReferenceRequestValidator);

            if (errors != null)
            {
                return(errors);
            }

            var cancellationToken = new CancellationToken();

            var reference = await _repository.Get(refNumber, cancellationToken);

            if (reference == null)
            {
                return(Error(HttpStatusCode.NotFound, new[]
                {
                    new Error
                    {
                        Code = "150",
                        InternalMessage = Resources.Strings.ReferenceServiceError150,
                    }
                }));
            }

            if (reference.Status != Constants.ReferenceStatusPdfOk)
            {
                return(Error((HttpStatusCode)422, new[]
                {
                    new Error
                    {
                        Code = "151",
                        InternalMessage = Resources.Strings.ReferenceServiceError151,
                    }
                }, "Unprocessable Entity"));
            }

            reference.Status         = Constants.ReferenceStatusSended;
            reference.DeliveryMethod = request.Delivery.Method;
            reference.DeliveryEmail  = request.Delivery.Email;

            if (request.Delivery.Method == Constants.DeliveryMethodEmail)
            {
                try
                {
                    await _eventHub.Send(reference, Request.GetCorrelationId().ToString("N"), cancellationToken);
                }
                catch (ReferenceServiceException ex)
                {
                    return(Error(HttpStatusCode.InternalServerError, ex.ToErrors()));
                }
            }

            reference.Status = Constants.ReferenceStatusDelivered;
            await _repository.Update(reference, cancellationToken);

            return(Ok());
        }