diff --git a/Tests/Fluent.Tests/PagedCollectionTests.cs b/Tests/Fluent.Tests/PagedCollectionTests.cs new file mode 100644 index 000000000..f436e011e --- /dev/null +++ b/Tests/Fluent.Tests/PagedCollectionTests.cs @@ -0,0 +1,62 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using Microsoft.Azure.KeyVault.Models; +using Microsoft.Azure.Management.ResourceManager.Fluent.Core; +using Microsoft.Rest.Azure; +using Newtonsoft.Json; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace Fluent.Tests +{ + // For serialzation to Page + [JsonObject] + public class MockPage + { + [JsonProperty("nextLink")] + public string NextPageLink { get; set; } + + [JsonProperty("value")] + public IList Items { get; set; } + } + + public class PagedCollection + { + [Fact] + public void CanLoadEmptyPageWithNextLink() + { + var taskLoadPage = PagedCollection.LoadPage( + // first page, valid nextLink + (cancellation) => Task.FromResult(ConvertToPage(new MockPage { Items = new List { "1", "2" }, NextPageLink = "2" })), + (nextLink, cancellation) => + { + if (nextLink == "2") + { + // empty values, valid nextLink + return Task.FromResult(ConvertToPage(new MockPage { Items = new List(), NextPageLink = "3" })); + } + else if (nextLink == "3") + { + // non-empty values, null nextLink + return Task.FromResult(ConvertToPage(new MockPage { Items = new List { "3", "4", "5" }, NextPageLink = null })); + } + else + { + Assert.False(true); + return Task.FromResult(ConvertToPage(new MockPage { Items = new List (), NextPageLink = null })); + } + }, + (str) => str, true, CancellationToken.None); + + Assert.Equal(new List { "1", "2", "3", "4", "5" }, taskLoadPage.Result); + } + + private static IPage ConvertToPage(MockPage mockPage) + { + return Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject>(Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(mockPage)); + } + } +} diff --git a/src/ResourceManagement/ResourceManager/Core/PagedCollection.cs b/src/ResourceManagement/ResourceManager/Core/PagedCollection.cs index 1ebfb93ee..9432b1a12 100644 --- a/src/ResourceManagement/ResourceManager/Core/PagedCollection.cs +++ b/src/ResourceManagement/ResourceManager/Core/PagedCollection.cs @@ -118,11 +118,14 @@ IEnumerator IEnumerable.GetEnumerator() private async Task AddCollection(IPage currentPage, PagedCollection pagedCollection, CancellationToken cancellationToken) { - if (currentPage != null && currentPage.Any()) + if (currentPage != null) { pagedCollection.NextPageLink = currentPage.NextPageLink; - var resources = await Task.WhenAll(currentPage.Select(async (inner) => await this.WrapModelAsyncDelegate(inner, cancellationToken))); - ((List)pagedCollection.innerCollection).AddRange(resources); + if (currentPage.Any()) + { + var resources = await Task.WhenAll(currentPage.Select(async (inner) => await this.WrapModelAsyncDelegate(inner, cancellationToken))); + ((List)pagedCollection.innerCollection).AddRange(resources); + } } }