diff --git a/CleanArchitecture.IntegrationTests/ExternalServices/RedisTestFixture.cs b/CleanArchitecture.IntegrationTests/ExternalServices/RedisTestFixture.cs new file mode 100644 index 0000000..17b429c --- /dev/null +++ b/CleanArchitecture.IntegrationTests/ExternalServices/RedisTestFixture.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using CleanArchitecture.Domain.Entities; +using CleanArchitecture.Infrastructure.Database; +using CleanArchitecture.IntegrationTests.Fixtures; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.DependencyInjection; + +namespace CleanArchitecture.IntegrationTests.ExternalServices; + +public sealed class RedisTestFixture : TestFixtureBase +{ + public Guid CreatedTenantId { get; } = Guid.NewGuid(); + + public IDistributedCache DistributedCache { get; } + + public RedisTestFixture() + { + DistributedCache = Factory.Services.GetRequiredService(); + } + + public async Task SeedTestData() + { + await GlobalSetupFixture.RespawnDatabaseAsync(); + + using var context = Factory.Services.GetRequiredService(); + + context.Tenants.Add(new Tenant( + CreatedTenantId, + "Test Tenant")); + + await context.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/CleanArchitecture.IntegrationTests/ExternalServices/RedisTests.cs b/CleanArchitecture.IntegrationTests/ExternalServices/RedisTests.cs new file mode 100644 index 0000000..8ead306 --- /dev/null +++ b/CleanArchitecture.IntegrationTests/ExternalServices/RedisTests.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using CleanArchitecture.Application.ViewModels.Tenants; +using CleanArchitecture.Domain; +using CleanArchitecture.Domain.Entities; +using CleanArchitecture.IntegrationTests.Extensions; +using FluentAssertions; +using Microsoft.Extensions.Caching.Distributed; +using Newtonsoft.Json; + +namespace CleanArchitecture.IntegrationTests.ExternalServices; + +public sealed class RedisTests +{ + private readonly RedisTestFixture _fixture = new(); + + [OneTimeSetUp] + public async Task Setup() => await _fixture.SeedTestData(); + + [Test, Order(0)] + public async Task Should_Get_Tenant_By_Id_And_Ensure_Cache() + { + var response = await _fixture.ServerClient.GetAsync($"/api/v1/Tenant/{_fixture.CreatedTenantId}"); + var message = await response.Content.ReadAsJsonAsync(); + message!.Data!.Id.Should().Be(_fixture.CreatedTenantId); + + var json = await _fixture.DistributedCache.GetStringAsync(CacheKeyGenerator.GetEntityCacheKey(_fixture.CreatedTenantId)); + json.Should().NotBeNullOrEmpty(); + + var tenant = JsonConvert.DeserializeObject(json!)!; + + tenant.Should().NotBeNull(); + tenant.Id.Should().Be(_fixture.CreatedTenantId); + } +} \ No newline at end of file