public void Handle_CallsApiClient_ReturnException(
            [Frozen] Mock <IRoatpCourseManagementApiClient <RoatpV2ApiConfiguration> > apiClientMock,
            UpdateStandardSubRegionsCommandHandler sut,
            UpdateStandardSubRegionsCommand command,
            CancellationToken cancellationToken,
            HttpRequestContentException expectedException)
        {
            apiClientMock.Setup(a => a.Get <List <ProviderLocationModel> >(It.IsAny <GetAllProviderLocationsQuery>())).Throws(expectedException);

            var actualException = Assert.ThrowsAsync <HttpRequestContentException>(() => sut.Handle(command, cancellationToken));

            actualException.Should().Be(expectedException);
        }
        public void Handle_CallsApiClient_ReturnException(
            [Frozen] Mock <IRoatpCourseManagementApiClient <RoatpV2ApiConfiguration> > apiClientMock,
            UpdateContactDetailsCommandHandler sut,
            UpdateContactDetailsCommand command,
            CancellationToken cancellationToken,
            GetProviderCourseResponse apiResponse,
            HttpRequestContentException expectedException)
        {
            apiClientMock.Setup(a => a.Get <GetProviderCourseResponse>(It.IsAny <GetProviderCourseRequest>())).ReturnsAsync(apiResponse);

            apiClientMock.Setup(c => c.Put(It.IsAny <ProviderCourseUpdateRequest>())).Throws(expectedException);

            var actualException = Assert.ThrowsAsync <HttpRequestContentException>(() => sut.Handle(command, cancellationToken));

            actualException.Should().Be(expectedException);
        }
        public async Task Then_a_bad_request_response_is_returned_if_the_inner_api_call_is_not_successful()
        {
            // Arrange
            var fixture   = new Fixture();
            var mediator  = new Mock <IMediator>();
            var exception = new HttpRequestContentException("API error", HttpStatusCode.BadRequest, "Invalid request");

            mediator.Setup(x => x.Send(It.IsAny <RecalculateEarningsCommand>(), It.IsAny <CancellationToken>())).Throws(exception);

            var controller = new ApprenticeshipIncentiveController(mediator.Object);
            var request    = fixture.Create <RecalculateEarningsRequest>();

            // Act
            var result = await controller.RecalculateEarnings(request) as BadRequestObjectResult;

            // Assert
            result.Should().NotBeNull();
            result.Value.Should().Be(exception.ErrorContent);
        }
        private IActionResult BuildErrorResponse(HttpRequestContentException e)
        {
            SingleMessageResponse response = null;

            if (!string.IsNullOrWhiteSpace(e.ErrorContent))
            {
                response = JsonConvert.DeserializeObject <SingleMessageResponse>(e.ErrorContent);
            }

            switch (e.StatusCode)
            {
            case HttpStatusCode.NotFound:
                return(new NotFoundObjectResult(response));

            case HttpStatusCode.BadRequest:
                return(new BadRequestObjectResult(response));
            }

            return(null);
        }
Пример #5
0
        public void And_Demand_Not_Saved_Then_No_Confirmation_Email(
            RegisterDemandCommand command,
            HttpRequestContentException apiException,
            [Frozen] Mock <IEmployerDemandApiClient <EmployerDemandApiConfiguration> > apiClient,
            [Frozen] Mock <INotificationService> mockNotificationService,
            RegisterDemandCommandHandler handler)
        {
            //Arrange
            apiClient
            .Setup(client => client.PostWithResponseCode <PostCreateCourseDemand>(It.IsAny <PostCreateCourseDemandRequest>()))
            .ThrowsAsync(apiException);

            //Act
            Func <Task> act = async() => await handler.Handle(command, CancellationToken.None);

            //Assert
            act.Should().Throw <HttpRequestContentException>();
            mockNotificationService.Verify(service => service.Send(It.IsAny <SendEmailCommand>()),
                                           Times.Never);
        }
        public void And_Demand_Not_Saved_Then_No_Confirmation_Email(
            string errorContent,
            RegisterDemandCommand command,
            HttpRequestContentException apiException,
            [Frozen] Mock <IEmployerDemandApiClient <EmployerDemandApiConfiguration> > apiClient,
            [Frozen] Mock <INotificationService> mockNotificationService,
            RegisterDemandCommandHandler handler)
        {
            //Arrange
            var apiResponse = new ApiResponse <PostEmployerCourseDemand>(null, HttpStatusCode.BadRequest, errorContent);

            apiClient
            .Setup(client => client.PostWithResponseCode <PostEmployerCourseDemand>(It.IsAny <PostCreateCourseDemandRequest>()))
            .ReturnsAsync(apiResponse);

            //Act
            Func <Task> act = async() => await handler.Handle(command, CancellationToken.None);

            //Assert
            act.Should().Throw <HttpRequestContentException>().WithMessage($"Response status code does not indicate success: {(int)HttpStatusCode.BadRequest} ({HttpStatusCode.BadRequest})")
            .Which.ErrorContent.Should().Be(errorContent);
            mockNotificationService.Verify(service => service.Send(It.IsAny <SendEmailCommand>()),
                                           Times.Never);
        }