Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonz120 committed Nov 5, 2024
1 parent 54dca60 commit c8917d7
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/ServiceTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static class ServiceTypes
public static readonly string Version510 = "/5.1.0";
internal const string Version670 = "/6.7.0";
internal const string Version6110 = "/6.11.0";
internal const string Version6120 = "/6.12.0";
internal const string Version6130 = "/6.13.0";

public static readonly string[] SearchQueryService = { "SearchQueryService" + Versioned, "SearchQueryService" + Version340, "SearchQueryService" + Version300beta };
public static readonly string[] RegistrationsBaseUrl = { $"RegistrationsBaseUrl{Versioned}", $"RegistrationsBaseUrl{Version360}", $"RegistrationsBaseUrl{Version340}", $"RegistrationsBaseUrl{Version300rc}", $"RegistrationsBaseUrl{Version300beta}", "RegistrationsBaseUrl" };
public static readonly string[] SearchAutocompleteService = { "SearchAutocompleteService" + Versioned, "SearchAutocompleteService" + Version300beta };
public static readonly string[] ReportAbuse = { "ReportAbuseUriTemplate" + Versioned, "ReportAbuseUriTemplate" + Version300 };
internal static readonly string[] ReadmeFileUrl = { "ReadmeUriTemplate" + Versioned, "ReadmeUriTemplate" + Version6120 };
internal static readonly string[] ReadmeFileUrl = { "ReadmeUriTemplate" + Versioned, "ReadmeUriTemplate" + Version6130 };
public static readonly string[] PackageDetailsUriTemplate = { "PackageDetailsUriTemplate" + Version510 };
public static readonly string[] LegacyGallery = { "LegacyGallery" + Versioned, "LegacyGallery" + Version200 };
public static readonly string[] PackagePublish = { "PackagePublish" + Versioned, "PackagePublish" + Version200 };
Expand Down
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; }
}
}
}
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());
}
}
}

0 comments on commit c8917d7

Please sign in to comment.