Skip to content

Commit

Permalink
2.0.6
Browse files Browse the repository at this point in the history
Started creating unit testing methods for all the related solution folders.
  • Loading branch information
1plam committed Jun 12, 2023
1 parent 285aab3 commit bcf4d20
Show file tree
Hide file tree
Showing 15 changed files with 721 additions and 14 deletions.
1 change: 1 addition & 0 deletions FleetJourney.Application/FleetJourney.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Azure.Identity" Version="1.9.0" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
<PackageReference Include="FastEndpoints" Version="5.11.0" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MassTransit.AmazonSQS" Version="8.0.16" />
<PackageReference Include="MassTransit.AspNetCore" Version="7.3.1" />
Expand Down
2 changes: 1 addition & 1 deletion FleetJourney.Application/Helpers/CacheKeys.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FleetJourney.Application.Helpers;

internal static class CacheKeys
public static class CacheKeys
{
public static class Employees
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FleetJourney.Application.Helpers;

internal sealed class PrefixKeyVaultSecretManager : KeyVaultSecretManager
public sealed class PrefixKeyVaultSecretManager : KeyVaultSecretManager
{
private readonly string _prefix;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using FleetJourney.Application.Attributes;
using FleetJourney.Application.Extensions;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace FleetJourney.Application.Tests.Extensions;

public sealed class ApplicationServiceExtensionsTests
{
private interface IExampleService
{
void DoSomething();
}

private sealed class ExampleService : IExampleService
{
public void DoSomething()
{
}
}

[Fact]
public void AddApplicationService_ShouldRegisterTypes_WhenInterfaceTypeProvided()
{
// Arrange
var services = new ServiceCollection();
var interfaceType = typeof(IExampleService);

// Act
services.AddApplicationService(interfaceType);

// Assert
services.Should().Contain(x => x.ServiceType == interfaceType);
services.Should().OnlyHaveUniqueItems(x => x.ServiceType);
services.Should().OnlyHaveUniqueItems(x => x.ImplementationType);
}

[Fact]
public void AddApplicationService_ShouldNotRegisterTypesWithCachingDecoratorAttribute_WhenInterfaceTypeProvided()
{
// Arrange
var services = new ServiceCollection();
var interfaceType = typeof(IExampleService);

// Act
services.AddApplicationService(interfaceType);

// Assert
services.Where(x => x.ServiceType == interfaceType)
.Should().NotContain(x => x.ImplementationType!.GetCustomAttributes(typeof(CachingDecorator), true).Any());
}

[Fact]
public void AddApplicationService_ShouldRegisterTypes_WhenGenericTypeProvided()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddApplicationService<IExampleService>();

// Assert
services.Should().Contain(x => x.ServiceType == typeof(IExampleService));
services.Should().OnlyHaveUniqueItems(x => x.ServiceType);
services.Should().OnlyHaveUniqueItems(x => x.ImplementationType);
}

[Fact]
public void AddApplicationService_ShouldNotRegisterTypesWithCachingDecoratorAttribute_WhenGenericTypeProvided()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddApplicationService<IExampleService>();

// Assert
services.Where(x => x.ServiceType == typeof(IExampleService))
.Should().NotContain(x => x.ImplementationType!.GetCustomAttributes(typeof(CachingDecorator), true).Any());
}

[Fact]
public void AddApplicationService_ShouldThrowArgumentNullException_WhenInterfaceTypeIsNull()
{
// Arrange
var services = new ServiceCollection();

// Act
Action act = () => services.AddApplicationService(null!);

// Assert
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AddApplicationService_ShouldThrowArgumentNullException_WhenGenericTypeIsNull()
{
// Arrange
var services = new ServiceCollection();

// Act
Action act = () => services.AddApplicationService(null!);

// Assert
act.Should().Throw<ArgumentNullException>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FleetJourney.Application.Extensions;
using FleetJourney.Application.Services.Abstractions;
using Moq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace FleetJourney.Application.Tests.Extensions;

public sealed class CacheServiceExtensionsTests
{
[Fact]
public async Task RemoveCachesAsync_ShouldRemoveCaches()
{
// Arrange
var cacheServiceMock = new Mock<ICacheService<object>>();
var cancellationToken = CancellationToken.None;
var cacheKeys = new[] { "cacheKey1", "cacheKey2", "cacheKey3" };

// Act
await cacheServiceMock.Object.RemoveCachesAsync(cancellationToken, cacheKeys);

// Assert
foreach (var cacheKey in cacheKeys)
{
cacheServiceMock.Verify(x => x.RemoveAsync(cacheKey, cancellationToken), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Testcontainers.MySql" Version="3.2.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Repositories\" />
<Folder Include="Services\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\FleetJourney.Application\FleetJourney.Application.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using FleetJourney.Application.Helpers;
using FluentAssertions;
using Xunit;

namespace FleetJourney.Application.Tests.Helpers;

public sealed class CacheKeysTests
{
[Theory]
[InlineData("employees-all")]
public void Employees_GetAll_Should_Return_Correct_Key(string expectedKey)
{
// Act
var key = CacheKeys.Employees.GetAll;

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("employees-", "")]
public void Employees_Get_Should_Return_Correct_Key(string prefix, string suffix)
{
// Arrange
var employeeId = Guid.NewGuid();
var expectedKey = $"{prefix}{employeeId}{suffix}";

// Act
var key = CacheKeys.Employees.Get(employeeId);

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("employees-email-", "test@example.com")]
public void Employees_GetByEmail_Should_Return_Correct_Key(string prefix, string email)
{
// Arrange
var expectedKey = $"{prefix}{email}";

// Act
var key = CacheKeys.Employees.GetByEmail(email);

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("cars-all")]
public void CarPool_GetAll_Should_Return_Correct_Key(string expectedKey)
{
// Act
var key = CacheKeys.CarPool.GetAll;

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("cars-", "")]
public void CarPool_Get_Should_Return_Correct_Key(string prefix, string suffix)
{
// Arrange
var carId = Guid.NewGuid();
var expectedKey = $"{prefix}{carId}{suffix}";

// Act
var key = CacheKeys.CarPool.Get(carId);

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("trips-all")]
public void Trips_GetAll_Should_Return_Correct_Key(string expectedKey)
{
// Act
var key = CacheKeys.Trips.GetAll;

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("trips-", "-all")]
public void Trips_GetAllByEmployeeId_Should_Return_Correct_Key(string prefix, string suffix)
{
// Arrange
var employeeId = Guid.NewGuid();
var expectedKey = $"{prefix}{employeeId}{suffix}";

// Act
var key = CacheKeys.Trips.GetAllByEmployeeId(employeeId);

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("trips-", "")]
public void Trips_Get_Should_Return_Correct_Key(string prefix, string suffix)
{
// Arrange
var tripId = Guid.NewGuid();
var expectedKey = $"{prefix}{tripId}{suffix}";

// Act
var key = CacheKeys.Trips.Get(tripId);

// Assert
key.Should().Be(expectedKey);
}

[Theory]
[InlineData("trips-", "")]
public void Trips_GetByCarId_Should_Return_Correct_Key(string prefix, string suffix)
{
// Arrange
var carId = Guid.NewGuid();
var expectedKey = $"{prefix}{carId}{suffix}";

// Act
var key = CacheKeys.Trips.GetByCarId(carId);

// Assert
key.Should().Be(expectedKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Azure.Security.KeyVault.Secrets;
using FleetJourney.Application.Helpers;
using FluentAssertions;
using Xunit;

namespace FleetJourney.Application.Tests.Helpers;

public sealed class PrefixKeyVaultSecretManagerTests
{
[Theory]
[InlineData("prefix")]
public void Load_Should_Return_True_For_Secret_With_Correct_Prefix(string prefix)
{
// Arrange
var secret = new SecretProperties("prefix-secret-name");

// Act
var manager = new PrefixKeyVaultSecretManager(prefix);
var result = manager.Load(secret);

// Assert
result.Should().BeTrue();
}

[Theory]
[InlineData("prefix")]
public void Load_Should_Return_False_For_Secret_With_Incorrect_Prefix(string prefix)
{
// Arrange
var secret = new SecretProperties("other-prefix-secret-name");

// Act
var manager = new PrefixKeyVaultSecretManager(prefix);
var result = manager.Load(secret);

// Assert
result.Should().BeFalse();
}

[Theory]
[InlineData("prefix", "prefix-secret-name", "secret-name")]
[InlineData("prefix", "prefix-secret--name", "secret:name")]
public void GetKey_Should_Return_Correct_Key(string prefix, string secretName, string expectedKey)
{
// Arrange
var secret = new KeyVaultSecret(secretName, string.Empty);

// Act
var manager = new PrefixKeyVaultSecretManager(prefix);
var key = manager.GetKey(secret);

// Assert
key.Should().Be(expectedKey);
}
}
Loading

0 comments on commit bcf4d20

Please sign in to comment.