-
-
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.
Various improvements, mocked WebApi for tests
- Loading branch information
1 parent
1f8cc1e
commit 5f5d9f1
Showing
9 changed files
with
263 additions
and
44 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
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
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
110 changes: 89 additions & 21 deletions
110
tests/BitzArt.Communicator.REST.Tests/MockedRestServiceTests.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,35 +1,103 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Text.Json.Serialization; | ||
using BitzArt.Pagination; | ||
using Castle.Components.DictionaryAdapter.Xml; | ||
using Microsoft.Extensions.Logging; | ||
using RichardSzalay.MockHttp; | ||
using System.Net.Http.Json; | ||
using System.Net; | ||
|
||
namespace BitzArt.Communicator; | ||
|
||
file class Country | ||
{ | ||
[JsonPropertyName("cca3")] | ||
public required string CountryCode { get; set; } | ||
} | ||
|
||
public class MockedRestServiceTests | ||
{ | ||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(10)] | ||
[InlineData(100)] | ||
[InlineData(1000)] | ||
public async Task GetAllAsync_MockedHttpClient_ReturnsAll(int entityCount) | ||
{ | ||
var entityContext = TestEntityContext.GetTestEntityContext(entityCount, x => | ||
{ | ||
x.When($"{MockedService.BaseUrl}/entity") | ||
.Respond(HttpStatusCode.OK, | ||
JsonContent.Create(TestEntity.GetAll(entityCount))); | ||
}); | ||
|
||
var result = await entityContext.GetAllAsync(); | ||
|
||
Assert.NotNull(result); | ||
if (entityCount > 0) Assert.True(result.Any()); | ||
Assert.True(result.Count() == entityCount); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, 0, 0)] | ||
[InlineData(0, 0, 10)] | ||
[InlineData(100, 0, 10)] | ||
[InlineData(1000, 0, 10)] | ||
[InlineData(10, 5, 10)] | ||
[InlineData(100, 1, 100)] | ||
public async Task GetPageAsync_MockedHttpClient_ReturnsPage(int entityCount, int offset, int limit) | ||
{ | ||
var entityContext = TestEntityContext.GetTestEntityContext(entityCount, x => | ||
{ | ||
x.When($"{MockedService.BaseUrl}/entity?offset={offset}&limit={limit}") | ||
.Respond(HttpStatusCode.OK, | ||
JsonContent.Create(TestEntity.GetPage(entityCount, offset, limit))); | ||
}); | ||
|
||
var result = await entityContext.GetPageAsync(offset, limit); | ||
|
||
Assert.NotNull(result); | ||
Assert.NotNull(result.Data); | ||
if (offset < entityCount) Assert.True(result.Data.Any()); | ||
if (offset + limit > entityCount) | ||
{ | ||
var shouldCount = entityCount - offset; | ||
Assert.Equal(shouldCount, result.Data.Count()); | ||
} | ||
} | ||
|
||
[Fact] | ||
// TODO: Make it a mocked REST service instead of a real WebAPI | ||
public async Task AddCommunicator_RealService_AbleToRequestAndParseResponse() | ||
public async Task GetAsync_MockedHttpClient_ReturnsEntity() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddCommunicator(x => | ||
var entityCount = 10; | ||
var id = 1; | ||
|
||
var entityContext = TestEntityContext.GetTestEntityContext(entityCount, x => | ||
{ | ||
x.When($"{MockedService.BaseUrl}/entity/{id}") | ||
.Respond(HttpStatusCode.OK, | ||
JsonContent.Create( | ||
TestEntity.GetAll(entityCount).FirstOrDefault(x => x.Id == id))); | ||
}); | ||
|
||
var result = await entityContext.GetAsync(id); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal(id, result.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task GetAsync_CustomIdEndpointLogic_ReturnsEntity() | ||
{ | ||
var entityCount = 1; | ||
|
||
var entityContext = TestEntityContext.GetTestEntityContext(entityCount, x => | ||
{ | ||
x.AddService("RestCountries") | ||
.UsingRest("https://restcountries.com/v3.1") | ||
.AddEntity<Country>().WithEndpoint("all?fields=cca3"); | ||
x.When($"{MockedService.BaseUrl}/entity/specific") | ||
.Respond(HttpStatusCode.OK, | ||
JsonContent.Create( | ||
TestEntity.GetAll(entityCount).FirstOrDefault(x => x.Id == 1))); | ||
}); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
((CommunicatorRestEntityContext<TestEntity>)entityContext) | ||
.EntityOptions.GetIdEndpointAction = (key) => "entity/specific"; | ||
|
||
var entityCommunicator = serviceProvider.GetRequiredService<ICommunicationContext<Country>>(); | ||
Assert.NotNull(entityCommunicator); | ||
var result = await entityContext.GetAsync(1); | ||
|
||
var countries = await entityCommunicator.GetAllAsync(); | ||
Assert.NotNull(countries); | ||
Assert.True(countries.Any()); | ||
Assert.NotNull(result); | ||
Assert.Equal(1, result.Id); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/BitzArt.Communicator.REST.Tests/MockedService/MockedService.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,31 @@ | ||
using RichardSzalay.MockHttp; | ||
using System.Net.Http.Json; | ||
using System.Net; | ||
|
||
namespace BitzArt.Communicator; | ||
|
||
internal class MockedService | ||
{ | ||
public const string BaseUrl = "https://mockedservice"; | ||
|
||
private readonly MockHttpMessageHandler _handler; | ||
private readonly int EntityCount; | ||
|
||
public MockedService(int entityCount, Action<MockHttpMessageHandler> configure) | ||
{ | ||
EntityCount = entityCount; | ||
_handler = new(); | ||
configure(_handler); | ||
} | ||
|
||
public HttpClient GetHttpClient() => new(_handler) | ||
{ | ||
BaseAddress = new Uri(BaseUrl) | ||
}; | ||
|
||
public static MockedService GetService(int entityCount, Action<MockHttpMessageHandler>? configureMockWebApi = null) | ||
=> new(entityCount, x => | ||
{ | ||
if (configureMockWebApi is not null) configureMockWebApi(x); | ||
}); | ||
} |
Oops, something went wrong.