Skip to content

Commit 1e4ad8a

Browse files
authored
Add first Unit and Integration Tests (#2)
1 parent 60c2801 commit 1e4ad8a

8 files changed

Lines changed: 157 additions & 13 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ Then, you can use the client like this:
2929
```csharp
3030

3131
```
32+
33+
## Development
34+
35+
To run the integration test login to your docker to access the transformer.bee image.
36+
```bash
37+
docker login ghcr.io -u YOUR_GITHUB_USERNAME
38+
```
39+
then paste your PAT similarly to described in the [integration test CI pipeline](.github/workflows/integrationtests.yml)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Xunit;
3+
4+
namespace TransformerBeeClient.IntegrationTest;
5+
6+
/// <summary>
7+
/// A fixture that sets up the http client factory and an injectable service collection
8+
/// </summary>
9+
public class ClientFixture : IClassFixture<ClientFixture>
10+
{
11+
public readonly IHttpClientFactory HttpClientFactory;
12+
13+
public readonly ServiceCollection ServiceCollection;
14+
15+
public ClientFixture()
16+
{
17+
var services = new ServiceCollection();
18+
services.AddHttpClient("TransformerBee", client =>
19+
{
20+
client.BaseAddress = new Uri("http://localhost:5021"); // Check docker-compose.yml
21+
});
22+
var serviceProvider = services.BuildServiceProvider();
23+
ServiceCollection = services;
24+
HttpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
25+
}
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Xunit;
4+
5+
namespace TransformerBeeClient.IntegrationTest;
6+
7+
/// <summary>
8+
/// Tests that a connection to the API can be established
9+
/// </summary>
10+
public class ConnectionTests : IClassFixture<ClientFixture>
11+
{
12+
13+
private readonly ClientFixture _client;
14+
15+
public ConnectionTests(ClientFixture clientFixture)
16+
{
17+
_client = clientFixture;
18+
}
19+
20+
[Fact]
21+
public async Task IsAvailable_Returns_True_If_Service_Is_Available()
22+
{
23+
var httpClientFactory = _client.HttpClientFactory;
24+
var client = new TransformerBeeRestClient(httpClientFactory);
25+
var result = await client.IsAvailable();
26+
result.Should().BeTrue();
27+
}
28+
29+
[Fact]
30+
public async Task IsAvailable_Throws_Exception_If_Host_Is_Unavailable()
31+
{
32+
var services = new ServiceCollection();
33+
services.AddHttpClient("TransformerBee", client =>
34+
{
35+
client.BaseAddress = new Uri("http://localhost:1234"); // <-- no service running under this address
36+
});
37+
var serviceProvider = services.BuildServiceProvider();
38+
var client = new TransformerBeeRestClient(serviceProvider.GetService<IHttpClientFactory>());
39+
var checkIfIsAvailable = async () => await client.IsAvailable();
40+
await checkIfIsAvailable.Should().ThrowAsync<HttpRequestException>();
41+
}
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Xunit;
4+
5+
namespace TransformerBeeClient.UnitTest;
6+
7+
public class ConnectionTests
8+
{
9+
[Fact]
10+
public void IsAvailable_Throws_ArgumentNullException_If_BaseAddress_Is_Not_Configured()
11+
{
12+
var services = new ServiceCollection();
13+
services.AddHttpClient("TransformerBee", client =>
14+
{
15+
client.BaseAddress = null;
16+
});
17+
var serviceProvider = services.BuildServiceProvider();
18+
var instantiateClient = () => new TransformerBeeRestClient(serviceProvider.GetService<IHttpClientFactory>());
19+
instantiateClient.Should().Throw<ArgumentNullException>();
20+
}
21+
}

TransformerBeeClient/TransformerBeeClient.UnitTest/UnitTest1.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=de4efd69_002D9f4a_002D4e00_002D83fa_002Ddaa381865bd8/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="ConnectionTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
3+
&lt;TestAncestor&gt;&#xD;
4+
&lt;TestId&gt;xUnit::80EF1570-CB4E-4E56-A8BC-56C787A48543::net8.0::TransformerBeeClient.UnitTest.ConnectionTests&lt;/TestId&gt;&#xD;
5+
&lt;/TestAncestor&gt;&#xD;
6+
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace TransformerBeeClient;
2+
3+
/// <summary>
4+
/// a client for the transformer.bee REST API
5+
/// </summary>
6+
public class TransformerBeeRestClient
7+
{
8+
private readonly HttpClient _httpClient;
9+
10+
/// <summary>
11+
/// Provide the constructor with a http client factory.
12+
/// It will create a client from said factory and use the <paramref name="clientName"/> for that.
13+
/// </summary>
14+
/// <param name="httpClientFactory">factory to create the http client from</param>
15+
/// <param name="clientName">name used to create the client</param>
16+
public TransformerBeeRestClient(IHttpClientFactory httpClientFactory, string clientName = "TransformerBee")
17+
{
18+
_httpClient = httpClientFactory.CreateClient(clientName);
19+
if (_httpClient.BaseAddress == null)
20+
{
21+
throw new ArgumentNullException(nameof(httpClientFactory), $"The http client factory must provide a base address for the client with name '{clientName}'");
22+
}
23+
}
24+
25+
/// <summary>
26+
/// tests if transformer bee is available
27+
/// </summary>
28+
/// <remarks>
29+
/// Note that this does _not_ check if you're authenticated.
30+
/// The method will probably throw an <see cref="HttpRequestException"/> if the host cannot be found.
31+
/// </remarks>
32+
/// <returns>
33+
/// Returns true iff the transformer bee is available under the configured base address.
34+
/// </returns>
35+
public async Task<bool> IsAvailable()
36+
{
37+
var uriBuilder = new UriBuilder(_httpClient!.BaseAddress)
38+
{
39+
Path = "/version"
40+
};
41+
42+
var versionUrl = uriBuilder.Uri.AbsoluteUri;
43+
var response = await _httpClient.GetAsync(versionUrl);
44+
return response.IsSuccessStatusCode;
45+
}
46+
}

TransformerBeeClient/TransformerBeeClient/TransformerBeeClient.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@
66
<TargetFrameworks>net8.0;net6.0;net7.0</TargetFrameworks>
77
</PropertyGroup>
88

9+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
10+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
15+
</ItemGroup>
16+
917
</Project>

0 commit comments

Comments
 (0)