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
89 changes: 85 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ env:
BUILD_CONFIGURATION: "Release"
SOLUTION_PATH: source/AAS.TwinEngine.DataEngine.sln
TEST_PROJECT: source/AAS.TwinEngine.DataEngine.UnitTests/AAS.TwinEngine.DataEngine.UnitTests.csproj
MODULE_TEST_PROJECT: source/AAS.TwinEngine.DataEngine.ModuleTests/AAS.TwinEngine.DataEngine.ModuleTests.csproj

jobs:

Expand All @@ -25,6 +26,7 @@ jobs:
runs-on: ubuntu-latest

permissions:
contents: read
checks: write
pull-requests: write

Expand All @@ -44,12 +46,91 @@ jobs:
run: dotnet build ${{ env.SOLUTION_PATH }} --configuration ${{ env.BUILD_CONFIGURATION }} --no-restore

- name: Run Unit Tests
run: dotnet test ${{ env.TEST_PROJECT }} --configuration Release --no-build --logger "trx;LogFileName=test_results.trx"
run: dotnet test ${{ env.TEST_PROJECT }} --configuration Release --no-build --logger "trx;LogFileName=unit_test_results.trx" --collect:"XPlat Code Coverage" --settings source/coverlet.runsettings

- name: Run Module Tests
run: dotnet test ${{ env.MODULE_TEST_PROJECT }} --configuration Release --no-build --logger "trx;LogFileName=module_test_results.trx" --collect:"XPlat Code Coverage" --settings source/coverlet.runsettings

# https://github.com/dorny/test-reporter
- name: Publish Test Results
uses: dorny/test-reporter@fe45e9537387dac839af0d33ba56eed8e24189e8 # v2.3.0
id: test-results
if: github.event.pull_request.head.repo.fork == false
with:
name: Unit Tests
path: "**/test_results.trx"
name: Test Results (Unit & Module)
path: "**/*_test_results.trx"
reporter: dotnet-trx

- name: Combine Reports (Test Results)
if: github.event.pull_request.head.repo.fork == false
run: |
echo "# Test & Coverage Report" > results.md
echo "" >> results.md
echo "## Test Results Summary" >> results.md
echo "" >> results.md
echo "| Metric | Count |" >> results.md
echo "|--------|-------|" >> results.md
echo "| ✅ Passed | ${{ steps.test-results.outputs.passed }} |" >> results.md
echo "| ❌ Failed | ${{ steps.test-results.outputs.failed }} |" >> results.md
echo "| ⏭️ Skipped | ${{ steps.test-results.outputs.skipped }} |" >> results.md
echo "" >> results.md
echo "[View Detailed Test Results](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})" >> results.md
echo "" >> results.md
echo "---" >> results.md
echo "" >> results.md

- name: Code Coverage Report - Unit Tests
if: github.event.pull_request.head.repo.fork == false
uses: irongut/CodeCoverageSummary@51cc3a756ddcd398d447c044c02cb6aa83fdae95 # v1.3.0
with:
filename: "source/AAS.TwinEngine.DataEngine.UnitTests/TestResults/*/coverage.cobertura.xml"
badge: false
fail_below_min: true
format: markdown
hide_branch_rate: false
hide_complexity: false
indicators: true
output: both
thresholds: "80 80"

- name: Combine Reports (Unit Test Coverage Results)
if: github.event.pull_request.head.repo.fork == false
run: |
echo "## Code Coverage" >> results.md
echo "" >> results.md
echo "### Unit Tests Coverage" >> results.md
cat code-coverage-results.md >> results.md
echo "" >> results.md

- name: Code Coverage Report - Module Tests
if: github.event.pull_request.head.repo.fork == false
uses: irongut/CodeCoverageSummary@51cc3a756ddcd398d447c044c02cb6aa83fdae95 # v1.3.0
with:
filename: "source/AAS.TwinEngine.DataEngine.ModuleTests/TestResults/*/coverage.cobertura.xml"
badge: false
fail_below_min: false
format: markdown
hide_branch_rate: false
hide_complexity: false
indicators: true
output: both

- name: Combine Reports (Module Test Coverage Results)
if: github.event.pull_request.head.repo.fork == false
run: |
echo "### Module Tests Coverage" >> results.md
cat code-coverage-results.md >> results.md


