public async Task HandleShouldReturnFailWhenNotFound() { // Arrange var id = Guid.NewGuid(); var version = 1; var userRepositoryMock = new Mock <IUserRepository>(); userRepositoryMock.Setup(x => x.Delete(id, version)).Throws <EntityNotFoundDbException>(); var userRepository = userRepositoryMock.Object; var publishIntegrationEventsServiceMock = new Mock <IPublishIntegrationEventsService>(); publishIntegrationEventsServiceMock.Setup(x => x.PublishUserCreated(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.CompletedTask); var publishIntegrationEventsService = publishIntegrationEventsServiceMock.Object; var command = new DeleteCommand(id, version); var handler = new DeleteCommandHandler(userRepository, publishIntegrationEventsService); // Act var result = await handler.Handle(command, CancellationToken.None); // Assert result.IsFailure.Should().BeTrue(); result.Failures.Should().OnlyContain(x => x.Code == HandlerFaultCode.NotFound.Name && x.Message == HandlerFailures.NotFound && x.Target == "id"); }
public static object HandleDataPacket(DataPacket dataPacket) { CommandHandler commandHandler = null; if (dataPacket.Command is SearchCommand) { commandHandler = new SearchCommandHandler(dataPacket); } if (dataPacket.Command is SaveCommand) { commandHandler = new SaveCommandHandler(dataPacket); } if (dataPacket.Command is DeleteCommand) { commandHandler = new DeleteCommandHandler(dataPacket); } if (dataPacket.Command is UpdateCommand) { commandHandler = new UpdateCommandHandler(dataPacket); } return(commandHandler?.HandleCommand()); }
public async Task HandleShouldReturnOk() { // Arrange var id = Guid.NewGuid(); var version = 1; var userRepositoryMock = new Mock <IUserRepository>(); userRepositoryMock.Setup(x => x.Delete(id, version)).Returns(Task.CompletedTask); var userRepository = userRepositoryMock.Object; var publishIntegrationEventsServiceMock = new Mock <IPublishIntegrationEventsService>(); publishIntegrationEventsServiceMock.Setup(x => x.PublishUserCreated(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.CompletedTask); var publishIntegrationEventsService = publishIntegrationEventsServiceMock.Object; var command = new DeleteCommand(id, version); var handler = new DeleteCommandHandler(userRepository, publishIntegrationEventsService); // Act var result = await handler.Handle(command, CancellationToken.None); // Assert result.IsFailure.Should().BeFalse(); result.Should().BeOfType(typeof(Result)); }
public async Task HandleShouldReturnFailWhenDatabaseSpecificErrorOccurs() { var id = Guid.NewGuid(); var version = 1; var userRepositoryMock = new Mock <IUserRepository>(); userRepositoryMock.Setup(x => x.Delete(id, version)).Throws <SomeDatabaseSpecificException>(); var userRepository = userRepositoryMock.Object; var publishIntegrationEventsServiceMock = new Mock <IPublishIntegrationEventsService>(); publishIntegrationEventsServiceMock.Setup(x => x.PublishUserCreated(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(Task.CompletedTask); var publishIntegrationEventsService = publishIntegrationEventsServiceMock.Object; var command = new DeleteCommand(id, version); var handler = new DeleteCommandHandler(userRepository, publishIntegrationEventsService); // Act var result = await handler.Handle(command, CancellationToken.None); // Assert result.IsFailure.Should().BeTrue(); result.Failures.Should().OnlyContain(x => x.Message == CustomFailures.DeleteUserFailure); }
public async void ShouldCreateNewTaskWhenValid() { // Arrange var validator = new Mock <IDeleteTodoTaskValidator>(); var repository = new Mock <ITodoTaskEntityRepository>(); validator.Setup(m => m.ValidateAndThrow(It.IsAny <TodoTaskEntity>())); var task = new TodoTaskEntity(); repository.Setup(m => m.GetById(It.IsAny <string>())).ReturnsAsync(task); repository.Setup(m => m.DeleteAsync(It.IsAny <TodoTaskEntity>())); var handler = new DeleteCommandHandler( repository.Object, validator.Object); // Act await handler.HandleAsync("afab15bc-d16b-49b8-a072-6bc1bc0d5156"); // Assert validator.Verify(m => m.ValidateAndThrow(It.IsAny <TodoTaskEntity>()), Times.Once); repository.Verify( m => m.GetById(It.Is <string>(p => p == "afab15bc-d16b-49b8-a072-6bc1bc0d5156")), Times.Once); repository.Verify(m => m.DeleteAsync(It.Is <TodoTaskEntity>(p => p == task)), Times.Once); }
public void When_InputIsNull_MapThrowsException() { DeleteCommand deleteCommand = null; _target = _fixture.Freeze <DeleteCommandHandler>(); Assert.ThrowsAsync <ArgumentNullException>(async() => await _target.Handle(deleteCommand, default(CancellationToken))); }
public void Should_Throw_When_CommandIsNull() { var jobScheduler = Substitute.For <IJobScheduler>(); var settingsStore = Substitute.For <ISettingsStore>(); var dialogService = Substitute.For <IDialogService>(); var handler = new DeleteCommandHandler(jobScheduler, settingsStore, dialogService); handler.Handle(null); }
public void When_ServiceFails_HandlerThrowsException() { var command = _fixture.Create <DeleteCommand>(); _mockContactsService = _fixture.Freeze <Mock <IContactService> >(); _mockContactsService.Setup(x => x.DeleteContactAsync(It.IsAny <Contact>())).ThrowsAsync(new ArgumentNullException()); _target = _fixture.Create <DeleteCommandHandler>(); Assert.ThrowsAsync <ArgumentNullException>(async() => await _target.Handle(command, default(CancellationToken))); _mockContactsService.Verify(x => x.DeleteContactAsync(It.IsAny <Contact>()), Times.Once); }
public void When_InputIsValid_HandlerDeleteContactLogSuccessfully() { var expected = _fixture.Create <bool>(); var command = _fixture.Create <DeleteCommand>(); _mockContactsService = _fixture.Freeze <Mock <IContactService> >(); _mockContactsService.Setup(x => x.DeleteContactAsync(It.IsAny <Contact>())).ReturnsAsync(expected); _target = _fixture.Create <DeleteCommandHandler>(); var actual = _target.Handle(command, default(CancellationToken)); Assert.AreEqual(expected, actual.Result); _mockContactsService.Verify(x => x.DeleteContactAsync(It.IsAny <Contact>()), Times.Once); }
public void Handle_WithDeleteCommand_CallsDeleteAPI() { const string testFilePath = "TestFilePath"; // Arrange var mockClientProtocol = new Mock <IRestClientProtocol>(); var sut = new DeleteCommandHandler(mockClientProtocol.Object); var stubDeleteCommand = new DeleteCommand() { FilePath = testFilePath }; // Act sut.Handle(stubDeleteCommand); // Assert mockClientProtocol.Verify(x => x.Delete(testFilePath)); }
public void Should_RemoveItemFromParentAndJobScheduler() { var jobScheduler = Substitute.For <IJobScheduler>(); var settingsStore = Substitute.For <ISettingsStore>(); var dialogService = Substitute.For <IDialogService>(); var item = Substitute.For <Dummy>(); dialogService.Show(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <MessageBoxButton>(), Arg.Any <MessageBoxImage>()) .Returns(MessageBoxResult.Yes); var command = new DeleteCommand(item); var handler = new DeleteCommandHandler(jobScheduler, settingsStore, dialogService); handler.Handle(command); item.Received().Delete(); jobScheduler.Received().Remove(item); }
public void TearDown() { _mockContactsService = null; _fixture = null; _target = null; }
async Task act(DeleteCalendarItemCycleCommand command) { await DeleteCommandHandler.HandleAsync(command); }