This commit is contained in:
Meik
2025-11-11 11:03:42 +01:00
commit dc3e8a2e4c
582 changed files with 191465 additions and 0 deletions

View File

@@ -0,0 +1,176 @@
using C4IT.FASD.Base;
using C4IT.FASD.Cockpit.Communication;
using FasdDesktopUi.Basics.Services.RelationService;
using NSubstitute;
using ServiceUnderTest = FasdDesktopUi.Basics.Services.RelationService;
namespace F4SD.Cockpit.Client.Test.Basics.Sevices.RelationService;
public class RelationServiceTest
{
private readonly ServiceUnderTest.RelationService _relationService = new();
private readonly cFasdCockpitCommunicationBase _communication = Substitute.For<cFasdCockpitCommunicationBase>();
public RelationServiceTest()
{
cFasdCockpitCommunicationBase.Instance = _communication;
}
[Fact(Timeout = 2_000)]
public async Task LoadedRelation_IsIn_GetLoadedRelations_Test()
{
// Arrange
Guid taskGuid = Guid.NewGuid();
cF4sdApiSearchResultRelation mockRelation = new()
{
id = Guid.NewGuid(),
Type = enumF4sdSearchResultClass.User
};
_communication.StartGatheringRelations(Arg.Any<IEnumerable<cFasdApiSearchResultEntry>>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelationTaskId() { Id = taskGuid }));
_communication.GetStagedRelations(taskGuid, Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelations()
{
PendingInformationClasses = [],
Relations = [mockRelation]
}));
TaskCompletionSource tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
_relationService.RelationsFound += OnRelationsFound;
try
{
// Act
await _relationService.LoadRelationsAsync(null, TestContext.Current.CancellationToken);
// Assert
await tcs.Task;
var actual = _relationService.GetLoadedRelations();
Assert.Contains(mockRelation, actual);
}
finally
{
_relationService.RelationsFound -= OnRelationsFound;
}
void OnRelationsFound(object? s, EventArgs e)
{
tcs.TrySetResult();
}
}
[Fact(Timeout = 2_000)]
public async Task LoadRelations_Raises_RelationsFound()
{
// Arrange
Guid taskGuid = Guid.NewGuid();
cF4sdApiSearchResultRelation mockRelation = new()
{
id = Guid.NewGuid(),
Type = enumF4sdSearchResultClass.User
};
_communication.StartGatheringRelations(Arg.Any<IEnumerable<cFasdApiSearchResultEntry>>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelationTaskId() { Id = taskGuid }));
_communication.GetStagedRelations(taskGuid, Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelations()
{
PendingInformationClasses = [],
Relations = [mockRelation]
}));
TaskCompletionSource<StagedSearchResultRelationsEventArgs> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
_relationService.RelationsFound += OnRelationsFound;
try
{
await _relationService.LoadRelationsAsync(null, TestContext.Current.CancellationToken);
// Assert
var eventResult = await tcs.Task;
Assert.NotNull(eventResult);
Assert.Contains(mockRelation, eventResult.StagedResultRelations.Relations);
}
finally
{
_relationService.RelationsFound -= OnRelationsFound;
}
void OnRelationsFound(object? sender, StagedSearchResultRelationsEventArgs e)
{
tcs.TrySetResult(e);
}
}
[Fact(Timeout = 2_000)]
public async Task GetLoadedRelations_ContainsOnly_ActualRelations_Test()
{
// Arrange
Guid taskGuid = Guid.NewGuid();
cF4sdApiSearchResultRelation mockRelation1 = new()
{
id = Guid.NewGuid(),
Type = enumF4sdSearchResultClass.User
};
cF4sdApiSearchResultRelation mockRelation2 = new()
{
id = Guid.NewGuid(),
Type = enumF4sdSearchResultClass.Computer
};
_communication.StartGatheringRelations(Arg.Any<IEnumerable<cFasdApiSearchResultEntry>>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelationTaskId() { Id = taskGuid }));
TaskCompletionSource tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
try
{
// Act
// load relations first time
_communication.GetStagedRelations(taskGuid, Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelations()
{
PendingInformationClasses = [],
Relations = [mockRelation1]
}));
_relationService.RelationsFound += OnRelationsFound;
await _relationService.LoadRelationsAsync(null, TestContext.Current.CancellationToken);
await tcs.Task;
// load relations second time
tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
_communication.GetStagedRelations(taskGuid, Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new cF4sdStagedSearchResultRelations()
{
PendingInformationClasses = [],
Relations = [mockRelation2]
}));
await _relationService.LoadRelationsAsync(null, TestContext.Current.CancellationToken);
await tcs.Task;
// Assert
var actual = _relationService.GetLoadedRelations();
Assert.DoesNotContain(mockRelation1, actual);
Assert.Contains(mockRelation2, actual);
}
finally
{
_relationService.RelationsFound -= OnRelationsFound;
}
void OnRelationsFound(object? s, EventArgs e)
{
tcs.TrySetResult();
}
}
}

