From 59b33280d1d061901a00af0d869eb8022d76fe74 Mon Sep 17 00:00:00 2001 From: ClaudineL <50966943+ClaudineL@users.noreply.github.com> Date: Tue, 19 May 2020 11:29:52 -0400 Subject: [PATCH] Paginate GetSections call (#478) * Changed signature and code of GetSections to use GenericPagedGet * Converted paged signature to overload, added same code and test for GetCategories * Added Async methods for GetSections and GetCategories + unit tests, changed Assert syntax --- .../Requests/HelpCenter/Categories.cs | 14 ++- .../Requests/HelpCenter/Sections.cs | 13 +++ .../HelpCenter/CategoryTests.cs | 95 +++++++++++++++++-- .../HelpCenter/SectionTests.cs | 86 +++++++++++++++++ 4 files changed, 200 insertions(+), 8 deletions(-) diff --git a/src/ZendeskApi_v2/Requests/HelpCenter/Categories.cs b/src/ZendeskApi_v2/Requests/HelpCenter/Categories.cs index a3d74deb..2eff06a8 100644 --- a/src/ZendeskApi_v2/Requests/HelpCenter/Categories.cs +++ b/src/ZendeskApi_v2/Requests/HelpCenter/Categories.cs @@ -11,6 +11,7 @@ public interface ICategories : ICore { #if SYNC GroupCategoryResponse GetCategories(); + GroupCategoryResponse GetCategories(int perPage, int page); IndividualCategoryResponse GetCategoryById(long id); IndividualCategoryResponse CreateCategory(Category category); IndividualCategoryResponse UpdateCategory(Category category); @@ -19,6 +20,7 @@ public interface ICategories : ICore #if ASYNC Task GetCategoriesAsync(); + Task GetCategoriesAsync(int perPage, int page); Task GetCategoryByIdAsync(long id); Task CreateCategoryAsync(Category category); Task UpdateCategoryAsync(Category category); @@ -46,7 +48,12 @@ public GroupCategoryResponse GetCategories() return GenericGet($"{GeneralCategoriesPath}.json"); } - public IndividualCategoryResponse GetCategoryById(long id) + public GroupCategoryResponse GetCategories(int perPage, int page) + { + return GenericPagedGet($"{GeneralCategoriesPath}.json", perPage, page); + } + + public IndividualCategoryResponse GetCategoryById(long id) { return GenericGet($"{GeneralCategoriesPath}/{id}.json"); } @@ -81,6 +88,11 @@ public async Task GetCategoriesAsync() return await GenericGetAsync($"{GeneralCategoriesPath}.json"); } + public async Task GetCategoriesAsync(int perPage, int page) + { + return await GenericPagedGetAsync($"{GeneralCategoriesPath}.json", perPage, page); + } + public async Task GetCategoryByIdAsync(long id) { return await GenericGetAsync($"{GeneralCategoriesPath}/{id}.json"); diff --git a/src/ZendeskApi_v2/Requests/HelpCenter/Sections.cs b/src/ZendeskApi_v2/Requests/HelpCenter/Sections.cs index 10dd92df..17c7eb93 100644 --- a/src/ZendeskApi_v2/Requests/HelpCenter/Sections.cs +++ b/src/ZendeskApi_v2/Requests/HelpCenter/Sections.cs @@ -11,6 +11,7 @@ public interface ISections : ICore { #if SYNC GroupSectionResponse GetSections(); + GroupSectionResponse GetSections(int perPage, int page); GroupSectionResponse GetSectionsByCategoryId(long categoryId); IndividualSectionResponse GetSectionById(long id); IndividualSectionResponse CreateSection(Section section); @@ -24,6 +25,7 @@ public interface ISections : ICore #if ASYNC Task GetSectionsAsync(); + Task GetSectionsAsync(int perPage, int page); Task GetSectionsByCategoryIdAsync(long categoryId); Task GetSectionByIdAsync(long id); Task CreateSectionAsync(Section section); @@ -53,6 +55,12 @@ public GroupSectionResponse GetSections() { return GenericGet($"{_generalSectionsPath}.json?include=access_policies"); } + + public GroupSectionResponse GetSections(int perPage, int page) + { + return GenericPagedGet($"{_generalSectionsPath}.json?include=access_policies", perPage, page); + } + public GroupSectionResponse GetSectionsByCategoryId(long categoryId) { return GenericGet(GetSectionPathWithCategory(categoryId)); @@ -105,6 +113,11 @@ public async Task GetSectionsAsync() return await GenericGetAsync($"{_generalSectionsPath}.json?include=access_policies"); } + public async Task GetSectionsAsync(int perPage, int page) + { + return await GenericPagedGetAsync($"{_generalSectionsPath}.json?include=access_policies", perPage, page); + } + public async Task GetSectionsByCategoryIdAsync(long categoryId) { return await GenericGetAsync(GetSectionPathWithCategory(categoryId)); diff --git a/test/ZendeskApi_v2.Test/HelpCenter/CategoryTests.cs b/test/ZendeskApi_v2.Test/HelpCenter/CategoryTests.cs index 760211f0..a4401943 100644 --- a/test/ZendeskApi_v2.Test/HelpCenter/CategoryTests.cs +++ b/test/ZendeskApi_v2.Test/HelpCenter/CategoryTests.cs @@ -1,6 +1,8 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using NUnit.Framework; using ZendeskApi_v2; +using ZendeskApi_v2.Extensions; using ZendeskApi_v2.Models.HelpCenter.Categories; namespace Tests.HelpCenter @@ -15,11 +17,11 @@ public class CategoryTests [OneTimeSetUp] public async Task Setup() { - var categotriesResponse = await api.HelpCenter.Categories.GetCategoriesAsync(); + var categoriesResponse = await api.HelpCenter.Categories.GetCategoriesAsync(); do { - foreach (var category in categotriesResponse.Categories) + foreach (var category in categoriesResponse.Categories) { if (category.Name == "My Test category") { @@ -27,12 +29,12 @@ public async Task Setup() } } - if (!string.IsNullOrWhiteSpace(categotriesResponse.NextPage)) + if (!string.IsNullOrWhiteSpace(categoriesResponse.NextPage)) { - categotriesResponse = await api.HelpCenter.Articles.GetByPageUrlAsync(categotriesResponse.NextPage, 100); + categoriesResponse = await api.HelpCenter.Articles.GetByPageUrlAsync(categoriesResponse.NextPage, 100); } - } while (!string.IsNullOrWhiteSpace(categotriesResponse.NextPage)); + } while (!string.IsNullOrWhiteSpace(categoriesResponse.NextPage)); } @@ -46,6 +48,85 @@ public void CanGetCategories() Assert.AreEqual(res1.Category.Id, res.Categories[0].Id.Value); } + [Test] + public void CanGetCategoriesPaged() + { + var category1 = api.HelpCenter.Categories.CreateCategory(new Category { + Name = "My Test category 1", + Position = 0, + Description = "First category" + }); + + var category2 = api.HelpCenter.Categories.CreateCategory(new Category { + Name = "My Test category 2", + Position = 0, + Description = "Second category" + }); + + const int count = 2; + var categories = api.HelpCenter.Categories.GetCategories(count, 1); + + Assert.That(count, Is.EqualTo(categories.Categories.Count)); // 2 + Assert.That(categories.Count, Is.Not.EqualTo(categories.Categories.Count)); // 2 != total count of categories (assumption) + + const int page = 2; + var secondPage = api.HelpCenter.Categories.GetCategories(count, page); + + Assert.That(count, Is.EqualTo(secondPage.Categories.Count)); + + var nextPage = secondPage.NextPage.GetQueryStringDict() + .Where(x => x.Key == "page") + .Select(x => x.Value) + .FirstOrDefault(); + + Assert.That(nextPage, Is.Not.Null); + Assert.That(nextPage, Is.EqualTo((page + 1).ToString())); + Assert.That(api.HelpCenter.Categories.DeleteCategory(category1.Category.Id.Value), Is.True); + Assert.That(api.HelpCenter.Categories.DeleteCategory(category2.Category.Id.Value), Is.True); + } + + [Test] + public void CanGetCategoriesPagedAsync() + { + var category1 = api.HelpCenter.Categories.CreateCategory(new Category { + Name = "My Test category 1", + Position = 0, + Description = "First category" + }); + + var category2 = api.HelpCenter.Categories.CreateCategory(new Category { + Name = "My Test category 2", + Position = 0, + Description = "Second category" + }); + + const int count = 2; + var categoriesAsync = api.HelpCenter.Categories.GetCategoriesAsync(count, 1).Result; + + Assert.That(categoriesAsync.Count, Is.GreaterThan(0)); + + var categoryById1 = api.HelpCenter.Categories.GetCategoryById(categoriesAsync.Categories[0].Id.Value); + + Assert.That(categoryById1.Category.Id, Is.EqualTo(categoriesAsync.Categories[0].Id.Value)); + + const int page = 2; + var secondPage = api.HelpCenter.Categories.GetCategoriesAsync(count, page).Result; + var categoryById2 = api.HelpCenter.Categories.GetCategoryById(secondPage.Categories[0].Id.Value); + + Assert.That(count, Is.EqualTo(secondPage.Categories.Count)); + Assert.That(categoryById2.Category.Id, Is.EqualTo(secondPage.Categories[0].Id.Value)); + + var nextPage = secondPage.NextPage.GetQueryStringDict() + .Where(x => x.Key == "page") + .Select(x => x.Value) + .FirstOrDefault(); + + Assert.That(nextPage, Is.Not.Null); + Assert.That(nextPage, Is.EqualTo((page + 1).ToString())); + Assert.That(api.HelpCenter.Categories.DeleteCategory(category1.Category.Id.Value), Is.True); + Assert.That(api.HelpCenter.Categories.DeleteCategory(category2.Category.Id.Value), Is.True); + } + [Test] public void CanCreateUpdateAndDeleteCategories() { @@ -65,4 +146,4 @@ public void CanCreateUpdateAndDeleteCategories() Assert.True(api.HelpCenter.Categories.DeleteCategory(res.Category.Id.Value)); } } -} \ No newline at end of file +} diff --git a/test/ZendeskApi_v2.Test/HelpCenter/SectionTests.cs b/test/ZendeskApi_v2.Test/HelpCenter/SectionTests.cs index 49c925ea..07e9df84 100644 --- a/test/ZendeskApi_v2.Test/HelpCenter/SectionTests.cs +++ b/test/ZendeskApi_v2.Test/HelpCenter/SectionTests.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using NUnit.Framework; using ZendeskApi_v2; +using ZendeskApi_v2.Extensions; using ZendeskApi_v2.Models.Sections; namespace Tests.HelpCenter @@ -45,6 +46,91 @@ public void CanGetSections() Assert.AreEqual(res1.Section.Id, res.Sections[0].Id.Value); } + [Test] + public void CanGetSectionsPaged() + { + //https://csharpapi.zendesk.com/hc/en-us/categories/200382245-Category-1 + long category_id = 200382245; + + var section1 = api.HelpCenter.Sections.CreateSection(new Section { + Name = "My Test section 1", + Position = 0, + CategoryId = category_id + }); + + var section2 = api.HelpCenter.Sections.CreateSection(new Section { + Name = "My Test section 2", + Position = 0, + CategoryId = category_id + }); + + const int count = 2; + var sections = api.HelpCenter.Sections.GetSections(count, 1); + + Assert.That(count, Is.EqualTo(sections.Sections.Count)); // 2 + Assert.That(sections.Count, Is.Not.EqualTo(sections.Sections.Count)); // 2 != total count of sections (assumption) + + const int page = 2; + var secondPage = api.HelpCenter.Sections.GetSections(count, page); + + Assert.That(count, Is.EqualTo(secondPage.Sections.Count)); + + var nextPage = secondPage.NextPage.GetQueryStringDict() + .Where(x => x.Key == "page") + .Select(x => x.Value) + .FirstOrDefault(); + + Assert.That(nextPage, Is.Not.Null); + Assert.That(nextPage, Is.EqualTo((page + 1).ToString())); + Assert.That(api.HelpCenter.Sections.DeleteSection(section1.Section.Id.Value), Is.True); + Assert.That(api.HelpCenter.Sections.DeleteSection(section2.Section.Id.Value), Is.True); + } + + [Test] + public void CanGetSectionsPagedAsync() + { + //https://csharpapi.zendesk.com/hc/en-us/categories/200382245-Category-1 + long category_id = 200382245; + + var section1 = api.HelpCenter.Sections.CreateSection(new Section { + Name = "My Test section 1", + Position = 0, + CategoryId = category_id + }); + + var section2 = api.HelpCenter.Sections.CreateSection(new Section { + Name = "My Test section 2", + Position = 0, + CategoryId = category_id + }); + + const int count = 2; + var sectionsAsync = api.HelpCenter.Sections.GetSectionsAsync(count, 1).Result; + + Assert.That(sectionsAsync.Count, Is.GreaterThan(0)); + + var sectionById1 = api.HelpCenter.Sections.GetSectionById(sectionsAsync.Sections[0].Id.Value); + + Assert.That(sectionById1.Section.Id, Is.EqualTo(sectionsAsync.Sections[0].Id.Value)); + + const int page = 2; + var secondPage = api.HelpCenter.Sections.GetSectionsAsync(count, page).Result; + var sectionById2 = api.HelpCenter.Sections.GetSectionById(secondPage.Sections[0].Id.Value); + + Assert.That(count, Is.EqualTo(secondPage.Sections.Count)); + Assert.That(sectionById2.Section.Id, Is.EqualTo(secondPage.Sections[0].Id.Value)); + + var nextPage = secondPage.NextPage.GetQueryStringDict() + .Where(x => x.Key == "page") + .Select(x => x.Value) + .FirstOrDefault(); + + Assert.That(nextPage, Is.Not.Null); + Assert.That(nextPage, Is.EqualTo((page + 1).ToString())); + Assert.That(api.HelpCenter.Sections.DeleteSection(section1.Section.Id.Value), Is.True); + Assert.That(api.HelpCenter.Sections.DeleteSection(section2.Section.Id.Value), Is.True); + } + [Test] public void CanCreateUpdateAndDeleteSections() {