Inheritance: Microsoft.AspNetCore.Mvc.Controller
        public async Task PostReturnsBadRequest_WhenProviderIdIsNull()
        {
            var sut = new RequestApiController(Mock.Of<IMediator>());
            var result = await sut.Post(new RequestViewModel {ProviderId = null});

            Assert.IsType<BadRequestObjectResult>(result);
        }
        public async Task PostSendsAddRequestCommandAsyncWithCorrectData()
        {
            var mediator = new Mock<IMediator>();
            var sut = new RequestApiController(mediator.Object);
            var ourViewModel = new RequestViewModel
            {
                ProviderId = "Crazy-Eights",
                ProviderData = "Go-Fish",
                Address = "Address",
                City = "Citty",
                State = "Sttate",
                Zip = "ZZiipp",
                Phone = "Fone",
                Name = "ONAMEO",
                Email = "ANEMAILADDRESS",
                Status = RequestStatus.Assigned.ToString()
            };

            await sut.Post(ourViewModel);

            mediator.Verify(x => x.SendAsync(It.Is<AddRequestCommandAsync>(y => y.Request.ProviderId == ourViewModel.ProviderId
            && y.Request.Status == RequestStatus.Assigned
            && y.Request.ProviderData == ourViewModel.ProviderData
            && y.Request.State == ourViewModel.State
            && y.Request.Address == ourViewModel.Address
            && y.Request.City == ourViewModel.City
            && y.Request.Phone == ourViewModel.Phone
            && y.Request.Zip == ourViewModel.Zip
            && y.Request.Name == ourViewModel.Name
            && y.Request.Email == ourViewModel.Email)), Times.Once);
        }
        public async Task PostReturnsBadRequest_WhenStatusIsNotNew()
        {
            var sut = new RequestApiController(Mock.Of<IMediator>());
            var result = await sut.Post(new RequestApiViewModel { Status = "NotNew" });

            Assert.IsType<BadRequestResult>(result);
        }
        public async Task PostReturnsBadRequest_WhenRequestIdIsNotAGuid()
        {
            var sut = new RequestApiController(Mock.Of<IMediator>());
            var result = await sut.Post(new RequestViewModel {RequestId = "NotAGuid"});

            Assert.IsType<BadRequestObjectResult>(result);
        }
        public async Task PostReturnsBadRequestResultWhenAnErrorIsNotInternal()
        {
            var mediator = new Mock<IMediator>();
            var outReason = "anythingReally";
            var pid = "someIDValue";

            var error = new AddRequestError
            {
                ProviderId = pid,
                Reason = outReason
            };

            mediator.Setup(x => x.SendAsync(It.IsAny<AddRequestCommandAsync>())).ReturnsAsync(error);

            var sut = new RequestApiController(mediator.Object);
            var result = await sut.Post(new RequestViewModel()) as BadRequestObjectResult;

            Assert.NotNull(result);

            var resultObject = result.Value as AddRequestError;

            Assert.NotNull(resultObject);
            Assert.Equal(outReason, resultObject.Reason);
            Assert.Equal(pid, resultObject.ProviderId);
        }
        public async Task PostReturnsBadRequest_WhenIncomingStatusCannotBeMappedToRequestStatus()
        {
            var sut = new RequestApiController(Mock.Of<IMediator>());
            var result = await sut.Post(new RequestViewModel {Status = "NotMappable"});

            Assert.IsType<BadRequestObjectResult>(result);
        }
        public async Task PostReturns202StatusCode()
        {
            var sut = new RequestApiController(Mock.Of<IMediator>());
            var result = await sut.Post(new RequestApiViewModel { Status = "new" }) as StatusCodeResult;

            Assert.IsType<StatusCodeResult>(result);
            Assert.Equal(result.StatusCode, 202);
        }
        public async Task PostReturnsBadRequest_WhenModelStateIsInvalid()
        {
            var sut = new RequestApiController(Mock.Of<IMediator>());
            sut.AddModelStateError();
            var result = await sut.Post(new RequestApiViewModel());

            Assert.IsType<BadRequestResult>(result);
        }
        public async Task PostReturnsHttpStatusCodeResultOf201()
        {
            var mediator = new Mock<IMediator>();
            var sut = new RequestApiController(mediator.Object);

            var result = await sut.Post(new RequestViewModel()) as CreatedResult;

            Assert.NotNull(result);
            Assert.Equal(result.StatusCode, 201);
        }
        public async Task PostSendsAddApiRequestCommandWithCorrectViewModel()
        {
            var viewModel = new RequestApiViewModel { Status = "new" };
            var mediator = new Mock<IMediator>();

            var sut = new RequestApiController(mediator.Object);
            await sut.Post(viewModel);

            mediator.Verify(x => x.SendAsync(It.Is<AddApiRequestCommand>(y => y.ViewModel == viewModel)), Times.Once);
        }
        public async Task PostSendsRequestExistsByProviderIdQueryWithCorrectProviderRequestId()
        {
            const string providerRequestId = "ProviderRequestId";

            var mediator = new Mock<IMediator>();
            var sut = new RequestApiController(mediator.Object);
            await sut.Post(new RequestApiViewModel { Status = "new", ProviderRequestId = providerRequestId });

            mediator.Verify(x => x.Send(It.Is<RequestExistsByProviderIdQuery>(y => y.ProviderRequestId == providerRequestId)), Times.Once);
        }
        public async Task PostReturnsCreatedResult()
        {
            var mediator = new Mock<IMediator>();
            var viewModel = new RequestViewModel {ProviderId = "ProviderId", RequestId = Guid.NewGuid().ToString()};

            var sut = new RequestApiController(mediator.Object);
            var result = await sut.Post(viewModel);

            Assert.IsType<CreatedResult>(result);
        }
        public async Task PostSendsAddRequestCommandWithTheCorrectParameters()
        {
            var mediator = new Mock<IMediator>();
            var viewModel = new RequestViewModel {ProviderId = "ProviderId", RequestId = Guid.NewGuid().ToString()};

            var sut = new RequestApiController(mediator.Object);
            await sut.Post(viewModel);

            mediator.Verify(x => x.SendAsync(It.Is<AddApiRequestCommand>((y => y.RequestViewModel == viewModel))));
        }
        public async Task PostReturnsBadRequest_WhenRequestAlreadyExists()
        {
            const string providerRequestId = "ProviderRequestId";

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<RequestExistsByProviderIdQuery>())).Returns(true);

            var sut = new RequestApiController(mediator.Object);
            var result = await sut.Post(new RequestApiViewModel { Status = "new", ProviderRequestId = providerRequestId });

            Assert.IsType<BadRequestResult>(result);
        }
        public async Task PostEnqueuesProcessApiRequestsJobWithCorrectViewModel()
        {
            var viewModel = new RequestApiViewModel { Status = "new" };
            var mediator = new Mock<IMediator>();
            var backgroundJobClient = new Mock<IBackgroundJobClient>();

            var sut = new RequestApiController(mediator.Object, backgroundJobClient.Object);
            await sut.Post(viewModel);

            backgroundJobClient.Verify(x => x.Create(It.Is<Job>(job => 
                job.Method.Name == nameof(ProcessApiRequests.Process) && 
                job.Args[0] == viewModel), It.IsAny<EnqueuedState>()), Times.Once);
        }
 public void ControllerHasProducesAtttributeWithTheCorrectContentType()
 {
     var sut = new RequestApiController(null);
     var attribute = sut.GetAttributes().OfType<ProducesAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
     Assert.Equal(attribute.ContentTypes.Select(x => x).First(), "application/json");
 }
 public void PostHasHttpPostAttribute()
 {
     var sut = new RequestApiController(null);
     var attribute = sut.GetAttributesOn(x => x.Post(It.IsAny<RequestViewModel>())).OfType<HttpPostAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
 }
 public void ControllerHasRouteAtttributeWithTheCorrectRoute()
 {
     var sut = new RequestApiController(null);
     var attribute = sut.GetAttributes().OfType<RouteAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Template, "api/request");
 }