View File

@@ -0,0 +1,65 @@
using C4IT.FASD.Base;
using FasdDesktopUi.Basics.Services.RelationService;
using FasdDesktopUi.Basics.Services.SupportCase;
using NSubstitute;
namespace F4SD.Cockpit.Client.Test.Basics.Sevices.SupportCase;
public class SupportCaseTest
{
private readonly ISupportCase _supportCase;
private readonly IRelationService _relationService;
public SupportCaseTest()
{
_relationService = Substitute.For<IRelationService>();
cF4sdIdentityEntry primaryIdentity = new() { Class = enumFasdInformationClass.User, Id = Guid.NewGuid() };
_supportCase = SupportCaseFactory.Get(primaryIdentity, _relationService, null);
}
[Fact]
public void GetCaseRelations_Contains_AddCaseRelations()
{
var actualRelations = _supportCase.GetCaseRelations();
Assert.Empty(actualRelations);
ILookup<enumFasdInformationClass, cF4sdApiSearchResultRelation> relations = new List<cF4sdApiSearchResultRelation>()
{
new() { Type = enumF4sdSearchResultClass.Computer, Name = "My computer", Identities = [
new() { Id = Guid.NewGuid(), Class = enumFasdInformationClass.Computer }
] }
}.ToLookup(r => cF4sdIdentityEntry.GetFromSearchResult(r.Type), r => r);
_supportCase.AddCaseRelations(relations);
actualRelations = _supportCase.GetCaseRelations();
Assert.Single(actualRelations);
Assert.Contains(relations.FirstOrDefault(), actualRelations);
}
[Fact]
public void AddCaseRelations_Raise_CaseRelationsAdded()
{
ILookup<enumFasdInformationClass, cF4sdApiSearchResultRelation> relations = new List<cF4sdApiSearchResultRelation>()
{
new()
{
Type = enumF4sdSearchResultClass.Computer,
Name = "My computer",
Identities =
[
new() { Id = Guid.NewGuid(), Class = enumFasdInformationClass.Computer }
]
}
}.ToLookup(r => cF4sdIdentityEntry.GetFromSearchResult(r.Type), r => r);
var raisedEvent = Assert.Raises<RelationEventArgs>(
h => _supportCase.CaseRelationsAdded += h,
h => _supportCase.CaseRelationsAdded -= h,
() => _supportCase.AddCaseRelations(relations));
Assert.NotNull(raisedEvent);
Assert.Equal(_supportCase, raisedEvent.Sender);
Assert.Equal(relations, raisedEvent.Arguments.Relations);
}
}

View File

@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<RootNamespace>F4SD.Cockpit.Client.Test</RootNamespace>
<TargetFramework>net9.0</TargetFramework>
<!--
To enable the Microsoft Testing Platform 'dotnet test' experience, add property:
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
To enable the Microsoft Testing Platform native command line experience, add property:
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
For more information on Microsoft Testing Platform support in xUnit.net, please visit:
https://xunit.net/docs/getting-started/v3/microsoft-testing-platform
-->
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit.v3" Version="3.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FasdDesktopUi\F4SD-Cockpit-Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,3 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json"
}