diff --git a/src/IF.Lastfm.Core.Tests/Api/Commands/UserGetRecentTracksCommandTests.cs b/src/IF.Lastfm.Core.Tests/Api/Commands/UserGetRecentTracksCommandTests.cs index b607a07..52ef5f5 100644 --- a/src/IF.Lastfm.Core.Tests/Api/Commands/UserGetRecentTracksCommandTests.cs +++ b/src/IF.Lastfm.Core.Tests/Api/Commands/UserGetRecentTracksCommandTests.cs @@ -1,5 +1,7 @@ -using IF.Lastfm.Core.Api.Commands.UserApi; +using IF.Lastfm.Core.Api; +using IF.Lastfm.Core.Api.Commands.UserApi; using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; using IF.Lastfm.Core.Tests.Resources; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -7,6 +9,8 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using Moq; +using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Tests.Api.Commands { @@ -82,4 +86,67 @@ public async Task HandleErrorResponse() Assert.IsTrue(parsed.Error == LastFmApiError.MissingParameters); } } + + [TestClass] + public class UserGetTopAlbumsCommandTests : CommandTestsBase + { + private const string USER = "test"; + private const LastStatsTimeSpan SPAN = LastStatsTimeSpan.Month; + + private GetTopAlbumsCommand _command; + private Mock _mockAuth; + + [TestInitialize] + public void TestInitialise() + { + _mockAuth = new Mock(); + _command = new GetTopAlbumsCommand(_mockAuth.Object, USER, SPAN) + { + Page = 5, + Count = 20 + }; + + _command.SetParameters(); + } + + [TestMethod] + public void CorrectParameters() + { + var expected = new Dictionary + { + {"user", USER}, + {"period", SPAN.GetApiName()}, + {"limit", "20"}, + {"page", "5"}, + {"disablecachetoken", ""} + }; + + _command.Parameters["disablecachetoken"] = ""; + + TestHelper.AssertEqual(expected, _command.Parameters); + } + + public void HandleErrorResponse() + { + + } + + [TestMethod] + public void HandleResponseEmpty() + { + + } + + [TestMethod] + public void HandleResponseSingle() + { + + } + + [TestMethod] + public void HandleResponseMultiple() + { + + } + } } diff --git a/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj b/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj index ade9a5d..b9eb271 100644 --- a/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj +++ b/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj @@ -95,6 +95,7 @@ + True diff --git a/src/IF.Lastfm.Core.Tests/OrderedContractResolver.cs b/src/IF.Lastfm.Core.Tests/OrderedContractResolver.cs new file mode 100644 index 0000000..854b72e --- /dev/null +++ b/src/IF.Lastfm.Core.Tests/OrderedContractResolver.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace IF.Lastfm.Core.Tests +{ + /// + /// http://stackoverflow.com/a/11309106/268555 + /// + public class OrderedContractResolver : DefaultContractResolver + { + protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) + { + return base.CreateProperties(type, memberSerialization).OrderBy(p => p.PropertyName).ToList(); + } + } +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core.Tests/TestHelper.cs b/src/IF.Lastfm.Core.Tests/TestHelper.cs index 3767969..c22d5dd 100644 --- a/src/IF.Lastfm.Core.Tests/TestHelper.cs +++ b/src/IF.Lastfm.Core.Tests/TestHelper.cs @@ -1,29 +1,60 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using IF.Lastfm.Core.Api.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Tests { public static class TestHelper { - private static readonly JsonSerializerSettings _testSerialiserSettings; - - static TestHelper() + private static JsonSerializer GetTestSerialiser() { - _testSerialiserSettings = new JsonSerializerSettings + return new JsonSerializer { DateFormatString = "yyyy-MM-dd HH:mm:ss.fff", - NullValueHandling = NullValueHandling.Ignore + NullValueHandling = NullValueHandling.Ignore, + ContractResolver = new OrderedContractResolver() }; } + private static JObject WithSortedProperties(this JObject jo) + { + var result = new JObject(); + foreach (var prop in jo.Properties().OrderBy(p => p.Name)) + { + var nestedJo = prop.Value as JObject; + if (nestedJo != null) + { + result.Add(nestedJo.WithSortedProperties()); + } + else + { + result.Add(prop); + } + } + return result; + } + public static string TestSerialise(this T poco) { - return JsonConvert.SerializeObject(poco, Formatting.Indented, _testSerialiserSettings); + var serialiser = GetTestSerialiser(); + var jo = JObject.FromObject(poco, serialiser); + var ordered = jo.WithSortedProperties(); + + return ordered.ToString(); + } + + public static void AssertEqual(T one, T two) + { + var ones = one.TestSerialise(); + var twos = two.TestSerialise(); + + Assert.AreEqual(ones, twos, ones.DifferencesTo(twos)); } public static string DifferencesTo(this IEnumerable expected, IEnumerable actual) diff --git a/src/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs b/src/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs index 54eef7e..238b4f3 100644 --- a/src/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs +++ b/src/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs @@ -22,7 +22,7 @@ public GetTopAlbumsCommand(ILastAuth auth, string username, LastStatsTimeSpan sp public override void SetParameters() { - Parameters.Add("username", Username); + Parameters.Add("user", Username); Parameters.Add("period", TimeSpan.GetApiName()); AddPagingParameters();