Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions SySML2.NET.REST.Tests/Integration/DefaultSeedModel.cs
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DTO OwningMembership has ownedMemberElement { get; internal set; }, so it cannot be set from the test project. memberElement, source and target therefore go out as 00000000-0000-0000-0000-000000000000 in every seed commit. Membership::memberElement is [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 AssignOwnership and converts them with ToDto(includeDerivedProperties: true) (SySML2.NET.REST.Tests/Helpers/ModelBuilder.cs); its EndpointRecorder flags exactly these empty references. Suggest building the seed with ModelBuilder once both are in. Related: #424.

{
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 })]
};
}
}
}
97 changes: 97 additions & 0 deletions SySML2.NET.REST.Tests/Integration/LiveServerTestHost.cs
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);
}
}
}
Loading
Loading