public void Should_Validate_Book_Already_Published()
        {
            _publisher = new PublisherBuilder().Build();

            var author = new AuthorBuilder()
                         .WithPublisher(_publisher)
                         .Build();

            _book = new BookBuilder()
                    .WithPublisher(_publisher)
                    .Build();

            var authorBook  = new AuthorBookBuilder().WithAuthor(author).WithBook(_book).Build();
            var authorsBook = new List <AuthorBookEntity> {
                authorBook
            };

            _book.AuthorsBook = authorsBook;

            Action act = () => _bookPublishValidator.ValidatePublish(_book, _publisher);

            act.Should()
            .Throw <BusinessException>()
            .WithMessage($"Book ({_book.Id}) is already published!");
        }
        public void Should_Validate_Publish_With_Excedent_Authors()
        {
            _publisher = new PublisherBuilder().Build();

            var authorBuilder = new AuthorBuilder().WithPublisher(_publisher);
            var author        = authorBuilder.Build();
            var author2       = authorBuilder.Build();
            var author3       = authorBuilder.Build();
            var author4       = authorBuilder.Build();

            _book = new BookBuilder().Build();

            var authorBookBuilder = new AuthorBookBuilder().WithBook(_book);
            var authorBook        = authorBookBuilder.WithAuthor(author).Build();
            var authorBook2       = authorBookBuilder.WithAuthor(author2).Build();
            var authorBook3       = authorBookBuilder.WithAuthor(author3).Build();
            var authorBook4       = authorBookBuilder.WithAuthor(author4).Build();

            var authorsBook = new List <AuthorBookEntity> {
                authorBook, authorBook2, authorBook3, authorBook4
            };

            _book.AuthorsBook = authorsBook;

            Action act = () => _bookPublishValidator.ValidatePublish(_book, _publisher);

            act.Should()
            .Throw <BusinessException>()
            .WithMessage($"Book can't have more than 3 authors!");
        }
        public void Should_Validate_Publisher_Different_From_Authors()
        {
            _publisher = new PublisherBuilder().Build();
            var anotherPublisher = new PublisherBuilder().Build();

            var author = new AuthorBuilder()
                         .WithPublisher(_publisher)
                         .Build();

            _book = new BookBuilder()
                    .Build();

            var authorBook  = new AuthorBookBuilder().WithAuthor(author).WithBook(_book).Build();
            var authorsBook = new List <AuthorBookEntity> {
                authorBook
            };

            _book.AuthorsBook = authorsBook;

            Action act = () => _bookPublishValidator.ValidatePublish(_book, anotherPublisher);

            act.Should()
            .Throw <BusinessException>()
            .WithMessage($"{Environment.NewLine}The publisher of publication's book must be from one of the authors!");
        }
        public void Publish(BookEntity book, PublishierEntity publisher)
        {
            _bookPublishValidator.ValidatePublish(book, publisher);

            book.Publish(publisher);

            _bookRepository.Update(book);
        }
        public BookBuilder WithPublisherId(Guid?publisherId)
        {
            _publisherId = publisherId;

            if (!publisherId.HasValue)
            {
                _publisher = null;
            }

            return(this);
        }
        private void ValidatePublisherFromAnyAuthor(BookEntity book, PublishierEntity publisher)
        {
            var publisherFromAnyAuthor = book.AuthorsBook
                                         .Select(x => x.Author.PublishierId)
                                         .Contains(publisher.Id);

            if (!publisherFromAnyAuthor)
            {
                _erros.Add($"The publisher of publication's book must be from one of the authors!");
            }
        }
        public void ValidatePublish(BookEntity book, PublishierEntity publisher)
        {
            var erros = new List <string>();

            foreach (var strategy in _validationStrategies)
            {
                erros.AddRange(strategy.Validate(book, publisher));
            }

            if (erros.Any())
            {
                throw new BusinessException(string.Join(Environment.NewLine, erros));
            }
        }
Ejemplo n.º 8
0
        public IList <string> Validate(BookEntity book, PublishierEntity publisher)
        {
            var erros = new List <string>();

            if (book == null)
            {
                return(erros);
            }

            if (book.Publishier != null)
            {
                erros.Add($"Book ({book.Id}) is already published!");
            }

            return(erros);
        }
        public IList <string> Validate(BookEntity book, PublishierEntity publisher)
        {
            var erros = new List <string>();

            if (book == null)
            {
                erros.Add("Book must be set to publish a book");
            }

            if (publisher == null)
            {
                erros.Add("Publisher must be set to publish a book");
            }

            return(erros);
        }
        public void Should_Validate_Null_References()
        {
            var              bookPublishValidator = Resolve <IBookPublishValidator>();
            BookEntity       book             = null;
            PublishierEntity publishierEntity = null;
            var              errors           = new List <string>
            {
                "Book must be set to publish a book",
                "Publisher must be set to publish a book"
            };

            Action act = () => bookPublishValidator.ValidatePublish(book, publishierEntity);

            act.Should()
            .Throw <BusinessException>()
            .WithMessage(string.Join(Environment.NewLine, errors));
        }
        public IList <string> Validate(BookEntity book, PublishierEntity publisher)
        {
            if (book == null || publisher == null)
            {
                return(_erros);
            }

            if (book.AuthorsBook.Count > 3)
            {
                _erros.Add($"Book can't have more than 3 authors!");
            }

            ValidateAuthorsWithPublisher(book);

            ValidatePublisherFromAnyAuthor(book, publisher);

            return(_erros);
        }
        public void Should_Validate_Author_Without_Publisher()
        {
            _publisher = new PublisherBuilder().Build();

            var author = new AuthorBuilder()
                         .Build();

            _book = new BookBuilder()
                    .Build();

            var authorBook  = new AuthorBookBuilder().WithAuthor(author).WithBook(_book).Build();
            var authorsBook = new List <AuthorBookEntity> {
                authorBook
            };

            _book.AuthorsBook = authorsBook;

            Action act = () => _bookPublishValidator.ValidatePublish(_book, _publisher);

            act.Should()
            .Throw <BusinessException>()
            .WithMessage($"The following authors do not have publishers on his register:{Environment.NewLine}{author.FirstName}" +
                         $"{Environment.NewLine}The publisher of publication's book must be from one of the authors!");
        }
 public BookBuilder WithPublisher(PublishierEntity publisher)
 {
     _publisher   = publisher;
     _publisherId = publisher?.Id ?? _publisherId;
     return(this);
 }
 public void SetUp()
 {
     _bookPublishValidator = new BookPublishValidator();
     _book      = null;
     _publisher = null;
 }
 public AuthorBuilder WithPublisher(PublishierEntity publishier)
 {
     _publishier = publishier;
     return(this);
 }