Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
GH-109: Add method for retrieving comment likes in comments module
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Jun 15, 2018
1 parent 6e1b42a commit 7d0c7b1
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Source/Lib/TraktApiSharp/Modules/TraktCommentsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ public Task<TraktResponse<ITraktComment>> GetCommentAsync(uint commentId, Cancel
cancellationToken);
}

/// <summary>
/// Gets likes for comment with the given id.
/// <para>OAuth authorization not required.</para>
/// <para>
/// See <a href="https://trakt.docs.apiary.io/#reference/comments/likes/get-all-users-who-liked-a-comment">"Trakt API Doc - Comments: Likes"</a> for more information.
/// </para>
/// </summary>
/// <param name="commentId">The comment's id.</param>
/// <param name="extendedInfo">
/// The extended info, which determines how much data about the comment's likes should be queried.
/// See also <seealso cref="TraktExtendedInfo" />.
/// </param>
/// <param name="pagedParameters"></param>
/// <param name="cancellationToken"></param>
/// <returns>
/// An <see cref="TraktPagedResponse{ITraktCommentLike}"/> instance containing the queried likes and which also
/// contains the queried page number, the page's item count, maximum page count and maximum item count.
/// <para>
/// See also <seealso cref="TraktPagedResponse{ListItem}" /> and <seealso cref="ITraktCommentLike" />.
/// </para>
/// </returns>
/// <exception cref="TraktException">Thrown, if the request fails.</exception>
/// <exception cref="ArgumentException">Thrown, if the given comment id is null, empty or contains spaces.</exception>
public Task<TraktPagedResponse<ITraktCommentLike>> GetCommentLikesAsync(uint commentId, TraktExtendedInfo extendedInfo = null,
TraktPagedParameters pagedParameters = null,
CancellationToken cancellationToken = default)
{
ValidateId(commentId);
var requestHandler = new RequestHandler(Client);

return requestHandler.ExecutePagedRequestAsync(new CommentLikesRequest
{
Id = commentId.ToString(),
ExtendedInfo = extendedInfo,
Page = pagedParameters?.Page,
Limit = pagedParameters?.Limit
},
cancellationToken);
}

/// <summary>
/// Gets multiple different <see cref="ITraktComment" />s or replies at once with the given Trakt-Ids or -Slugs.
/// <para>OAuth authorization not required.</para>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
namespace TraktApiSharp.Tests.Modules.TraktCommentsModule
{
using FluentAssertions;
using System;
using System.Net;
using System.Threading.Tasks;
using TestUtils;
using Traits;
using TraktApiSharp.Exceptions;
using TraktApiSharp.Objects.Basic;
using TraktApiSharp.Requests.Parameters;
using TraktApiSharp.Responses;
using Xunit;

[Category("Modules.Comments")]
public partial class TraktCommentsModule_Tests
{
private readonly string GET_COMMENT_LIKES_URI = $"comments/{GET_COMMENT_ID}/likes";

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI,
COMMENT_LIKES_JSON, 1, 10, 1, ITEM_COUNT);

TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(10u);
response.Page.Should().Be(1u);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes_With_ExtendedInfo()
{
TraktClient client = TestUtility.GetMockClient($"{GET_COMMENT_LIKES_URI}?extended={EXTENDED_INFO}",
COMMENT_LIKES_JSON, 1, 10, 1, ITEM_COUNT);

TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID, EXTENDED_INFO);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(10u);
response.Page.Should().Be(1u);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes_With_Page()
{
TraktClient client = TestUtility.GetMockClient($"{GET_COMMENT_LIKES_URI}?page={PAGE}",
COMMENT_LIKES_JSON, PAGE, 10, 1, ITEM_COUNT);

var pagedParameters = new TraktPagedParameters(PAGE);
TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID, null, pagedParameters);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(10u);
response.Page.Should().Be(PAGE);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes_With_Limit()
{
TraktClient client = TestUtility.GetMockClient($"{GET_COMMENT_LIKES_URI}?limit={LIMIT}",
COMMENT_LIKES_JSON, 1, LIMIT, 1, ITEM_COUNT);

var pagedParameters = new TraktPagedParameters(null, LIMIT);
TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID, null, pagedParameters);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(LIMIT);
response.Page.Should().Be(1u);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes_With_ExtendedInfo_And_Page()
{
TraktClient client = TestUtility.GetMockClient($"{GET_COMMENT_LIKES_URI}?extended={EXTENDED_INFO}&page={PAGE}",
COMMENT_LIKES_JSON, PAGE, 10, 1, ITEM_COUNT);

var pagedParameters = new TraktPagedParameters(PAGE);
TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID, EXTENDED_INFO, pagedParameters);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(10u);
response.Page.Should().Be(PAGE);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes_With_ExtendedInfo_And_Limit()
{
TraktClient client = TestUtility.GetMockClient($"{GET_COMMENT_LIKES_URI}?extended={EXTENDED_INFO}&limit={LIMIT}",
COMMENT_LIKES_JSON, 1, LIMIT, 1, ITEM_COUNT);

var pagedParameters = new TraktPagedParameters(null, LIMIT);
TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID, EXTENDED_INFO, pagedParameters);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(LIMIT);
response.Page.Should().Be(1u);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public async Task Test_TraktCommentsModule_GetCommentLikes_Complete()
{
TraktClient client = TestUtility.GetMockClient($"{GET_COMMENT_LIKES_URI}?page={PAGE}&limit={LIMIT}",
COMMENT_LIKES_JSON, PAGE, LIMIT, 1, ITEM_COUNT);

var pagedParameters = new TraktPagedParameters(PAGE, LIMIT);
TraktPagedResponse<ITraktCommentLike> response = await client.Comments.GetCommentLikesAsync(GET_COMMENT_ID, null, pagedParameters);

response.Should().NotBeNull();
response.IsSuccess.Should().BeTrue();
response.HasValue.Should().BeTrue();
response.Value.Should().NotBeNull().And.HaveCount(ITEM_COUNT);
response.ItemCount.Should().HaveValue().And.Be(ITEM_COUNT);
response.Limit.Should().Be(LIMIT);
response.Page.Should().Be(PAGE);
response.PageCount.Should().HaveValue().And.Be(1);
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_NotFoundException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.NotFound);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktCommentNotFoundException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_AuthorizationException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.Unauthorized);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktAuthorizationException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_BadRequestException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.BadRequest);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktBadRequestException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ForbiddenException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.Forbidden);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktForbiddenException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_MethodNotFoundException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.MethodNotAllowed);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktMethodNotFoundException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ConflictException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.Conflict);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktConflictException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ServerException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.InternalServerError);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktServerException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_BadGatewayException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, HttpStatusCode.BadGateway);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktBadGatewayException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_PreconditionFailedException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)412);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktPreconditionFailedException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ValidationException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)422);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktValidationException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_RateLimitException()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)429);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktRateLimitException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ServerUnavailableException_503()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)503);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktServerUnavailableException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ServerUnavailableException_504()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)504);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktServerUnavailableException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ServerUnavailableException_520()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)520);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktServerUnavailableException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ServerUnavailableException_521()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)521);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktServerUnavailableException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_Throws_ServerUnavailableException_522()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI, (HttpStatusCode)522);
Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(GET_COMMENT_ID);
act.Should().Throw<TraktServerUnavailableException>();
}

