-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOpenSourceOrgRepositoryTest.cs
More file actions
107 lines (86 loc) · 3.32 KB
/
OpenSourceOrgRepositoryTest.cs
File metadata and controls
107 lines (86 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Net;
using System.Net.Mime;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using Shouldly;
using ThirdPartyLibraries.Generic.Internal.Domain;
namespace ThirdPartyLibraries.Generic.Internal;
[TestFixture]
public class OpenSourceOrgRepositoryTest
{
private MockHttpMessageHandler _mockHttp = null!;
private OpenSourceOrgRepository _sut = null!;
[SetUp]
public void BeforeEachTest()
{
_mockHttp = new MockHttpMessageHandler();
_sut = new OpenSourceOrgRepository(_mockHttp.ToHttpClient);
}
[Test]
public async Task LoadIndexAsync()
{
_mockHttp
.When(HttpMethod.Get, "https://opensource.org/api/license/")
.Respond(
MediaTypeNames.Application.Json,
TempFile.OpenResource(GetType(), "OpenSourceOrgRepositoryTest.Index.json"));
await _sut.LoadIndexAsync(default).ConfigureAwait(false);
_sut.Index.ShouldNotBeNull();
_sut.Index.TryGetCode("apache-2-0", out var code).ShouldBeTrue();
code.ShouldBe("Apache-2.0");
_sut.Index.TryGetCode(new Uri("https://www.apache.org/licenses/license-2.0"), out code).ShouldBeTrue();
code.ShouldBe("Apache-2.0");
_sut.Index.TryGetCode("bsd-3-clause", out code).ShouldBeTrue();
code.ShouldBe("BSD-3-Clause");
}
[Test]
public async Task GetOrLoadIndexNotFoundAsync()
{
_mockHttp
.When(HttpMethod.Get, "https://opensource.org/api/license/")
.Respond(HttpStatusCode.NotFound);
await _sut.LoadIndexAsync(default).ConfigureAwait(false);
_sut.Index.ShouldNotBeNull();
_sut.Index.TryGetCode("apache-2-0", out _).ShouldBeFalse();
}
[Test]
[TestCase("https://opensource.org/license/apache-2.0")]
[TestCase("https://opensource.org/license/apache-2-0")]
[TestCase("https://opensource.org/licenses/apache-2-0")]
[TestCase("https://www.apache.org/licenses/license-2.0")]
[TestCase("https://opensource.org/api/licenses/apache-2-0")]
[TestCase("https://opensource.org/api/license/apache-2-0")]
[TestCase("https://api.opensource.org/license/apache-2-0")] // deprecated API
[TestCase("https://api.opensource.org/licenses/apache-2-0")]
[TestCase("https://api.opensource.org/license/apache-2.0")]
public void TryFindLicenseCodeByUrl(string url)
{
var entry = new OsiApprovedLicense
{
Id = "apache-2-0",
SpdxId = "Apache-2.0",
LicenseStewardUrl = "https://www.apache.org/licenses/LICENSE-2.0"
};
_sut.Index = new OsiLicenseIndex(1, 1);
_sut.Index.Add(entry.Id, entry.SpdxId);
_sut.Index.Add(entry.SpdxId, new Uri(entry.LicenseStewardUrl));
_sut.TryFindLicenseCodeByUrl(new Uri(url), out var actual).ShouldBeTrue();
actual.ShouldBe("Apache-2.0");
}
[Test]
[TestCase("apache-2.0")]
[TestCase("apache-2-0")]
[TestCase("Apache-2.0")]
public void TryFindLicenseCode(string code)
{
var entry = new OsiApprovedLicense
{
Id = "apache-2-0",
SpdxId = "Apache-2.0"
};
_sut.Index = new OsiLicenseIndex(1, 0);
_sut.Index.Add(entry.Id, entry.SpdxId);
_sut.TryFindLicenseCode(code, out var actual).ShouldBeTrue();
actual.ShouldBe("Apache-2.0");
}
}