- name: Add PR Comment
if: github.event.pull_request.head.repo.fork == false
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0
with:
recreate: true
path: results.md

- name: Upload TRX as artifact (Fork PR fallback)
if: github.event.pull_request.head.repo.fork == true
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
name: test-results
path: "**/*_test_results.trx"
2 changes: 1 addition & 1 deletion source/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ dotnet_diagnostic.CA1846.severity = warning
dotnet_diagnostic.CA1847.severity = warning

# CA1848: Use the LoggerMessage delegates
dotnet_diagnostic.CA1848.severity = warning
dotnet_diagnostic.CA1848.severity = none

# CA1849: Call async methods when in an async method
dotnet_diagnostic.CA1849.severity = warning
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.20" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AAS.TwinEngine.DataEngine\AAS.TwinEngine.DataEngine.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json.Nodes;

using AAS.TwinEngine.DataEngine.ApplicationLogic.Exceptions.Infrastructure;
using AAS.TwinEngine.DataEngine.ApplicationLogic.Services.AasEnvironment.Providers;
using AAS.TwinEngine.DataEngine.ApplicationLogic.Services.Plugin;
using AAS.TwinEngine.DataEngine.ApplicationLogic.Services.Plugin.Providers;
using AAS.TwinEngine.DataEngine.Infrastructure.Http.Clients;
using AAS.TwinEngine.DataEngine.Infrastructure.Providers.PluginDataProvider.Config;
using AAS.TwinEngine.DataEngine.ModuleTests.ApplicationLogic.Services.AasRegistry;

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;

using NSubstitute;
using NSubstitute.ExceptionExtensions;

namespace AAS.TwinEngine.DataEngine.ModuleTests.Api.Services.AasRegistry;

public class ShellDescriptorControllerTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly ITemplateProvider _mockTemplateProvider;
private readonly HttpClient _client;
private readonly ICreateClient _httpClientFactory;

public ShellDescriptorControllerTests(WebApplicationFactory<Program> factory)
{
_mockTemplateProvider = Substitute.For<ITemplateProvider>();
var mockPluginManifestProvider = Substitute.For<IPluginManifestProvider>();
var mockPluginManifestConflictHandler = Substitute.For<IPluginManifestConflictHandler>();
_httpClientFactory = Substitute.For<ICreateClient>();

var factory1 = factory.WithWebHostBuilder(builder =>
{
_ = builder.ConfigureServices(services =>
{
_ = services.AddSingleton(_httpClientFactory);
_ = services.AddSingleton(mockPluginManifestProvider);
_ = services.AddSingleton(_mockTemplateProvider);
_ = services.AddSingleton(mockPluginManifestConflictHandler);
});
});

_client = factory1.CreateClient();
_ = mockPluginManifestConflictHandler.Manifests.Returns(TestData.CreatePluginManifests());
}

[Fact]
public async Task GetAllShellDescriptorsAsync_ReturnsOkAsync()
{
// Arrange
var template = TestData.CreateShellDescriptorsTemplate();
using var messageHandlerPlugin1 = new FakeHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(TestData.CreatePlugin1ResponseForShellDescriptors())
}));

using var messageHandlerPlugin2 = new FakeHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(TestData.CreatePlugin2ResponseForShellDescriptors())
}));

using var httpClientPlugin1 = new HttpClient(messageHandlerPlugin1);
httpClientPlugin1.BaseAddress = new Uri("https://testendpoint1.com");

using var httpClientPlugin2 = new HttpClient(messageHandlerPlugin2);
httpClientPlugin2.BaseAddress = new Uri("https://testendpoint2.com");

const string HttpClientNamePlugin1 = $"{PluginConfig.HttpClientNamePrefix}TestPlugin1";
_httpClientFactory.CreateClient(HttpClientNamePlugin1).Returns(httpClientPlugin1);

const string HttpClientNamePlugin2 = $"{PluginConfig.HttpClientNamePrefix}TestPlugin2";
_httpClientFactory.CreateClient(HttpClientNamePlugin2).Returns(httpClientPlugin2);

_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Returns(template);

