Skip to content

Commit

Permalink
Merge pull request #160 from glhays/users/glhays/acceptance-init-project
Browse files Browse the repository at this point in the history
ACCEPTANCE: RESTFulSense WireMock Tests
  • Loading branch information
hassanhabib authored Mar 6, 2024
2 parents 00254ad + 1bc2ded commit 24d6aba
Show file tree
Hide file tree
Showing 20 changed files with 1,961 additions and 127 deletions.
15 changes: 15 additions & 0 deletions RESTFulSense.Tests.Acceptance/Models/TEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;

namespace RESTFulSense.Tests.Acceptance.Models
{
public class TEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public DateTimeOffset CreateDate { get; set; }
}
}
34 changes: 34 additions & 0 deletions RESTFulSense.Tests.Acceptance/RESTFulSense.Tests.Acceptance.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>

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

<ItemGroup>
<PackageReference Include="CleanMoq" Version="1.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.8" />
<PackageReference Include="WireMock.Net" Version="1.5.47" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RESTFulSense\RESTFulSense.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using RESTFulSense.Tests.Acceptance.Models;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using Xunit;

namespace RESTFulSense.Tests.Acceptance.Tests
{
public partial class RestfulApiClientTests
{

[Fact]
private async Task ShouldDeleteContentAsync()
{
// given
this.wiremockServer
.Given(Request.Create()
.WithPath(relativeUrl)
.UsingDelete())
.RespondWith(Response.Create()
.WithStatusCode(200));

// when . then
await this.restfulApiClient.DeleteContentAsync(
relativeUrl: relativeUrl);
}

[Fact]
private async Task ShouldDeleteContentCancellationTokenAsync()
{
// given
var cancellationToken = new CancellationToken(canceled: false);

this.wiremockServer
.Given(Request.Create()
.WithPath(relativeUrl)
.UsingDelete())
.RespondWith(Response.Create()
.WithStatusCode(200));

// when . then
await this.restfulApiClient.DeleteContentAsync(
relativeUrl: relativeUrl,
cancellationToken: cancellationToken);
}

[Fact]
private async Task ShouldDeleteContentReturnsExpectedContentDeserializeAsync()
{
// given
TEntity randomTEntity = GetRandomTEntity();
TEntity expectedDeletedTEntity = randomTEntity;

this.wiremockServer
.Given(Request.Create()
.WithPath(relativeUrl)
.UsingDelete())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBodyAsJson(randomTEntity));

// when
TEntity actualDeletedTEntity =
await this.restfulApiClient.DeleteContentAsync<TEntity>(
relativeUrl: relativeUrl,
deserializationFunction: DeserializationContentFunction);

// then
actualDeletedTEntity.Should().BeEquivalentTo(expectedDeletedTEntity);
}

[Fact]
private async Task ShouldDeleteContentReturnsExpectedContentCancellationTokenAsync()
{
// given
TEntity randomTEntity = GetRandomTEntity();
TEntity expectedDeletedTEntity = randomTEntity;
var cancellationToken = new CancellationToken(canceled: false);

this.wiremockServer
.Given(Request.Create()
.WithPath(relativeUrl)
.UsingDelete())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBodyAsJson(randomTEntity));

// when
TEntity actualDeletedTEntity =
await this.restfulApiClient.DeleteContentAsync<TEntity>(
relativeUrl: relativeUrl,
cancellationToken: cancellationToken);

// then
actualDeletedTEntity.Should().BeEquivalentTo(expectedDeletedTEntity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using RESTFulSense.Tests.Acceptance.Models;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using Xunit;

namespace RESTFulSense.Tests.Acceptance.Tests
{
public partial class RestfulApiClientTests
{

[Fact]
private async Task ShouldGetContentWithDeserializationFunctionAsync()
{
// given
TEntity randomTEntity = GetRandomTEntity();
TEntity expectedTEntity = randomTEntity;

this.wiremockServer.Given(Request.Create()
.WithPath(relativeUrl)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBodyAsJson(expectedTEntity));

// when
TEntity actualTEntityEntity =
await this.restfulApiClient.GetContentAsync<TEntity>(
relativeUrl: relativeUrl,
deserializationFunction: DeserializationContentFunction);

// then
actualTEntityEntity.Should().BeEquivalentTo(expectedTEntity);
}

[Fact]
private async Task ShouldCancelGetContentDeserializationIfCancellationInvokedAsync()
{
// given
TEntity someContent = GetRandomTEntity();
var expectedCanceledTaskException = new TaskCanceledException();
var taskCancelInvoked = new CancellationToken(canceled: true);

this.wiremockServer.Given(Request.Create()
.WithPath(relativeUrl)
.UsingGet())
.RespondWith(Response.Create()
.WithBodyAsJson(someContent));

// when
TaskCanceledException actualCanceledTask =
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
await this.restfulApiClient.GetContentAsync<TEntity>(
relativeUrl: relativeUrl,
cancellationToken: taskCancelInvoked,
deserializationFunction: DeserializationContentFunction));

// then
actualCanceledTask.Should().BeEquivalentTo(expectedCanceledTaskException);
}

[Fact]
private async Task ShouldReturnStringOnGetContentStringAsync()
{
// given
string someContent = CreateRandomContent();

this.wiremockServer.Given(Request.Create()
.WithPath(relativeUrl)
.UsingGet())
.RespondWith(Response.Create()
.WithBody(someContent));

// when
string actualContent =
await this.restfulApiClient.GetContentStringAsync(relativeUrl);

// then
actualContent.Should().BeEquivalentTo(someContent);
}

[Fact]
private async Task ShouldReturnStreamOnGetContentStreamAsync()
{
// given
string expectedContent = CreateRandomContent();

this.wiremockServer.Given(Request.Create()
.WithPath(relativeUrl)
.UsingGet())
.RespondWith(Response.Create()
.WithBody(expectedContent));

// when
Stream expectedContentStream =
await this.restfulApiClient.GetContentStreamAsync(
relativeUrl: relativeUrl);

string actualReadStream =
await ReadStreamToEndAsync(expectedContentStream);

// then
actualReadStream.Should().BeEquivalentTo(expectedContent);
}
}
}
Loading

0 comments on commit 24d6aba

Please sign in to comment.