public async Task DownloadMetadata2CatalogAsync_WhenCreatedPackagesIsTrue_WithNoPackages_ReturnsLastCreated()
        {
            using (var test = new DownloadMetadata2CatalogAsyncTest())
            {
                test.CreatedPackages = true;

                var result = await test.DownloadMetadata2CatalogAsync();

                Assert.Equal(test.LastCreated, result);
            }
        }
        public async Task DownloadMetadata2CatalogAsync_WhenCreatedPackagesIsNull_WithNoPackages_ReturnsDateTimeMinValue()
        {
            using (var test = new DownloadMetadata2CatalogAsyncTest())
            {
                test.CreatedPackages = null;

                var result = await test.DownloadMetadata2CatalogAsync();

                Assert.Equal(DateTime.MinValue, result);
            }
        }
        public async Task DownloadMetadata2CatalogAsync_WithOnePackage_UpdatesStorage(bool createdPackages, bool updateCreatedFromEdited)
        {
            using (var test = new DownloadMetadata2CatalogAsyncTest())
            {
                test.CreatedPackages         = createdPackages;
                test.UpdateCreatedFromEdited = updateCreatedFromEdited;
                test.Packages.Add(test.UtcNow, new List <FeedPackageDetails>()
                {
                    test.FeedPackageDetails
                });

                NupkgMetadata nupkgMetadata = GetNupkgMetadata("Newtonsoft.Json.9.0.2-beta1.nupkg");

                var packageCatalogItem = new PackageCatalogItem(
                    nupkgMetadata,
                    test.FeedPackageDetails.CreatedDate,
                    test.FeedPackageDetails.LastEditedDate,
                    test.FeedPackageDetails.PublishedDate);

                test.PackageCatalogItemCreator.Setup(x => x.CreateAsync(
                                                         It.Is <FeedPackageDetails>(details => details == test.FeedPackageDetails),
                                                         It.Is <DateTime>(timestamp => timestamp == test.UtcNow),
                                                         It.IsAny <CancellationToken>()))
                .ReturnsAsync(packageCatalogItem);

                test.Storage.SetupGet(x => x.BaseAddress)
                .Returns(test.CatalogBaseUri);

                var blobs = new List <CatalogBlob>();

                test.Storage.Setup(x => x.SaveAsync(
                                       It.IsNotNull <Uri>(),
                                       It.IsNotNull <StorageContent>(),
                                       It.IsAny <CancellationToken>()))
                .Callback <Uri, StorageContent, CancellationToken>((uri, content, token) =>
                {
                    blobs.Add(new CatalogBlob(uri, content));
                })
                .Returns(Task.FromResult(0));

                test.Storage.Setup(x => x.LoadStringAsync(
                                       It.Is <Uri>(uri => uri == test.CatalogIndexUri),
                                       It.IsAny <CancellationToken>()))
                .ReturnsAsync(CatalogTestData.GetBeforeIndex(test.CatalogIndexUri).ToString());

                test.TelemetryService.Setup(x => x.TrackCatalogIndexWriteDuration(
                                                It.Is <TimeSpan>(duration => duration > TimeSpan.Zero),
                                                It.Is <Uri>(uri => uri == test.CatalogIndexUri)));

                var result = await test.DownloadMetadata2CatalogAsync();

                Assert.Equal(test.UtcNow, result);

                Assert.Equal(3, blobs.Count);

                var catalogPackageDetailsUri = new Uri($"{test.CatalogBaseUri}data/{packageCatalogItem.TimeStamp.ToString("yyyy.MM.dd.HH.mm.ss")}/newtonsoft.json.9.0.2-beta1.json");
                var catalogPageUri           = new Uri($"{test.CatalogBaseUri}page0.json");

                // Verify package details blob
                Assert.Equal(catalogPackageDetailsUri, blobs[0].Uri);
                Assert.IsType <StringStorageContent>(blobs[0].Content);

                var stringContent = (StringStorageContent)blobs[0].Content;

                Assert.Equal("no-store", stringContent.CacheControl);
                Assert.Equal("application/json", stringContent.ContentType);

                var expectedContent = CatalogTestData.GetPackageDetails(
                    catalogPackageDetailsUri,
                    packageCatalogItem.CommitId,
                    packageCatalogItem.TimeStamp,
                    packageCatalogItem.CreatedDate.Value,
                    packageCatalogItem.LastEditedDate.Value,
                    packageCatalogItem.PublishedDate.Value);
                var actualContent = JObject.Parse(stringContent.Content);

                Assert.Equal(expectedContent.ToString(), actualContent.ToString());

                // Verify page blob
                Assert.Equal(catalogPageUri, blobs[1].Uri);
                Assert.IsType <JTokenStorageContent>(blobs[1].Content);

                var jtokenContent = (JTokenStorageContent)blobs[1].Content;

                expectedContent = CatalogTestData.GetPage(
                    catalogPageUri,
                    packageCatalogItem.CommitId,
                    packageCatalogItem.TimeStamp,
                    test.CatalogIndexUri,
                    catalogPackageDetailsUri);

                Assert.Equal("no-store", jtokenContent.CacheControl);
                Assert.Equal("application/json", jtokenContent.ContentType);
                Assert.Equal(expectedContent.ToString(), jtokenContent.Content.ToString());

                // Verify index blob
                Assert.Equal(test.CatalogIndexUri, blobs[2].Uri);
                Assert.IsType <JTokenStorageContent>(blobs[2].Content);

                jtokenContent = (JTokenStorageContent)blobs[2].Content;

                var lastEdited  = createdPackages ? test.LastEdited : test.UtcNow;
                var lastCreated = updateCreatedFromEdited ? lastEdited : (createdPackages ? test.UtcNow : test.LastCreated);

                expectedContent = CatalogTestData.GetAfterIndex(
                    test.CatalogIndexUri,
                    packageCatalogItem.CommitId,
                    packageCatalogItem.TimeStamp,
                    lastCreated,
                    test.LastDeleted,
                    lastEdited,
                    catalogPageUri);

                Assert.Equal("no-store", jtokenContent.CacheControl);
                Assert.Equal("application/json", jtokenContent.ContentType);
                Assert.Equal(expectedContent.ToString(), jtokenContent.Content.ToString());
            }
        }