Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
V3: fix missing initialization of IsDelete (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtivel authored Nov 21, 2018
1 parent ae0d22f commit 49ed106
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
9 changes: 7 additions & 2 deletions src/Catalog/CatalogIndexEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,25 @@ public static PackageTimestampMetadata CreateForPackageMissingFromFeed(DateTime?
};
}

public static async Task<PackageTimestampMetadata> FromCatalogEntry(CollectorHttpClient client,
public static async Task<PackageTimestampMetadata> 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<DateTimeOffset>();
var deleted = catalogLeaf.GetValue("published").Value<DateTimeOffset>();

return CreateForPackageMissingFromFeed(deleted.DateTime);
}
else
{
var created = catalogPage.GetValue("created").Value<DateTimeOffset>();
var lastEdited = catalogPage.GetValue("lastEdited").Value<DateTimeOffset>();
var created = catalogLeaf.GetValue("created").Value<DateTimeOffset>();
var lastEdited = catalogLeaf.GetValue("lastEdited").Value<DateTimeOffset>();

return CreateForPackageExistingOnFeed(created.DateTime, lastEdited.DateTime);
}
Expand All @@ -81,7 +82,8 @@ public static async Task<PackageTimestampMetadata> FromCatalogEntry(CollectorHtt
}
}

public static async Task<PackageTimestampMetadata> FromCatalogEntries(CollectorHttpClient client,
public static async Task<PackageTimestampMetadata> FromCatalogEntries(
CollectorHttpClient client,
IEnumerable<CatalogIndexEntry> catalogEntries)
{
var packageTimestampMetadatas =
Expand All @@ -90,4 +92,4 @@ public static async Task<PackageTimestampMetadata> FromCatalogEntries(CollectorH
return packageTimestampMetadatas.FirstOrDefault(p => p.Last == maxTimestamp);
}
}
}
}
35 changes: 28 additions & 7 deletions tests/CatalogTests/CatalogIndexEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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<CatalogIndexEntry>(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<CatalogIndexEntry>(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()));
}
Expand Down

0 comments on commit 49ed106

Please sign in to comment.