Repository navigation
Client api test for external server. #432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zeynepatsoprasteria
merged 3 commits into
development
from
407-integration-against-public-api
Nov 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // <copyright file="DefaultSeedModel.cs" company="Starion Group S.A."> | ||
| // | ||
| // Copyright (C) 2022-2026 Starion Group S.A. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| 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; | ||
|
|
||
| /// <summary> | ||
| /// Builds the deterministic default model committed to a live server: a root package owning one part definition | ||
| /// </summary> | ||
| public static class DefaultSeedModel | ||
| { | ||
| /// <summary> | ||
| /// Gets the identifier of the <see cref="OwningMembership"/> between the package and the part definition | ||
| /// </summary> | ||
| public static Guid MembershipId { get; } = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a61"); | ||
|
|
||
| /// <summary> | ||
| /// Gets the identifier of the root <see cref="Package"/> | ||
| /// </summary> | ||
| public static Guid PackageId { get; } = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a60"); | ||
|
|
||
| /// <summary> | ||
| /// Gets the identifier of the <see cref="PartDefinition"/> | ||
| /// </summary> | ||
| public static Guid PartDefinitionId { get; } = Guid.Parse("8c1f4b2a-3d5e-4f60-9a7b-1c2d3e4f5a62"); | ||
|
|
||
| /// <summary> | ||
| /// Gets the declared name of the <see cref="PartDefinition"/> in the seed commit | ||
| /// </summary> | ||
| public static string PartDefinitionName { get; } = "Vehicle"; | ||
|
|
||
| /// <summary> | ||
| /// Creates the <see cref="CommitRequest"/> that adds the whole default model | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The <see cref="CommitRequest"/> carrying one change per element | ||
| /// </returns> | ||
| 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)]); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates the <see cref="CommitRequest"/> that renames the <see cref="PartDefinition"/> | ||
| /// </summary> | ||
| /// <param name="name"> | ||
| /// The new declared name | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="CommitRequest"/> carrying the one changed element | ||
| /// </returns> | ||
| public static CommitRequest CreateRenameRequest(string name) | ||
| { | ||
| return CreateCommitRequest(string.Create(CultureInfo.InvariantCulture, $"Rename the part definition to {name}"), [CreatePartDefinition(name)]); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates the <see cref="PartDefinition"/> owned by the seed membership | ||
| /// </summary> | ||
| /// <param name="name"> | ||
| /// The declared name | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="PartDefinition"/> | ||
| /// </returns> | ||
| private static PartDefinition CreatePartDefinition(string name) | ||
| { | ||
| return new PartDefinition | ||
| { | ||
| Id = PartDefinitionId, | ||
| ElementId = PartDefinitionId.ToString(), | ||
| DeclaredName = name, | ||
| OwningRelationship = MembershipId | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Wraps each element in a <see cref="DataVersionRequest"/> of one <see cref="CommitRequest"/> | ||
| /// </summary> | ||
| /// <param name="description"> | ||
| /// The description of the commit | ||
| /// </param> | ||
| /// <param name="elements"> | ||
| /// The elements to commit | ||
| /// </param> | ||
| /// <returns> | ||
| /// The <see cref="CommitRequest"/> | ||
| /// </returns> | ||
| private static CommitRequest CreateCommitRequest(string description, IEnumerable<IElement> elements) | ||
| { | ||
| return new CommitRequest | ||
| { | ||
| Description = description, | ||
| Change = [.. elements.Select(element => new DataVersionRequest { Identity = new DataIdentityRequest { Id = element.Id }, Payload = element })] | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // <copyright file="LiveServerTestHost.cs" company="Starion Group S.A."> | ||
| // | ||
| // Copyright (C) 2022-2026 Starion Group S.A. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // </copyright> | ||
| // ------------------------------------------------------------------------------------------------ | ||
|
|
||
| namespace SySML2.NET.REST.Tests.Integration | ||
| { | ||
| using System; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| using SysML2.NET.Serializer.Json; | ||
|
|
||
| /// <summary> | ||
| /// Creates an <see cref="IPsmApiClient"/> on the Systems Modeling API server named by <see cref="BaseAddressVariable"/>, | ||
| /// ignoring the owning fixture when no server is configured or reachable, and failing it when the server refuses the probe | ||
| /// </summary> | ||
| public sealed class LiveServerTestHost : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// The name of the environment variable that holds the absolute base address of the server | ||
| /// </summary> | ||
| private const string BaseAddressVariable = "SYSML2_API_BASE_ADDRESS"; | ||
|
|
||
| /// <summary> | ||
| /// The <see cref="HttpClient"/> used by the <see cref="Client"/> | ||
| /// </summary> | ||
| private readonly HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(180) }; | ||
|
|
||
| /// <summary> | ||
| /// The <see cref="ILoggerFactory"/> that writes the warnings of the client to the console | ||
| /// </summary> | ||
| private readonly ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole()); | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="IPsmApiClient"/> bound to the server | ||
| /// </summary> | ||
| public IPsmApiClient Client { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Disposes the <see cref="HttpClient"/> and the <see cref="ILoggerFactory"/> | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| this.httpClient.Dispose(); | ||
| this.loggerFactory.Dispose(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 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 | ||
| /// </summary> | ||
| /// <returns> | ||
| /// An awaitable <see cref="Task"/> | ||
| /// </returns> | ||
| 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}/"); | ||
|
|
||
| HttpResponseMessage response = null; | ||
|
|
||
| try | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DTO
OwningMembershiphasownedMemberElement { get; internal set; }, so it cannot be set from the test project.memberElement,sourceandtargettherefore go out as00000000-0000-0000-0000-000000000000in every seed commit.Membership::memberElementis[1..1]. A server that validates the commit rejects it and the whole write fixture fails in setup; a server that stores it holds a broken membership, and no test reads the membership back.This is why #423 builds payloads as POCOs wired with
AssignOwnershipand converts them withToDto(includeDerivedProperties: true)(SySML2.NET.REST.Tests/Helpers/ModelBuilder.cs); itsEndpointRecorderflags exactly these empty references. Suggest building the seed withModelBuilderonce both are in. Related: #424.