private void searchIsbnButton_Click(object sender, EventArgs e) { BookSearchService bookSearchService = new BookSearchService(new GoogleApiIsbnSearchInvoker()); string jsonBookInfo = bookSearchService.GetBookInfo(textBoxIsbn.Text); if (jsonBookInfo == null) { string errorMessage = String.Format("No book found with ISBN '{0}'. Please try another ISBN.", textBoxIsbn.Text); MessageBox.Show(errorMessage); } else { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); IBookInfo bookInfo = jsSerializer.Deserialize <BookInfo>(jsonBookInfo); labelTitle.Text = bookInfo.Title; labelSubTitle.Text = bookInfo.SubTitle; labelDescription.Text = bookInfo.Description; labelAuthors.Text = String.Join(", ", bookInfo.Authors); if (!string.IsNullOrWhiteSpace(bookInfo.Cover)) { pictureBoxCover.LoadAsync(bookInfo.Cover); } } }
private void AddIsbnButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(isbnTextBox.Text)) { string errorMessage = String.Format("Nenašla se knížka. Zkuste znovu zadat ISBN.", isbnTextBox.Text); MessageBox.Show(errorMessage, "Knížka nenalezena", MessageBoxButton.OK, MessageBoxImage.Warning); return; } try { BookSearchService bookSearchService = new BookSearchService(new GoogleApiIsbnSearchInvoker()); string jsonBookInfo = bookSearchService.GetBookInfo(isbnTextBox.Text); if (jsonBookInfo == null) { string errorMessage = String.Format("Nenašla se knížka polde ISBN: '{0}'. Zkuste znovu zadat ISBN.", isbnTextBox.Text); MessageBox.Show(errorMessage, "Knížka nenalezena", MessageBoxButton.OK, MessageBoxImage.Warning); } else { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); IBookInfo bookInfo = jsSerializer.Deserialize <BookInfo>(jsonBookInfo); titleTextBox.Text = bookInfo.Title; authorTextBox.Text = String.Join(", ", bookInfo.Authors); noteTextBox.Text = bookInfo.Description; } } catch { string errorMessage = String.Format("Nelze se připojit k serveru. Zkontrolujte připojení k internetu"); MessageBox.Show(errorMessage, "Knížka nenalezena", MessageBoxButton.OK, MessageBoxImage.Warning); } }
public void ICanCallGoogleApiWithIsbnContainingHyphens() { IsbnSearchInvoker googleApiIsbnSearchInvoker = new GoogleApiIsbnSearchInvoker(); BookSearchService bookSearchService = new BookSearchService(googleApiIsbnSearchInvoker); string jsonBookInfo = bookSearchService.GetBookInfo(PragmaticProgrammerIsbnWithHyphens); JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); IBookInfo bookInfo = jsSerializer.Deserialize <BookInfo>(jsonBookInfo); Assert.AreEqual("The Pragmatic Programmer", bookInfo.Title); Assert.AreEqual(2, bookInfo.Authors.Count()); Assert.AreEqual("Andrew Hunt", bookInfo.Authors.ElementAt(0)); Assert.AreEqual("David Thomas", bookInfo.Authors.ElementAt(1)); }
public void ReturnsNullWhenIsbnIsNotFound() { IBookInfo bookInfo = null; MockFactory mockFactory = new MockFactory(); Mock <IsbnSearchInvoker> isbnSearchInvokerMocked = mockFactory.CreateMock <IsbnSearchInvoker>(); isbnSearchInvokerMocked.Expects.One.Method(invoker => invoker.GetBookInfo(null)) .With(WrongIsbn) .WillReturn(bookInfo); BookSearchService bookSearchService = new BookSearchService(isbnSearchInvokerMocked.MockObject); string jsonBookInfo = bookSearchService.GetBookInfo(WrongIsbn); Assert.IsNull(jsonBookInfo); }
public void ICanCallGoogleApiToSearchForABookWithIsbn() { IsbnSearchInvoker googleApiIsbnSearchInvoker = new GoogleApiIsbnSearchInvoker(); BookSearchService bookSearchService = new BookSearchService(googleApiIsbnSearchInvoker); string jsonBookInfo = bookSearchService.GetBookInfo(ImpactMappingIsbn); JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); IBookInfo bookInfo = jsSerializer.Deserialize <BookInfo>(jsonBookInfo); Assert.AreEqual("Impact Mapping", bookInfo.Title); Assert.AreEqual("Making a Big Impact with Software Products and Projects", bookInfo.SubTitle); Assert.AreEqual(1, bookInfo.Authors.Count()); Assert.AreEqual("Gojko Adzic", bookInfo.Authors.ElementAt(0)); StringAssert.Contains(bookInfo.Description, "A practical guide to impact mapping, a simple yet incredibly effective method for"); Assert.AreEqual("http://books.google.com/books/content?id=6tNoMwEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api", bookInfo.Cover); }
public void ReturnsJsonResponseWithBookInfoWhenIsbnIsGood() { string bookTitle = "Impact mapping"; string bookSubTitle = "Making a big impact with software products and projects"; string bookAuthor = "Gojko Adzik"; string bookDescription = "long description for Impact mapping book"; string bookCover = "http://books.google.com/books/content?id=6tNoMwEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"; IBookInfo impactMappingBookInfo = BookBuilder.GetInstance() .WithTitle(bookTitle) .WithSubTitle(bookSubTitle) .WithAuthors(bookAuthor) .WithDescription(bookDescription) .WithCover(bookCover) .Build(); MockFactory mockFactory = new MockFactory(); Mock <IsbnSearchInvoker> isbnSearchInvokerMocked = mockFactory.CreateMock <IsbnSearchInvoker>(); isbnSearchInvokerMocked.Expects.One.Method(invoker => invoker.GetBookInfo(null)) .With(GoodIsbn) .WillReturn(impactMappingBookInfo); BookSearchService bookSearchService = new BookSearchService(isbnSearchInvokerMocked.MockObject); string jsonBookInfo = bookSearchService.GetBookInfo(GoodIsbn); JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); IBookInfo bookInfo = jsSerializer.Deserialize <BookInfo>(jsonBookInfo); Assert.AreEqual(bookTitle, bookInfo.Title); Assert.AreEqual(bookSubTitle, bookInfo.SubTitle); Assert.AreEqual(1, bookInfo.Authors.Count()); Assert.AreEqual(bookAuthor, bookInfo.Authors.ElementAt(0)); Assert.AreEqual(bookDescription, bookInfo.Description); Assert.AreEqual(bookCover, bookInfo.Cover); }