public MovieInfo GetMovieInfo(string title) { string query = baseUrl + "t=" + title; MovieInfo result = new MovieInfo(); XDocument xDoc = XDocument.Load(query); string response = xDoc.Descendants("root").Attributes("response").First().Value; if (response == "True") { var moviesQuery = from root in xDoc.Root.Descendants("movie") select new { Title = root.Attribute("title").Value, Year = root.Attribute("year").Value, Rated = root.Attribute("rated").Value, Released = root.Attribute("released").Value, Runtime = root.Attribute("runtime").Value, Genre = root.Attribute("genre").Value, Director = root.Attribute("director").Value, Writer = root.Attribute("writer").Value, Actors = root.Attribute("actors").Value, Plot = root.Attribute("plot").Value, Language = root.Attribute("language").Value, Country = root.Attribute("country").Value, Awards = root.Attribute("awards").Value, Rating = root.Attribute("imdbRating").Value }; var movies = moviesQuery.ToList(); foreach (var movie in movies) { result.Title = movie.Title; result.Year = movie.Year; result.Rated = movie.Rated; result.Released = movie.Released; result.RunTime = movie.Runtime; result.Genre = movie.Genre; result.Director = movie.Director; result.Writer = movie.Writer; result.Actors = movie.Actors; result.Plot = movie.Plot; result.Language = movie.Language; result.Country = movie.Country; result.Awards = movie.Awards; result.Rating = movie.Rating; } } else { throw new TitleNotFoundException("The movie was not found!"); } return result; }
public void GetMovieInfoTest() { OmdbService service = OmdbService.Service; string title = "The matrix"; MovieInfo expected = new MovieInfo(); expected.Title = "The Matrix"; expected.Year = "1999"; expected.Rated = "R"; expected.Released = "31 Mar 1999"; expected.RunTime = "136 min"; expected.Genre = "Action, Sci-Fi"; expected.Director = "Andy Wachowski, Lana Wachowski"; expected.Writer = "Andy Wachowski, Lana Wachowski"; expected.Actors = "Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving"; expected.Plot = "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers."; expected.Language = "English"; expected.Country = "USA, Australia"; expected.Awards = "Won 4 Oscars. Another 33 wins & 40 nominations."; expected.Rating = "8.7"; MovieInfo actual = service.GetMovieInfo(title); Assert.AreNotEqual(expected, actual); }