-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from wemogy/main
Release
- Loading branch information
Showing
9 changed files
with
314 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 11 additions & 2 deletions
13
...gy.CQRS.UnitTests/TestApplication/Commands/TrackUserLogin/TrackUserLoginCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Wemogy.CQRS.Commands.Abstractions; | ||
|
||
namespace Wemogy.CQRS.UnitTests.TestApplication.Commands.TrackUserLogin; | ||
|
||
public class TrackUserLoginCommandHandler : ICommandHandler<TrackUserLoginCommand> | ||
{ | ||
public static int CallCount { get; private set; } | ||
public static Dictionary<string, int> ExecutedCount { get; } = new (); | ||
|
||
public Task HandleAsync(TrackUserLoginCommand command) | ||
{ | ||
CallCount++; | ||
if (ExecutedCount.TryGetValue(command.UserId, out var count)) | ||
{ | ||
ExecutedCount[command.UserId] = count + 1; | ||
} | ||
else | ||
{ | ||
ExecutedCount[command.UserId] = 1; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...S.Extensions.FastEndpoints.UnitTests/HttpRemoteQueryRunners/HttpRemoteQueryRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using Moq; | ||
using RestSharp; | ||
using Wemogy.CQRS.Common.ValueObjects; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.Common; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.RemoteQueryRunners; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.Queries.RequestTestContext; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.ValueObjects; | ||
|
||
namespace Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.HttpRemoteQueryRunners; | ||
|
||
public class HttpRemoteQueryRunnerTests | ||
{ | ||
[Fact] | ||
public async Task QueryAsync_ShouldRetryRequestAndReturnResult() | ||
{ | ||
// Arrange | ||
var query = new RequestTestContextQuery(); | ||
var testContext = new TestContext() | ||
{ | ||
UserId = Guid.NewGuid().ToString() | ||
}; | ||
var resultContent = JsonSerializer.Serialize(testContext, JsonOptions.JsonSerializerOptions); | ||
var responses = new Queue<RestResponse>(); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = resultContent, | ||
|
||
// Set IsSuccessful to true | ||
IsSuccessStatusCode = true, | ||
ResponseStatus = ResponseStatus.Completed | ||
}); | ||
|
||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(responses.Dequeue); | ||
|
||
var httpRemoteQueryRunner = new HttpRemoteQueryRunner<RequestTestContextQuery, TestContext>(restClientMock.Object, string.Empty); | ||
var request = new QueryRequest<RequestTestContextQuery>(query, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var result = await httpRemoteQueryRunner.QueryAsync(request, CancellationToken.None); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo(testContext); | ||
responses.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task QueryAsync_ShouldThrowWithoutResultAfterMaxRetryReachedRequest() | ||
{ | ||
// Arrange | ||
var query = new RequestTestContextQuery(); | ||
var testContext = new TestContext() | ||
{ | ||
UserId = Guid.NewGuid().ToString() | ||
}; | ||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(() => new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
|
||
var httpRemoteQueryRunner = new HttpRemoteQueryRunner<RequestTestContextQuery, TestContext>(restClientMock.Object, string.Empty); | ||
var request = new QueryRequest<RequestTestContextQuery>(query, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => httpRemoteQueryRunner.QueryAsync(request, CancellationToken.None)); | ||
|
||
// Assert | ||
exception.Should().NotBeNull(); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
...S.Extensions.FastEndpoints.UnitTests/RemoteCommandRunners/HttpRemoteCommandRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using System.Net; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using Moq; | ||
using RestSharp; | ||
using Wemogy.CQRS.Common.ValueObjects; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.Common; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.RemoteCommandRunners; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.Commands.Greeting; | ||
using Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.Commands.LogTestContext; | ||
|
||
namespace Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.RemoteCommandRunners; | ||
|
||
public class HttpRemoteCommandRunnerTests | ||
{ | ||
[Fact] | ||
public async Task RunAsync_ShouldRetryRequest() | ||
{ | ||
// Arrange | ||
var command = new LogTestContextCommand(); | ||
var responses = new Queue<RestResponse>(); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
|
||
// Set IsSuccessful to true | ||
IsSuccessStatusCode = true, | ||
ResponseStatus = ResponseStatus.Completed | ||
}); | ||
|
||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(responses.Dequeue); | ||
|
||
var httpRemoteCommandRunner = new HttpRemoteCommandRunner<LogTestContextCommand>(restClientMock.Object, string.Empty); | ||
var request = new CommandRequest<LogTestContextCommand>(command, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => httpRemoteCommandRunner.RunAsync(request)); | ||
|
||
// Assert | ||
exception.Should().BeNull(); | ||
responses.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task RunAsync_ShouldThrowAfterMaxRetryReachedRequest() | ||
{ | ||
// Arrange | ||
var command = new LogTestContextCommand(); | ||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(() => new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
|
||
var httpRemoteCommandRunner = new HttpRemoteCommandRunner<LogTestContextCommand>(restClientMock.Object, string.Empty); | ||
var request = new CommandRequest<LogTestContextCommand>(command, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => httpRemoteCommandRunner.RunAsync(request)); | ||
|
||
// Assert | ||
exception.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task RunAsync_ShouldRetryRequestAndReturnResult() | ||
{ | ||
// Arrange | ||
var command = new GreetingCommand("Max"); | ||
var resultContent = JsonSerializer.Serialize("Hello, Max!", JsonOptions.JsonSerializerOptions); | ||
var responses = new Queue<RestResponse>(); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
responses.Enqueue(new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = resultContent, | ||
|
||
// Set IsSuccessful to true | ||
IsSuccessStatusCode = true, | ||
ResponseStatus = ResponseStatus.Completed | ||
}); | ||
|
||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(responses.Dequeue); | ||
|
||
var httpRemoteCommandRunner = new HttpRemoteCommandRunner<GreetingCommand, string>(restClientMock.Object, string.Empty); | ||
var request = new CommandRequest<GreetingCommand>(command, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var result = await httpRemoteCommandRunner.RunAsync(request); | ||
|
||
// Assert | ||
result.Should().Be("Hello, Max!"); | ||
responses.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task RunAsync_ShouldThrowWithoutResultAfterMaxRetryReachedRequest() | ||
{ | ||
// Arrange | ||
var command = new GreetingCommand("Max"); | ||
var restClientMock = new Mock<IRestClient>(); | ||
restClientMock | ||
.Setup( | ||
m => m.ExecuteAsync( | ||
It.IsAny<RestRequest>(), | ||
It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(() => new RestResponse() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable | ||
}); | ||
|
||
var httpRemoteCommandRunner = new HttpRemoteCommandRunner<GreetingCommand, string>(restClientMock.Object, string.Empty); | ||
var request = new CommandRequest<GreetingCommand>(command, new List<CommandQueryDependency>()); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => httpRemoteCommandRunner.RunAsync(request)); | ||
|
||
// Assert | ||
exception.Should().NotBeNull(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...S.Extensions.FastEndpoints.UnitTests/TestApplication/Commands/Greeting/GreetingCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Wemogy.CQRS.Commands.Abstractions; | ||
|
||
namespace Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.Commands.Greeting; | ||
|
||
public class GreetingCommand : ICommand<string> | ||
{ | ||
public string Name { get; } | ||
|
||
public GreetingCommand(string name) | ||
{ | ||
Name = name; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...sions.FastEndpoints.UnitTests/TestApplication/Commands/Greeting/GreetingCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Wemogy.CQRS.Commands.Abstractions; | ||
|
||
namespace Wemogy.CQRS.Extensions.FastEndpoints.UnitTests.TestApplication.Commands.Greeting; | ||
|
||
public class GreetingCommandHandler : ICommandHandler<GreetingCommand, string> | ||
{ | ||
public Task<string> HandleAsync(GreetingCommand command) | ||
{ | ||
return Task.FromResult($"Hello, {command.Name}!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.