[Fact]
public void Test_TraktCommentsModule_GetCommentLikes_ArgumentExceptions()
{
TraktClient client = TestUtility.GetMockClient(GET_COMMENT_LIKES_URI,
COMMENT_LIKES_JSON, 1, 10, ITEM_COUNT);

Func<Task<TraktPagedResponse<ITraktCommentLike>>> act = () => client.Comments.GetCommentLikesAsync(0);
act.Should().Throw<ArgumentException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class TraktCommentsModule_Tests
private const uint GET_COMMENT_ID = 76957U;
private const uint GET_COMMENT_REPLIES_ID = 190U;
private const int COMMENT_REPLIES_ITEM_COUNT = 2;
private const int ITEM_COUNT = 2;
private const uint PAGE = 2;
private const uint LIMIT = 4;
private const string COMMENT_TEXT = "one two three four five reply";
Expand Down Expand Up @@ -132,6 +133,36 @@ public TraktCommentsModule_Tests()
}
}";

private const string COMMENT_LIKES_JSON =
@"[
{
""liked_at"": ""2014-09-01T09:10:11.000Z"",
""user"": {
""username"": ""sean"",
""private"": false,
""name"": ""Sean Rudford"",
""vip"": true,
""vip_ep"": false,
""ids"": {
""slug"": ""sean""
}
}
},
{
""liked_at"": ""2014-09-01T09:10:11.000Z"",
""user"": {
""username"": ""justin"",
""private"": false,
""name"": ""Justin Nemeth"",
""vip"": true,
""vip_ep"": false,
""ids"": {
""slug"": ""justin""
}
}
}
]";

private const string COMMENT_REPLIES_JSON =
@"[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
<Compile Include="Modules\TraktCheckinsModule\TraktCheckinsModule_Tests_Json_Data.cs" />
<Compile Include="Modules\TraktCommentsModule\TraktCommentsModule_DeleteComment_Tests.cs" />
<Compile Include="Modules\TraktCommentsModule\TraktCommentsModule_GetCommentItem_Tests.cs" />
<Compile Include="Modules\TraktCommentsModule\TraktCommentsModule_GetCommentLikes_Tests.cs" />
<Compile Include="Modules\TraktCommentsModule\TraktCommentsModule_GetCommentReplies_Tests.cs" />
<Compile Include="Modules\TraktCommentsModule\TraktCommentsModule_GetComment_Tests.cs" />
<Compile Include="Modules\TraktCommentsModule\TraktCommentsModule_LikeComment_Tests.cs" />
Expand Down

0 comments on commit 7d0c7b1

Please sign in to comment.