From 98cb79512fd9a68d12f7c7794bb5da56d4112613 Mon Sep 17 00:00:00 2001 From: Nikola Nenov Date: Thu, 23 Jun 2016 08:00:37 +0300 Subject: [PATCH] Reworked tests. Reworked AddParameter method in UriExtensions. Reworked MovieDb methods. --- .../MovieInfo.Tests/ActorTests.cs | 53 +++++++++++-------- .../MovieInfoApp/MovieInfoApp/MovieDb.cs | 35 +++++------- .../MovieInfoApp/MovieInfoApp.csproj | 1 + .../MovieInfoApp/MovieInfoApp/Startup.cs | 21 ++++++++ .../MovieInfoApp/UriExtensions.cs | 12 +++-- 5 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 Web Services And Cloud/MovieInfoApp/MovieInfoApp/Startup.cs diff --git a/Web Services And Cloud/MovieInfoApp/MovieInfo.Tests/ActorTests.cs b/Web Services And Cloud/MovieInfoApp/MovieInfo.Tests/ActorTests.cs index dd62763..a7d108f 100644 --- a/Web Services And Cloud/MovieInfoApp/MovieInfo.Tests/ActorTests.cs +++ b/Web Services And Cloud/MovieInfoApp/MovieInfo.Tests/ActorTests.cs @@ -1,47 +1,58 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace MovieInfo.Tests +namespace MovieInfo.Tests { + using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; + using MovieInfoApp; + using MovieInfoApp.Models; + [TestClass] public class ActorTests { + private static ActorModel actorInfo; + private static CreditsModel credits; + private static string creditsId; + private static SeriesInfo seriesInfo; + private static List episodes; + private static List seasons; + + [ClassInitialize] + public static void InitializeData(TestContext context) + { + GetData(); + } + //In Game of Thrones Nell Tiger Free plays Myrcella Baratheon, write a test to verify the following; [TestMethod] public void ActorIsNotInSeason6Episode1() { - var actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free"); - var credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id); - var creditsId = credits.FirstOrDefault().CreditsId; - var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId); - - Assert.IsFalse(seriesInfo.Episodes.Any(e => e.Season == 6 && e.EpisodeNumber == 1)); + Assert.IsFalse(episodes.Any(e => e.Season == 6 && e.EpisodeNumber == 1)); } [TestMethod] public void ActorIsInSeason5Episode1AsMainCastMemberButNotAsGuest() { - var actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free"); - var credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id); - var creditsId = credits.FirstOrDefault().CreditsId; - var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId); - - Assert.IsTrue(seriesInfo.Seasons.Any(s => s.SeasonNumber == 5)); - Assert.IsFalse(seriesInfo.Episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 1)); + Assert.IsTrue(seasons.Any(s => s.SeasonNumber == 5)); + Assert.IsFalse(episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 1)); } [TestMethod] public void ActorIsInSeason5Episode2AsMainCastMemberAndAsGuest() { - var actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free"); - var credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id); - var creditsId = credits.FirstOrDefault().CreditsId; - var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId); + Assert.IsTrue(seasons.Any(s => s.SeasonNumber == 5)); + Assert.IsTrue(episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 2)); + } - Assert.IsTrue(seriesInfo.Seasons.Any(s => s.SeasonNumber == 5)); - Assert.IsTrue(seriesInfo.Episodes.Any(e => e.Season == 5 && e.EpisodeNumber == 2)); + private static void GetData() + { + actorInfo = MovieDb.GetActorInfoByName("Nell Tiger Free").Result; + credits = MovieDb.GetTVCreditsByActorId(actorInfo.Id).Result.FirstOrDefault(); + creditsId = credits.CreditsId; + seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId).Result; + episodes = seriesInfo.Episodes; + seasons = seriesInfo.Seasons; } } } diff --git a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieDb.cs b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieDb.cs index 4eec910..69cc607 100644 --- a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieDb.cs +++ b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieDb.cs @@ -8,27 +8,18 @@ using Newtonsoft.Json; using Models; + using System.Threading.Tasks; - public class MovieDb + public static class MovieDb { private static readonly Uri BaseUrl = new Uri("https://api.themoviedb.org/"); private static readonly string PeopleEndpoint = "3/search/person"; private static readonly string TVCreditsEndpointFormat = "3/person/{0}/tv_credits"; private static readonly string CreditDetailsEndpointFormat = "3/credit/{0}"; - private const string ApiKey = "xxxxxxxxxxxx"; + private const string ApiKey = "8e44e41d1573ab3d390b11fc6d35d95e"; - public static void Main() - { - var actor = GetActorInfoByName("Nell Tiger Free"); - var credits = GetTVCreditsByActorId(actor.Id); - var creditsId = credits.FirstOrDefault().CreditsId; - var seriesInfo = GetTVSeriesDetailsByCreditsId(creditsId); - - Console.WriteLine(); - } - - public static ActorModel GetActorInfoByName(string name) + public static async Task GetActorInfoByName(string name) { string responseData; string nameQuery = "query=" + Uri.EscapeUriString(name); @@ -42,17 +33,15 @@ public static ActorModel GetActorInfoByName(string name) client.BaseAddress = targetUrl.Uri; - - var response = client.GetAsync("").Result; - responseData = response.Content.ReadAsStringAsync().Result; - + Task response = client.GetStringAsync(""); + responseData = await response; var actors = JsonConvert.DeserializeObject(responseData); return actors.Results.FirstOrDefault(); } - public static List GetTVCreditsByActorId(int actorId) + public static async Task> GetTVCreditsByActorId(int actorId) { string responseData; var endPointWithActorId = string.Format(TVCreditsEndpointFormat, actorId); @@ -63,15 +52,15 @@ public static List GetTVCreditsByActorId(int actorId) var targetUrl = CreateUrl(BaseUrl, endPointWithActorId); client.BaseAddress = targetUrl.Uri; - var response = client.GetAsync("").Result; - responseData = response.Content.ReadAsStringAsync().Result; + Task response = client.GetStringAsync(""); + responseData = await response; var credits = JsonConvert.DeserializeObject(responseData); return credits.Credits; } - public static SeriesInfo GetTVSeriesDetailsByCreditsId(string creditsId) + public static async Task GetTVSeriesDetailsByCreditsId(string creditsId) { string responseData; @@ -83,8 +72,8 @@ public static SeriesInfo GetTVSeriesDetailsByCreditsId(string creditsId) var targetUrl = CreateUrl(BaseUrl, endPointWithCreditsId); client.BaseAddress = targetUrl.Uri; - var response = client.GetAsync("").Result; - responseData = response.Content.ReadAsStringAsync().Result; + var response = client.GetStringAsync(""); + responseData = await response; var result = JsonConvert.DeserializeObject(responseData); diff --git a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieInfoApp.csproj b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieInfoApp.csproj index 7b476a2..ab775af 100644 --- a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieInfoApp.csproj +++ b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/MovieInfoApp.csproj @@ -58,6 +58,7 @@ + diff --git a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/Startup.cs b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/Startup.cs new file mode 100644 index 0000000..303369f --- /dev/null +++ b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/Startup.cs @@ -0,0 +1,21 @@ +using System; +using System.Linq; + + +namespace MovieInfoApp +{ + class Startup + { + public static void Main() + { + + var actor = MovieDb.GetActorInfoByName("Nell Tiger Free"); + var credits = MovieDb.GetTVCreditsByActorId(actor.Id); + var creditsId = credits.Result.FirstOrDefault().CreditsId; + var seriesInfo = MovieDb.GetTVSeriesDetailsByCreditsId(creditsId); + + Console.WriteLine(creditsId); + Console.WriteLine(seriesInfo.Result.Episodes); + } + } +} diff --git a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/UriExtensions.cs b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/UriExtensions.cs index ca0ad27..f45ecc5 100644 --- a/Web Services And Cloud/MovieInfoApp/MovieInfoApp/UriExtensions.cs +++ b/Web Services And Cloud/MovieInfoApp/MovieInfoApp/UriExtensions.cs @@ -8,9 +8,15 @@ public static class UriExtensions public static Uri AddParameter(this Uri url, string paramName, string paramValue) { var uriBuilder = new UriBuilder(url); - var query = HttpUtility.ParseQueryString(uriBuilder.Query); - query[paramName] = paramValue; - uriBuilder.Query = query.ToString(); + + if (uriBuilder.Query != null && uriBuilder.Query.Length > 1) + { + uriBuilder.Query = uriBuilder.Query.Substring(1) + "&" + paramName + "=" + paramValue; + } + else + { + uriBuilder.Query = paramName + "=" + paramValue; + } return uriBuilder.Uri; }