Esempio n. 1
0
        public void  Get_ReturnsBooks()
        {
            var bookEntities = new List <BookEntity>
            {
                new BookEntity {
                    Title = "B1", Author = "A1", Id = 0
                },
                new BookEntity {
                    Title = "B2", Author = "A2", Id = 1
                },
                new BookEntity {
                    Title = "B3", Author = "A3", Id = 2
                },
                new BookEntity {
                    Title = "B4", Author = "A4", Id = 3
                }
            };

            _bookRepository.Setup(x => x.Get())
            .Returns(bookEntities);

            var response = _bookController.Get();

            Assert.IsNotNull(response);
            var okResult = response.Result as OkObjectResult;

            Assert.IsNotNull(okResult);
            var books = okResult.Value as IEnumerable <Book>;

            Assert.IsNotNull(books);
            var list = new List <Book>(books);

            Assert.IsTrue(list.Count == 4);
        }
Esempio n. 2
0
        public async Task Get_NullIsbnString_ReturnsBadRequest()
        {
            string isbn = null;

            var actionResult = await bookController.Get(isbn);

            Assert.IsType <BadRequestResult>(actionResult.Result);
        }
Esempio n. 3
0
        public void IfBookCollectionThenReturn200()
        {
            _controller.Post(new Book {
                Count = 2, Title = "Hellow"
            });
            var result = _controller.Get();

            Assert.IsInstanceOf <OkResult>(result);
        }
Esempio n. 4
0
        public void TestGetAll()
        {
            IEnumerable <Book> books = _controller.Get();

            Assert.AreEqual(24, books.Count());

            Book book = _controller.Get(8); //Get by id

            Assert.AreEqual("Moby Dick", book.Title);
        }
Esempio n. 5
0
        public void Test6()
        {
            var gettedBook = new Book()
            {
                Name = "B2", Author = "Aut2", Year = 1928
            };
            List <Book> books = new List <Book>()
            {
                new Book()
                {
                    Name = "A1", Author = "Aut1", Year = 1929
                },
                gettedBook,
                new Book()
                {
                    Name = "B3", Author = "Aut1", Year = 2012
                }
            };

            database = GetDatabase(books);
            var service = new BookController(database, _logger);

            var result = service.Get("B2");

            Assert.AreEqual(200, result.StatusCode);
            Assert.AreEqual(true, BookEqual(gettedBook, (Book)result.Value));
        }
Esempio n. 6
0
        public void Test5()
        {
            List <Book> books = new List <Book>()
            {
                new Book()
                {
                    Name = "A1", Author = "Aut1", Year = 1929
                },
                new Book()
                {
                    Name = "B2", Author = "Aut2", Year = 1928
                },
                new Book()
                {
                    Name = "B3", Author = "Aut1", Year = 2012
                }
            };

            database = GetDatabase(books);
            var service = new BookController(database, _logger);

            var result = service.Get("B400");

            Assert.AreEqual(404, result.StatusCode);
            Assert.AreEqual("No books with this name found", result.Value);
        }
Esempio n. 7
0
        public void Get_Returns_OkObjectResult_When_RequestedBookCanBeFound()
        {
            // Arrange
            var          targetBookId   = Guid.NewGuid();
            const string targetAuthor   = "A.N Author";
            var          mockedIGetBook = new Mock <IGetBook>();

            mockedIGetBook.Setup(x => x.GetBook(targetBookId)).Returns(new Book
            {
                Id     = targetBookId,
                Author = targetAuthor
            });

            var controller = new BookController(mockedIGetBook.Object);

            // Act
            var response = controller.Get(targetBookId);

            // Assert
            Assert.NotNull(response);

            var asOkObjectResult = response as OkObjectResult;

            Assert.NotNull(asOkObjectResult);

            var value = asOkObjectResult.Value;

            Assert.NotNull(value);

            var asBook = value as Book;

            Assert.NotNull(asBook);
            Assert.Equal(targetAuthor, asBook.Author);
            Assert.Equal(targetBookId, asBook.Id);
        }
        public async Task get_book_with_existing_id()
        {
            // Arrange
            var repository   = BookContextMocker.GetInMemoryBookRepository(nameof(get_book_with_existing_id));
            var controller   = new BookController(repository);
            var expectedBook = new Book()
            {
                Id        = 1,
                Title     = "Book_01",
                Isbn      = "1234567890123",
                Published = Convert.ToDateTime("2000/10/25")
            };

            // Act
            var response = await controller.Get(1) as ObjectResult;

            var book = response.Value as Book;

            // Assert
            Assert.Equal(200, response.StatusCode);
            Assert.Equal(expectedBook.Id, book.Id);
            Assert.Equal(expectedBook.Isbn, book.Isbn);
            Assert.Equal(expectedBook.Published, book.Published);
            Assert.Equal(expectedBook.Title, book.Title);
        }
