From 51f0b48a1325cdb4e5fac2a9d8d17824ea7f210a Mon Sep 17 00:00:00 2001 From: Zeynep Delal Mutlu Date: Sun, 8 Nov 2026 13:17:48 +0100 Subject: [PATCH 1/3] Client api test for external server. --- .../Integration/DefaultSeedModel.cs | 132 +++++ .../Integration/LiveServerTestHost.cs | 84 +++ .../PsmApiClientReadIntegrationTestFixture.cs | 516 ++++++++++++++++++ ...PsmApiClientWriteIntegrationTestFixture.cs | 390 +++++++++++++ .../PsmApiClientTestFixture.cs | 12 +- 5 files changed, 1128 insertions(+), 6 deletions(-) create mode 100644 SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs create mode 100644 SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs create mode 100644 SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs create mode 100644 SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs diff --git a/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs b/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs new file mode 100644 index 000000000..54ed1d0f5 --- /dev/null +++ b/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs @@ -0,0 +1,132 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SySML2.NET.REST.Tests.Integration +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Linq; + + using SysML2.NET.Core.DTO.Kernel.Packages; + using SysML2.NET.Core.DTO.Root.Elements; + using SysML2.NET.Core.DTO.Root.Namespaces; + using SysML2.NET.Core.DTO.Systems.Parts; + using SysML2.NET.Core.Root.Namespaces; + using SysML2.NET.PSM.DTO; + + /// + /// Builds the deterministic default model committed to a live server: a root package owning one part definition + /// + public static class DefaultSeedModel + { + /// + /// The identifier of the between the package and the part definition + /// + private static readonly Guid MembershipId = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a61"); + + /// + /// Gets the identifier of the root + /// + public static Guid PackageId { get; } = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a60"); + + /// + /// Gets the identifier of the + /// + public static Guid PartDefinitionId { get; } = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a62"); + + /// + /// Gets the declared name of the in the seed commit + /// + public static string PartDefinitionName { get; } = "Vehicle"; + + /// + /// Creates the that adds the whole default model + /// + /// + /// The carrying one change per element + /// + public static CommitRequest CreateCommitRequest() + { + var package = new Package + { + Id = PackageId, + ElementId = PackageId.ToString(), + DeclaredName = "SysML2.NET seed", + OwnedRelationship = [MembershipId] + }; + + var membership = new OwningMembership + { + Id = MembershipId, + ElementId = MembershipId.ToString(), + Visibility = VisibilityKind.Public, + OwningRelatedElement = PackageId, + OwnedRelatedElement = [PartDefinitionId] + }; + + return CreateCommitRequest("Seed the default model", [package, membership, CreatePartDefinition(PartDefinitionName)]); + } + + /// + /// Creates the that renames the + /// + /// + /// The new declared name + /// + /// + /// The carrying the one changed element + /// + public static CommitRequest CreateRenameRequest(string name) + { + return CreateCommitRequest(string.Create(CultureInfo.InvariantCulture, $"Rename the part definition to {name}"), [CreatePartDefinition(name)]); + } + + /// + /// Creates the owned by the seed membership + /// + /// + /// The declared name + /// + /// + /// The + /// + private static PartDefinition CreatePartDefinition(string name) + { + return new PartDefinition + { + Id = PartDefinitionId, + ElementId = PartDefinitionId.ToString(), + DeclaredName = name, + OwningRelationship = MembershipId + }; + } + + /// + /// Wraps each element in a of one + /// + /// + /// The description of the commit + /// + /// + /// The elements to commit + /// + /// + /// The + /// + private static CommitRequest CreateCommitRequest(string description, IEnumerable elements) + { + return new CommitRequest + { + Description = description, + Change = [.. elements.Select(element => new DataVersionRequest { Identity = new DataIdentityRequest { Id = element.Id }, Payload = element })] + }; + } + } +} diff --git a/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs b/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs new file mode 100644 index 000000000..2cbe7b60d --- /dev/null +++ b/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs @@ -0,0 +1,84 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SySML2.NET.REST.Tests.Integration +{ + using System; + using System.Net.Http; + using System.Threading.Tasks; + + using Microsoft.Extensions.Logging; + + using NUnit.Framework; + + using SysML2.NET.Serializer.Json; + + /// + /// Creates an on the Systems Modeling API server named by , + /// ignoring the owning fixture when no server is configured or reachable + /// + public sealed class LiveServerTestHost : IDisposable + { + /// + /// The name of the environment variable that holds the absolute base address of the server + /// + private const string BaseAddressVariable = "SYSML2_API_BASE_ADDRESS"; + + /// + /// The used by the + /// + private readonly HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(180) }; + + /// + /// The that writes the warnings of the client to the console + /// + private readonly ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole()); + + /// + /// Gets the bound to the server + /// + public IPsmApiClient Client { get; private set; } + + /// + /// Disposes the and the + /// + public void Dispose() + { + this.httpClient.Dispose(); + this.loggerFactory.Dispose(); + } + + /// + /// Creates the client, ignoring the owning fixture when the server is not configured or not reachable + /// + /// + /// An awaitable + /// + public async Task OpenAsync() + { + if (!Uri.TryCreate(Environment.GetEnvironmentVariable(BaseAddressVariable), UriKind.Absolute, out var baseAddress)) + { + Assert.Ignore($"Set {BaseAddressVariable} to the absolute base address of a Systems Modeling API server to run the live tests"); + } + + this.httpClient.BaseAddress = baseAddress.AbsoluteUri.EndsWith('/') ? baseAddress : new Uri($"{baseAddress.AbsoluteUri}/"); + + try + { + using var response = await this.httpClient.GetAsync("projects?page%5Bsize%5D=1"); + } + catch (Exception exception) when (exception is HttpRequestException or TaskCanceledException) + { + Assert.Ignore($"The Systems Modeling API server at {baseAddress} is not reachable: {exception.Message}"); + } + + this.Client = new PsmApiClient(this.httpClient, new DeSerializer(), new Serializer(), this.loggerFactory); + } + } +} diff --git a/SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs b/SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs new file mode 100644 index 000000000..1a3cffb29 --- /dev/null +++ b/SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs @@ -0,0 +1,516 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SySML2.NET.REST.Tests.Integration +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net; + using System.Text.Json; + using System.Threading.Tasks; + + using NUnit.Framework; + + using SysML2.NET.Core.DTO.Root.Elements; + using SysML2.NET.PSM; + using SysML2.NET.PSM.DTO; + using SysML2.NET.PSM.Enumerations; + using SysML2.NET.REST.Exceptions; + + [TestFixture] + [Category("Integration")] + public class PsmApiClientReadIntegrationTestFixture + { + private const int PageSize = 2; + + private const int DiscoveryPageLimit = 40; + + private static readonly Guid UnknownId = Guid.Parse("5b0c3f1e-7d2a-4e9b-8f6c-1a2b3c4d5e6f"); + + private LiveServerTestHost host; + + private IPsmApiClient client; + + private Guid projectId; + + private Guid branchId; + + private Guid commitId; + + private IElement root; + + private Query query; + + private Tag tag; + + [OneTimeSetUp] + public async Task OneTimeSetUp() + { + this.host = new LiveServerTestHost(); + + await this.host.OpenAsync(); + + this.client = this.host.Client; + + foreach (var project in await this.DiscoverProjectsAsync()) + { + if (this.root is null) + { + await this.TrySelectProjectAsync(project); + } + + if (this.query is null) + { + var queries = await this.client.GetQueriesByProjectAsync(project.Id, new QueryParameters { PageSize = 1 }); + this.query = queries.Records.Count > 0 ? queries.Records[0] : null; + } + + if (this.tag is null) + { + var tags = await this.client.GetTagsByProjectAsync(project.Id, new QueryParameters { PageSize = 1 }); + this.tag = tags.Records.Count > 0 ? tags.Records[0] : null; + } + } + + if (this.root is null) + { + Assert.Ignore("No readable project has a default branch whose head commit has root elements"); + } + + await TestContext.Out.WriteLineAsync($"Reading project {this.projectId}, branch {this.branchId}, commit {this.commitId}, query {this.query?.Id}, tag {this.tag?.Id}"); + } + + [OneTimeTearDown] + public void OneTimeTearDown() + { + this.host.Dispose(); + } + + [Test] + public async Task VerifyGetProjectsAsync() + { + var firstPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(firstPage.Records, Has.Count.EqualTo(PageSize)); + Assert.That(firstPage.Next, Is.Not.Null); + } + + var secondPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize, PageAfter = firstPage.Next }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(secondPage.Records, Is.Not.Empty); + Assert.That(secondPage.Records.Select(project => project.Id), Has.None.AnyOf(firstPage.Records.Select(project => project.Id).ToArray())); + Assert.That(secondPage.Previous, Is.Not.Null); + } + + var previousPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize, PageBefore = secondPage.Previous }); + + Assert.That(previousPage.Records.Select(project => project.Id), Is.EqualTo(firstPage.Records.Select(project => project.Id))); + } + + [Test] + public async Task VerifyGetProjectByIdAsync() + { + var project = await this.client.GetProjectByIdAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(project.Id, Is.EqualTo(this.projectId)); + Assert.That(project.DefaultBranch, Is.EqualTo(this.branchId)); + } + + await Assert.ThatAsync(() => this.client.GetProjectByIdAsync(UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetBranchesByProjectAsync() + { + var branches = await this.client.GetBranchesByProjectAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(branches.Records.Select(branch => branch.Id), Does.Contain(this.branchId)); + Assert.That(branches.Records.Select(branch => branch.OwningProject), Is.All.EqualTo(this.projectId)); + } + + await AssertEmptyOrNotFoundAsync(() => this.client.GetBranchesByProjectAsync(UnknownId)); + } + + [Test] + public async Task VerifyGetBranchesByProjectAndIdAsync() + { + var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, this.branchId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(branch.Id, Is.EqualTo(this.branchId)); + Assert.That(branch.Head, Is.EqualTo(this.commitId)); + Assert.That(branch.OwningProject, Is.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetBranchesByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetTagsByProjectAsync() + { + var tags = await this.client.GetTagsByProjectAsync(this.projectId); + + Assert.That(tags.Records.Select(tag => tag.OwningProject), Is.All.EqualTo(this.projectId)); + + await AssertEmptyOrNotFoundAsync(() => this.client.GetTagsByProjectAsync(UnknownId)); + } + + [Test] + public async Task VerifyGetCommitsByProjectAsync() + { + var commits = await this.client.GetCommitsByProjectAsync(this.projectId, new QueryParameters { PageSize = PageSize }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(commits.Records, Has.Count.InRange(1, PageSize)); + Assert.That(commits.Records.Select(commit => commit.OwningProject), Is.All.EqualTo(this.projectId)); + } + + await AssertEmptyOrNotFoundAsync(() => this.client.GetCommitsByProjectAsync(UnknownId)); + } + + [Test] + public async Task VerifyGetCommitByProjectAndIdAsync() + { + var commit = await this.client.GetCommitByProjectAndIdAsync(this.projectId, this.commitId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(commit.Id, Is.EqualTo(this.commitId)); + Assert.That(commit.OwningProject, Is.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetCommitByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetChangesByProjectCommitAsync() + { + var changes = await this.client.GetChangesByProjectCommitAsync(this.projectId, this.commitId, new QueryParameters { PageSize = PageSize }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(changes.Records, Has.Count.InRange(0, PageSize)); + Assert.That(changes.Records.Select(change => change.Identity), Has.None.Null); + } + + await AssertEmptyOrNotFoundAsync(() => this.client.GetChangesByProjectCommitAsync(this.projectId, UnknownId)); + } + + [Test] + public async Task VerifyGetChangeByProjectCommitIdAsync() + { + var changes = await this.client.GetChangesByProjectCommitAsync(this.projectId, this.commitId, new QueryParameters { PageSize = 1 }); + + if (changes.Records.Count > 0) + { + var change = await this.client.GetChangeByProjectCommitIdAsync(this.projectId, this.commitId, changes.Records[0].Id); + + Assert.That(change.Id, Is.EqualTo(changes.Records[0].Id)); + } + + await Assert.ThatAsync(() => this.client.GetChangeByProjectCommitIdAsync(this.projectId, this.commitId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetElementsByProjectCommitAsync() + { + var firstPage = await this.client.GetElementsByProjectCommitAsync(this.projectId, this.commitId, queryParameters: new QueryParameters { PageSize = PageSize }); + + Assert.That(firstPage.Records, Has.Count.InRange(1, PageSize)); + + if (firstPage.Next is { } next) + { + var secondPage = await this.client.GetElementsByProjectCommitAsync(this.projectId, this.commitId, queryParameters: new QueryParameters { PageSize = PageSize, PageAfter = next }); + + Assert.That(secondPage.Records.Select(element => element.Id), Has.None.AnyOf(firstPage.Records.Select(element => element.Id).ToArray())); + } + + await AssertEmptyOrNotFoundAsync(() => this.client.GetElementsByProjectCommitAsync(this.projectId, UnknownId)); + } + + [Test] + public async Task VerifyGetElementByProjectCommitIdAsync() + { + var element = await this.client.GetElementByProjectCommitIdAsync(this.projectId, this.commitId, this.root.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(element.Id, Is.EqualTo(this.root.Id)); + Assert.That(element.GetType(), Is.EqualTo(this.root.GetType())); + } + + await Assert.ThatAsync(() => this.client.GetElementByProjectCommitIdAsync(this.projectId, this.commitId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetRootsByProjectCommitAsync() + { + var roots = await this.client.GetRootsByProjectCommitAsync(this.projectId, this.commitId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(roots.Records.Select(element => element.Id), Does.Contain(this.root.Id)); + Assert.That(roots.Records.Select(element => element.OwningRelationship), Is.All.Null); + } + + await AssertEmptyOrNotFoundAsync(() => this.client.GetRootsByProjectCommitAsync(this.projectId, UnknownId)); + } + + [Test] + public async Task VerifyGetRelationshipsByProjectCommitRelatedElementAsync() + { + var page = new QueryParameters { PageSize = PageSize }; + var outgoing = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, this.root.Id, Direction.@out, queryParameters: page); + var incoming = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, this.root.Id, Direction.@in, queryParameters: page); + var both = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, this.root.Id, Direction.both, queryParameters: page); + + using (Assert.EnterMultipleScope()) + { + Assert.That(outgoing.Records.Select(relationship => relationship.Source), Has.All.Contains(this.root.Id)); + Assert.That(incoming.Records.Select(relationship => relationship.Target), Has.All.Contains(this.root.Id)); + Assert.That(both.Records, Has.All.Matches(relationship => relationship.Source.Contains(this.root.Id) || relationship.Target.Contains(this.root.Id))); + } + + await AssertEmptyOrNotFoundAsync(() => this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, UnknownId)); + } + + [Test] + public async Task VerifyGetProjectUsageByProjectCommitElementAsync() + { + await Assert.ThatAsync(() => this.client.GetProjectUsageByProjectCommitElementAsync(this.projectId, this.commitId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetQueriesByProjectAsync() + { + var queries = await this.client.GetQueriesByProjectAsync(this.projectId); + + Assert.That(queries.Records.Select(query => query.OwningProject), Is.All.EqualTo(this.projectId)); + + await AssertEmptyOrNotFoundAsync(() => this.client.GetQueriesByProjectAsync(UnknownId)); + } + + [Test] + public async Task VerifyGetDatatypesAsync() + { + var datatypes = await this.client.GetDatatypesAsync(new QueryParameters { PageSize = PageSize }); + + try + { + Assert.That(datatypes.Records, Has.Count.InRange(1, PageSize)); + } + finally + { + foreach (var datatype in datatypes.Records) + { + datatype.Dispose(); + } + } + } + + [Test] + public async Task VerifyGetDatatypeByIdAsync() + { + const string datatypeId = "https://www.omg.org/spec/SysML/20250201/PartUsage"; + + using (var datatype = await this.client.GetDatatypeByIdAsync(datatypeId)) + { + Assert.That(datatype.RootElement.GetProperty("$id").GetString(), Is.EqualTo(datatypeId)); + } + + await Assert.ThatAsync(() => this.client.GetDatatypeByIdAsync($"{datatypeId}Unknown"), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetTagByProjectAndIdAsync() + { + await Assert.ThatAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + + if (this.tag is null) + { + Assert.Inconclusive("No readable project holds a tag"); + } + + var readTag = await this.client.GetTagByProjectAndIdAsync(this.tag.OwningProject, this.tag.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(readTag.Id, Is.EqualTo(this.tag.Id)); + Assert.That(readTag.TaggedCommit, Is.EqualTo(this.tag.TaggedCommit)); + } + } + + [Test] + public async Task VerifyGetQueryByProjectAndIdAsync() + { + await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + + if (this.query is null) + { + Assert.Inconclusive("No readable project holds a query"); + } + + var readQuery = await this.client.GetQueryByProjectAndIdAsync(this.query.OwningProject, this.query.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(readQuery.Id, Is.EqualTo(this.query.Id)); + Assert.That(readQuery.Select, Is.EqualTo(this.query.Select)); + Assert.That(readQuery.Where?.GetType(), Is.EqualTo(this.query.Where?.GetType())); + } + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryIdAsync() + { + if (this.query is null) + { + Assert.Inconclusive("No readable project holds a query"); + } + + var queryProject = await this.client.GetProjectByIdAsync(this.query.OwningProject); + var queryBranch = await this.client.GetBranchesByProjectAndIdAsync(queryProject.Id, queryProject.DefaultBranch); + + var results = await this.client.GetQueryResultsByProjectIdQueryIdAsync(queryProject.Id, this.query.Id, queryBranch.Head, new QueryParameters { PageSize = PageSize }); + + Assert.That(results.Records, Has.Count.InRange(0, PageSize)); + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, UnknownId, this.commitId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryAsync() + { + var request = CreateIdentifierQuery(this.root.Id); + + var results = await this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, request, this.commitId); + + Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([this.root.Id])); + + var unknownResults = await this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreateIdentifierQuery(UnknownId), this.commitId); + + Assert.That(unknownResults.Records, Is.Empty); + } + + [Test] + public async Task VerifyDiffAsync() + { + var differences = await this.client.DiffAsync(this.projectId, this.commitId, this.commitId); + + Assert.That(differences.Records, Is.Empty); + + await Assert.ThatAsync(() => this.client.DiffAsync(this.projectId, this.commitId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + private static QueryRequest CreateIdentifierQuery(Guid elementId) + { + return new QueryRequest + { + Select = ["@id", "@type"], + Where = new PrimitiveConstraintRequest + { + Property = "@id", + Operator = PrimitiveConstraintRequestOperator.equalto, + Inverse = false, + Value = [elementId.ToString()] + } + }; + } + + private static async Task AssertEmptyOrNotFoundAsync(Func>> query) + { + try + { + var result = await query(); + + Assert.That(result.Records, Is.Empty); + } + catch (PsmApiException exception) + { + Assert.That(exception.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); + } + } + + private async Task> DiscoverProjectsAsync() + { + var projects = new List(); + var parameters = new QueryParameters { PageSize = 1 }; + + for (var pageIndex = 0; pageIndex < DiscoveryPageLimit; pageIndex++) + { + PagedResult page; + + try + { + page = await this.client.GetProjectsAsync(parameters); + } + catch (JsonException exception) + { + await TestContext.Out.WriteLineAsync($"Project discovery stops at page {pageIndex + 1}, its record is not readable: {exception.Message}"); + break; + } + + projects.AddRange(page.Records); + + if (page.Next is not { } next) + { + break; + } + + parameters = new QueryParameters { PageSize = 1, PageAfter = next }; + } + + return projects; + } + + private async Task TrySelectProjectAsync(Project project) + { + var branch = await this.client.GetBranchesByProjectAndIdAsync(project.Id, project.DefaultBranch); + + if (branch.Head is not { } head) + { + return; + } + + var roots = await this.client.GetRootsByProjectCommitAsync(project.Id, head, queryParameters: new QueryParameters { PageSize = 1 }); + + if (roots.Records.Count == 0) + { + return; + } + + this.projectId = project.Id; + this.branchId = branch.Id; + this.commitId = head; + this.root = roots.Records[0]; + } + } +} diff --git a/SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs b/SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs new file mode 100644 index 000000000..599da75df --- /dev/null +++ b/SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs @@ -0,0 +1,390 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SySML2.NET.REST.Tests.Integration +{ + using System; + using System.Linq; + using System.Net; + using System.Text.Json; + using System.Threading.Tasks; + + using NUnit.Framework; + + using SysML2.NET.Core.DTO.Systems.Parts; + using SysML2.NET.PSM.DTO; + using SysML2.NET.PSM.Enumerations; + using SysML2.NET.REST.Exceptions; + + [TestFixture] + [Category("Integration")] + public class PsmApiClientWriteIntegrationTestFixture + { + private const string ProjectNamePrefix = "SysML2.NET-it-"; + + private static readonly Guid UnknownId = Guid.Parse("5b0c3f1e-7d2a-4e9b-8f6c-1a2b3c4d5e6f"); + + private static readonly string[] SelectedProperties = ["@id", "@type"]; + + private LiveServerTestHost host; + + private IPsmApiClient client; + + private Guid projectId; + + private Guid defaultBranchId; + + private Guid seedCommitId; + + [OneTimeSetUp] + public async Task OneTimeSetUp() + { + this.host = new LiveServerTestHost(); + + await this.host.OpenAsync(); + + this.client = this.host.Client; + + var project = await this.client.PostProjectAsync(new ProjectRequest { Name = $"{ProjectNamePrefix}{Guid.NewGuid()}", Description = "Throw-away project of the SysML2.NET integration tests" }); + + this.projectId = project.Id; + this.defaultBranchId = project.DefaultBranch; + this.seedCommitId = await this.CommitAsync(DefaultSeedModel.CreateCommitRequest(), this.defaultBranchId); + + await TestContext.Out.WriteLineAsync($"Writing to project {this.projectId}, seed commit {this.seedCommitId}"); + } + + [OneTimeTearDown] + public async Task OneTimeTearDown() + { + try + { + if (this.projectId != Guid.Empty) + { + await this.client.DeleteProjectByIdAsync(this.projectId); + } + } + finally + { + this.host.Dispose(); + } + } + + [Test] + public async Task VerifyPutProjectByIdAsync() + { + var name = $"{ProjectNamePrefix}{Guid.NewGuid()}"; + + var project = await this.client.PutProjectByIdAsync(this.projectId, new ProjectRequest { Name = name, DefaultBranch = this.defaultBranchId }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(project.Id, Is.EqualTo(this.projectId)); + Assert.That(project.Name, Is.EqualTo(name)); + Assert.That((await this.client.GetProjectByIdAsync(this.projectId)).Name, Is.EqualTo(name)); + } + + await Assert.ThatAsync(() => this.client.PutProjectByIdAsync(UnknownId, new ProjectRequest { Name = name }), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyDeleteProjectByIdAsync() + { + var project = await this.client.PostProjectAsync(new ProjectRequest { Name = $"{ProjectNamePrefix}{Guid.NewGuid()}" }); + + await this.client.DeleteProjectByIdAsync(project.Id); + + await Assert.ThatAsync(() => this.client.GetProjectByIdAsync(project.Id), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + + await Assert.ThatAsync(() => this.client.DeleteProjectByIdAsync(UnknownId), Throws.TypeOf()); + } + + [Test] + public async Task VerifyPostCommitByProjectAsync() + { + var commit = await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Car"), this.defaultBranchId); + var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, this.defaultBranchId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(commit.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(branch.Head, Is.EqualTo(commit.Id)); + } + + await Assert.ThatAsync(() => this.client.PostCommitByProjectAsync(UnknownId, DefaultSeedModel.CreateRenameRequest("Car")), Throws.TypeOf()); + } + + [Test] + public async Task VerifyGetRootsByProjectCommitAsync() + { + var roots = await this.client.GetRootsByProjectCommitAsync(this.projectId, this.seedCommitId); + var partDefinition = await this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PartDefinitionId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(roots.Records.Select(root => root.Id), Is.EquivalentTo([DefaultSeedModel.PackageId])); + Assert.That(partDefinition, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo(DefaultSeedModel.PartDefinitionName)); + } + + await Assert.ThatAsync(() => this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyPostBranchByProjectAsync() + { + var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "feature", Head = this.seedCommitId }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(branch.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(branch.Head, Is.EqualTo(this.seedCommitId)); + Assert.That((await this.client.GetBranchesByProjectAsync(this.projectId)).Records.Select(record => record.Id), Does.Contain(branch.Id)); + } + + await Assert.ThatAsync(() => this.client.PostBranchByProjectAsync(UnknownId, new BranchRequest { Name = "feature", Head = this.seedCommitId }), Throws.TypeOf()); + } + + [Test] + public async Task VerifyDeleteBranchByProjectAndIdAsync() + { + var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "to-delete", Head = this.seedCommitId }); + + await this.client.DeleteBranchByProjectAndIdAsync(this.projectId, branch.Id); + + var deletedBranch = await GetOrNullWhenNotFoundAsync(() => this.client.GetBranchesByProjectAndIdAsync(this.projectId, branch.Id)); + + Assert.That(deletedBranch is null || deletedBranch.Deleted is not null, Is.True); + + await Assert.ThatAsync(() => this.client.DeleteBranchByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyPostTagByProjectAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "seed", TaggedCommit = this.seedCommitId }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(tag.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(tag.TaggedCommit, Is.EqualTo(this.seedCommitId)); + } + + await Assert.ThatAsync(() => this.client.PostTagByProjectAsync(UnknownId, new TagRequest { Name = "seed", TaggedCommit = this.seedCommitId }), Throws.TypeOf()); + } + + [Test] + public async Task VerifyGetTagByProjectAndIdAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "read", TaggedCommit = this.seedCommitId }); + + var readTag = await this.client.GetTagByProjectAndIdAsync(this.projectId, tag.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(readTag.Id, Is.EqualTo(tag.Id)); + Assert.That(readTag.Name, Is.EqualTo("read")); + } + + await Assert.ThatAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyDeleteTagByProjectAndIdAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "to-delete", TaggedCommit = this.seedCommitId }); + + await this.client.DeleteTagByProjectAndIdAsync(this.projectId, tag.Id); + + var deletedTag = await GetOrNullWhenNotFoundAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, tag.Id)); + + Assert.That(deletedTag is null || deletedTag.Deleted is not null, Is.True); + + await Assert.ThatAsync(() => this.client.DeleteTagByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyPostQueryByProjectAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + using (Assert.EnterMultipleScope()) + { + Assert.That(query.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(query.Select, Is.EquivalentTo(SelectedProperties)); + Assert.That(query.Where, Is.TypeOf()); + } + + await Assert.ThatAsync(() => this.client.PostQueryByProjectAsync(UnknownId, CreatePartDefinitionQuery()), Throws.TypeOf()); + } + + [Test] + public async Task VerifyGetQueryByProjectAndIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + Assert.That((await this.client.GetQueryByProjectAndIdAsync(this.projectId, query.Id)).Id, Is.EqualTo(query.Id)); + + await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyPutQueryByProjectAndIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + var request = CreatePartDefinitionQuery(); + request.Name = "renamed"; + + Assert.That((await this.client.PutQueryByProjectAndIdAsync(this.projectId, query.Id, request)).Name, Is.EqualTo("renamed")); + + await Assert.ThatAsync(() => this.client.PutQueryByProjectAndIdAsync(this.projectId, UnknownId, request), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyDeleteQueryByProjectAndIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + await this.client.DeleteQueryByProjectAndIdAsync(this.projectId, query.Id); + + await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, query.Id), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + + await Assert.ThatAsync(() => this.client.DeleteQueryByProjectAndIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + var results = await this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, query.Id, this.seedCommitId); + + Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryAsync() + { + var results = await SupportedAsync(() => this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreatePartDefinitionQuery(), this.seedCommitId)); + + Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryAsync(UnknownId, CreatePartDefinitionQuery()), Throws.TypeOf()); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() + { + var results = await this.client.GetQueryResultsByProjectIdQueryPostAsync(this.projectId, CreatePartDefinitionQuery(), this.seedCommitId); + + Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryPostAsync(UnknownId, CreatePartDefinitionQuery()), Throws.TypeOf()); + } + + [Test] + public async Task VerifyDiffAsync() + { + var renameCommitId = await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Truck"), this.defaultBranchId); + + var differences = await SupportedAsync(() => this.client.DiffAsync(this.projectId, this.seedCommitId, renameCommitId)); + + Assert.That(differences.Records.Select(difference => difference.CompareData?.Identity?.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.DiffAsync(this.projectId, this.seedCommitId, UnknownId), + Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); + } + + [Test] + public async Task VerifyMergeAsync() + { + var source = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "merge-source", Head = this.seedCommitId }); + var target = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "merge-target", Head = this.seedCommitId }); + var sourceCommitId = await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Bicycle"), source.Id); + + var merge = await SupportedAsync(() => this.client.MergeAsync(this.projectId, [sourceCommitId], target.Id, description: "merge")); + + Assert.That(merge.OwningProject, Is.EqualTo(this.projectId)); + + await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Boat"), source.Id); + var conflictingCommitId = await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Plane"), target.Id); + + await Assert.ThatAsync(() => this.client.MergeAsync(this.projectId, [conflictingCommitId], source.Id), Throws.TypeOf()); + } + + private static QueryRequest CreatePartDefinitionQuery() + { + return new QueryRequest + { + Name = "part definitions", + Select = [.. SelectedProperties], + Where = new PrimitiveConstraintRequest + { + Property = "@type", + Operator = PrimitiveConstraintRequestOperator.equalto, + Inverse = false, + Value = ["PartDefinition"] + } + }; + } + + private static async Task GetOrNullWhenNotFoundAsync(Func> read) where T : class + { + try + { + return await read(); + } + catch (PsmApiException exception) when (exception.StatusCode == HttpStatusCode.NotFound) + { + return null; + } + } + + private static async Task SupportedAsync(Func> operation) + { + try + { + return await operation(); + } + catch (PsmApiException exception) when (exception.StatusCode is HttpStatusCode.MethodNotAllowed or HttpStatusCode.NotImplemented) + { + Assert.Inconclusive($"The server does not support the operation: {(int)exception.StatusCode} {exception.StatusCode}"); + throw; + } + } + + private async Task CommitAsync(CommitRequest request, Guid branchId) + { + try + { + return (await this.client.PostCommitByProjectAsync(this.projectId, request, branchId)).Id; + } + catch (InvalidOperationException exception) + { + await TestContext.Out.WriteLineAsync($"The commit response could not be read, the head of the branch is used instead: {exception.Message}"); + + var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, branchId); + + return branch.Head ?? throw new InvalidOperationException("The branch has no head after the commit", exception); + } + } + } +} diff --git a/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs b/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs index 2c8ff879a..7cd9ec393 100644 --- a/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs +++ b/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs @@ -200,7 +200,7 @@ public async Task VerifyPostCommitByProjectAsync() this.mockHttp.Expect(HttpMethod.Post, commitsUri) .WithExactQueryString($"branchId={BranchId}") - .With(request => request.Content.Headers.ContentType.MediaType == JsonMediaType + .With(request => request.Content is { Headers.ContentType.MediaType: JsonMediaType } && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the commit description")) .Respond(HttpStatusCode.Created, JsonMediaType, ReadPayload("commit.json")); @@ -209,7 +209,7 @@ public async Task VerifyPostCommitByProjectAsync() Assert.That(commit.Id, Is.EqualTo(CommitId)); this.mockHttp.Expect(HttpMethod.Post, commitsUri) - .With(request => string.IsNullOrWhiteSpace(request.RequestUri.Query)) + .With(request => request.RequestUri != null && string.IsNullOrWhiteSpace(request.RequestUri.Query)) .Respond(HttpStatusCode.BadRequest, JsonMediaType, ErrorPayload); await Assert.ThatAsync(() => this.client.PostCommitByProjectAsync(ProjectId, commitRequest), @@ -312,7 +312,7 @@ public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() this.mockHttp.Expect(HttpMethod.Post, queryResultsUri) .WithExactQueryString($"page[size]=2&commitId={CommitId}") - .With(request => request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the query name")) + .With(request => request.Content != null && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the query name")) .Respond(JsonMediaType, ReadPayload("roots.json")); var results = await this.client.GetQueryResultsByProjectIdQueryPostAsync(ProjectId, queryRequest, CommitId, new QueryParameters { PageSize = 2 }); @@ -325,7 +325,7 @@ public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() } this.mockHttp.Expect(HttpMethod.Post, queryResultsUri) - .With(request => string.IsNullOrWhiteSpace(request.RequestUri.Query)) + .With(request => request.RequestUri != null && string.IsNullOrWhiteSpace(request.RequestUri.Query)) .Respond(HttpStatusCode.BadRequest, JsonMediaType, ErrorPayload); await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryPostAsync(ProjectId, queryRequest), @@ -341,7 +341,7 @@ public async Task VerifyPutProjectByIdAsync() var projectRequest = new ProjectRequest { Name = "the project name" }; this.mockHttp.Expect(HttpMethod.Put, projectUri) - .With(request => request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the project name")) + .With(request => request.Content != null && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the project name")) .Respond(JsonMediaType, ReadPayload("project.json")); var project = await this.client.PutProjectByIdAsync(ProjectId, projectRequest); @@ -377,7 +377,7 @@ public async Task VerifyMergeAsync() this.mockHttp.Expect(HttpMethod.Post, mergeUri) .WithExactQueryString($"sourceCommitId={CommitId},{OtherCommitId}&description=the%20merge%20description") - .With(request => request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains(RootNamespaceId.ToString())) + .With(request => request.Content != null && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains(RootNamespaceId.ToString())) .Respond(HttpStatusCode.Created, JsonMediaType, ReadPayload("commit.json")); var mergeCommit = await this.client.MergeAsync(ProjectId, [CommitId, OtherCommitId], BranchId, [new Namespace { Id = RootNamespaceId }], "the merge description"); From a637625aa7a343650f5ce92bb4f62c071cbc204a Mon Sep 17 00:00:00 2001 From: Zeynep Delal Mutlu Date: Tue, 10 Nov 2026 15:47:25 +0100 Subject: [PATCH 2/3] Fix #407: single integration fixture with assertions and status checks --- .../Integration/DefaultSeedModel.cs | 4 +- .../Integration/LiveServerTestHost.cs | 19 +- .../PsmApiClientIntegrationTestFixture.cs | 786 ++++++++++++++++++ .../PsmApiClientReadIntegrationTestFixture.cs | 516 ------------ ...PsmApiClientWriteIntegrationTestFixture.cs | 390 --------- .../PsmApiClientTestFixture.cs | 12 +- 6 files changed, 810 insertions(+), 917 deletions(-) create mode 100644 SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs delete mode 100644 SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs delete mode 100644 SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs diff --git a/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs b/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs index 54ed1d0f5..df60bebd3 100644 --- a/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs +++ b/SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs @@ -27,9 +27,9 @@ namespace SySML2.NET.REST.Tests.Integration public static class DefaultSeedModel { /// - /// The identifier of the between the package and the part definition + /// Gets the identifier of the between the package and the part definition /// - private static readonly Guid MembershipId = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a61"); + public static Guid MembershipId { get; } = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a61"); /// /// Gets the identifier of the root diff --git a/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs b/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs index 2cbe7b60d..5c46b4198 100644 --- a/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs +++ b/SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs @@ -10,6 +10,7 @@ namespace SySML2.NET.REST.Tests.Integration { using System; + using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -21,7 +22,7 @@ namespace SySML2.NET.REST.Tests.Integration /// /// Creates an on the Systems Modeling API server named by , - /// ignoring the owning fixture when no server is configured or reachable + /// ignoring the owning fixture when no server is configured or reachable, and failing it when the server refuses the probe /// public sealed class LiveServerTestHost : IDisposable { @@ -55,7 +56,8 @@ public void Dispose() } /// - /// Creates the client, ignoring the owning fixture when the server is not configured or not reachable + /// Creates the client, ignoring the owning fixture when the server is not configured or not reachable, + /// and failing it when the server answers the probe with any status other than 200 OK /// /// /// An awaitable @@ -69,15 +71,26 @@ public async Task OpenAsync() this.httpClient.BaseAddress = baseAddress.AbsoluteUri.EndsWith('/') ? baseAddress : new Uri($"{baseAddress.AbsoluteUri}/"); + HttpResponseMessage response = null; + try { - using var response = await this.httpClient.GetAsync("projects?page%5Bsize%5D=1"); + response = await this.httpClient.GetAsync("projects?page%5Bsize%5D=1"); } catch (Exception exception) when (exception is HttpRequestException or TaskCanceledException) { Assert.Ignore($"The Systems Modeling API server at {baseAddress} is not reachable: {exception.Message}"); } + using (response) + { + if (response.StatusCode != HttpStatusCode.OK) + { + var body = await response.Content.ReadAsStringAsync(); + Assert.Fail($"The Systems Modeling API server at {baseAddress} answered GET /projects with {(int)response.StatusCode} {response.StatusCode}: {body}"); + } + } + this.Client = new PsmApiClient(this.httpClient, new DeSerializer(), new Serializer(), this.loggerFactory); } } diff --git a/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs b/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs new file mode 100644 index 000000000..5a32b48d3 --- /dev/null +++ b/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs @@ -0,0 +1,786 @@ +// ------------------------------------------------------------------------------------------------ +// +// +// Copyright (C) 2022-2026 Starion Group S.A. +// SPDX-License-Identifier: Apache-2.0 +// +// +// ------------------------------------------------------------------------------------------------ + +namespace SySML2.NET.REST.Tests.Integration +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net; + using System.Threading.Tasks; + + using NUnit.Framework; + using NUnit.Framework.Constraints; + + using SysML2.NET.Core.DTO.Kernel.Packages; + using SysML2.NET.Core.DTO.Root.Namespaces; + using SysML2.NET.Core.DTO.Systems.Parts; + using SysML2.NET.PSM; + using SysML2.NET.PSM.DTO; + using SysML2.NET.PSM.Enumerations; + using SysML2.NET.REST.Exceptions; + + [TestFixture] + [Category("Integration")] + public class PsmApiClientIntegrationTestFixture + { + private const int PageSize = 2; + + private const int DatatypePageSize = 100; + + private const string ProjectNamePrefix = "SysML2.NET-it-"; + + private const string ProjectDescription = "Throw-away project of the SysML2.NET integration tests"; + + private const string PartUsageDatatypeId = "https://www.omg.org/spec/SysML/20250201/PartUsage"; + + private const HttpStatusCode LastClientError = (HttpStatusCode)499; + + private static readonly Guid UnknownId = Guid.Parse("5b0c3f1e-7d2a-4e9b-8f6c-1a2b3c4d5e6f"); + + private static readonly string[] SelectedProperties = ["@id", "@type"]; + + private readonly List createdProjectIds = []; + + private LiveServerTestHost host; + + private IPsmApiClient client; + + private Guid projectId; + + private Guid defaultBranchId; + + private Guid seedCommitId; + + [OneTimeSetUp] + public async Task OneTimeSetUp() + { + this.host = new LiveServerTestHost(); + + await this.host.OpenAsync(); + + this.client = this.host.Client; + + var project = await this.CreateProjectAsync(); + + this.projectId = project.Id; + this.defaultBranchId = project.DefaultBranch; + this.seedCommitId = (await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateCommitRequest(), this.defaultBranchId)).Id; + + await TestContext.Out.WriteLineAsync($"Testing on project {this.projectId}, seed commit {this.seedCommitId}"); + } + + [OneTimeTearDown] + public async Task OneTimeTearDown() + { + try + { + foreach (var createdProjectId in this.createdProjectIds) + { + try + { + await this.client.DeleteProjectByIdAsync(createdProjectId); + } + catch (PsmApiException exception) + { + await TestContext.Out.WriteLineAsync($"Project {createdProjectId} is left on the server, its delete answered {(int)exception.StatusCode} {exception.StatusCode}"); + } + } + } + finally + { + this.host.Dispose(); + } + } + + [Test] + public async Task VerifyDeleteBranchByProjectAndIdAsync() + { + var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "to-delete", Head = this.seedCommitId }); + + await this.client.DeleteBranchByProjectAndIdAsync(this.projectId, branch.Id); + + var deletedBranch = await GetOrNullWhenNotFoundAsync(() => this.client.GetBranchesByProjectAndIdAsync(this.projectId, branch.Id)); + + Assert.That(deletedBranch, Is.Null.Or.Property(nameof(Branch.Deleted)).Not.Null); + + await Assert.ThatAsync(() => this.client.DeleteBranchByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyDeleteProjectByIdAsync() + { + var project = await this.CreateProjectAsync(); + + await this.client.DeleteProjectByIdAsync(project.Id); + this.createdProjectIds.Remove(project.Id); + + await Assert.ThatAsync(() => this.client.GetProjectByIdAsync(project.Id), ThrowsNotFound()); + + try + { + await this.client.DeleteProjectByIdAsync(UnknownId); + } + catch (PsmApiException exception) + { + // DELETE /projects/{projectId} declares no 404: an idempotent success and a client error both conform. + Assert.That(exception.StatusCode, Is.InRange(HttpStatusCode.BadRequest, LastClientError)); + } + } + + [Test] + public async Task VerifyDeleteQueryByProjectAndIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + await this.client.DeleteQueryByProjectAndIdAsync(this.projectId, query.Id); + + await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, query.Id), ThrowsNotFound()); + + await Assert.ThatAsync(() => this.client.DeleteQueryByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyDeleteTagByProjectAndIdAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "to-delete", TaggedCommit = this.seedCommitId }); + + await this.client.DeleteTagByProjectAndIdAsync(this.projectId, tag.Id); + + var deletedTag = await GetOrNullWhenNotFoundAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, tag.Id)); + + Assert.That(deletedTag, Is.Null.Or.Property(nameof(Tag.Deleted)).Not.Null); + + await Assert.ThatAsync(() => this.client.DeleteTagByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyDiffAsync() + { + var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "diff", Head = this.seedCommitId }); + var renameCommit = await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Truck"), branch.Id); + + var differences = await SupportedAsync(() => this.client.DiffAsync(this.projectId, this.seedCommitId, renameCommit.Id)); + var noDifferences = await this.client.DiffAsync(this.projectId, this.seedCommitId, this.seedCommitId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(differences.Records.Select(difference => difference.CompareData?.Identity?.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + Assert.That(noDifferences.Records, Is.Empty); + } + + await Assert.ThatAsync(() => this.client.DiffAsync(this.projectId, this.seedCommitId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetBranchesByProjectAndIdAsync() + { + var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, this.defaultBranchId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(branch.Id, Is.EqualTo(this.defaultBranchId)); + Assert.That(branch.Head, Is.EqualTo(this.seedCommitId)); + Assert.That(branch.OwningProject, Is.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetBranchesByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetBranchesByProjectAsync() + { + var branches = await this.client.GetBranchesByProjectAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(branches.Records.Select(branch => branch.Id), Does.Contain(this.defaultBranchId)); + Assert.That(branches.Records.Select(branch => branch.OwningProject), Is.All.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetBranchesByProjectAsync(UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetChangeByProjectCommitIdAsync() + { + var changes = await this.client.GetChangesByProjectCommitAsync(this.projectId, this.seedCommitId); + var partDefinitionChange = changes.Records.Single(change => change.Identity?.Id == DefaultSeedModel.PartDefinitionId); + + var change = await this.client.GetChangeByProjectCommitIdAsync(this.projectId, this.seedCommitId, partDefinitionChange.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(change.Id, Is.EqualTo(partDefinitionChange.Id)); + Assert.That(change.Identity?.Id, Is.EqualTo(DefaultSeedModel.PartDefinitionId)); + Assert.That(change.Payload, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo(DefaultSeedModel.PartDefinitionName)); + } + + await Assert.ThatAsync(() => this.client.GetChangeByProjectCommitIdAsync(this.projectId, this.seedCommitId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetChangesByProjectCommitAsync() + { + var changes = await this.client.GetChangesByProjectCommitAsync(this.projectId, this.seedCommitId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(changes.Records.Select(change => change.Identity?.Id), Is.EquivalentTo([DefaultSeedModel.PackageId, DefaultSeedModel.MembershipId, DefaultSeedModel.PartDefinitionId])); + Assert.That(changes.Records.Select(change => change.Payload?.GetType()), Is.EquivalentTo([typeof(Package), typeof(OwningMembership), typeof(PartDefinition)])); + } + + await Assert.ThatAsync(() => this.client.GetChangesByProjectCommitAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetCommitByProjectAndIdAsync() + { + var commit = await this.client.GetCommitByProjectAndIdAsync(this.projectId, this.seedCommitId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(commit.Id, Is.EqualTo(this.seedCommitId)); + Assert.That(commit.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(commit.PreviousCommit, Is.Empty); + } + + await Assert.ThatAsync(() => this.client.GetCommitByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetCommitsByProjectAsync() + { + var commits = await this.client.GetCommitsByProjectAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(commits.Records.Select(commit => commit.Id), Does.Contain(this.seedCommitId)); + Assert.That(commits.Records.Select(commit => commit.OwningProject), Is.All.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetCommitsByProjectAsync(UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetDatatypeByIdAsync() + { + using (var datatype = await this.client.GetDatatypeByIdAsync(PartUsageDatatypeId)) + { + Assert.That(datatype.RootElement.GetProperty("$id").GetString(), Is.EqualTo(PartUsageDatatypeId)); + } + + await Assert.ThatAsync(() => this.client.GetDatatypeByIdAsync($"{PartUsageDatatypeId}Unknown"), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetDatatypesAsync() + { + var firstPage = await this.client.GetDatatypesAsync(new QueryParameters { PageSize = PageSize }); + + try + { + Assert.That(firstPage.Records, Has.Count.EqualTo(1)); + + using (Assert.EnterMultipleScope()) + { + Assert.That(firstPage.Records[0].RootElement.GetProperty("$defs").EnumerateObject().Select(datatype => datatype.Name), Has.Exactly(PageSize).Items); + Assert.That(firstPage.Next, Is.Not.Null); + } + } + finally + { + foreach (var datatypes in firstPage.Records) + { + datatypes.Dispose(); + } + } + + Assert.That(await this.ReadDatatypeIdsAsync(), Has.Member(PartUsageDatatypeId)); + } + + [Test] + public async Task VerifyGetElementByProjectCommitIdAsync() + { + var element = await this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PartDefinitionId); + + Assert.That(element, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo(DefaultSeedModel.PartDefinitionName)); + + await Assert.ThatAsync(() => this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetElementsByProjectCommitAsync() + { + var firstPage = await this.client.GetElementsByProjectCommitAsync(this.projectId, this.seedCommitId, queryParameters: new QueryParameters { PageSize = PageSize }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(firstPage.Records, Has.Count.EqualTo(PageSize)); + Assert.That(firstPage.Next, Is.Not.Null); + } + + var secondPage = await this.client.GetElementsByProjectCommitAsync(this.projectId, this.seedCommitId, queryParameters: new QueryParameters { PageSize = PageSize, PageAfter = firstPage.Next }); + + Assert.That(firstPage.Records.Concat(secondPage.Records).Select(element => element.Id), Is.EquivalentTo([DefaultSeedModel.PackageId, DefaultSeedModel.MembershipId, DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.GetElementsByProjectCommitAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetProjectByIdAsync() + { + var project = await this.client.GetProjectByIdAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(project.Id, Is.EqualTo(this.projectId)); + Assert.That(project.DefaultBranch, Is.EqualTo(this.defaultBranchId)); + } + + await Assert.ThatAsync(() => this.client.GetProjectByIdAsync(UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetProjectsAsync() + { + for (var projectIndex = 0; projectIndex < PageSize; projectIndex++) + { + await this.CreateProjectAsync(); + } + + var firstPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(firstPage.Records, Has.Count.EqualTo(PageSize)); + Assert.That(firstPage.Next, Is.Not.Null); + } + + var secondPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize, PageAfter = firstPage.Next }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(secondPage.Records, Is.Not.Empty); + Assert.That(secondPage.Records.Select(project => project.Id), Has.None.AnyOf(firstPage.Records.Select(project => project.Id).ToArray())); + Assert.That(secondPage.Previous, Is.Not.Null); + } + + var previousPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize, PageBefore = secondPage.Previous }); + + Assert.That(previousPage.Records.Select(project => project.Id), Is.EqualTo(firstPage.Records.Select(project => project.Id))); + } + + [Test] + public async Task VerifyGetProjectUsageByProjectCommitElementAsync() + { + await Assert.ThatAsync(() => this.client.GetProjectUsageByProjectCommitElementAsync(this.projectId, this.seedCommitId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetQueriesByProjectAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + var queries = await this.client.GetQueriesByProjectAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(queries.Records.Select(record => record.Id), Does.Contain(query.Id)); + Assert.That(queries.Records.Select(record => record.OwningProject), Is.All.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetQueriesByProjectAsync(UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetQueryByProjectAndIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + var readQuery = await this.client.GetQueryByProjectAndIdAsync(this.projectId, query.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(readQuery.Id, Is.EqualTo(query.Id)); + Assert.That(readQuery.Select, Is.EquivalentTo(SelectedProperties)); + Assert.That(readQuery.Where, Is.TypeOf()); + } + + await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryAsync() + { + var partDefinitions = await SupportedAsync(() => this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreatePartDefinitionQuery(), this.seedCommitId)); + var package = await this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreateIdentifierQuery(DefaultSeedModel.PackageId), this.seedCommitId); + var unknownElement = await this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreateIdentifierQuery(UnknownId), this.seedCommitId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(partDefinitions.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + Assert.That(package.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PackageId])); + Assert.That(unknownElement.Records, Is.Empty); + } + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryAsync(UnknownId, CreatePartDefinitionQuery()), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + var results = await this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, query.Id, this.seedCommitId); + + Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, UnknownId, this.seedCommitId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() + { + var results = await this.client.GetQueryResultsByProjectIdQueryPostAsync(this.projectId, CreatePartDefinitionQuery(), this.seedCommitId); + + Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); + + await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryPostAsync(UnknownId, CreatePartDefinitionQuery()), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetRelationshipsByProjectCommitRelatedElementAsync() + { + var packageOutgoing = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PackageId, Direction.@out); + var packageIncoming = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PackageId, Direction.@in); + var packageBoth = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PackageId, Direction.both); + var partDefinitionOutgoing = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PartDefinitionId, Direction.@out); + var partDefinitionIncoming = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PartDefinitionId, Direction.@in); + + using (Assert.EnterMultipleScope()) + { + Assert.That(packageOutgoing.Records.Select(relationship => relationship.Id), Is.EquivalentTo([DefaultSeedModel.MembershipId])); + Assert.That(packageIncoming.Records, Is.Empty); + Assert.That(packageBoth.Records.Select(relationship => relationship.Id), Is.EquivalentTo([DefaultSeedModel.MembershipId])); + Assert.That(partDefinitionOutgoing.Records, Is.Empty); + Assert.That(partDefinitionIncoming.Records.Select(relationship => relationship.Id), Is.EquivalentTo([DefaultSeedModel.MembershipId])); + } + + await Assert.ThatAsync(() => this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.seedCommitId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetRootsByProjectCommitAsync() + { + var roots = await this.client.GetRootsByProjectCommitAsync(this.projectId, this.seedCommitId); + + Assert.That(roots.Records.Select(root => root.Id), Is.EquivalentTo([DefaultSeedModel.PackageId])); + + await Assert.ThatAsync(() => this.client.GetRootsByProjectCommitAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetTagByProjectAndIdAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "read", TaggedCommit = this.seedCommitId }); + + var readTag = await this.client.GetTagByProjectAndIdAsync(this.projectId, tag.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(readTag.Id, Is.EqualTo(tag.Id)); + Assert.That(readTag.Name, Is.EqualTo("read")); + Assert.That(readTag.TaggedCommit, Is.EqualTo(this.seedCommitId)); + } + + await Assert.ThatAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyGetTagsByProjectAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "listed", TaggedCommit = this.seedCommitId }); + + var tags = await this.client.GetTagsByProjectAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(tags.Records.Select(record => record.Id), Does.Contain(tag.Id)); + Assert.That(tags.Records.Select(record => record.OwningProject), Is.All.EqualTo(this.projectId)); + } + + await Assert.ThatAsync(() => this.client.GetTagsByProjectAsync(UnknownId), ThrowsNotFound()); + } + + [Test] + public async Task VerifyMergeAsync() + { + var source = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "merge-source", Head = this.seedCommitId }); + var target = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "merge-target", Head = this.seedCommitId }); + var sourceCommit = await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Bicycle"), source.Id); + + var merge = await SupportedAsync(() => this.client.MergeAsync(this.projectId, [sourceCommit.Id], target.Id, description: "merge")); + + var mergedTarget = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, target.Id); + var mergedPartDefinition = await this.client.GetElementByProjectCommitIdAsync(this.projectId, merge.Id, DefaultSeedModel.PartDefinitionId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(merge.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(mergedTarget.Head, Is.EqualTo(merge.Id)); + Assert.That(mergedPartDefinition, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo("Bicycle")); + } + + await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Boat"), source.Id); + var conflictingCommit = await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Plane"), target.Id); + + MergeConflictException conflict = null; + + try + { + await this.client.MergeAsync(this.projectId, [conflictingCommit.Id], source.Id); + } + catch (MergeConflictException exception) + { + conflict = exception; + } + + Assert.That(conflict, Is.Not.Null); + + if (conflict.Conflicts.Count == 0) + { + Assert.Inconclusive("The server answered the merge conflict without listing the conflicting data"); + } + + Assert.That(conflict.Conflicts.Select(identity => identity.Id), Does.Contain(DefaultSeedModel.PartDefinitionId)); + } + + [Test] + public async Task VerifyPostBranchByProjectAsync() + { + var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "feature", Head = this.seedCommitId }); + + var branches = await this.client.GetBranchesByProjectAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(branch.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(branch.Head, Is.EqualTo(this.seedCommitId)); + Assert.That(branches.Records.Select(record => record.Id), Does.Contain(branch.Id)); + } + + await Assert.ThatAsync(() => this.client.PostBranchByProjectAsync(UnknownId, new BranchRequest { Name = "feature", Head = this.seedCommitId }), ThrowsClientError()); + } + + [Test] + public async Task VerifyPostCommitByProjectAsync() + { + var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "rename", Head = this.seedCommitId }); + + var commit = await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Car"), branch.Id); + + var committedBranch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, branch.Id); + var renamed = await this.client.GetElementByProjectCommitIdAsync(this.projectId, commit.Id, DefaultSeedModel.PartDefinitionId); + var original = await this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PartDefinitionId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(commit.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(commit.PreviousCommit, Is.EquivalentTo([this.seedCommitId])); + Assert.That(committedBranch.Head, Is.EqualTo(commit.Id)); + Assert.That(renamed, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo("Car")); + Assert.That(original, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo(DefaultSeedModel.PartDefinitionName)); + } + + await Assert.ThatAsync(() => this.client.PostCommitByProjectAsync(UnknownId, DefaultSeedModel.CreateRenameRequest("Car")), ThrowsClientError()); + } + + [Test] + public async Task VerifyPostProjectAsync() + { + var project = await this.CreateProjectAsync(); + + var readProject = await this.client.GetProjectByIdAsync(project.Id); + + using (Assert.EnterMultipleScope()) + { + Assert.That(project.Name, Does.StartWith(ProjectNamePrefix)); + Assert.That(project.Description, Is.EqualTo(ProjectDescription)); + Assert.That(project.DefaultBranch, Is.Not.EqualTo(Guid.Empty)); + Assert.That(readProject.Name, Is.EqualTo(project.Name)); + } + } + + [Test] + public async Task VerifyPostQueryByProjectAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + + using (Assert.EnterMultipleScope()) + { + Assert.That(query.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(query.Select, Is.EquivalentTo(SelectedProperties)); + Assert.That(query.Where, Is.TypeOf()); + } + + await Assert.ThatAsync(() => this.client.PostQueryByProjectAsync(UnknownId, CreatePartDefinitionQuery()), ThrowsClientError()); + } + + [Test] + public async Task VerifyPostTagByProjectAsync() + { + var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "seed", TaggedCommit = this.seedCommitId }); + + using (Assert.EnterMultipleScope()) + { + Assert.That(tag.OwningProject, Is.EqualTo(this.projectId)); + Assert.That(tag.TaggedCommit, Is.EqualTo(this.seedCommitId)); + } + + await Assert.ThatAsync(() => this.client.PostTagByProjectAsync(UnknownId, new TagRequest { Name = "seed", TaggedCommit = this.seedCommitId }), ThrowsClientError()); + } + + [Test] + public async Task VerifyPutProjectByIdAsync() + { + var name = $"{ProjectNamePrefix}{Guid.NewGuid()}"; + + var project = await this.client.PutProjectByIdAsync(this.projectId, new ProjectRequest { Name = name, DefaultBranch = this.defaultBranchId }); + + var readProject = await this.client.GetProjectByIdAsync(this.projectId); + + using (Assert.EnterMultipleScope()) + { + Assert.That(project.Id, Is.EqualTo(this.projectId)); + Assert.That(project.Name, Is.EqualTo(name)); + Assert.That(readProject.Name, Is.EqualTo(name)); + } + + await Assert.ThatAsync(() => this.client.PutProjectByIdAsync(UnknownId, new ProjectRequest { Name = name }), ThrowsNotFound()); + } + + [Test] + public async Task VerifyPutQueryByProjectAndIdAsync() + { + var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); + var request = CreatePartDefinitionQuery(); + request.Name = "renamed"; + + var updatedQuery = await this.client.PutQueryByProjectAndIdAsync(this.projectId, query.Id, request); + + using (Assert.EnterMultipleScope()) + { + Assert.That(updatedQuery.Id, Is.EqualTo(query.Id)); + Assert.That(updatedQuery.Name, Is.EqualTo("renamed")); + } + + await Assert.ThatAsync(() => this.client.PutQueryByProjectAndIdAsync(this.projectId, UnknownId, request), ThrowsNotFound()); + } + + private static QueryRequest CreateIdentifierQuery(Guid elementId) + { + return new QueryRequest + { + Select = [.. SelectedProperties], + Where = new PrimitiveConstraintRequest + { + Property = "@id", + Operator = PrimitiveConstraintRequestOperator.equalto, + Inverse = false, + Value = [elementId.ToString()] + } + }; + } + + private static QueryRequest CreatePartDefinitionQuery() + { + return new QueryRequest + { + Name = "part definitions", + Select = [.. SelectedProperties], + Where = new PrimitiveConstraintRequest + { + Property = "@type", + Operator = PrimitiveConstraintRequestOperator.equalto, + Inverse = false, + Value = ["PartDefinition"] + } + }; + } + + private static async Task GetOrNullWhenNotFoundAsync(Func> read) where T : class + { + try + { + return await read(); + } + catch (PsmApiException exception) when (exception.StatusCode == HttpStatusCode.NotFound) + { + return null; + } + } + + private static async Task SupportedAsync(Func> operation) + { + try + { + return await operation(); + } + catch (PsmApiException exception) when (exception.StatusCode is HttpStatusCode.MethodNotAllowed or HttpStatusCode.NotImplemented) + { + Assert.Inconclusive($"The server does not support the operation: {(int)exception.StatusCode} {exception.StatusCode}"); + throw; + } + } + + private static IResolveConstraint ThrowsClientError() + { + return Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).InRange(HttpStatusCode.BadRequest, LastClientError); + } + + private static IResolveConstraint ThrowsNotFound() + { + return Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound); + } + + private async Task CreateProjectAsync() + { + var project = await this.client.PostProjectAsync(new ProjectRequest { Name = $"{ProjectNamePrefix}{Guid.NewGuid()}", Description = ProjectDescription }); + + this.createdProjectIds.Add(project.Id); + + return project; + } + + private async Task> ReadDatatypeIdsAsync() + { + var datatypeIds = new List(); + Page? pageAfter = null; + + do + { + var page = await this.client.GetDatatypesAsync(new QueryParameters { PageSize = DatatypePageSize, PageAfter = pageAfter }); + + foreach (var datatypes in page.Records) + { + using (datatypes) + { + datatypeIds.AddRange(datatypes.RootElement.GetProperty("$defs").EnumerateObject().Select(datatype => datatype.Name)); + } + } + + pageAfter = page.Next; + } + while (pageAfter.HasValue); + + return datatypeIds; + } + } +} diff --git a/SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs b/SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs deleted file mode 100644 index 1a3cffb29..000000000 --- a/SySML2.NET.REST.Tests/Integration/PsmApiClientReadIntegrationTestFixture.cs +++ /dev/null @@ -1,516 +0,0 @@ -// ------------------------------------------------------------------------------------------------ -// -// -// Copyright (C) 2022-2026 Starion Group S.A. -// SPDX-License-Identifier: Apache-2.0 -// -// -// ------------------------------------------------------------------------------------------------ - -namespace SySML2.NET.REST.Tests.Integration -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Net; - using System.Text.Json; - using System.Threading.Tasks; - - using NUnit.Framework; - - using SysML2.NET.Core.DTO.Root.Elements; - using SysML2.NET.PSM; - using SysML2.NET.PSM.DTO; - using SysML2.NET.PSM.Enumerations; - using SysML2.NET.REST.Exceptions; - - [TestFixture] - [Category("Integration")] - public class PsmApiClientReadIntegrationTestFixture - { - private const int PageSize = 2; - - private const int DiscoveryPageLimit = 40; - - private static readonly Guid UnknownId = Guid.Parse("5b0c3f1e-7d2a-4e9b-8f6c-1a2b3c4d5e6f"); - - private LiveServerTestHost host; - - private IPsmApiClient client; - - private Guid projectId; - - private Guid branchId; - - private Guid commitId; - - private IElement root; - - private Query query; - - private Tag tag; - - [OneTimeSetUp] - public async Task OneTimeSetUp() - { - this.host = new LiveServerTestHost(); - - await this.host.OpenAsync(); - - this.client = this.host.Client; - - foreach (var project in await this.DiscoverProjectsAsync()) - { - if (this.root is null) - { - await this.TrySelectProjectAsync(project); - } - - if (this.query is null) - { - var queries = await this.client.GetQueriesByProjectAsync(project.Id, new QueryParameters { PageSize = 1 }); - this.query = queries.Records.Count > 0 ? queries.Records[0] : null; - } - - if (this.tag is null) - { - var tags = await this.client.GetTagsByProjectAsync(project.Id, new QueryParameters { PageSize = 1 }); - this.tag = tags.Records.Count > 0 ? tags.Records[0] : null; - } - } - - if (this.root is null) - { - Assert.Ignore("No readable project has a default branch whose head commit has root elements"); - } - - await TestContext.Out.WriteLineAsync($"Reading project {this.projectId}, branch {this.branchId}, commit {this.commitId}, query {this.query?.Id}, tag {this.tag?.Id}"); - } - - [OneTimeTearDown] - public void OneTimeTearDown() - { - this.host.Dispose(); - } - - [Test] - public async Task VerifyGetProjectsAsync() - { - var firstPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(firstPage.Records, Has.Count.EqualTo(PageSize)); - Assert.That(firstPage.Next, Is.Not.Null); - } - - var secondPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize, PageAfter = firstPage.Next }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(secondPage.Records, Is.Not.Empty); - Assert.That(secondPage.Records.Select(project => project.Id), Has.None.AnyOf(firstPage.Records.Select(project => project.Id).ToArray())); - Assert.That(secondPage.Previous, Is.Not.Null); - } - - var previousPage = await this.client.GetProjectsAsync(new QueryParameters { PageSize = PageSize, PageBefore = secondPage.Previous }); - - Assert.That(previousPage.Records.Select(project => project.Id), Is.EqualTo(firstPage.Records.Select(project => project.Id))); - } - - [Test] - public async Task VerifyGetProjectByIdAsync() - { - var project = await this.client.GetProjectByIdAsync(this.projectId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(project.Id, Is.EqualTo(this.projectId)); - Assert.That(project.DefaultBranch, Is.EqualTo(this.branchId)); - } - - await Assert.ThatAsync(() => this.client.GetProjectByIdAsync(UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetBranchesByProjectAsync() - { - var branches = await this.client.GetBranchesByProjectAsync(this.projectId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(branches.Records.Select(branch => branch.Id), Does.Contain(this.branchId)); - Assert.That(branches.Records.Select(branch => branch.OwningProject), Is.All.EqualTo(this.projectId)); - } - - await AssertEmptyOrNotFoundAsync(() => this.client.GetBranchesByProjectAsync(UnknownId)); - } - - [Test] - public async Task VerifyGetBranchesByProjectAndIdAsync() - { - var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, this.branchId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(branch.Id, Is.EqualTo(this.branchId)); - Assert.That(branch.Head, Is.EqualTo(this.commitId)); - Assert.That(branch.OwningProject, Is.EqualTo(this.projectId)); - } - - await Assert.ThatAsync(() => this.client.GetBranchesByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetTagsByProjectAsync() - { - var tags = await this.client.GetTagsByProjectAsync(this.projectId); - - Assert.That(tags.Records.Select(tag => tag.OwningProject), Is.All.EqualTo(this.projectId)); - - await AssertEmptyOrNotFoundAsync(() => this.client.GetTagsByProjectAsync(UnknownId)); - } - - [Test] - public async Task VerifyGetCommitsByProjectAsync() - { - var commits = await this.client.GetCommitsByProjectAsync(this.projectId, new QueryParameters { PageSize = PageSize }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(commits.Records, Has.Count.InRange(1, PageSize)); - Assert.That(commits.Records.Select(commit => commit.OwningProject), Is.All.EqualTo(this.projectId)); - } - - await AssertEmptyOrNotFoundAsync(() => this.client.GetCommitsByProjectAsync(UnknownId)); - } - - [Test] - public async Task VerifyGetCommitByProjectAndIdAsync() - { - var commit = await this.client.GetCommitByProjectAndIdAsync(this.projectId, this.commitId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(commit.Id, Is.EqualTo(this.commitId)); - Assert.That(commit.OwningProject, Is.EqualTo(this.projectId)); - } - - await Assert.ThatAsync(() => this.client.GetCommitByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetChangesByProjectCommitAsync() - { - var changes = await this.client.GetChangesByProjectCommitAsync(this.projectId, this.commitId, new QueryParameters { PageSize = PageSize }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(changes.Records, Has.Count.InRange(0, PageSize)); - Assert.That(changes.Records.Select(change => change.Identity), Has.None.Null); - } - - await AssertEmptyOrNotFoundAsync(() => this.client.GetChangesByProjectCommitAsync(this.projectId, UnknownId)); - } - - [Test] - public async Task VerifyGetChangeByProjectCommitIdAsync() - { - var changes = await this.client.GetChangesByProjectCommitAsync(this.projectId, this.commitId, new QueryParameters { PageSize = 1 }); - - if (changes.Records.Count > 0) - { - var change = await this.client.GetChangeByProjectCommitIdAsync(this.projectId, this.commitId, changes.Records[0].Id); - - Assert.That(change.Id, Is.EqualTo(changes.Records[0].Id)); - } - - await Assert.ThatAsync(() => this.client.GetChangeByProjectCommitIdAsync(this.projectId, this.commitId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetElementsByProjectCommitAsync() - { - var firstPage = await this.client.GetElementsByProjectCommitAsync(this.projectId, this.commitId, queryParameters: new QueryParameters { PageSize = PageSize }); - - Assert.That(firstPage.Records, Has.Count.InRange(1, PageSize)); - - if (firstPage.Next is { } next) - { - var secondPage = await this.client.GetElementsByProjectCommitAsync(this.projectId, this.commitId, queryParameters: new QueryParameters { PageSize = PageSize, PageAfter = next }); - - Assert.That(secondPage.Records.Select(element => element.Id), Has.None.AnyOf(firstPage.Records.Select(element => element.Id).ToArray())); - } - - await AssertEmptyOrNotFoundAsync(() => this.client.GetElementsByProjectCommitAsync(this.projectId, UnknownId)); - } - - [Test] - public async Task VerifyGetElementByProjectCommitIdAsync() - { - var element = await this.client.GetElementByProjectCommitIdAsync(this.projectId, this.commitId, this.root.Id); - - using (Assert.EnterMultipleScope()) - { - Assert.That(element.Id, Is.EqualTo(this.root.Id)); - Assert.That(element.GetType(), Is.EqualTo(this.root.GetType())); - } - - await Assert.ThatAsync(() => this.client.GetElementByProjectCommitIdAsync(this.projectId, this.commitId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetRootsByProjectCommitAsync() - { - var roots = await this.client.GetRootsByProjectCommitAsync(this.projectId, this.commitId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(roots.Records.Select(element => element.Id), Does.Contain(this.root.Id)); - Assert.That(roots.Records.Select(element => element.OwningRelationship), Is.All.Null); - } - - await AssertEmptyOrNotFoundAsync(() => this.client.GetRootsByProjectCommitAsync(this.projectId, UnknownId)); - } - - [Test] - public async Task VerifyGetRelationshipsByProjectCommitRelatedElementAsync() - { - var page = new QueryParameters { PageSize = PageSize }; - var outgoing = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, this.root.Id, Direction.@out, queryParameters: page); - var incoming = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, this.root.Id, Direction.@in, queryParameters: page); - var both = await this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, this.root.Id, Direction.both, queryParameters: page); - - using (Assert.EnterMultipleScope()) - { - Assert.That(outgoing.Records.Select(relationship => relationship.Source), Has.All.Contains(this.root.Id)); - Assert.That(incoming.Records.Select(relationship => relationship.Target), Has.All.Contains(this.root.Id)); - Assert.That(both.Records, Has.All.Matches(relationship => relationship.Source.Contains(this.root.Id) || relationship.Target.Contains(this.root.Id))); - } - - await AssertEmptyOrNotFoundAsync(() => this.client.GetRelationshipsByProjectCommitRelatedElementAsync(this.projectId, this.commitId, UnknownId)); - } - - [Test] - public async Task VerifyGetProjectUsageByProjectCommitElementAsync() - { - await Assert.ThatAsync(() => this.client.GetProjectUsageByProjectCommitElementAsync(this.projectId, this.commitId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetQueriesByProjectAsync() - { - var queries = await this.client.GetQueriesByProjectAsync(this.projectId); - - Assert.That(queries.Records.Select(query => query.OwningProject), Is.All.EqualTo(this.projectId)); - - await AssertEmptyOrNotFoundAsync(() => this.client.GetQueriesByProjectAsync(UnknownId)); - } - - [Test] - public async Task VerifyGetDatatypesAsync() - { - var datatypes = await this.client.GetDatatypesAsync(new QueryParameters { PageSize = PageSize }); - - try - { - Assert.That(datatypes.Records, Has.Count.InRange(1, PageSize)); - } - finally - { - foreach (var datatype in datatypes.Records) - { - datatype.Dispose(); - } - } - } - - [Test] - public async Task VerifyGetDatatypeByIdAsync() - { - const string datatypeId = "https://www.omg.org/spec/SysML/20250201/PartUsage"; - - using (var datatype = await this.client.GetDatatypeByIdAsync(datatypeId)) - { - Assert.That(datatype.RootElement.GetProperty("$id").GetString(), Is.EqualTo(datatypeId)); - } - - await Assert.ThatAsync(() => this.client.GetDatatypeByIdAsync($"{datatypeId}Unknown"), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetTagByProjectAndIdAsync() - { - await Assert.ThatAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - - if (this.tag is null) - { - Assert.Inconclusive("No readable project holds a tag"); - } - - var readTag = await this.client.GetTagByProjectAndIdAsync(this.tag.OwningProject, this.tag.Id); - - using (Assert.EnterMultipleScope()) - { - Assert.That(readTag.Id, Is.EqualTo(this.tag.Id)); - Assert.That(readTag.TaggedCommit, Is.EqualTo(this.tag.TaggedCommit)); - } - } - - [Test] - public async Task VerifyGetQueryByProjectAndIdAsync() - { - await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - - if (this.query is null) - { - Assert.Inconclusive("No readable project holds a query"); - } - - var readQuery = await this.client.GetQueryByProjectAndIdAsync(this.query.OwningProject, this.query.Id); - - using (Assert.EnterMultipleScope()) - { - Assert.That(readQuery.Id, Is.EqualTo(this.query.Id)); - Assert.That(readQuery.Select, Is.EqualTo(this.query.Select)); - Assert.That(readQuery.Where?.GetType(), Is.EqualTo(this.query.Where?.GetType())); - } - } - - [Test] - public async Task VerifyGetQueryResultsByProjectIdQueryIdAsync() - { - if (this.query is null) - { - Assert.Inconclusive("No readable project holds a query"); - } - - var queryProject = await this.client.GetProjectByIdAsync(this.query.OwningProject); - var queryBranch = await this.client.GetBranchesByProjectAndIdAsync(queryProject.Id, queryProject.DefaultBranch); - - var results = await this.client.GetQueryResultsByProjectIdQueryIdAsync(queryProject.Id, this.query.Id, queryBranch.Head, new QueryParameters { PageSize = PageSize }); - - Assert.That(results.Records, Has.Count.InRange(0, PageSize)); - - await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, UnknownId, this.commitId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetQueryResultsByProjectIdQueryAsync() - { - var request = CreateIdentifierQuery(this.root.Id); - - var results = await this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, request, this.commitId); - - Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([this.root.Id])); - - var unknownResults = await this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreateIdentifierQuery(UnknownId), this.commitId); - - Assert.That(unknownResults.Records, Is.Empty); - } - - [Test] - public async Task VerifyDiffAsync() - { - var differences = await this.client.DiffAsync(this.projectId, this.commitId, this.commitId); - - Assert.That(differences.Records, Is.Empty); - - await Assert.ThatAsync(() => this.client.DiffAsync(this.projectId, this.commitId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - private static QueryRequest CreateIdentifierQuery(Guid elementId) - { - return new QueryRequest - { - Select = ["@id", "@type"], - Where = new PrimitiveConstraintRequest - { - Property = "@id", - Operator = PrimitiveConstraintRequestOperator.equalto, - Inverse = false, - Value = [elementId.ToString()] - } - }; - } - - private static async Task AssertEmptyOrNotFoundAsync(Func>> query) - { - try - { - var result = await query(); - - Assert.That(result.Records, Is.Empty); - } - catch (PsmApiException exception) - { - Assert.That(exception.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); - } - } - - private async Task> DiscoverProjectsAsync() - { - var projects = new List(); - var parameters = new QueryParameters { PageSize = 1 }; - - for (var pageIndex = 0; pageIndex < DiscoveryPageLimit; pageIndex++) - { - PagedResult page; - - try - { - page = await this.client.GetProjectsAsync(parameters); - } - catch (JsonException exception) - { - await TestContext.Out.WriteLineAsync($"Project discovery stops at page {pageIndex + 1}, its record is not readable: {exception.Message}"); - break; - } - - projects.AddRange(page.Records); - - if (page.Next is not { } next) - { - break; - } - - parameters = new QueryParameters { PageSize = 1, PageAfter = next }; - } - - return projects; - } - - private async Task TrySelectProjectAsync(Project project) - { - var branch = await this.client.GetBranchesByProjectAndIdAsync(project.Id, project.DefaultBranch); - - if (branch.Head is not { } head) - { - return; - } - - var roots = await this.client.GetRootsByProjectCommitAsync(project.Id, head, queryParameters: new QueryParameters { PageSize = 1 }); - - if (roots.Records.Count == 0) - { - return; - } - - this.projectId = project.Id; - this.branchId = branch.Id; - this.commitId = head; - this.root = roots.Records[0]; - } - } -} diff --git a/SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs b/SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs deleted file mode 100644 index 599da75df..000000000 --- a/SySML2.NET.REST.Tests/Integration/PsmApiClientWriteIntegrationTestFixture.cs +++ /dev/null @@ -1,390 +0,0 @@ -// ------------------------------------------------------------------------------------------------ -// -// -// Copyright (C) 2022-2026 Starion Group S.A. -// SPDX-License-Identifier: Apache-2.0 -// -// -// ------------------------------------------------------------------------------------------------ - -namespace SySML2.NET.REST.Tests.Integration -{ - using System; - using System.Linq; - using System.Net; - using System.Text.Json; - using System.Threading.Tasks; - - using NUnit.Framework; - - using SysML2.NET.Core.DTO.Systems.Parts; - using SysML2.NET.PSM.DTO; - using SysML2.NET.PSM.Enumerations; - using SysML2.NET.REST.Exceptions; - - [TestFixture] - [Category("Integration")] - public class PsmApiClientWriteIntegrationTestFixture - { - private const string ProjectNamePrefix = "SysML2.NET-it-"; - - private static readonly Guid UnknownId = Guid.Parse("5b0c3f1e-7d2a-4e9b-8f6c-1a2b3c4d5e6f"); - - private static readonly string[] SelectedProperties = ["@id", "@type"]; - - private LiveServerTestHost host; - - private IPsmApiClient client; - - private Guid projectId; - - private Guid defaultBranchId; - - private Guid seedCommitId; - - [OneTimeSetUp] - public async Task OneTimeSetUp() - { - this.host = new LiveServerTestHost(); - - await this.host.OpenAsync(); - - this.client = this.host.Client; - - var project = await this.client.PostProjectAsync(new ProjectRequest { Name = $"{ProjectNamePrefix}{Guid.NewGuid()}", Description = "Throw-away project of the SysML2.NET integration tests" }); - - this.projectId = project.Id; - this.defaultBranchId = project.DefaultBranch; - this.seedCommitId = await this.CommitAsync(DefaultSeedModel.CreateCommitRequest(), this.defaultBranchId); - - await TestContext.Out.WriteLineAsync($"Writing to project {this.projectId}, seed commit {this.seedCommitId}"); - } - - [OneTimeTearDown] - public async Task OneTimeTearDown() - { - try - { - if (this.projectId != Guid.Empty) - { - await this.client.DeleteProjectByIdAsync(this.projectId); - } - } - finally - { - this.host.Dispose(); - } - } - - [Test] - public async Task VerifyPutProjectByIdAsync() - { - var name = $"{ProjectNamePrefix}{Guid.NewGuid()}"; - - var project = await this.client.PutProjectByIdAsync(this.projectId, new ProjectRequest { Name = name, DefaultBranch = this.defaultBranchId }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(project.Id, Is.EqualTo(this.projectId)); - Assert.That(project.Name, Is.EqualTo(name)); - Assert.That((await this.client.GetProjectByIdAsync(this.projectId)).Name, Is.EqualTo(name)); - } - - await Assert.ThatAsync(() => this.client.PutProjectByIdAsync(UnknownId, new ProjectRequest { Name = name }), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyDeleteProjectByIdAsync() - { - var project = await this.client.PostProjectAsync(new ProjectRequest { Name = $"{ProjectNamePrefix}{Guid.NewGuid()}" }); - - await this.client.DeleteProjectByIdAsync(project.Id); - - await Assert.ThatAsync(() => this.client.GetProjectByIdAsync(project.Id), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - - await Assert.ThatAsync(() => this.client.DeleteProjectByIdAsync(UnknownId), Throws.TypeOf()); - } - - [Test] - public async Task VerifyPostCommitByProjectAsync() - { - var commit = await this.client.PostCommitByProjectAsync(this.projectId, DefaultSeedModel.CreateRenameRequest("Car"), this.defaultBranchId); - var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, this.defaultBranchId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(commit.OwningProject, Is.EqualTo(this.projectId)); - Assert.That(branch.Head, Is.EqualTo(commit.Id)); - } - - await Assert.ThatAsync(() => this.client.PostCommitByProjectAsync(UnknownId, DefaultSeedModel.CreateRenameRequest("Car")), Throws.TypeOf()); - } - - [Test] - public async Task VerifyGetRootsByProjectCommitAsync() - { - var roots = await this.client.GetRootsByProjectCommitAsync(this.projectId, this.seedCommitId); - var partDefinition = await this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, DefaultSeedModel.PartDefinitionId); - - using (Assert.EnterMultipleScope()) - { - Assert.That(roots.Records.Select(root => root.Id), Is.EquivalentTo([DefaultSeedModel.PackageId])); - Assert.That(partDefinition, Is.TypeOf().With.Property(nameof(PartDefinition.DeclaredName)).EqualTo(DefaultSeedModel.PartDefinitionName)); - } - - await Assert.ThatAsync(() => this.client.GetElementByProjectCommitIdAsync(this.projectId, this.seedCommitId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyPostBranchByProjectAsync() - { - var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "feature", Head = this.seedCommitId }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(branch.OwningProject, Is.EqualTo(this.projectId)); - Assert.That(branch.Head, Is.EqualTo(this.seedCommitId)); - Assert.That((await this.client.GetBranchesByProjectAsync(this.projectId)).Records.Select(record => record.Id), Does.Contain(branch.Id)); - } - - await Assert.ThatAsync(() => this.client.PostBranchByProjectAsync(UnknownId, new BranchRequest { Name = "feature", Head = this.seedCommitId }), Throws.TypeOf()); - } - - [Test] - public async Task VerifyDeleteBranchByProjectAndIdAsync() - { - var branch = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "to-delete", Head = this.seedCommitId }); - - await this.client.DeleteBranchByProjectAndIdAsync(this.projectId, branch.Id); - - var deletedBranch = await GetOrNullWhenNotFoundAsync(() => this.client.GetBranchesByProjectAndIdAsync(this.projectId, branch.Id)); - - Assert.That(deletedBranch is null || deletedBranch.Deleted is not null, Is.True); - - await Assert.ThatAsync(() => this.client.DeleteBranchByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyPostTagByProjectAsync() - { - var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "seed", TaggedCommit = this.seedCommitId }); - - using (Assert.EnterMultipleScope()) - { - Assert.That(tag.OwningProject, Is.EqualTo(this.projectId)); - Assert.That(tag.TaggedCommit, Is.EqualTo(this.seedCommitId)); - } - - await Assert.ThatAsync(() => this.client.PostTagByProjectAsync(UnknownId, new TagRequest { Name = "seed", TaggedCommit = this.seedCommitId }), Throws.TypeOf()); - } - - [Test] - public async Task VerifyGetTagByProjectAndIdAsync() - { - var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "read", TaggedCommit = this.seedCommitId }); - - var readTag = await this.client.GetTagByProjectAndIdAsync(this.projectId, tag.Id); - - using (Assert.EnterMultipleScope()) - { - Assert.That(readTag.Id, Is.EqualTo(tag.Id)); - Assert.That(readTag.Name, Is.EqualTo("read")); - } - - await Assert.ThatAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyDeleteTagByProjectAndIdAsync() - { - var tag = await this.client.PostTagByProjectAsync(this.projectId, new TagRequest { Name = "to-delete", TaggedCommit = this.seedCommitId }); - - await this.client.DeleteTagByProjectAndIdAsync(this.projectId, tag.Id); - - var deletedTag = await GetOrNullWhenNotFoundAsync(() => this.client.GetTagByProjectAndIdAsync(this.projectId, tag.Id)); - - Assert.That(deletedTag is null || deletedTag.Deleted is not null, Is.True); - - await Assert.ThatAsync(() => this.client.DeleteTagByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyPostQueryByProjectAsync() - { - var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); - - using (Assert.EnterMultipleScope()) - { - Assert.That(query.OwningProject, Is.EqualTo(this.projectId)); - Assert.That(query.Select, Is.EquivalentTo(SelectedProperties)); - Assert.That(query.Where, Is.TypeOf()); - } - - await Assert.ThatAsync(() => this.client.PostQueryByProjectAsync(UnknownId, CreatePartDefinitionQuery()), Throws.TypeOf()); - } - - [Test] - public async Task VerifyGetQueryByProjectAndIdAsync() - { - var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); - - Assert.That((await this.client.GetQueryByProjectAndIdAsync(this.projectId, query.Id)).Id, Is.EqualTo(query.Id)); - - await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyPutQueryByProjectAndIdAsync() - { - var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); - var request = CreatePartDefinitionQuery(); - request.Name = "renamed"; - - Assert.That((await this.client.PutQueryByProjectAndIdAsync(this.projectId, query.Id, request)).Name, Is.EqualTo("renamed")); - - await Assert.ThatAsync(() => this.client.PutQueryByProjectAndIdAsync(this.projectId, UnknownId, request), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyDeleteQueryByProjectAndIdAsync() - { - var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); - - await this.client.DeleteQueryByProjectAndIdAsync(this.projectId, query.Id); - - await Assert.ThatAsync(() => this.client.GetQueryByProjectAndIdAsync(this.projectId, query.Id), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - - await Assert.ThatAsync(() => this.client.DeleteQueryByProjectAndIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetQueryResultsByProjectIdQueryIdAsync() - { - var query = await this.client.PostQueryByProjectAsync(this.projectId, CreatePartDefinitionQuery()); - - var results = await this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, query.Id, this.seedCommitId); - - Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); - - await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryIdAsync(this.projectId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyGetQueryResultsByProjectIdQueryAsync() - { - var results = await SupportedAsync(() => this.client.GetQueryResultsByProjectIdQueryAsync(this.projectId, CreatePartDefinitionQuery(), this.seedCommitId)); - - Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); - - await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryAsync(UnknownId, CreatePartDefinitionQuery()), Throws.TypeOf()); - } - - [Test] - public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() - { - var results = await this.client.GetQueryResultsByProjectIdQueryPostAsync(this.projectId, CreatePartDefinitionQuery(), this.seedCommitId); - - Assert.That(results.Records.Select(result => result.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); - - await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryPostAsync(UnknownId, CreatePartDefinitionQuery()), Throws.TypeOf()); - } - - [Test] - public async Task VerifyDiffAsync() - { - var renameCommitId = await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Truck"), this.defaultBranchId); - - var differences = await SupportedAsync(() => this.client.DiffAsync(this.projectId, this.seedCommitId, renameCommitId)); - - Assert.That(differences.Records.Select(difference => difference.CompareData?.Identity?.Id), Is.EquivalentTo([DefaultSeedModel.PartDefinitionId])); - - await Assert.ThatAsync(() => this.client.DiffAsync(this.projectId, this.seedCommitId, UnknownId), - Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound)); - } - - [Test] - public async Task VerifyMergeAsync() - { - var source = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "merge-source", Head = this.seedCommitId }); - var target = await this.client.PostBranchByProjectAsync(this.projectId, new BranchRequest { Name = "merge-target", Head = this.seedCommitId }); - var sourceCommitId = await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Bicycle"), source.Id); - - var merge = await SupportedAsync(() => this.client.MergeAsync(this.projectId, [sourceCommitId], target.Id, description: "merge")); - - Assert.That(merge.OwningProject, Is.EqualTo(this.projectId)); - - await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Boat"), source.Id); - var conflictingCommitId = await this.CommitAsync(DefaultSeedModel.CreateRenameRequest("Plane"), target.Id); - - await Assert.ThatAsync(() => this.client.MergeAsync(this.projectId, [conflictingCommitId], source.Id), Throws.TypeOf()); - } - - private static QueryRequest CreatePartDefinitionQuery() - { - return new QueryRequest - { - Name = "part definitions", - Select = [.. SelectedProperties], - Where = new PrimitiveConstraintRequest - { - Property = "@type", - Operator = PrimitiveConstraintRequestOperator.equalto, - Inverse = false, - Value = ["PartDefinition"] - } - }; - } - - private static async Task GetOrNullWhenNotFoundAsync(Func> read) where T : class - { - try - { - return await read(); - } - catch (PsmApiException exception) when (exception.StatusCode == HttpStatusCode.NotFound) - { - return null; - } - } - - private static async Task SupportedAsync(Func> operation) - { - try - { - return await operation(); - } - catch (PsmApiException exception) when (exception.StatusCode is HttpStatusCode.MethodNotAllowed or HttpStatusCode.NotImplemented) - { - Assert.Inconclusive($"The server does not support the operation: {(int)exception.StatusCode} {exception.StatusCode}"); - throw; - } - } - - private async Task CommitAsync(CommitRequest request, Guid branchId) - { - try - { - return (await this.client.PostCommitByProjectAsync(this.projectId, request, branchId)).Id; - } - catch (InvalidOperationException exception) - { - await TestContext.Out.WriteLineAsync($"The commit response could not be read, the head of the branch is used instead: {exception.Message}"); - - var branch = await this.client.GetBranchesByProjectAndIdAsync(this.projectId, branchId); - - return branch.Head ?? throw new InvalidOperationException("The branch has no head after the commit", exception); - } - } - } -} diff --git a/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs b/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs index 7cd9ec393..2c8ff879a 100644 --- a/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs +++ b/SySML2.NET.REST.Tests/PsmApiClientTestFixture.cs @@ -200,7 +200,7 @@ public async Task VerifyPostCommitByProjectAsync() this.mockHttp.Expect(HttpMethod.Post, commitsUri) .WithExactQueryString($"branchId={BranchId}") - .With(request => request.Content is { Headers.ContentType.MediaType: JsonMediaType } + .With(request => request.Content.Headers.ContentType.MediaType == JsonMediaType && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the commit description")) .Respond(HttpStatusCode.Created, JsonMediaType, ReadPayload("commit.json")); @@ -209,7 +209,7 @@ public async Task VerifyPostCommitByProjectAsync() Assert.That(commit.Id, Is.EqualTo(CommitId)); this.mockHttp.Expect(HttpMethod.Post, commitsUri) - .With(request => request.RequestUri != null && string.IsNullOrWhiteSpace(request.RequestUri.Query)) + .With(request => string.IsNullOrWhiteSpace(request.RequestUri.Query)) .Respond(HttpStatusCode.BadRequest, JsonMediaType, ErrorPayload); await Assert.ThatAsync(() => this.client.PostCommitByProjectAsync(ProjectId, commitRequest), @@ -312,7 +312,7 @@ public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() this.mockHttp.Expect(HttpMethod.Post, queryResultsUri) .WithExactQueryString($"page[size]=2&commitId={CommitId}") - .With(request => request.Content != null && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the query name")) + .With(request => request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the query name")) .Respond(JsonMediaType, ReadPayload("roots.json")); var results = await this.client.GetQueryResultsByProjectIdQueryPostAsync(ProjectId, queryRequest, CommitId, new QueryParameters { PageSize = 2 }); @@ -325,7 +325,7 @@ public async Task VerifyGetQueryResultsByProjectIdQueryPostAsync() } this.mockHttp.Expect(HttpMethod.Post, queryResultsUri) - .With(request => request.RequestUri != null && string.IsNullOrWhiteSpace(request.RequestUri.Query)) + .With(request => string.IsNullOrWhiteSpace(request.RequestUri.Query)) .Respond(HttpStatusCode.BadRequest, JsonMediaType, ErrorPayload); await Assert.ThatAsync(() => this.client.GetQueryResultsByProjectIdQueryPostAsync(ProjectId, queryRequest), @@ -341,7 +341,7 @@ public async Task VerifyPutProjectByIdAsync() var projectRequest = new ProjectRequest { Name = "the project name" }; this.mockHttp.Expect(HttpMethod.Put, projectUri) - .With(request => request.Content != null && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the project name")) + .With(request => request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains("the project name")) .Respond(JsonMediaType, ReadPayload("project.json")); var project = await this.client.PutProjectByIdAsync(ProjectId, projectRequest); @@ -377,7 +377,7 @@ public async Task VerifyMergeAsync() this.mockHttp.Expect(HttpMethod.Post, mergeUri) .WithExactQueryString($"sourceCommitId={CommitId},{OtherCommitId}&description=the%20merge%20description") - .With(request => request.Content != null && request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains(RootNamespaceId.ToString())) + .With(request => request.Content.ReadAsStringAsync().GetAwaiter().GetResult().Contains(RootNamespaceId.ToString())) .Respond(HttpStatusCode.Created, JsonMediaType, ReadPayload("commit.json")); var mergeCommit = await this.client.MergeAsync(ProjectId, [CommitId, OtherCommitId], BranchId, [new Namespace { Id = RootNamespaceId }], "the merge description"); From 31152a7f3a987f15d4d1a07ea01246df2fbf9ce1 Mon Sep 17 00:00:00 2001 From: Zeynep Delal Mutlu Date: Tue, 10 Nov 2026 16:39:23 +0100 Subject: [PATCH 3/3] sonarqube check --- .../Integration/PsmApiClientIntegrationTestFixture.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs b/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs index 5a32b48d3..0ceabf295 100644 --- a/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs +++ b/SySML2.NET.REST.Tests/Integration/PsmApiClientIntegrationTestFixture.cs @@ -740,12 +740,12 @@ private static async Task SupportedAsync(Func> operation) } } - private static IResolveConstraint ThrowsClientError() + private static RangeConstraint ThrowsClientError() { return Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).InRange(HttpStatusCode.BadRequest, LastClientError); } - private static IResolveConstraint ThrowsNotFound() + private static EqualConstraint ThrowsNotFound() { return Throws.TypeOf().With.Property(nameof(PsmApiException.StatusCode)).EqualTo(HttpStatusCode.NotFound); }