diff --git a/src/Catalog/CatalogIndexEntry.cs b/src/Catalog/CatalogIndexEntry.cs index 901cf0624..c973466f7 100644 --- a/src/Catalog/CatalogIndexEntry.cs +++ b/src/Catalog/CatalogIndexEntry.cs @@ -69,7 +69,13 @@ public CatalogIndexEntry( public NuGetVersion Version { get; private set; } [JsonIgnore] - public bool IsDelete { get; private set; } + public bool IsDelete + { + get + { + return Types.Any(type => type == "nuget:PackageDelete" || type == "PackageDelete" || type == Schema.DataTypes.PackageDelete.ToString()); + } + } public int CompareTo(CatalogIndexEntry other) { @@ -116,7 +122,6 @@ private void Initialize( } Types = types; - IsDelete = types.Any(type => type == "nuget:PackageDelete" || type == "PackageDelete" || type == Schema.DataTypes.PackageDelete.ToString()); if (string.IsNullOrWhiteSpace(commitId)) { diff --git a/src/NuGet.Services.Metadata.Catalog.Monitoring/Model/PackageTimestampMetadata.cs b/src/NuGet.Services.Metadata.Catalog.Monitoring/Model/PackageTimestampMetadata.cs index f1943d6f9..6601e182e 100644 --- a/src/NuGet.Services.Metadata.Catalog.Monitoring/Model/PackageTimestampMetadata.cs +++ b/src/NuGet.Services.Metadata.Catalog.Monitoring/Model/PackageTimestampMetadata.cs @@ -53,24 +53,25 @@ public static PackageTimestampMetadata CreateForPackageMissingFromFeed(DateTime? }; } - public static async Task FromCatalogEntry(CollectorHttpClient client, + public static async Task FromCatalogEntry( + CollectorHttpClient client, CatalogIndexEntry catalogEntry) { - var catalogPage = await client.GetJObjectAsync(catalogEntry.Uri); + var catalogLeaf = await client.GetJObjectAsync(catalogEntry.Uri); try { if (catalogEntry.IsDelete) { // On the catalog page for a delete, the published value is the timestamp the package was deleted from the audit records. - var deleted = catalogPage.GetValue("published").Value(); + var deleted = catalogLeaf.GetValue("published").Value(); return CreateForPackageMissingFromFeed(deleted.DateTime); } else { - var created = catalogPage.GetValue("created").Value(); - var lastEdited = catalogPage.GetValue("lastEdited").Value(); + var created = catalogLeaf.GetValue("created").Value(); + var lastEdited = catalogLeaf.GetValue("lastEdited").Value(); return CreateForPackageExistingOnFeed(created.DateTime, lastEdited.DateTime); } @@ -81,7 +82,8 @@ public static async Task FromCatalogEntry(CollectorHtt } } - public static async Task FromCatalogEntries(CollectorHttpClient client, + public static async Task FromCatalogEntries( + CollectorHttpClient client, IEnumerable catalogEntries) { var packageTimestampMetadatas = @@ -90,4 +92,4 @@ public static async Task FromCatalogEntries(CollectorH return packageTimestampMetadatas.FirstOrDefault(p => p.Last == maxTimestamp); } } -} +} \ No newline at end of file diff --git a/tests/CatalogTests/CatalogIndexEntryTests.cs b/tests/CatalogTests/CatalogIndexEntryTests.cs index eb459c794..f9fb44c60 100644 --- a/tests/CatalogTests/CatalogIndexEntryTests.cs +++ b/tests/CatalogTests/CatalogIndexEntryTests.cs @@ -277,7 +277,7 @@ public void JsonSerialization_ReturnsCorrectJson() _commitTimeStamp, _packageIdentity); - var jObject = CreateCatalogIndexJObject(); + var jObject = CreateCatalogIndexJObject(CatalogConstants.NuGetPackageDetails); var expectedResult = jObject.ToString(Formatting.None, _settings.Converters.ToArray()); var actualResult = JsonConvert.SerializeObject(entry, Formatting.None, _settings); @@ -294,7 +294,7 @@ public void JsonSerialization_ReturnsCorrectJson() [InlineData(CatalogConstants.NuGetVersion)] public void JsonDeserialization_WhenRequiredPropertyIsMissing_Throws(string propertyToRemove) { - var jObject = CreateCatalogIndexJObject(); + var jObject = CreateCatalogIndexJObject(CatalogConstants.NuGetPackageDetails); jObject.Remove(propertyToRemove); @@ -307,28 +307,49 @@ public void JsonDeserialization_WhenRequiredPropertyIsMissing_Throws(string prop } [Fact] - public void JsonDeserialization_ReturnsCorrectObject() + public void JsonDeserialization_WhenTypeIsPackageDetails_ReturnsCorrectObject() { - var jObject = CreateCatalogIndexJObject(); + var jObject = CreateCatalogIndexJObject(CatalogConstants.NuGetPackageDetails); var json = jObject.ToString(Formatting.None, _settings.Converters.ToArray()); var entry = JsonConvert.DeserializeObject(json, _settings); Assert.Equal(_uri.AbsoluteUri, entry.Uri.AbsoluteUri); Assert.Equal(CatalogConstants.NuGetPackageDetails, entry.Types.Single()); + Assert.False(entry.IsDelete); + Assert.Equal(_commitId, entry.CommitId); + Assert.Equal(_commitTimeStamp, entry.CommitTimeStamp); + Assert.Equal(_packageId, entry.Id); + Assert.Equal(_packageVersion, entry.Version); + } + + [Theory] + [InlineData(CatalogConstants.NuGetPackageDelete)] + [InlineData(CatalogConstants.PackageDelete)] + [InlineData("http://schema.nuget.org/schema#PackageDelete")] + public void JsonDeserialization_WhenTypeIsPackageDelete_ReturnsCorrectObject(string type) + { + var jObject = CreateCatalogIndexJObject(type); + var json = jObject.ToString(Formatting.None, _settings.Converters.ToArray()); + + var entry = JsonConvert.DeserializeObject(json, _settings); + + Assert.Equal(_uri.AbsoluteUri, entry.Uri.AbsoluteUri); + Assert.Equal(type, entry.Types.Single()); + Assert.True(entry.IsDelete); Assert.Equal(_commitId, entry.CommitId); Assert.Equal(_commitTimeStamp, entry.CommitTimeStamp); Assert.Equal(_packageId, entry.Id); Assert.Equal(_packageVersion, entry.Version); } - private JObject CreateCatalogIndexJObject(string commitTimeStamp = null) + private JObject CreateCatalogIndexJObject(string type) { return new JObject( new JProperty(CatalogConstants.IdKeyword, _uri), - new JProperty(CatalogConstants.TypeKeyword, CatalogConstants.NuGetPackageDetails), + new JProperty(CatalogConstants.TypeKeyword, type), new JProperty(CatalogConstants.CommitId, _commitId), - new JProperty(CatalogConstants.CommitTimeStamp, commitTimeStamp ?? _commitTimeStamp.ToString(CatalogConstants.CommitTimeStampFormat)), + new JProperty(CatalogConstants.CommitTimeStamp, _commitTimeStamp.ToString(CatalogConstants.CommitTimeStampFormat)), new JProperty(CatalogConstants.NuGetId, _packageId), new JProperty(CatalogConstants.NuGetVersion, _packageVersion.ToNormalizedString())); }