public void Then_If_Error_For_Verify_An_Exception_Is_Thrown( string errorContent, StopEmployerDemandCommand command, GetEmployerDemandResponse getDemandResponse, [Frozen] Mock <IEmployerDemandApiClient <EmployerDemandApiConfiguration> > mockApiClient, [Frozen] Mock <INotificationService> mockNotificationService, StopEmployerDemandCommandHandler handler) { //Arrange getDemandResponse.Stopped = false; var stopResponse = new ApiResponse <string>(null, HttpStatusCode.InternalServerError, errorContent); mockApiClient .Setup(client => client.PatchWithResponseCode( It.Is <PatchCourseDemandRequest>(request => request.PatchUrl.Contains(command.EmployerDemandId.ToString())))) .ReturnsAsync(stopResponse); mockApiClient .Setup(client => client.Get <GetEmployerDemandResponse>( It.Is <GetEmployerDemandRequest>(request => request.GetUrl.Contains($"demand/{command.EmployerDemandId}")))) .ReturnsAsync(getDemandResponse); //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.InternalServerError} ({HttpStatusCode.InternalServerError})") .Which.ErrorContent.Should().Be(errorContent); mockNotificationService.Verify(service => service.Send(It.IsAny <SendEmailCommand>()), Times.Never); }
public async Task Then_The_Api_Is_Called_And_Email_Sent_If_ResponseCode_Is_Ok_And_Audit_Created( StopEmployerDemandCommand command, GetEmployerDemandResponse getDemandResponse, GetEmployerDemandResponse stopResponseBody, [Frozen] Mock <IEmployerDemandApiClient <EmployerDemandApiConfiguration> > mockApiClient, [Frozen] Mock <INotificationService> mockNotificationService, StopEmployerDemandCommandHandler handler) { //Arrange getDemandResponse.Stopped = false; var stopResponse = new ApiResponse <string>(JsonConvert.SerializeObject(stopResponseBody), HttpStatusCode.OK, ""); mockApiClient .Setup(client => client.PatchWithResponseCode( It.Is <PatchCourseDemandRequest>(c => c.PatchUrl.Contains(command.EmployerDemandId.ToString()) && c.Data.FirstOrDefault().Path.Equals("Stopped") && c.Data.FirstOrDefault().Value.Equals(true) ))) .ReturnsAsync(stopResponse); mockApiClient .Setup(client => client.Get <GetEmployerDemandResponse>( It.Is <GetEmployerDemandRequest>(request => request.GetUrl.Contains($"demand/{command.EmployerDemandId}")))) .ReturnsAsync(getDemandResponse); SendEmailCommand actualEmail = null; mockNotificationService .Setup(service => service.Send(It.IsAny <SendEmailCommand>())) .Callback((SendEmailCommand args) => actualEmail = args) .Returns(Task.CompletedTask); var expectedEmail = new StopSharingEmployerDemandEmail( stopResponseBody.ContactEmailAddress, stopResponseBody.OrganisationName, stopResponseBody.Course.Title, stopResponseBody.Course.Level, stopResponseBody.Location.Name, stopResponseBody.NumberOfApprentices, stopResponseBody.StartSharingUrl); //Act var actual = await handler.Handle(command, CancellationToken.None); //Assert actual.EmployerDemand.Should().BeEquivalentTo(stopResponseBody); actualEmail.Tokens.Should().BeEquivalentTo(expectedEmail.Tokens); actualEmail.RecipientsAddress.Should().BeEquivalentTo(expectedEmail.RecipientAddress); actualEmail.TemplateId.Should().BeEquivalentTo(expectedEmail.TemplateId); mockApiClient.Verify( x => x.PostWithResponseCode <object>(It.Is <PostEmployerDemandNotificationAuditRequest>(c => c.PostUrl.Contains($"{command.EmployerDemandId}/notification-audit/{command.Id}?notificationType={(short)NotificationType.StoppedByUser}"))), Times.Once); }
public async Task And_Demand_Not_Found_Then_Not_Send_Email( StopEmployerDemandCommand command, [Frozen] Mock <IEmployerDemandApiClient <EmployerDemandApiConfiguration> > mockApiClient, [Frozen] Mock <INotificationService> mockNotificationService, StopEmployerDemandCommandHandler handler) { //Arrange mockApiClient.Setup(x => x.Get <GetEmployerDemandResponse>( It.Is <GetEmployerDemandRequest>(c => c.GetUrl.Contains($"demand/{command.EmployerDemandId}")))) .ReturnsAsync((GetEmployerDemandResponse)null); //Act var actual = await handler.Handle(command, CancellationToken.None); //Assert mockApiClient.Verify(client => client.PatchWithResponseCode(It.IsAny <PatchCourseDemandRequest>()), Times.Never); mockNotificationService.Verify(service => service.Send(It.IsAny <SendEmailCommand>()), Times.Never); actual.EmployerDemand.Should().BeNull(); }