Esempio n. 9
0
        public void Test3()
        {
            List <Book> books = new List <Book>()
            {
                new Book()
                {
                    Name = "A1", Author = "Aut1", Year = 1929
                },
                new Book()
                {
                    Name = "B2", Author = "Aut2", Year = 1928
                },
                new Book()
                {
                    Name = "B3", Author = "Aut1", Year = 2012
                }
            };

            database = GetDatabase(books);
            var service = new BookController(database, _logger);

            var result = service.Get();

            Assert.AreEqual(200, result.StatusCode);
            var list = ((DbSet <Book>)result.Value).ToList();

            Assert.AreEqual(books.Count, list.Count);
            for (int i = 0; i < books.Count; i++)
            {
                Assert.AreEqual(true, BookEqual(books[i], list[i]));
            }
        }
        public void GetBooks_ShouldReturnAllBooks()
        {
            //Arrange
            var books = SetupBooks();

            if (books == null)
            {
                Assert.Fail("Books collection is null");
            }

            //Create Mock
            mock.Setup(m => m.GetBooks()).Returns(books);

            var bookController = new BookController(mock.Object);

            UnitTestHelper.SetupControllerForTests(bookController);

            //Act
            var result = bookController.Get();
            IEnumerable <BookDTO> resultBooksBookDtos;

            result.TryGetContentValue(out resultBooksBookDtos);

            //Assert
            Assert.IsNotNull(resultBooksBookDtos);
            Assert.IsTrue(resultBooksBookDtos.Any());
        }
        public async Task update_existing_book()
        {
            // Arrange
            var  repository = BookContextMocker.GetInMemoryBookRepository(nameof(get_book_with_existing_id));
            var  controller = new BookController(repository);
            Book newBook    = new Book()
            {
                Title     = "New_Book",
                Isbn      = "6622557799110",
                Published = Convert.ToDateTime("2002/11/06")
            };

            // Act
            await controller.Put(3, newBook);

            var response = await controller.Get(3) as ObjectResult;

            var book = response.Value as Book;

            // Assert
            Assert.Equal(200, response.StatusCode);
            Assert.Equal(newBook.Isbn, book.Isbn);
            Assert.Equal(newBook.Published, book.Published);
            Assert.Equal(newBook.Title, book.Title);
        }
Esempio n. 12
0
        public void Ok_WhenCalled_Get_Without_Parameters()
        {
            Ok_WhenCalled_Post_Without_Cover();

            var result = _bookController.Get();

            Assert.True(result is OkObjectResult);

            var okResult = (OkObjectResult)result;

            Assert.True(okResult.Value is List <BookListModel>);

            var resultModel = (List <BookListModel>)okResult.Value;

            Assert.True(resultModel.Count > 0);
        }
        public void GetAll( )
        {
            List <BookModel> books = new List <BookModel>( )
            {
                new BookModel( )
                {
                    Title       = "Title",
                    SubTitle    = "SubTitle",
                    Edition     = "1",
                    Active      = true,
                    Author      = "Author",
                    Publisher   = "Publisher",
                    YearEdition = "YearEdition"
                }
            };

            Mock <IBookServices>             mockBookServices = new Mock <IBookServices>( );
            Mock <ILogger <BookController> > mockLogger       = new Mock <ILogger <BookController> >( );

            mockBookServices
            .Setup((a) => a.GetAll( ))
            .Returns(books);

            BookController bookController = new BookController(mockBookServices.Object, mockLogger.Object);

            var result = bookController.Get( ).Result;

            result.Should( ).BeOfType <OkObjectResult>( );
        }
        public void DeleteBookTest(string id)
        {
            var controller = new BookController(this.GetContextWithData());

            controller.Delete(id);
            Assert.That("null", Is.EqualTo(controller.Get(id)));
        }
        public void GetBookTest(GetTestValue testValue)
        {
            var controller = new BookController(this.GetContextWithData());
            var response   = controller.Get(testValue.Id);

            Assert.That(response, Is.EqualTo(JsonConvert.SerializeObject(testValue.ReferenceBook)));
        }
        public void GetBook_ShouldReturnFirstBook()
        {
            //Book
            var book = SetupBooks().FirstOrDefault(s => s.BookId.Equals(2));

            if (book == null)
            {
                Assert.Fail("Book is null");
            }

            //Create Mock
            mock.Setup(m => m.GetBook(2)).Returns(book);

            //Act
            var authorController = new BookController(mock.Object);

            UnitTestHelper.SetupControllerForTests(authorController);
            var     result = authorController.Get(2); //The Trials of Apollo
            BookDTO responseBookDto;

            result.TryGetContentValue(out responseBookDto);

            //Assert
            Assert.IsNotNull(responseBookDto);
            Assert.AreSame(book.Title, responseBookDto.Title);
        }
