Skip to content
Draft
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
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<PackageVersion Include="coverlet.collector" Version="10.1.0" />
<PackageVersion Include="coverlet.msbuild" Version="10.1.0" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="10.0.14" />
<PackageVersion Include="JsonSchema.Net" Version="9.4.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="10.0.14" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageVersion Include="Moq" Version="4.21.0" />
Expand All @@ -57,6 +58,7 @@
<PackageVersion Include="Serilog.Extensions.Logging" Version="10.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageVersion Include="XMLUnit.Core" Version="2.11.2" />
<PackageVersion Include="Testcontainers" Version="4.16.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.16.0" />
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
SysML2.NET
Copyright 2022-2025 Starion Group S.A.
Copyright 2022-2025 Starion Group S.A.

This product includes the file SySML2.NET.REST.Tests/Resources/Flexo/cluster.trig, taken from
Flexo MMS SysML v2 Microservice (https://github.com/Open-MBEE/flexo-mms-sysmlv2), commit 836d904d,
licensed under the Apache License, Version 2.0.
70 changes: 70 additions & 0 deletions SySML2.NET.REST.Tests/Helpers/BearerTokenHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ------------------------------------------------------------------------------------------------
// <copyright file="BearerTokenHandler.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.Helpers
{
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// A <see cref="DelegatingHandler"/> that adds a bearer token to every request, because the
/// <see cref="Session"/> has no authentication option of its own
/// </summary>
public sealed class BearerTokenHandler : DelegatingHandler
{
/// <summary>
/// The token that is added to every request
/// </summary>
private readonly string token;

/// <summary>
/// Initializes a new instance of the <see cref="BearerTokenHandler"/> class
/// </summary>
/// <param name="token">
/// The token that is added to every request
/// </param>
/// <param name="innerHandler">
/// The handler that sends the request on
/// </param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="token"/> is null or whitespace
/// </exception>
public BearerTokenHandler(string token, HttpMessageHandler innerHandler) : base(innerHandler)
{
if (string.IsNullOrWhiteSpace(token))
{
throw new ArgumentException("The token may not be null or whitespace", nameof(token));
}

this.token = token;
}

/// <summary>
/// Adds the bearer token and sends the request on
/// </summary>
/// <param name="request">
/// The request to send
/// </param>
/// <param name="cancellationToken">
/// The <see cref="CancellationToken"/> that cancels the operation
/// </param>
/// <returns>
/// The response of the server
/// </returns>
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.token);

return base.SendAsync(request, cancellationToken);
}
}
}
31 changes: 31 additions & 0 deletions SySML2.NET.REST.Tests/Helpers/ContractViolation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ------------------------------------------------------------------------------------------------
// <copyright file="ContractViolation.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.Helpers
{
/// <summary>
/// One way in which a request or a response breaks the OpenAPI document
/// </summary>
/// <param name="Kind">
/// The kind of violation
/// </param>
/// <param name="Message">
/// What was found, for assertion messages
/// </param>
public sealed record ContractViolation(ContractViolationKind Kind, string Message)
{
/// <summary>
/// Returns the violation as <c>Kind: Message</c>
/// </summary>
/// <returns>
/// The text of the violation
/// </returns>
public override string ToString() => $"{this.Kind}: {this.Message}";
}
}
57 changes: 57 additions & 0 deletions SySML2.NET.REST.Tests/Helpers/ContractViolationKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// ------------------------------------------------------------------------------------------------
// <copyright file="ContractViolationKind.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.Helpers
{
/// <summary>
/// The ways in which a request or a response can break the OpenAPI document
/// </summary>
public enum ContractViolationKind
{
/// <summary>
/// A query parameter that the operation does not declare
/// </summary>
UndeclaredParameter,

/// <summary>
/// A required query parameter that is missing
/// </summary>
MissingParameter,

/// <summary>
/// A query parameter whose value does not fit its schema
/// </summary>
InvalidParameter,

/// <summary>
/// A status that the operation declares neither explicitly nor through <c>default</c>
/// </summary>
UndeclaredStatus,

/// <summary>
/// A body, or its absence, where the document declares the opposite
/// </summary>
UnexpectedBody,

/// <summary>
/// A body whose media type is not declared
/// </summary>
UndeclaredMediaType,

/// <summary>
/// A body with a JSON media type that is not JSON
/// </summary>
InvalidJson,

/// <summary>
/// A JSON body that does not validate against its schema
/// </summary>
SchemaMismatch
}
}
192 changes: 192 additions & 0 deletions SySML2.NET.REST.Tests/Helpers/CoverageVerdict.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// ------------------------------------------------------------------------------------------------
// <copyright file="CoverageVerdict.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.Helpers
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

