From 4acdeec6b34d7eec8fad680ba06e9bd8dfd48128 Mon Sep 17 00:00:00 2001 From: Rasmus Wulff Jensen Date: Fri, 22 Jun 2018 12:08:48 +0200 Subject: [PATCH] - Added support for issue #330 - Added support for CRUD operations of Comments on Articles and Posts --- .../Requests/HelpCenter/Comments.cs | 101 ++++++++- .../HelpCenter/CommentTests.cs | 205 ++++++++++++++++++ 2 files changed, 301 insertions(+), 5 deletions(-) create mode 100644 test/ZendeskApi_v2.Test/HelpCenter/CommentTests.cs diff --git a/src/ZendeskApi_v2/Requests/HelpCenter/Comments.cs b/src/ZendeskApi_v2/Requests/HelpCenter/Comments.cs index e7a1d583..1cbbf7d4 100644 --- a/src/ZendeskApi_v2/Requests/HelpCenter/Comments.cs +++ b/src/ZendeskApi_v2/Requests/HelpCenter/Comments.cs @@ -1,8 +1,6 @@ -using System; -#if ASYNC +#if ASYNC using System.Threading.Tasks; #endif -using ZendeskApi_v2.Extensions; using ZendeskApi_v2.Models.HelpCenter.Comments; namespace ZendeskApi_v2.Requests.HelpCenter @@ -14,14 +12,29 @@ public interface IComments : ICore GroupCommentResponse GetCommentsForArticle(long? articleId, int? perPage = null, int? page = null); GroupCommentResponse GetCommentsForUser(long? userId); GroupCommentResponse GetCommentsForCurrentUser(); + GroupCommentResponse GetCommentsForPost(long? postId, int? perPage = null, int? page = null); + IndividualCommentsResponse CreateCommentForArticle(long articleId, Comment comment); + IndividualCommentsResponse CreateCommentForPost(long postId, Comment comment); + IndividualCommentsResponse UpdateCommentForArticle(long articleId, Comment comment); + IndividualCommentsResponse UpdateCommentForPost(long postId, Comment comment); + bool DeleteCommentForArticle(long articleId, long commentId); + bool DeleteCommentForPost(long postId, long commentId); #endif #if ASYNC - Task GetCommentsForArticleAsync(long? articleId); + Task GetCommentsForArticleAsync(long? articleId); Task GetCommentsForUserAsync(long? userId); Task GetCommentsForCurrentUserAsync(); + Task GetCommentsForPostAsync(long? postId); + Task CreateCommentForArticleAsync(long articleId, Comment comment); + Task CreateCommentForPostAsync(long postId, Comment comment); + Task UpdateCommentForArticleAsync(long articleId, Comment comment); + Task UpdateCommentForPostAsync(long postId, Comment comment); + Task DeleteCommentForArticleAsync(long articleId, long commentId); + Task DeleteCommentForPostAsync(long postId, long commentId); #endif - } + + } /// /// https://developer.zendesk.com/rest_api/docs/help_center/comments @@ -49,6 +62,45 @@ public GroupCommentResponse GetCommentsForCurrentUser() { return GenericGet("help_center/users/me/comments.json"); } + + public GroupCommentResponse GetCommentsForPost(long? postId, int? perPage = null, int? page = null) + { + return GenericPagedGet($"community/posts/{postId}/comments.json", perPage, page); + } + + public IndividualCommentsResponse CreateCommentForArticle(long articleId, Comment comment) + { + var body = new { comment }; + return GenericPost($"help_center/articles/{articleId}/comments.json", body); + } + + public IndividualCommentsResponse CreateCommentForPost(long postId, Comment comment) + { + var body = new { comment }; + return GenericPost($"community/posts/{postId}/comments.json", body); + } + + public IndividualCommentsResponse UpdateCommentForArticle(long articleId, Comment comment) + { + var body = new { comment }; + return GenericPut($"help_center/articles/{articleId}/comments/{comment.Id}.json", body); + } + + public IndividualCommentsResponse UpdateCommentForPost(long postId, Comment comment) + { + var body = new { comment }; + return GenericPut($"community/posts/{postId}/comments/{comment.Id}.json", body); + } + + public bool DeleteCommentForArticle(long articleId, long commentId) + { + return GenericDelete($"help_center/articles/{articleId}/comments/{commentId}.json"); + } + + public bool DeleteCommentForPost(long postId, long commentId) + { + return GenericDelete($"community/posts/{postId}/comments/{commentId}.json"); + } #endif #if ASYNC @@ -66,6 +118,45 @@ public async Task GetCommentsForCurrentUserAsync() { return await GenericGetAsync("help_center/users/me/comments.json"); } + + public async Task GetCommentsForPostAsync(long? postId) + { + return await GenericGetAsync($"community/posts/{postId}/comments.json"); + } + + public async Task CreateCommentForArticleAsync(long articleId, Comment comment) + { + var body = new { comment }; + return await GenericPostAsync($"help_center/articles/{articleId}/comments.json", body); + } + + public async Task CreateCommentForPostAsync(long postId, Comment comment) + { + var body = new { comment }; + return await GenericPostAsync($"community/posts/{postId}/comments.json", body); + } + + public async Task UpdateCommentForArticleAsync(long articleId, Comment comment) + { + var body = new { comment }; + return await GenericPutAsync($"help_center/articles/{articleId}/comments/{comment.Id}.json", body); + } + + public async Task UpdateCommentForPostAsync(long postId, Comment comment) + { + var body = new { comment }; + return await GenericPutAsync($"community/posts/{postId}/comments/{comment.Id}.json", body); + } + + public async Task DeleteCommentForArticleAsync(long articleId, long commentId) + { + return await GenericDeleteAsync($"help_center/articles/{articleId}/comments/{commentId}.json"); + } + + public async Task DeleteCommentForPostAsync(long postId, long commentId) + { + return await GenericDeleteAsync($"community/posts/{postId}/comments/{commentId}.json"); + } #endif } } diff --git a/test/ZendeskApi_v2.Test/HelpCenter/CommentTests.cs b/test/ZendeskApi_v2.Test/HelpCenter/CommentTests.cs new file mode 100644 index 00000000..f0691994 --- /dev/null +++ b/test/ZendeskApi_v2.Test/HelpCenter/CommentTests.cs @@ -0,0 +1,205 @@ +using System.Threading.Tasks; +using NUnit.Framework; +using ZendeskApi_v2; +using ZendeskApi_v2.Models.Articles; +using ZendeskApi_v2.Models.HelpCenter.Comments; +using ZendeskApi_v2.Models.HelpCenter.Post; + +namespace Tests +{ + [TestFixture] + public class CommentTests + { + private ZendeskApi api = new ZendeskApi(Settings.Site, Settings.AdminEmail, Settings.AdminPassword); + private long _testSectionIdForCommentsTest = 360000205286; //https://csharpapi.zendesk.com/hc/en-us/sections/360000205286-Test-Section-For-Comment-Tests + private long _testTopicIdForCommentsTest = 360000016546; //https://csharpapi.zendesk.com/hc/en-us/community/topics/360000016546-Test-Topic-For-Comment-Tests + + [OneTimeSetUp] + public void OneTimeSetUp() + { + DeleteAllArticlesFromTestSection(); //Get rid of all articles in the test-section (there should be none but there could be if previous tests have failed) + DeleteAllPostsFromTestTopic(); //Get rid of all posts in the test-topic (there should be none but there could be if previous tests have failed) + } + + [OneTimeTearDown] + public void OneTimeTearDown() + { + DeleteAllArticlesFromTestSection(); //Clean-up after tests + DeleteAllPostsFromTestTopic(); //Clean-up after tests + } + + private void DeleteAllArticlesFromTestSection() + { + var articlesBySectionId = api.HelpCenter.Articles.GetArticlesBySectionId(_testSectionIdForCommentsTest); + foreach (var article in articlesBySectionId.Articles) + { + api.HelpCenter.Articles.DeleteArticle(article.Id.Value); + } + } + + private void DeleteAllPostsFromTestTopic() + { + var articlesBySectionId = api.HelpCenter.Posts.GetPostsByTopicId(_testTopicIdForCommentsTest); + foreach (var post in articlesBySectionId.Posts) + { + api.HelpCenter.Posts.DeletePost(post.Id.Value); + } + } + + [Test] + public void CanCreateUpdateAndDeleteCommentsForArticle() + { + var articleId = CreateTestArticle("Test-Article-For-Comments"); + + //Create 3 Comments + var individualCommentsResponse1 = api.HelpCenter.Comments.CreateCommentForArticle(articleId, new Comment {Body = "Comment 1", Locale = "en-us"}); + Assert.NotNull(individualCommentsResponse1.Comment); + Assert.Greater(individualCommentsResponse1.Comment.Id, 0); + + var individualCommentsResponse2 = api.HelpCenter.Comments.CreateCommentForArticle(articleId, new Comment {Body = "Comment 2", Locale = "en-us" }); + Assert.NotNull(individualCommentsResponse2.Comment); + Assert.Greater(individualCommentsResponse2.Comment.Id, 0); + + var individualCommentsResponse3 = api.HelpCenter.Comments.CreateCommentForArticle(articleId, new Comment {Body = "Comment 3", Locale = "en-us" }); + Assert.NotNull(individualCommentsResponse3.Comment); + Assert.Greater(individualCommentsResponse3.Comment.Id, 0); + + Assert.AreEqual(3, api.HelpCenter.Comments.GetCommentsForArticle(articleId).Count); //That article have 3 comments + + Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body); + Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body); + Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body); + + //Update Comment + var updatedCommentBody = "Comment 2 Updated"; + individualCommentsResponse2.Comment.Body = updatedCommentBody; + var updateCommentForArticle = api.HelpCenter.Comments.UpdateCommentForArticle(articleId, individualCommentsResponse2.Comment); + Assert.AreEqual(updatedCommentBody, updateCommentForArticle.Comment.Body); + + //Delete Comment + api.HelpCenter.Comments.DeleteCommentForArticle(articleId, individualCommentsResponse2.Comment.Id.Value); + Assert.AreEqual(2, api.HelpCenter.Comments.GetCommentsForArticle(articleId).Count); //One less comments now + } + + [Test] + public void CanCreateUpdateAndDeleteCommentsForPost() + { + var postId = CreateTestPost("Test-Post-For-Comments"); + + //Create 3 Comments + var individualCommentsResponse1 = api.HelpCenter.Comments.CreateCommentForPost(postId, new Comment { Body = "Comment 1" }); + Assert.NotNull(individualCommentsResponse1.Comment); + Assert.Greater(individualCommentsResponse1.Comment.Id, 0); + + var individualCommentsResponse2 = api.HelpCenter.Comments.CreateCommentForPost(postId, new Comment { Body = "Comment 2" }); + Assert.NotNull(individualCommentsResponse2.Comment); + Assert.Greater(individualCommentsResponse2.Comment.Id, 0); + + var individualCommentsResponse3 = api.HelpCenter.Comments.CreateCommentForPost(postId, new Comment { Body = "Comment 3" }); + Assert.NotNull(individualCommentsResponse3.Comment); + Assert.Greater(individualCommentsResponse3.Comment.Id, 0); + + Assert.AreEqual(3, api.HelpCenter.Comments.GetCommentsForPost(postId).Count); //That post have 3 comments + + Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body); + Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body); + Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body); + + //Update Comment + var updatedCommentBody = "Comment 2 Updated"; + individualCommentsResponse2.Comment.Body = updatedCommentBody; + var updateCommentForPost = api.HelpCenter.Comments.UpdateCommentForPost(postId, individualCommentsResponse2.Comment); + Assert.AreEqual(updatedCommentBody, updateCommentForPost.Comment.Body); + + //Delete Comment + api.HelpCenter.Comments.DeleteCommentForPost(postId, individualCommentsResponse2.Comment.Id.Value); + Assert.AreEqual(2, api.HelpCenter.Comments.GetCommentsForPost(postId).Count); //One less comments now + } + + [Test] + public async Task CanCreateUpdateAndDeleteCommentsForArticleAsync() + { + var articleId = CreateTestArticle("Test-Article-For-Comments-Async"); + + //Create 3 Comments + var individualCommentsResponse1 = await api.HelpCenter.Comments.CreateCommentForArticleAsync(articleId, new Comment {Body = "Comment 1", Locale = "en-us"}); + Assert.NotNull(individualCommentsResponse1.Comment); + Assert.Greater(individualCommentsResponse1.Comment.Id, 0); + + var individualCommentsResponse2 = await api.HelpCenter.Comments.CreateCommentForArticleAsync(articleId, new Comment {Body = "Comment 2", Locale = "en-us" }); + Assert.NotNull(individualCommentsResponse2.Comment); + Assert.Greater(individualCommentsResponse2.Comment.Id, 0); + + var individualCommentsResponse3 = await api.HelpCenter.Comments.CreateCommentForArticleAsync(articleId, new Comment {Body = "Comment 3", Locale = "en-us" }); + Assert.NotNull(individualCommentsResponse3.Comment); + Assert.Greater(individualCommentsResponse3.Comment.Id, 0); + + Assert.AreEqual(3, (await api.HelpCenter.Comments.GetCommentsForArticleAsync(articleId)).Count); //That article have 3 comments + + Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body); + Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body); + Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body); + + //Update Comment + var updatedCommentBody = "Comment 2 Updated"; + individualCommentsResponse2.Comment.Body = updatedCommentBody; + var updateCommentForArticle = await api.HelpCenter.Comments.UpdateCommentForArticleAsync(articleId, individualCommentsResponse2.Comment); + Assert.AreEqual(updatedCommentBody, updateCommentForArticle.Comment.Body); + + //Delete Comment + await api.HelpCenter.Comments.DeleteCommentForArticleAsync(articleId, individualCommentsResponse2.Comment.Id.Value); + Assert.AreEqual(2, (await api.HelpCenter.Comments.GetCommentsForArticleAsync(articleId)).Count); //One less comments now + } + + [Test] + public async Task CanCreateUpdateAndDeleteCommentsForPostAsync() + { + var postId = CreateTestPost("Test-Post-For-Comments-Async"); + + //Create 3 Comments + var individualCommentsResponse1 = await api.HelpCenter.Comments.CreateCommentForPostAsync(postId, new Comment { Body = "Comment 1" }); + Assert.NotNull(individualCommentsResponse1.Comment); + Assert.Greater(individualCommentsResponse1.Comment.Id, 0); + + var individualCommentsResponse2 = await api.HelpCenter.Comments.CreateCommentForPostAsync(postId, new Comment { Body = "Comment 2" }); + Assert.NotNull(individualCommentsResponse2.Comment); + Assert.Greater(individualCommentsResponse2.Comment.Id, 0); + + var individualCommentsResponse3 = await api.HelpCenter.Comments.CreateCommentForPostAsync(postId, new Comment { Body = "Comment 3" }); + Assert.NotNull(individualCommentsResponse3.Comment); + Assert.Greater(individualCommentsResponse3.Comment.Id, 0); + + Assert.AreEqual(3, (await api.HelpCenter.Comments.GetCommentsForPostAsync(postId)).Count); //That post have 3 comments + + Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body); + Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body); + Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body); + + //Update Comment + var updatedCommentBody = "Comment 2 Updated"; + individualCommentsResponse2.Comment.Body = updatedCommentBody; + var updateCommentForPost = await api.HelpCenter.Comments.UpdateCommentForPostAsync(postId, individualCommentsResponse2.Comment); + Assert.AreEqual(updatedCommentBody, updateCommentForPost.Comment.Body); + + //Delete Comment + await api.HelpCenter.Comments.DeleteCommentForPostAsync(postId, individualCommentsResponse2.Comment.Id.Value); + Assert.AreEqual(2, (await api.HelpCenter.Comments.GetCommentsForPostAsync(postId)).Count); //One less comments now + } + + private long CreateTestArticle(string testArticleForComments) + { + var article = new Article {Title = testArticleForComments, Body = "Test"}; + + var individualArticleResponse = api.HelpCenter.Articles.CreateArticle(_testSectionIdForCommentsTest, article); + return individualArticleResponse.Article.Id.Value; + } + + private long CreateTestPost(string testPostForComments) + { + var post = new Post {Title = testPostForComments, TopicId = _testTopicIdForCommentsTest}; + + var individualArticleResponse = api.HelpCenter.Posts.CreatePost(post); + return individualArticleResponse.Post.Id.Value; + } + } +} \ No newline at end of file