public async Task WhenPostFromFile_ThenPublishAsync()
        {
            var request = new CopyImageRequest
            {
                FileName = FileName,
                RoutingKey = It.IsAny<string>()
            };

            ExpectExchangeToPublish();

            var sut = CreateController(new HttpRequestMessage());
            await sut.PostAsync(request);

            exchange.Verify(x => x.PublishAsync(request.FileName, It.IsAny<string>(), It.IsAny<string>()));
        }
        public async Task GivenInvalidModelState_WhenPostFromFile_ThenReturnBadRequest()
        {
            var request = new CopyImageRequest
            {
                FileName = FileName,
                RoutingKey = It.IsAny<string>()
            };

            var sut = CreateController(new HttpRequestMessage());
            sut.ModelState.AddModelError("FileName", "Required");

            var result = await sut.PostAsync(request);

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public async Task WhenPublishAsync_AndUnexpectedException_ThenReturnInternalServerError()
        {
            var request = new CopyImageRequest
            {
                FileName = FileName
            };

            exchange.Setup(x => x.PublishAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
                .Throws(new Exception());

            var sut = CreateController(new HttpRequestMessage());

            var result = await sut.PostAsync(request);

            Assert.AreEqual(HttpStatusCode.InternalServerError, result.StatusCode);
        }
        public void WhenANewCopyImageMessageRequestBodyIs(string name, string routingKey)
        {
            var message = new CopyImageRequest
            {
                FileName = name,
                RoutingKey = routingKey == "empty" ? null : routingKey
            };

            var httpClient = new HttpClient();
            var url = string.Format("{0}copyimages", ConfigurationHelper.MftAdapterApiUrl);
            var task = httpClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(message), System.Text.Encoding.UTF8, "application/json"));
            task.Wait();

            var response = task.Result;

            response.EnsureSuccessStatusCode();
        }
Beispiel #5
0
        public async Task<HttpResponseMessage> PostAsync(CopyImageRequest request)
        {
            Log.Information("Create CopyImage from file has been requested : {@createCopyImageRequest}", request);

            try
            {
                if (!ModelState.IsValid)
                {
                    Log.Error("Could not create CopyImage from file: {errors}", ModelState.SerializeForLog());
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }

                await copyImageExchangePublisher.PublishAsync(request.FileName, request.FileName, string.IsNullOrEmpty(request.RoutingKey) ? string.Empty : request.RoutingKey);

                return Request.CreateResponse(HttpStatusCode.OK,
                    new ApiResponse { message = string.Format("The request has completed successfully. A CopyImage has been queued for {0}.", request) });
            }
            catch (Exception ex)
            {
                const string message = "An error occurred while creating the CopyImage.";
                Log.Error(ex, message);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message);
            }
        }
        public async Task WhenPostFromFile_ThenReturnOk()
        {
            var request = new CopyImageRequest
            {
                FileName = FileName,
                RoutingKey = It.IsAny<string>()
            };

            ExpectExchangeToPublish();

            var sut = CreateController(new HttpRequestMessage());
            var result = await sut.PostAsync(request);

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }