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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MX.Api.Abstractions;

namespace XtremeIdiots.Portal.Integrations.Servers.Abstractions.Interfaces.V1;

public interface IApiHealthApi
{
Task<ApiResult> CheckHealth(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MX.Api.Abstractions;
using XtremeIdiots.Portal.Integrations.Servers.Abstractions.Models.V1;

namespace XtremeIdiots.Portal.Integrations.Servers.Abstractions.Interfaces.V1;

public interface IApiInfoApi
{
Task<ApiResult<ApiInfoDto>> GetApiInfo(CancellationToken cancellationToken = default);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace XtremeIdiots.Portal.Integrations.Servers.Abstractions.Models.V1;

public class ApiInfoDto
{
public string Version { get; set; } = string.Empty;
public string BuildVersion { get; set; } = string.Empty;
public string AssemblyVersion { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Net;
using XtremeIdiots.Portal.Integrations.Servers.Api.Client.Testing;

namespace XtremeIdiots.Portal.Integrations.Servers.Api.Client.Testing.Tests;

[Trait("Category", "Unit")]
public class FakeApiHealthApiTests
{
[Fact]
public async Task CheckHealth_DefaultStatusCode_ReturnsSuccess()
{
var fakeApi = new FakeApiHealthApi();

var result = await fakeApi.CheckHealth();

Assert.True(result.IsSuccess);
}

[Fact]
public async Task CheckHealth_WithErrorStatusCode_ReturnsError()
{
var fakeApi = new FakeApiHealthApi();
fakeApi.WithStatusCode(HttpStatusCode.ServiceUnavailable);

var result = await fakeApi.CheckHealth();

Assert.False(result.IsSuccess);
}

[Fact]
public void Reset_RestoresDefaultStatusCode()
{
var fakeApi = new FakeApiHealthApi();
fakeApi.WithStatusCode(HttpStatusCode.InternalServerError);

fakeApi.Reset();

Assert.Equal(HttpStatusCode.OK, fakeApi.StatusCode);
}

[Fact]
public void FluentApi_SupportsChainingCalls()
{
var fakeApi = new FakeApiHealthApi();

var result = fakeApi.WithStatusCode(HttpStatusCode.OK);

Assert.Same(fakeApi, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
namespace XtremeIdiots.Portal.Integrations.Servers.Api.Client.Testing.Tests;

[Trait("Category", "Unit")]
public class FakeRootApiTests
public class FakeApiInfoApiTests
{
[Fact]
public async Task GetRoot_DefaultStatusCode_ReturnsSuccess()
public async Task GetApiInfo_DefaultStatusCode_ReturnsSuccess()
{
var fakeApi = new FakeRootApi();
var fakeApi = new FakeApiInfoApi();

var result = await fakeApi.GetRoot();
var result = await fakeApi.GetApiInfo();

Assert.True(result.IsSuccess);
}

[Fact]
public async Task GetRoot_WithErrorStatusCode_ReturnsError()
public async Task GetApiInfo_WithErrorStatusCode_ReturnsError()
{
var fakeApi = new FakeRootApi();
var fakeApi = new FakeApiInfoApi();
fakeApi.WithStatusCode(HttpStatusCode.ServiceUnavailable);

var result = await fakeApi.GetRoot();
var result = await fakeApi.GetApiInfo();

Assert.False(result.IsSuccess);
}

[Fact]
public void Reset_RestoresDefaultStatusCode()
{
var fakeApi = new FakeRootApi();
var fakeApi = new FakeApiInfoApi();
fakeApi.WithStatusCode(HttpStatusCode.InternalServerError);

fakeApi.Reset();
Expand All @@ -41,7 +41,7 @@ public void Reset_RestoresDefaultStatusCode()
[Fact]
public void FluentApi_SupportsChainingCalls()
{
var fakeApi = new FakeRootApi();
var fakeApi = new FakeApiInfoApi();

var result = fakeApi.WithStatusCode(HttpStatusCode.OK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ public void Maps_DelegatesToFakeMaps()
}

[Fact]
public void Root_DelegatesToFakeRoot()
public void ApiHealth_DelegatesToFakeApiHealth()
{
var fake = new FakeServersApiClient();
Assert.Same(fake.FakeRoot, fake.Root.V1);
Assert.Same(fake.FakeApiHealth, fake.ApiHealth.V1);
}

[Fact]
public void ApiInfo_DelegatesToFakeApiInfo()
{
var fake = new FakeServersApiClient();
Assert.Same(fake.FakeApiInfo, fake.ApiInfo.V1);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public void AddFakeServersApiClient_RegistersAllServices()
Assert.NotNull(provider.GetService<IVersionedQueryApi>());
Assert.NotNull(provider.GetService<IVersionedRconApi>());
Assert.NotNull(provider.GetService<IVersionedMapsApi>());
Assert.NotNull(provider.GetService<IVersionedRootApi>());
Assert.NotNull(provider.GetService<IVersionedApiHealthApi>());
Assert.NotNull(provider.GetService<IVersionedApiInfoApi>());
Assert.NotNull(provider.GetService<IApiHealthApi>());
Assert.NotNull(provider.GetService<IApiInfoApi>());
Assert.NotNull(provider.GetService<IQueryApi>());
Assert.NotNull(provider.GetService<IRconApi>());
Assert.NotNull(provider.GetService<IMapsApi>());
Assert.NotNull(provider.GetService<IRootApi>());
}

[Fact]
Expand Down Expand Up @@ -57,6 +59,7 @@ public void AddFakeServersApiClient_WithoutConfiguration_WorksWithDefaults()
Assert.NotNull(client.Query);
Assert.NotNull(client.Rcon);
Assert.NotNull(client.Maps);
Assert.NotNull(client.Root);
Assert.NotNull(client.ApiHealth);
Assert.NotNull(client.ApiInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace XtremeIdiots.Portal.Integrations.Servers.Api.Client.Testing;

/// <summary>
/// In-memory fake of <see cref="IRootApi"/> for unit and integration testing.
/// In-memory fake of <see cref="IApiHealthApi"/> for unit and integration testing.
/// </summary>
public class FakeRootApi : IRootApi
public class FakeApiHealthApi : IApiHealthApi
{
public HttpStatusCode StatusCode { get; private set; } = HttpStatusCode.OK;

public FakeRootApi WithStatusCode(HttpStatusCode statusCode)
public FakeApiHealthApi WithStatusCode(HttpStatusCode statusCode)
{
StatusCode = statusCode;
return this;
Expand All @@ -22,7 +22,7 @@ public void Reset()
StatusCode = HttpStatusCode.OK;
}

public Task<ApiResult> GetRoot()
public Task<ApiResult> CheckHealth(CancellationToken cancellationToken = default)
{
return Task.FromResult(StatusCode == HttpStatusCode.OK
? new ApiResult(HttpStatusCode.OK, new ApiResponse())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Net;
using MX.Api.Abstractions;
using XtremeIdiots.Portal.Integrations.Servers.Abstractions.Interfaces.V1;
using XtremeIdiots.Portal.Integrations.Servers.Abstractions.Models.V1;

namespace XtremeIdiots.Portal.Integrations.Servers.Api.Client.Testing;

/// <summary>
/// In-memory fake of <see cref="IApiInfoApi"/> for unit and integration testing.
/// </summary>
public class FakeApiInfoApi : IApiInfoApi
{
public HttpStatusCode StatusCode { get; private set; } = HttpStatusCode.OK;

public FakeApiInfoApi WithStatusCode(HttpStatusCode statusCode)
{
StatusCode = statusCode;
return this;
}

public void Reset()
{
StatusCode = HttpStatusCode.OK;
}

public Task<ApiResult<ApiInfoDto>> GetApiInfo(CancellationToken cancellationToken = default)
{
return Task.FromResult(StatusCode == HttpStatusCode.OK
? new ApiResult<ApiInfoDto>(HttpStatusCode.OK, new ApiResponse<ApiInfoDto>(new ApiInfoDto
{
Version = "1.0.0",
BuildVersion = "1.0.0",
AssemblyVersion = "1.0.0.0"
}))
: new ApiResult<ApiInfoDto>(StatusCode, new ApiResponse<ApiInfoDto>(new ApiError("ERROR", "Error"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@ public class FakeServersApiClient : IServersApiClient
public FakeQueryApi FakeQuery { get; } = new();
public FakeRconApi FakeRcon { get; } = new();
public FakeMapsApi FakeMaps { get; } = new();
public FakeRootApi FakeRoot { get; } = new();
public FakeApiHealthApi FakeApiHealth { get; } = new();
public FakeApiInfoApi FakeApiInfo { get; } = new();

private readonly Lazy<IVersionedQueryApi> _versionedQuery;
private readonly Lazy<IVersionedRconApi> _versionedRcon;
private readonly Lazy<IVersionedMapsApi> _versionedMaps;
private readonly Lazy<IVersionedRootApi> _versionedRoot;
private readonly Lazy<IVersionedApiHealthApi> _versionedApiHealth;
private readonly Lazy<IVersionedApiInfoApi> _versionedApiInfo;

public FakeServersApiClient()
{
_versionedQuery = new Lazy<IVersionedQueryApi>(() => new FakeVersionedQueryApi(FakeQuery));
_versionedRcon = new Lazy<IVersionedRconApi>(() => new FakeVersionedRconApi(FakeRcon));
_versionedMaps = new Lazy<IVersionedMapsApi>(() => new FakeVersionedMapsApi(FakeMaps));
_versionedRoot = new Lazy<IVersionedRootApi>(() => new FakeVersionedRootApi(FakeRoot));
_versionedApiHealth = new Lazy<IVersionedApiHealthApi>(() => new FakeVersionedApiHealthApi(FakeApiHealth));
_versionedApiInfo = new Lazy<IVersionedApiInfoApi>(() => new FakeVersionedApiInfoApi(FakeApiInfo));
}

public IVersionedQueryApi Query => _versionedQuery.Value;
public IVersionedRconApi Rcon => _versionedRcon.Value;
public IVersionedMapsApi Maps => _versionedMaps.Value;
public IVersionedRootApi Root => _versionedRoot.Value;
public IVersionedApiHealthApi ApiHealth => _versionedApiHealth.Value;
public IVersionedApiInfoApi ApiInfo => _versionedApiInfo.Value;

public void Reset()
{
FakeQuery.Reset();
FakeRcon.Reset();
FakeMaps.Reset();
FakeRoot.Reset();
FakeApiHealth.Reset();
FakeApiInfo.Reset();
}

private sealed class FakeVersionedQueryApi(IQueryApi v1) : IVersionedQueryApi
Expand All @@ -55,8 +60,13 @@ private sealed class FakeVersionedMapsApi(IMapsApi v1) : IVersionedMapsApi
public IMapsApi V1 => v1;
}

private sealed class FakeVersionedRootApi(IRootApi v1) : IVersionedRootApi
private sealed class FakeVersionedApiHealthApi(IApiHealthApi v1) : IVersionedApiHealthApi
{
public IRootApi V1 => v1;
public IApiHealthApi V1 => v1;
}

private sealed class FakeVersionedApiInfoApi(IApiInfoApi v1) : IVersionedApiInfoApi
{
public IApiInfoApi V1 => v1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@ public static IServiceCollection AddFakeServersApiClient(
services.RemoveAll<IVersionedQueryApi>();
services.RemoveAll<IVersionedRconApi>();
services.RemoveAll<IVersionedMapsApi>();
services.RemoveAll<IVersionedRootApi>();
services.RemoveAll<IVersionedApiHealthApi>();
services.RemoveAll<IVersionedApiInfoApi>();
services.RemoveAll<IApiHealthApi>();
services.RemoveAll<IApiInfoApi>();
services.RemoveAll<IQueryApi>();
services.RemoveAll<IRconApi>();
services.RemoveAll<IMapsApi>();
services.RemoveAll<IRootApi>();

// Register fakes as singletons
services.AddSingleton(fakeClient);
services.AddSingleton<IServersApiClient>(fakeClient);
services.AddSingleton<IVersionedQueryApi>(fakeClient.Query);
services.AddSingleton<IVersionedRconApi>(fakeClient.Rcon);
services.AddSingleton<IVersionedMapsApi>(fakeClient.Maps);
services.AddSingleton<IVersionedRootApi>(fakeClient.Root);
services.AddSingleton<IVersionedApiHealthApi>(fakeClient.ApiHealth);
services.AddSingleton<IVersionedApiInfoApi>(fakeClient.ApiInfo);
services.AddSingleton<IApiHealthApi>(fakeClient.FakeApiHealth);
services.AddSingleton<IApiInfoApi>(fakeClient.FakeApiInfo);
services.AddSingleton<IQueryApi>(fakeClient.FakeQuery);
services.AddSingleton<IRconApi>(fakeClient.FakeRcon);
services.AddSingleton<IMapsApi>(fakeClient.FakeMaps);
services.AddSingleton<IRootApi>(fakeClient.FakeRoot);

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void ServersApiClient_CanBeResolvedFromDI_Successfully()
Assert.NotNull(client.Query);
Assert.NotNull(client.Rcon);
Assert.NotNull(client.Maps);
Assert.NotNull(client.Root);
Assert.NotNull(client.ApiHealth);
Assert.NotNull(client.ApiInfo);
}

[Fact]
Expand Down Expand Up @@ -82,7 +83,7 @@ public void ServersApiClient_MapsApi_CanBeResolved()
}

[Fact]
public void ServersApiClient_RootApi_CanBeResolved()
public void ServersApiClient_ApiHealthApi_CanBeResolved()
{
var services = new ServiceCollection();
services.AddLogging();
Expand All @@ -92,9 +93,26 @@ public void ServersApiClient_RootApi_CanBeResolved()
});

var provider = services.BuildServiceProvider();
var rootApi = provider.GetRequiredService<IVersionedRootApi>();
var apiHealth = provider.GetRequiredService<IVersionedApiHealthApi>();

Assert.NotNull(rootApi);
Assert.NotNull(rootApi.V1);
Assert.NotNull(apiHealth);
Assert.NotNull(apiHealth.V1);
}

[Fact]
public void ServersApiClient_ApiInfoApi_CanBeResolved()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddServersApiClient(options =>
{
options.WithBaseUrl("https://localhost");
});

var provider = services.BuildServiceProvider();
var apiInfo = provider.GetRequiredService<IVersionedApiInfoApi>();

Assert.NotNull(apiInfo);
Assert.NotNull(apiInfo.V1);
}
}
Loading
Loading