-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...NuGet.Core.Tests/NuGet.Protocol.Tests/Providers/ReadmeUriTemplateResourceProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Moq; | ||
using Newtonsoft.Json.Linq; | ||
using NuGet.Configuration; | ||
using NuGet.Protocol.Core.Types; | ||
using NuGet.Versioning; | ||
using Xunit; | ||
|
||
namespace NuGet.Protocol.Tests.Providers | ||
{ | ||
public class ReadmeUriTemplateResourceProviderTests | ||
{ | ||
private const string ResourceType = "ReadmeUriTemplate/6.13.0"; | ||
|
||
private readonly PackageSource _packageSource; | ||
private readonly ReadmeUriTemplateResourceProvider _target; | ||
|
||
public ReadmeUriTemplateResourceProviderTests() | ||
{ | ||
_packageSource = new PackageSource("https://unit.test"); | ||
_target = new ReadmeUriTemplateResourceProvider(); | ||
} | ||
|
||
[Fact] | ||
public async Task TryCreate_WhenResourceDoesNotExist_ReturnsNull() | ||
{ | ||
var resourceProviders = new ResourceProvider[] | ||
{ | ||
CreateServiceIndexResourceV3Provider(), | ||
_target | ||
}; | ||
var sourceRepository = new SourceRepository(_packageSource, resourceProviders); | ||
|
||
Tuple<bool, INuGetResource> result = await _target.TryCreate(sourceRepository, CancellationToken.None); | ||
|
||
Assert.False(result.Item1); | ||
Assert.Null(result.Item2); | ||
} | ||
|
||
[Fact] | ||
public async Task TryCreate_WhenResourceExists_ReturnsValidResourceAsync() | ||
{ | ||
var serviceEntry = new RawServiceIndexEntry("https://unit.test/packages/{lower_id}/{lower_version}/readme", ResourceType); | ||
var resourceProviders = new ResourceProvider[] | ||
{ | ||
CreateServiceIndexResourceV3Provider(serviceEntry), | ||
_target | ||
}; | ||
var sourceRepository = new SourceRepository(_packageSource, resourceProviders); | ||
|
||
Tuple<bool, INuGetResource> result = await _target.TryCreate(sourceRepository, CancellationToken.None); | ||
|
||
Assert.True(result.Item1); | ||
Assert.IsType<ReadmeUriTemplateResource>(result.Item2); | ||
Assert.Equal( | ||
"https://unit.test/packages/mypackage/1.0.0/readme", | ||
((ReadmeUriTemplateResource)result.Item2).GetReadmeUrl("MyPackage", NuGetVersion.Parse("1.0.0"))); | ||
} | ||
|
||
private static ServiceIndexResourceV3Provider CreateServiceIndexResourceV3Provider(params RawServiceIndexEntry[] entries) | ||
{ | ||
var provider = new Mock<ServiceIndexResourceV3Provider>(); | ||
|
||
provider.Setup(x => x.Name) | ||
.Returns(nameof(ServiceIndexResourceV3Provider)); | ||
provider.Setup(x => x.ResourceType) | ||
.Returns(typeof(ServiceIndexResourceV3)); | ||
|
||
var resources = new JArray(); | ||
|
||
foreach (var entry in entries) | ||
{ | ||
resources.Add( | ||
new JObject( | ||
new JProperty("@id", entry.Uri), | ||
new JProperty("@type", entry.Type))); | ||
} | ||
|
||
var index = new JObject(); | ||
|
||
index.Add("version", "3.0.0"); | ||
index.Add("resources", resources); | ||
index.Add("@context", | ||
new JObject( | ||
new JProperty("@vocab", "http://schema.nuget.org/schema#"), | ||
new JProperty("comment", "http://www.w3.org/2000/01/rdf-schema#comment"))); | ||
|
||
var serviceIndexResource = new ServiceIndexResourceV3(index, DateTime.UtcNow); | ||
var tryCreateResult = new Tuple<bool, INuGetResource>(true, serviceIndexResource); | ||
|
||
provider.Setup(x => x.TryCreate(It.IsAny<SourceRepository>(), It.IsAny<CancellationToken>())) | ||
.Returns(Task.FromResult(tryCreateResult)); | ||
|
||
return provider.Object; | ||
} | ||
|
||
private class RawServiceIndexEntry | ||
{ | ||
public RawServiceIndexEntry(string uri, string type) | ||
{ | ||
Uri = uri; | ||
Type = type ?? throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
public string Uri { get; } | ||
public string Type { get; } | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
test/NuGet.Core.Tests/NuGet.Protocol.Tests/ReadmeUriTemplateResourceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using NuGet.Versioning; | ||
using Xunit; | ||
|
||
namespace NuGet.Protocol.Tests | ||
{ | ||
public class ReadmeUriTemplateResourceTests | ||
{ | ||
[Theory] | ||
[InlineData("")] | ||
[InlineData(null)] | ||
public void ReadmeUriTemplateResource_GetReadmeUrl_BlankTemplate_ReturnsEmptyString(string uriTemplate) | ||
{ | ||
string expectedResult = string.Empty; | ||
var resource = new ReadmeUriTemplateResource(uriTemplate); | ||
|
||
var actual = resource.GetReadmeUrl("TestPackage", NuGetVersion.Parse("1.0.0")); | ||
|
||
Assert.Equal(expectedResult, actual); | ||
} | ||
|
||
[Fact] | ||
public void ReadmeUriTemplateResource_GetReadmeUrl_ReturnsFormedUrl() | ||
{ | ||
const string uriTemplate = "https://test.nuget.org/{lower_id}/{lower_version}/readme"; | ||
const string expectedResult = "https://test.nuget.org/testpackage/1.0.0/readme"; | ||
var resource = new ReadmeUriTemplateResource(uriTemplate); | ||
|
||
var actual = resource.GetReadmeUrl("TestPackage", NuGetVersion.Parse("1.0.0")); | ||
|
||
Assert.Equal(expectedResult, actual.ToString()); | ||
} | ||
} | ||
} |