Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FHIR CI Support #120

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fb18872
Add '#' style directive parsing.
GinoCanessa Aug 15, 2024
6d1b947
Spelling fixes and IDE warning cleanup.
GinoCanessa Aug 15, 2024
742f302
Fallback to Envrionment.GetFolderPath(UserProfile) if not found other…
GinoCanessa Aug 15, 2024
9547fca
Additions to PackageManifest to:
GinoCanessa Aug 15, 2024
7c0cc7e
Add extension function to fetch the highest 'latest' version from a c…
GinoCanessa Aug 15, 2024
f9cd809
WIP: Adding FhirCiClient
GinoCanessa Aug 16, 2024
aef457e
Undo overzealous spelling corrections (revert all variable names)
GinoCanessa Aug 20, 2024
efd98de
Pass of CI Version resolution using build timestamps as internal keys.
GinoCanessa Aug 22, 2024
d5ce4c6
Improvements to CI branch mangement.
GinoCanessa Aug 23, 2024
560a3ca
Added proper date conversion for manifest.
GinoCanessa Aug 23, 2024
9646340
Add package deletion to IPackageCache and DiskPackageCache
GinoCanessa Aug 26, 2024
e9002e7
Add support for CI builds of FHIR Core.
GinoCanessa Aug 26, 2024
8d019c2
Merge branch 'develop' into fhir-ci
mmsmits Aug 27, 2024
59ba74e
Refactored CI branch to QA functions.
GinoCanessa Aug 27, 2024
94a0840
Merge branch 'fhir-ci' of github.com:GinoCanessa/Firely.Fhir.Packages…
GinoCanessa Aug 27, 2024
29500e7
Removed tools-version from manifest. Some versions use an integer va…
GinoCanessa Aug 27, 2024
78bc93e
FhirCiClient: made branch from repo function public (need access exte…
GinoCanessa Aug 27, 2024
7cf3c06
CI client needs to delete existing content before installing - do not…
GinoCanessa Aug 28, 2024
7d1e338
CI Client: Fix download URL.
GinoCanessa Aug 28, 2024
35a5a1a
Merge branch 'develop' into fhir-ci
mmsmits Sep 3, 2024
751fed0
Fix: CI core builds were always pulling from the master branch.
GinoCanessa Sep 13, 2024
37f4d24
Split convertCoreBranchesToQAs per feedback.
GinoCanessa Sep 13, 2024
3a89087
Merge branch 'develop' into fhir-ci
mmsmits Sep 17, 2024
fc7739a
Fix: Incorrect download URLs for core FHIR CI builds.
GinoCanessa Sep 17, 2024
041d691
Merge branch 'fhir-ci' of github.com:GinoCanessa/Firely.Fhir.Packages…
GinoCanessa Sep 17, 2024
925836b
Merge branch 'develop' into fhir-ci
mmsmits Sep 27, 2024
c0b8cb0
Merge branch 'develop' into fhir-ci
mmsmits Oct 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
339 changes: 339 additions & 0 deletions Firely.Fhir.Packages.Tests/FhirCiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,339 @@
using FluentAssertions;
using Hl7.Fhir.Introspection;
using Hl7.Fhir.Model;
using Hl7.Fhir.Specification;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

#nullable enable


namespace Firely.Fhir.Packages.Tests
{
[TestClass]
public class FhirCiTests
{
private FhirCiHttpMessageHandler _ciHttpMessageHandler;
private HttpClient _httpClient;
private FhirCiClient _client;

public FhirCiTests()
{
_ciHttpMessageHandler = new();
_httpClient = new(_ciHttpMessageHandler);
_client = new FhirCiClient(client: _httpClient);
}

[DataTestMethod]
[DataRow(null, null, null, null, null, 445+1)] // 445 packages in qas-full.json + 1 core package
[DataRow("hl7.fhir.ca.baseline", null, null, null, null, 1)]
[DataRow(null, null, "http://hl7.org/fhir/ca/baseline/ImplementationGuide/hl7.fhir.ca.baseline", null, null, 1)]
[DataRow("hl7.fhir.ca.baseline", null, "http://hl7.org/fhir/ca/baseline/ImplementationGuide/hl7.fhir.ca.baseline", null, null, 1)]
[DataRow("hl7.fhir.ca.baseline", null, null, "HL7-Canada/ca-baseline", null, 1)]
[DataRow(null, null, null, "HL7-Canada/ca-baseline", null, 1)]
[DataRow("hl7.fhir.ca.baseline", null, null, "HL7-Canada/ca-baseline/branches/master", null, 1)]
[DataRow("hl7.fhir.ca.baseline", null, null, null, "master", 1)]
[DataRow("hl7.fhir.ca.baseline", null, null, null, "main", 0)]
[DataRow(null, "4.0.1", null, null, null, 345)] // 345 distinct packages in qas-full.json with FHIR version 4.0.1
[DataRow(null, "4.3.0", null, null, null, 13)] // 13 distinct packages in qas-full.json with FHIR version 4.3.0
[DataRow(null, "5.0.0", null, null, null, 91)] // 91 distinct packages in qas-full.json with FHIR version 5.0.0
[DataRow("hl7.fhir.r6.core", null, null, null, null, 1)]
public async Task TestFhirCiCatalog(
string? id,
string? fhirVersion,
string? siteUrl,
string? repo,
string? branch,
int expectedCount)
{
List<PackageCatalogEntry> entries = await _client.CatalogPackagesAsync(
pkgname: id,
fhirversion: fhirVersion,
site: siteUrl,
repo: repo,
branch: branch);
entries.Count.Should().Be(expectedCount);

if (id != null)
{
entries.All(e => e.Name == id).Should().BeTrue();
}
}

[DataTestMethod]
[DataRow("hl7.fhir.ca.baseline", 2)]
[DataRow("cinc.fhir.ig", 9)]
[DataRow("hl7.fhir.r6.core", 2)]
public async Task TestFhirCiDownloadListing(
string id,
int expectedCount)
{
PackageListing? listing = await _client.DownloadListingAsync(id);

if (expectedCount == 0)
{
listing.Should().BeNull();
return;
}

listing.Should().NotBeNull();
if (listing == null)
{
return;
}

listing.Versions.Should().NotBeNull();
if (listing.Versions == null)
{
return;
}

listing.Versions.Count.Should().Be(expectedCount);

listing.Versions.All(e => e.Value.Name == id).Should().BeTrue();
}

[DataTestMethod]
[DataRow("hl7.fhir.ca.baseline", 2)]
[DataRow("cinc.fhir.ig", 9)]
[DataRow("ihe.pcc.qedm", 4)]
[DataRow("hl7.fhir.r6.core", 2)]
public async Task TestFhirCiGetVersions(
string id,
int expectedCount)
{
Versions? versions = await _client.GetVersions(id);

versions.Should().NotBeNull();
if (versions == null)
{
return;
}

if (expectedCount == 0)
{
versions.Items.Count.Should().Be(0);
return;
}

versions.Items.Count.Should().Be(expectedCount);
versions.Items.All(e => e.ToString().Contains("-")).Should().BeTrue();
}


[DataTestMethod]
[DataRow("hl7.fhir.ca.baseline", null, "1.1.0-cibuild+20240809-194642Z")]
[DataRow("hl7.fhir.ca.baseline", "current", "1.1.0-cibuild+20240809-194642Z")]
[DataRow("hl7.fhir.ca.baseline", "master", "1.1.0-cibuild+20240809-194642Z")]
[DataRow("hl7.fhir.ca.baseline", "current$master", "1.1.0-cibuild+20240809-194642Z")]
[DataRow("cinc.fhir.ig", null, "0.4.0-cibuild+20240702-012714Z")]
[DataRow("cinc.fhir.ig", "current", "0.4.0-cibuild+20240702-012714Z")]
[DataRow("cinc.fhir.ig", "master", "0.4.0-cibuild+20240702-012714Z")]
[DataRow("cinc.fhir.ig", "current$master", "0.4.0-cibuild+20240702-012714Z")]
[DataRow("cinc.fhir.ig", "CommunicationPerson", "0.4.0-cibuild+20240627-051754Z")]
[DataRow("cinc.fhir.ig", "RFphase1", "0.3.9-cibuild+20240618-041305Z")]
[DataRow("ihe.pcc.qedm", null, "3.0.0-comment1+20240805-120740Z")]
[DataRow("hl7.fhir.r6.core", null, "6.0.0-ballot1+20240812-065417Z")]
[DataRow("hl7.fhir.r6.core", "current", "6.0.0-ballot1+20240812-065417Z")]
[DataRow("hl7.fhir.r6.core", "current$2024-08-gg-remove-maps", "6.0.0-ballot1+20240806-160403Z")]
public async Task TestFhirCiResolve(
string id,
string? versionDiscriminator,
string expectedVersion)
{
(PackageReference tagged, PackageReference resolved) = await _client.GetReferences(id, versionDiscriminator);

if (string.IsNullOrEmpty(expectedVersion))
{
tagged.Should().BeEquivalentTo(PackageReference.None);
resolved.Should().BeEquivalentTo(PackageReference.None);
return;
}

tagged.Name.Should().BeEquivalentTo(id);
if (string.IsNullOrEmpty(versionDiscriminator) || (versionDiscriminator == "current"))
{
tagged.Version.Should().BeEquivalentTo("current");
}
else if (versionDiscriminator.Contains('$'))
{
tagged.Version.Should().BeEquivalentTo(versionDiscriminator);
}
else
{
tagged.Version.Should().BeEquivalentTo($"current${versionDiscriminator}");
}

resolved.Name.Should().BeEquivalentTo(id);
resolved.Version.Should().BeEquivalentTo(expectedVersion);
}


[DataTestMethod]
[DataRow("not-a-real-package", null, true)]
[DataRow("cinc.fhir.ig", null, false)]
[DataRow("cinc.fhir.ig", null, false)]
[DataRow("cinc.fhir.ig", "0.4.0-cibuild+20240702-012714Z", false)]
[DataRow("cinc.fhir.ig", "current", false)]
[DataRow("cinc.fhir.ig", "0.3.9-cibuild+20240618-041305Z", false)]
[DataRow("cinc.fhir.ig", "current$RFphase1", false)]
[DataRow("ihe.pcc.qedm", "3.0.0-comment1+20240805-120740Z", false)]
[DataRow("ihe.pcc.qedm", "current", false)]
[DataRow("hl7.fhir.r6.core", null, false)]
[DataRow("hl7.fhir.r6.core", "current", false)]
[DataRow("hl7.fhir.r6.core", "current$2024-08-gg-remove-maps", false)]
public async Task TestFhirCiDownloadPackage(
string name,
string? version,
bool shouldThrow)
{
bool threw = false;
string message = string.Empty;

try
{
(PackageReference tagged, PackageReference resolved) = await _client.GetReferences(name, version);
byte[] packageData = await _client.GetPackage(resolved);
}
catch (Exception ex)
{
message = ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message;
threw = true;
}

threw.Should().Be(shouldThrow, message);
}
}

/// <summary>
/// Represents an HTTP message handler for FHIR Continuous Integration (CI) tests.
/// </summary>
public class FhirCiHttpMessageHandler : HttpMessageHandler
{
/// <summary>
/// Sends an HTTP request as an asynchronous operation.
/// </summary>
/// <param name="request">The HTTP request message to send.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
switch (request.RequestUri?.AbsoluteUri)
{
// qas.json
case "http://build.fhir.org/ig/qas.json":
case "https://build.fhir.org/ig/qas.json":
{
return Task.FromResult(JsonFile("TestData/ci/qas-full.json"));
}

// package downloads for cinc.fhir.ig
case "https://build.fhir.org/ig/tewhatuora/cinc-fhir-ig/package.tgz":
case "https://build.fhir.org/ig/tewhatuora/cinc-fhir-ig/branches/master/package.tgz":
case "https://build.fhir.org/ig/tewhatuora/cinc-fhir-ig/branches/RFphase1/package.tgz":
// package downloads for ihe.pcc.qedm
case "https://profiles.ihe.net/PCC/QEDm/package.tgz":
case "https://profiles.ihe.net/PCC/QEDm/branches/master/package.tgz":
// package downloads for hl7.fhir.r6.core
case "https://build.fhir.org/package.tgz":
case "https://build.fhir.org/branches/master/package.tgz":
case "https://build.fhir.org/ig/IHE/QEDm/branches/master/package.tgz":
case "https://build.fhir.org/branches/2024-08-gg-remove-maps/package.tgz":
{
return Task.FromResult(EmptyResponse("application/gzip"));
}

// CI Core build branch listing
case "https://build.fhir.org/branches/":
{
return Task.FromResult(JsonFile("TestData/ci/ci-core-branches.json"));
}

// CI Core build version info
case "https://build.fhir.org/version.info":
case "https://build.fhir.org/branches/master/version.info":
{
return Task.FromResult(IniFile("TestData/ci/core-version.info"));
}

case "https://build.fhir.org/branches/2024-08-gg-remove-maps/version.info":
{
return Task.FromResult(IniFile("TestData/ci/core-gg-version.info"));
}

default:
throw new NotImplementedException($"The request URI {request.RequestUri?.AbsoluteUri} is not implemented.");
}
}

internal static HttpResponseMessage EmptyResponse(
string mimeType = "text/plain",
HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new HttpResponseMessage(statusCode)
{
Content = new StringContent(string.Empty, new System.Net.Http.Headers.MediaTypeHeaderValue(mimeType)),
};
}


/// <summary>
/// Creates a JSON response message based on the content.
/// </summary>
/// <param name="filename">The filename of the JSON file.</param>
/// <param name="statusCode">(Optional) The status code of the response.</param>
/// <returns>A HttpResponseMessage object representing the JSON response.</returns>
internal static HttpResponseMessage JsonFile(
string filename,
HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new HttpResponseMessage(statusCode)
{
Content = new StringContent(File.ReadAllText(filename), new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")),
};
}

/// <summary>
/// Creates an INI response message based on the content.
/// </summary>
/// <param name="filename">The filename of the INI file.</param>
/// <param name="statusCode">(Optional) The status code of the response.</param>
/// <returns>A HttpResponseMessage object representing the INI response.</returns>
internal static HttpResponseMessage IniFile(
string filename,
HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new HttpResponseMessage(statusCode)
{
Content = new StringContent(File.ReadAllText(filename), new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain")),
};
}

/// <summary>
/// Creates a JSON response message based on the content.
/// </summary>
/// <param name="content">The content of the JSON response.</param>
/// <param name="statusCode">(Optional) The status code of the response.</param>
/// <returns>A HttpResponseMessage object representing the JSON response.</returns>
internal static HttpResponseMessage JsonContent(
string content,
HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new HttpResponseMessage(statusCode)
{
Content = new StringContent(content, new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")),
};
}
}
}

#nullable restore
10 changes: 9 additions & 1 deletion Firely.Fhir.Packages.Tests/Firely.Fhir.Packages.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
<Import Project="..\Firely.Fhir.Packages.Tests.props" />

<ItemGroup>
<Content Include="TestData\**\*.xml;TestData\**\*.json;TestData\testPackage.tgz">
<Content Include="TestData\**\*.xml;TestData\**\*.json;TestData\**\*.info;TestData\testPackage.tgz;">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<None Remove="testdata\ci\core-gg-version.info" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Firely.Fhir.Packages\Firely.Fhir.Packages.csproj" />
Expand Down
Loading
Loading