public async Task CreateBookForAuthorAsync_ThrowException_Test()
        {
            // Arrange
            var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper();

            var authorId           = Guid.Parse("a1da1d8e-1988-4634-b538-a01709477b77");
            var bookForCreationDto = new BookForCreationDto {
                Title = "The Book", Description = "The Description..."
            };
            var bookEntity = mapper.Map <Book>(bookForCreationDto);

            var mockRepo = new Mock <ILibraryRepository>();

            mockRepo.Setup(repo => repo.AuthorExistsAsync(authorId))
            .ReturnsAsync(true);
            mockRepo.Setup(repo => repo.AddBookForAuthorAsync(authorId, bookEntity))
            .Returns(Task.CompletedTask);
            mockRepo.Setup(repo => repo.SaveChangesAsync())
            .ReturnsAsync(false);
            var controller = new BooksController(mockRepo.Object, mapper);

            // Act
            var result = await Assert.ThrowsAsync <Exception>(() => controller.CreateBookForAuthorAsync(authorId, book: bookForCreationDto));

            // Assert
            Assert.Equal($"Creating a book for author {authorId} failed on save.", result.Message);
        }
        public async Task CreateBookForAuthorAsync_Test()
        {
            // Arrange
            var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper();

            var authorId           = Guid.Parse("a1da1d8e-1988-4634-b538-a01709477b77");
            var bookForCreationDto = new BookForCreationDto {
                Title = "The Book", Description = "The Description..."
            };
            var bookEntity = mapper.Map <Book>(bookForCreationDto);

            var mockRepo = new Mock <ILibraryRepository>();

            mockRepo.Setup(repo => repo.AuthorExistsAsync(authorId))
            .ReturnsAsync(true);
            mockRepo.Setup(repo => repo.AddBookForAuthorAsync(authorId, bookEntity))
            .Returns(Task.CompletedTask);
            mockRepo.Setup(repo => repo.SaveChangesAsync())
            .ReturnsAsync(true);
            var controller = new BooksController(mockRepo.Object, mapper);

            // Act
            var result = await controller.CreateBookForAuthorAsync(authorId, book : bookForCreationDto);

            // Assert
            var actionResult = Assert.IsType <ActionResult <BookDto> >(result);

            Assert.IsType <CreatedAtRouteResult>(actionResult.Result);
        }
        public async Task CreateBookForAuthorAsync_ReturnsBadRequest_Test()
        {
            // Arrange
            var authorId   = Guid.NewGuid();
            var mockRepo   = new Mock <ILibraryRepository>();
            var mapper     = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper();
            var controller = new BooksController(mockRepo.Object, mapper);

            // Act
            var result = await controller.CreateBookForAuthorAsync(authorId, book : null);

            // Assert
            var actionResult = Assert.IsType <ActionResult <BookDto> >(result);

            Assert.IsType <BadRequestResult>(actionResult.Result);
        }
        public async Task CreateBookForAuthorAsync_Returns433StatusCode_Test()
        {
            // Arrange
            var authorId = Guid.NewGuid();
            var book     = new BookForCreationDto {
                Title = "The same", Description = "The same"
            };
            var mockRepo   = new Mock <ILibraryRepository>();
            var mapper     = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper();
            var controller = new BooksController(mockRepo.Object, mapper);

            // Act
            var result = await controller.CreateBookForAuthorAsync(authorId, book);

            // Assert
            var actionResult = Assert.IsType <ActionResult <BookDto> >(result);

            Assert.IsType <UnprocessableEntityObjectResult>(actionResult.Result);
        }
        public async Task CreateBookForAuthorAsync_AuthorNotExist_Test()
        {
            // Arrange
            var authorId = Guid.NewGuid();
            var book     = new BookForCreationDto {
                Title = "The title", Description = "The description"
            };
            var mockRepo = new Mock <ILibraryRepository>();

            mockRepo.Setup(repo => repo.AuthorExistsAsync(authorId))
            .ReturnsAsync(false);
            var mapper     = new MapperConfiguration(cfg => cfg.AddProfile(new BookProfile())).CreateMapper();
            var controller = new BooksController(mockRepo.Object, mapper);

            // Act
            var result = await controller.CreateBookForAuthorAsync(authorId, book);

            // Assert
            var actionResult = Assert.IsType <ActionResult <BookDto> >(result);

            Assert.IsType <NotFoundResult>(actionResult.Result);
        }