public async Task Handle_MoveCardBetweenColumnsCommand_ShouldFailValidation_WhenCardNotFoundInFromColumn()
        {
            // Arrange
            var board      = new Board("Personal Board");
            var fromColumn = new Column("To do");

            board.AddColumn(fromColumn);

            var toColumn = new Column("Doing");

            board.AddColumn(toColumn);

            var command = new MoveCardBetweenColumnsCommand(board.Id, fromColumn.Id, toColumn.Id, Guid.NewGuid(), 2);

            _autoMocker.GetMock <IBoardRepository>()
            .Setup(x => x.GetById(command.BoardId)).ReturnsAsync(board);

            // Act
            var validationResult = await _boardCommandHandler.Handle(command, CancellationToken.None);

            // Assert
            _autoMocker.GetMock <IBoardRepository>()
            .Verify(x => x.GetById(It.Is <Guid>(x => x.Equals(command.BoardId))), Times.Once);

            Assert.False(validationResult.IsValid);
            Assert.Single(validationResult.Errors);
            Assert.Equal(UserMessages.NotFound.Format("Card").Message, validationResult.Errors.First().ErrorMessage);
        }
        public async Task Handle_MoveCardBetweenColumnsCommand_ShouldFailValidation_WhenPriorityInColumnLessThanOne()
        {
            // Arrange
            var command = new MoveCardBetweenColumnsCommand(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), 0);

            // Act
            var validationResult = await _boardCommandHandler.Handle(command, CancellationToken.None);

            // Assert
            Assert.False(validationResult.IsValid);
            Assert.Single(validationResult.Errors);
            Assert.Equal(UserMessages.MustBeGreatherThan.Format("CardPriorityInColumn", 0).Message, validationResult.Errors.First().ErrorMessage);
        }
        public async Task Handle_MoveCardBetweenColumnsCommand_ShouldFailValidation_WhenEmptyCardId()
        {
            // Arrange
            var command = new MoveCardBetweenColumnsCommand(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.Empty, 1);

            // Act
            var validationResult = await _boardCommandHandler.Handle(command, CancellationToken.None);

            // Assert
            Assert.False(validationResult.IsValid);
            Assert.Single(validationResult.Errors);
            Assert.Equal(UserMessages.RequiredField.Format("CardId").Message, validationResult.Errors.First().ErrorMessage);
        }
        public async Task Handle_MoveCardBetweenColumnsCommand_ShouldFailValidation_WhenBoardNotFound()
        {
            // Arrange
            var command = new MoveCardBetweenColumnsCommand(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), 1);

            _autoMocker.GetMock <IBoardRepository>()
            .Setup(x => x.GetById(command.BoardId)).ReturnsAsync((Board)null);

            // Act
            var validationResult = await _boardCommandHandler.Handle(command, CancellationToken.None);

            // Assert
            _autoMocker.GetMock <IBoardRepository>()
            .Verify(x => x.GetById(It.Is <Guid>(x => x.Equals(command.BoardId))), Times.Once);

            Assert.False(validationResult.IsValid);
            Assert.Single(validationResult.Errors);
            Assert.Equal(UserMessages.NotFound.Format("Board").Message, validationResult.Errors.First().ErrorMessage);
        }
        public async Task Handle_MoveCardBetweenColumnsCommand_ShouldUpdateAndCommit_WhenValid()
        {
            // Arrange
            var board      = new Board("Personal Board");
            var fromColumn = new Column("To do");

            board.AddColumn(fromColumn);

            var card = new Card("Sleep");

            board.AddCardToColumn(fromColumn.Id, card);

            var toColumn = new Column("Doing");

            board.AddColumn(toColumn);

            var command = new MoveCardBetweenColumnsCommand(board.Id, fromColumn.Id, toColumn.Id, card.Id, 2);

            _autoMocker.GetMock <IBoardRepository>()
            .Setup(x => x.GetById(command.BoardId)).ReturnsAsync(board);

            _autoMocker.GetMock <IBoardRepository>()
            .Setup(x => x.UnitOfWork).Returns(_autoMocker.GetMock <IUnitOfWork>().Object);

            _autoMocker.GetMock <IUnitOfWork>()
            .Setup(x => x.Commit()).Returns(Task.FromResult(true));

            // Act
            var validationResult = await _boardCommandHandler.Handle(command, CancellationToken.None);

            // Assert
            _autoMocker.GetMock <IBoardRepository>()
            .Verify(x => x.GetById(It.Is <Guid>(x => x.Equals(command.BoardId))), Times.Once);

            _autoMocker.GetMock <IUnitOfWork>()
            .Verify(x => x.Commit(), Times.Once);

            Assert.True(validationResult.IsValid);
        }
Exemplo n.º 6
0
        public async Task <ValidationResult> Handle(MoveCardBetweenColumnsCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                return(request.ValidationResult);
            }

            var board = await _boardRepository.GetById(request.BoardId);

            if (board == null)
            {
                AddError(UserMessages.NotFound.Format("Board").Message);
                return(ValidationResult);
            }

            if (!board.ColumnExists(request.FromColumnId))
            {
                AddError(UserMessages.NotFound.Format("FromColumn").Message);
                return(ValidationResult);
            }

            if (!board.ColumnExists(request.ToColumnId))
            {
                AddError(UserMessages.NotFound.Format("ToColumn").Message);
                return(ValidationResult);
            }

            if (!board.CardExistsInColumn(request.FromColumnId, request.CardId))
            {
                AddError(UserMessages.NotFound.Format("Card").Message);
                return(ValidationResult);
            }

            board.MoveCardBetweenColumns(request.CardId, request.FromColumnId, request.ToColumnId, request.CardPriorityInColumn);

            return(await Commit(_boardRepository.UnitOfWork));
        }