using SysML2.NET.Common;
using SysML2.NET.Core;

/// <summary>
/// Computes what the scenario has not yet covered, from what the server returned and the requests the client sent,
/// against lists taken from the generated DTOs and the OpenAPI document
/// </summary>
public sealed class CoverageVerdict
{
/// <summary>
/// A fresh instance per DTO class, holding the default value of every property
/// </summary>
private static readonly ConcurrentDictionary<Type, object> Defaults = new();

/// <summary>
/// Gets the properties whose value a metamodel constraint fixes, with that constraint; no well-formed model sets
/// them to another value or changes them
/// </summary>
public static IReadOnlyDictionary<string, string> FixedProperties { get; } = new Dictionary<string, string>
{
["CollectExpression.Operator"] = "KerML validateCollectExpressionOperator",
["FeatureChainExpression.Operator"] = "KerML validateFeatureChainExpressionOperator",
["IndexExpression.Operator"] = "KerML validateIndexExpressionOperator",
["SelectExpression.Operator"] = "KerML validateSelectExpressionOperator",
["ConnectionDefinition.IsSufficient"] = "SysML validateConnectionDefinitionIsSufficient",
["EnumerationDefinition.IsVariation"] = "SysML validateEnumerationDefinitionIsVariation",
["Expose.IsImportAll"] = "SysML validateExposeIsImportAll",
["Expose.Visibility"] = "SysML validateExposeVisibility",
["FramedConcernMembership.Kind"] = "SysML validateFramedConcernMembershipConstraintKind",
["RequirementVerificationMembership.Kind"] = "SysML validateRequirementVerificationMembershipKind"
};

/// <summary>
/// Gets the properties that no well-formed update of an element changes, with the reason
/// </summary>
public static IReadOnlyDictionary<string, string> UnchangeableProperties { get; } = new Dictionary<string, string>
{
["Element.ElementId"] = "KerML 8.3.2.1.2: it must not change during the lifetime of the Element"
};

/// <summary>
/// Initializes a new instance of the <see cref="CoverageVerdict"/> class
/// </summary>
/// <param name="readBack">
/// Every data item the server returned, with the commit it was read at
/// </param>
/// <param name="calls">
/// Every request the client sent
/// </param>
/// <param name="openApi">
/// The operations of the OpenAPI document
/// </param>
public CoverageVerdict(IReadOnlyCollection<(Guid CommitId, IData Data)> readBack, IReadOnlyCollection<EndpointCall> calls, OpenApiDocument openApi)
{
var elements = readBack.Where(entry => entry.Data != null).ToList();

var seenClasses = elements.Select(entry => entry.Data.GetType()).ToHashSet();
this.MissingMetaclasses = DtoMetadata.ConcreteClasses.Where(type => !seenClasses.Contains(type)).Select(type => type.Name).ToList();

this.UnsetProperties = DtoMetadata.DeclaredProperties
.Where(property => !FixedProperties.ContainsKey(property.Name))
.Where(property => !elements.Exists(entry => property.Interface.IsInstanceOfType(entry.Data) && !DtoMetadata.ValueEquals(property.ValueOf(entry.Data), property.ValueOf(DefaultOf(entry.Data.GetType())))))
.Select(property => property.Name)
.ToList();

var versions = elements.GroupBy(entry => entry.Data.Id).ToList();

this.UnchangedProperties = DtoMetadata.DeclaredProperties
.Where(property => !FixedProperties.ContainsKey(property.Name) && !UnchangeableProperties.ContainsKey(property.Name))
.Where(property => !versions.Exists(element => property.Interface.IsInstanceOfType(element.First().Data) && element.Select(entry => property.ValueOf(entry.Data)).Skip(1).Any(value => !DtoMetadata.ValueEquals(value, property.ValueOf(element.First().Data)))))
.Select(property => property.Name)
.ToList();

this.MissingLiterals = MissingEnumerationLiterals(elements.Select(entry => entry.Data)).ToList();

this.UncoveredOperations = openApi.Operations
.SelectMany(operation =>
{
var operationCalls = calls.Where(call => call.Operation == operation).ToList();
var missing = new List<string>();

if (!operationCalls.Exists(call => call.IsSuccess))
{
missing.Add($"{operation} (no 2xx)");
}

if (operation.DeclaresClientError && !operationCalls.Exists(call => call.IsClientError))
{
missing.Add($"{operation} (no 4xx)");
}

return missing;
})
.ToList();

this.UnmatchedRequests = calls.Where(call => call.Operation == null).Select(call => call.RequestLine).Distinct().ToList();
}

/// <summary>
/// Gets the concrete metaclasses of which the server returned no instance
/// </summary>
public IReadOnlyList<string> MissingMetaclasses { get; }

/// <summary>
/// Gets the non-derived properties that the server never returned with a non-default value
/// </summary>
public IReadOnlyList<string> UnsetProperties { get; }

/// <summary>
/// Gets the non-derived properties that the server never returned with two different values for one element
/// </summary>
public IReadOnlyList<string> UnchangedProperties { get; }

/// <summary>
/// Gets the enumeration literals that the server never returned
/// </summary>
/// <remarks>
/// The default literal of a non-nullable property counts as returned, as it cannot be told apart from a value
/// the server omitted.
/// </remarks>
public IReadOnlyList<string> MissingLiterals { get; }

/// <summary>
/// Gets the OpenAPI operations not yet called with a success answer, or with a client error answer where the
/// document declares one
/// </summary>
public IReadOnlyList<string> UncoveredOperations { get; }

/// <summary>
/// Gets the requests that addressed no operation of the OpenAPI document
/// </summary>
public IReadOnlyList<string> UnmatchedRequests { get; }

/// <summary>
/// Gets a fresh instance of a DTO class, whose property values are the defaults
/// </summary>
/// <param name="dtoType">
/// The DTO class
/// </param>
/// <returns>
/// The fresh instance
/// </returns>
private static object DefaultOf(Type dtoType) => Defaults.GetOrAdd(dtoType, type => Activator.CreateInstance(type));

/// <summary>
/// Computes the literals of the metamodel enumerations that no returned element holds; the enumerations are
/// those typing a non-derived property, which excludes <see cref="ClassKind"/>
/// </summary>
/// <param name="elements">
/// The returned elements
/// </param>
/// <returns>
/// The missing literals as <c>Enumeration.literal</c>
/// </returns>
private static IEnumerable<string> MissingEnumerationLiterals(IEnumerable<IData> elements)
{
var enumerationProperties = DtoMetadata.DeclaredProperties
.Where(property => (Nullable.GetUnderlyingType(property.Property.PropertyType) ?? property.Property.PropertyType).IsEnum)
.ToList();

var enumerations = enumerationProperties
.Select(property => Nullable.GetUnderlyingType(property.Property.PropertyType) ?? property.Property.PropertyType)
.Distinct();

var observed = elements
.SelectMany(element => enumerationProperties.Where(property => property.Interface.IsInstanceOfType(element)).Select(property => property.ValueOf(element)))
.Where(value => value != null)
.ToHashSet();

return enumerations
.SelectMany(enumeration => Enum.GetValues(enumeration).Cast<object>())
.Where(literal => !observed.Contains(literal))
.Select(literal => $"{literal.GetType().Name}.{literal}");
}
}
}
Loading
Loading