// Act
var response = await _client.GetAsync("/shell-descriptors?limit=2&cursor=next123");

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var json = await response.Content.ReadFromJsonAsync<JsonObject>();
Assert.NotNull(json);
var shellDescriptorsResponse = json.ToString();
var expectedShellDescriptors = TestData.CreateShellDescriptors();
Assert.Equal(shellDescriptorsResponse, expectedShellDescriptors);
}

[Fact]
public async Task GetAllShellDescriptorsAsync_WithNagetiveLimit_Returns400Async()
{
_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Throws(new ResourceNotFoundException());

var response = await _client.GetAsync("/shell-descriptors?limit=-1&cursor=next123");

Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task GetAllShellDescriptorsAsync_WithInValidCursor_Returns400Async()
{
_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Throws(new ResourceNotFoundException());

var response = await _client.GetAsync("/shell-descriptors?limit=4&cursor=invalid cursor");

Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task GetAllShellDescriptorsAsync_WithNotFound_Returns404Async()
{
_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Throws(new ResourceNotFoundException());

var response = await _client.GetAsync("/shell-descriptors?limit=5&cursor=next123");

Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public async Task GetAllShellDescriptorsAsync_WithInternalServerError_Returns500Async()
{
_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Throws(new ResponseParsingException());

var response = await _client.GetAsync("/shell-descriptors?limit=5&cursor=next123");

Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}

[Fact]
public async Task GetShellDescriptorByIdAsync_ReturnsOkAsync()
{
// Arrange
const string AasId = "aHR0cHM6Ly9leGFtcGxlLmNvbS9pZHMvYWFzLzExNzBfMTE2MF8zMDUyXzY1Njg=";
var template = TestData.CreateShellDescriptorsTemplate();

using var messageHandler1 = new FakeHttpMessageHandler((_, _) => Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(TestData.CreatePlugin1ResponseForShellDescriptor())
}));

using var messageHandler2 = new FakeHttpMessageHandler((request, _) => Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.NotFound
}));

using var httpClient1 = new HttpClient(messageHandler1);
httpClient1.BaseAddress = new Uri("https://testendpoint1.com");

using var httpClient2 = new HttpClient(messageHandler2);
httpClient2.BaseAddress = new Uri("https://testendpoint2.com");

const string HttpClientName1 = $"{PluginConfig.HttpClientNamePrefix}TestPlugin1";
_httpClientFactory.CreateClient(HttpClientName1).Returns(httpClient1);

const string HttpClientName2 = $"{PluginConfig.HttpClientNamePrefix}TestPlugin2";
_httpClientFactory.CreateClient(HttpClientName2).Returns(httpClient2);

_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Returns(template);

// Act
var response = await _client.GetAsync($"/shell-descriptors/{AasId}");

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var json = await response.Content.ReadFromJsonAsync<JsonObject>();
Assert.NotNull(json);
var shellDescriptorResponse = json.ToString();
var expectedShellDescriptor = TestData.CreateShellDescriptor();
Assert.Equal(shellDescriptorResponse, expectedShellDescriptor);
}

[Fact]
public async Task GetShellDescriptorByIdAsync_WithNotFound_Returns404Async()
{
const string AasId = "aHR0cHM6Ly9leGFtcGxlLmNvbS9pZHMvYWFzLzExNzBfMTE2MF8zMDUyXzY1Njg=";

_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Throws(new ResourceNotFoundException());

var response = await _client.GetAsync($"/shell-descriptors/{AasId}");

Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public async Task GetShellDescriptorByIdAsync_WithInternalServerError_Returns500Async()
{
const string AasId = "aHR0cHM6Ly9leGFtcGxlLmNvbS9pZHMvYWFzLzExNzBfMTE2MF8zMDUyXzY1Njg=";

_ = _mockTemplateProvider.GetShellDescriptorsTemplateAsync(Arg.Any<CancellationToken>()).Throws(new ResponseParsingException());

var response = await _client.GetAsync($"/shell-descriptors/{AasId}");

Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}

[Fact]
public async Task GetShellDescriptorByIdAsync_WhenIdentifierIsInValid_Returns400Async()
{
const string AasId = "in valid";

var response = await _client.GetAsync($"/shell-descriptors/{AasId}");

Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
}

public class FakeHttpMessageHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> send) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
=> send(request, cancellationToken);
}

Loading
Loading