From 6e1b42a6199cc9d67d5c6589067e69d9fe531ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Fr=C3=B6hling?= Date: Fri, 15 Jun 2018 16:39:54 +0200 Subject: [PATCH] GH-109: Add request implementation for comment like --- .../Requests/Comments/CommentLikesRequest.cs | 53 +++++ Source/Lib/TraktApiSharp/TraktApiSharp.csproj | 1 + .../Comments/CommentLikesRequest_Tests.cs | 199 ++++++++++++++++++ .../TraktApiSharp.Tests.csproj | 1 + 4 files changed, 254 insertions(+) create mode 100644 Source/Lib/TraktApiSharp/Requests/Comments/CommentLikesRequest.cs create mode 100644 Source/Tests/TraktApiSharp.Tests/Requests/Comments/CommentLikesRequest_Tests.cs diff --git a/Source/Lib/TraktApiSharp/Requests/Comments/CommentLikesRequest.cs b/Source/Lib/TraktApiSharp/Requests/Comments/CommentLikesRequest.cs new file mode 100644 index 000000000..7085e6931 --- /dev/null +++ b/Source/Lib/TraktApiSharp/Requests/Comments/CommentLikesRequest.cs @@ -0,0 +1,53 @@ +namespace TraktApiSharp.Requests.Comments +{ + using Base; + using Extensions; + using Interfaces; + using Objects.Basic; + using Parameters; + using System; + using System.Collections.Generic; + + internal sealed class CommentLikesRequest : AGetRequest, IHasId, ISupportsExtendedInfo, ISupportsPagination + { + public string Id { get; set; } + + public TraktExtendedInfo ExtendedInfo { get; set; } + + public RequestObjectType RequestObjectType => RequestObjectType.Comments; + + public uint? Page { get; set; } + + public uint? Limit { get; set; } + + public override string UriTemplate => "comments/{id}/likes{?extended,page,limit}"; + + public override IDictionary GetUriPathParameters() + { + var uriParams = new Dictionary + { + ["id"] = Id + }; + + if (ExtendedInfo != null && ExtendedInfo.HasAnySet) + uriParams.Add("extended", ExtendedInfo.ToString()); + + if (Page.HasValue) + uriParams.Add("page", Page.Value.ToString()); + + if (Limit.HasValue) + uriParams.Add("limit", Limit.Value.ToString()); + + return uriParams; + } + + public override void Validate() + { + if (Id == null) + throw new ArgumentNullException(nameof(Id)); + + if (Id == string.Empty || Id.ContainsSpace()) + throw new ArgumentException("comment id not valid", nameof(Id)); + } + } +} diff --git a/Source/Lib/TraktApiSharp/TraktApiSharp.csproj b/Source/Lib/TraktApiSharp/TraktApiSharp.csproj index f3b27def8..8f63e250f 100644 --- a/Source/Lib/TraktApiSharp/TraktApiSharp.csproj +++ b/Source/Lib/TraktApiSharp/TraktApiSharp.csproj @@ -948,6 +948,7 @@ + diff --git a/Source/Tests/TraktApiSharp.Tests/Requests/Comments/CommentLikesRequest_Tests.cs b/Source/Tests/TraktApiSharp.Tests/Requests/Comments/CommentLikesRequest_Tests.cs new file mode 100644 index 000000000..5ba75bd1a --- /dev/null +++ b/Source/Tests/TraktApiSharp.Tests/Requests/Comments/CommentLikesRequest_Tests.cs @@ -0,0 +1,199 @@ +namespace TraktApiSharp.Tests.Requests.Comments +{ + using FluentAssertions; + using System; + using System.Collections; + using System.Collections.Generic; + using Traits; + using TraktApiSharp.Requests.Base; + using TraktApiSharp.Requests.Comments; + using TraktApiSharp.Requests.Parameters; + using Xunit; + + [Category("Requests.Comments")] + public class CommentLikesRequest_Tests + { + [Fact] + public void Test_CommentLikesRequest_Has_AuthorizationRequirement_NotRequired() + { + var request = new CommentLikesRequest(); + request.AuthorizationRequirement.Should().Be(AuthorizationRequirement.NotRequired); + } + + [Fact] + public void Test_CommentLikesRequest_Returns_Valid_RequestObjectType() + { + var request = new CommentLikesRequest(); + request.RequestObjectType.Should().Be(RequestObjectType.Comments); + } + + [Fact] + public void Test_CommentLikesRequest_Has_Valid_UriTemplate() + { + var request = new CommentLikesRequest(); + request.UriTemplate.Should().Be("comments/{id}/likes{?extended,page,limit}"); + } + + [Fact] + public void Test_CommentLikesRequest_Validate_Throws_Exceptions() + { + // id is null + var request = new CommentLikesRequest(); + + Action act = () => request.Validate(); + act.Should().Throw(); + + // empty id + request = new CommentLikesRequest { Id = string.Empty }; + + act = () => request.Validate(); + act.Should().Throw(); + + // id with spaces + request = new CommentLikesRequest { Id = "invalid id" }; + + act = () => request.Validate(); + act.Should().Throw(); + } + + [Theory, ClassData(typeof(CommentLikesRequest_TestData))] + public void Test_CommentLikesRequest_Returns_Valid_UriPathParameters(IDictionary values, + IDictionary expected) + { + values.Should().NotBeNull().And.HaveCount(expected.Count); + + if (expected.Count > 0) + values.Should().Contain(expected); + } + + public class CommentLikesRequest_TestData : IEnumerable + { + private const string _id = "123"; + private static readonly TraktExtendedInfo _extendedInfo = new TraktExtendedInfo { Full = true }; + private const int _page = 5; + private const int _limit = 20; + + private static readonly CommentLikesRequest _request1 = new CommentLikesRequest + { + Id = _id + }; + + private static readonly CommentLikesRequest _request2 = new CommentLikesRequest + { + Id = _id, + Page = _page + }; + + private static readonly CommentLikesRequest _request3 = new CommentLikesRequest + { + Id = _id, + Limit = _limit + }; + + private static readonly CommentLikesRequest _request4 = new CommentLikesRequest + { + Id = _id, + Page = _page, + Limit = _limit + }; + + private static readonly CommentLikesRequest _request5 = new CommentLikesRequest + { + Id = _id, + ExtendedInfo = _extendedInfo + }; + + private static readonly CommentLikesRequest _request6 = new CommentLikesRequest + { + Id = _id, + ExtendedInfo = _extendedInfo, + Page = _page + }; + + private static readonly CommentLikesRequest _request7 = new CommentLikesRequest + { + Id = _id, + ExtendedInfo = _extendedInfo, + Limit = _limit + }; + + private static readonly CommentLikesRequest _request8 = new CommentLikesRequest + { + Id = _id, + ExtendedInfo = _extendedInfo, + Page = _page, + Limit = _limit + }; + + private static readonly List _data = new List(); + + public CommentLikesRequest_TestData() + { + SetupPathParamters(); + } + + private void SetupPathParamters() + { + var strExtendedInfo = _extendedInfo.ToString(); + var strPage = _page.ToString(); + var strLimit = _limit.ToString(); + + _data.Add(new object[] { _request1.GetUriPathParameters(), new Dictionary + { + ["id"] = _id + }}); + + _data.Add(new object[] { _request2.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["page"] = strPage + }}); + + _data.Add(new object[] { _request3.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["limit"] = strLimit + }}); + + _data.Add(new object[] { _request4.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["page"] = strPage, + ["limit"] = strLimit + }}); + + _data.Add(new object[] { _request5.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["extended"] = strExtendedInfo + }}); + + _data.Add(new object[] { _request6.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["extended"] = strExtendedInfo, + ["page"] = strPage + }}); + + _data.Add(new object[] { _request7.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["extended"] = strExtendedInfo, + ["limit"] = strLimit + }}); + + _data.Add(new object[] { _request8.GetUriPathParameters(), new Dictionary + { + ["id"] = _id, + ["extended"] = strExtendedInfo, + ["page"] = strPage, + ["limit"] = strLimit + }}); + } + + public IEnumerator GetEnumerator() => _data.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + } +} diff --git a/Source/Tests/TraktApiSharp.Tests/TraktApiSharp.Tests.csproj b/Source/Tests/TraktApiSharp.Tests/TraktApiSharp.Tests.csproj index 64c6d73ac..1e6a8307e 100644 --- a/Source/Tests/TraktApiSharp.Tests/TraktApiSharp.Tests.csproj +++ b/Source/Tests/TraktApiSharp.Tests/TraktApiSharp.Tests.csproj @@ -1298,6 +1298,7 @@ +