Esempio n. 17
0
        public void GetAllBooks_ShouldReturnFilteredBooks()
        {
            // Arrange
            var mockRepo = new Mock <IRepository <Book> >();

            mockRepo.Setup(m => m.SearchFor(It.IsAny <Expression <Func <Book, bool> > >())).Returns(new List <Book>()
            {
                new Book()
                {
                    Title = "title1"
                }, new Book()
                {
                    Title = "title2"
                }
            });

            BookController target = new BookController(mockRepo.Object);

            target.Request = new HttpRequestMessage();
            target.Request.SetConfiguration(new HttpConfiguration());
            target.Request.RequestUri = new Uri("http://localhost/api/Get?title=title2");
            // Act
            IEnumerable <Book> result = target.Get();

            // Assert
            mockRepo.Verify(m => m.SearchFor(It.IsAny <Expression <Func <Book, bool> > >()));
        }
Esempio n. 18
0
        public async void Task_GetBookById_Return_OkResult()
        {
            var controller = new BookController(context);
            var BookId     = 3;
            var data       = await controller.Get(BookId);

            Assert.IsType <OkObjectResult>(data);
        }
Esempio n. 19
0
        public async void Task_GetBookById_Return_NotFound()
        {
            var controller = new BookController(context);
            var BookId     = 6;
            var data       = await controller.Get(BookId);

            Assert.IsType <NotFoundResult>(data);
        }
Esempio n. 20
0
        public void Test1()
        {
            var service = new BookController(null, _logger);

            var result = service.Get();

            Assert.AreEqual(500, result.StatusCode);
            Assert.AreEqual("Problem with database while getting", result.Value);
        }
        public void PostBookTest(PostTestValue testValue)
        {
            var controller   = new BookController(this.GetContextWithData());
            var postResponse = controller.Post(testValue.Body.ToString());
            var expectedBook = new Book(postResponse, (string)testValue.Body["Title"], (string)testValue.Body["Author"], (string)testValue.Body["Publisher"]);
            var getResponse  = controller.Get(postResponse);

            Assert.That(JsonConvert.SerializeObject(expectedBook), Is.EqualTo(getResponse));
        }
Esempio n. 22
0
        public void get_ById_Test()
        {
            IBookRepo      BookData = new BookRepo(db);
            BookController obj      = new BookController(BookData);
            var            data     = obj.Get(2);
            var            okResult = data as OkObjectResult;

            Assert.AreEqual(200, okResult.StatusCode);
        }
Esempio n. 23
0
        public void Test2()
        {
            var service = new BookController(database, _logger);

            var result = service.Get();

            Assert.AreEqual(404, result.StatusCode);
            Assert.AreEqual("No books found", result.Value);
        }
        public void TryGetAllException( )
        {
            Mock <ILogger <BookController> > mockLogger = new Mock <ILogger <BookController> >( );

            BookController bookController = new BookController(It.IsAny <IBookServices>( ), mockLogger.Object);

            var result = bookController.Get( ).Result;

            result.Should( ).BeOfType <StatusCodeResult>( ).Equals(500);
        }
        public async Task Get_NoCondition_ReturnsAvailableBooks()
        {
            var bookServiceMock   = new Mock <IBookService>();
            var mockedBookService = bookServiceMock.Object;

            var bookController = new BookController(mockedBookService);
            var response       = await bookController.Get();

            bookServiceMock.Verify(x => x.GetCurrentBookAsync());
        }
Esempio n. 26
0
        public async Task GetAsync()
        {
            IEnumerable <Book> expect;
            string             search = "Flores";
            var service = new BookController();

            expect = await service.Get(search);

            Assert.NotNull(expect);
        }
        public async void Task_Delete_Book_Return_BadRequest()
        {
            //Arrange
            var controller = new BookController(context);
            int?id         = null;
            //Act
            var data = await controller.Get(id);

            //Assert
            Assert.IsType <BadRequestResult>(data);
        }
Esempio n. 28
0
        public void Get_ExistingBookPassed_ReturnsOkResult()
        {
            // Arange
            _bookService.Setup(book => book.GetById(It.IsAny <int>()));
            // Act
            var result = _controller.Get(4);

            // Assert

            Assert.IsType <OkObjectResult>(result.Result);
        }
Esempio n. 29
0
        public async void Task_Get_All_Books_Return_OkResult()
        {
            //Arrange
            var controller = new BookController(context);

            //Act
            var data = await controller.Get();

            //Assert
            Assert.IsType <OkObjectResult>(data);
        }
        public async Task get_book_with_not_existing_isbn()
        {
            var repository = BookContextMocker.GetInMemoryBooksRepository(nameof(get_book_with_not_existing_isbn));
            var controller = new BookController(repository);

            var response = await controller.Get(1234567) as ObjectResult;

            var book = response.Value as Book;

            Assert.Equal(200, response.StatusCode);
            Assert.Equal(null, book);
        }