-
-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c07909f
commit 6ea2ef6
Showing
29 changed files
with
1,212 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,67 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Core.Validation; | ||
|
||
public static class ValidationExtensions | ||
{ | ||
public static T AssertNotNull<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T NotNull<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : struct | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException(paramName); | ||
|
||
return (T)value; | ||
return value.Value; | ||
} | ||
|
||
public static string AssertNotEmpty(this string? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T NotNull<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : class | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException(paramName); | ||
|
||
return value; | ||
} | ||
|
||
public static string NotEmpty(this string? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
=> !string.IsNullOrWhiteSpace(value) ? value : throw new ArgumentOutOfRangeException(paramName); | ||
|
||
public static T AssertNotEmpty<T>(this T value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static Guid NotEmpty(this Guid? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
=> value!= null && value != Guid.Empty ? value.Value : throw new ArgumentOutOfRangeException(paramName); | ||
|
||
|
||
public static T NotEmpty<T>(this T value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : struct | ||
=> AssertNotEmpty((T?)value, paramName); | ||
=> NotEmpty((T?)value, paramName); | ||
|
||
public static T AssertNotEmpty<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T NotEmpty<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : struct | ||
{ | ||
var notNullValue = value.AssertNotNull(paramName); | ||
var notNullValue = value.NotNull(paramName); | ||
|
||
if (Equals(notNullValue, default(T))) | ||
throw new ArgumentOutOfRangeException(paramName); | ||
|
||
return notNullValue; | ||
} | ||
|
||
public static T AssertGreaterOrEqualThan<T>(this T value, T valueToCompare, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T GreaterOrEqualThan<T>(this T value, T valueToCompare, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : IComparable<T> | ||
{ | ||
if (value.CompareTo(valueToCompare) < 0) | ||
throw new ArgumentOutOfRangeException(paramName); | ||
|
||
return value; | ||
|
||
} | ||
|
||
public static T Positive<T>(this T value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : INumber<T> | ||
{ | ||
if (value == null || value.CompareTo(Convert.ChangeType(0, typeof(T))) <= 0) | ||
throw new ArgumentOutOfRangeException(paramName); | ||
|
||
return value; | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
...nToEventSourcing/08-ApplicationLogic.Marten.Tests/08-ApplicationLogic.Marten.Tests.csproj
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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>ApplicationLogic.Marten.Tests</RootNamespace> | ||
<AssemblyName>ApplicationLogic.Marten.Tests</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageReference Include="NSubstitute" Version="5.1.0" /> | ||
<PackageReference Include="xunit" Version="2.7.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.4" /> | ||
<PackageReference Include="Ogooreck" Version="0.8.0" /> | ||
<PackageReference Include="Bogus" Version="35.5.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\08-ApplicationLogic.Marten\08-ApplicationLogic.Marten.csproj" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\..\..\Tests.Build.props" /> | ||
|
||
</Project> |
95 changes: 95 additions & 0 deletions
95
...tSourcing/08-ApplicationLogic.Marten.Tests/Incidents/AddProductItemToShoppingCartTests.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,95 @@ | ||
using System.Net; | ||
using ApplicationLogic.Marten.Immutable.ShoppingCarts; | ||
using Bogus; | ||
using Ogooreck.API; | ||
using Xunit; | ||
using static Ogooreck.API.ApiSpecification; | ||
using static ApplicationLogic.Marten.Tests.Incidents.Scenarios; | ||
using static ApplicationLogic.Marten.Tests.Incidents.Endpoints; | ||
|
||
namespace ApplicationLogic.Marten.Tests.Incidents; | ||
|
||
public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api): | ||
IClassFixture<ApiSpecification<Program>> | ||
{ | ||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantAddProductItemToNotExistingShoppingCart(string apiPrefix) => | ||
api.Given() | ||
.When( | ||
POST, | ||
URI(ShoppingCartProductItems(apiPrefix, ClientId, NotExistingShoppingCartId)), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(NOT_FOUND); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task AddsProductItemToEmptyShoppingCart(string apiPrefix) => | ||
api.Given(OpenedShoppingCart(apiPrefix, ClientId)) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(NO_CONTENT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task AddsProductItemToNonEmptyShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem) | ||
) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(NO_CONTENT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantAddProductItemToConfirmedShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenConfirmed(apiPrefix, ClientId) | ||
) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantAddProductItemToCanceledShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenCanceled(apiPrefix, ClientId) | ||
) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task ReturnsNonEmptyShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem) | ||
) | ||
.When(GET, URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>()))) | ||
.Then(OK); | ||
|
||
private static readonly Faker Faker = new(); | ||
private readonly Guid NotExistingShoppingCartId = Guid.NewGuid(); | ||
private readonly Guid ClientId = Guid.NewGuid(); | ||
private readonly ProductItemRequest ProductItem = new(Guid.NewGuid(), Faker.Random.Number(1, 500)); | ||
} |
91 changes: 91 additions & 0 deletions
91
...tionToEventSourcing/08-ApplicationLogic.Marten.Tests/Incidents/CancelShoppingCartTests.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,91 @@ | ||
using System.Net; | ||
using ApplicationLogic.Marten.Immutable.ShoppingCarts; | ||
using Bogus; | ||
using Ogooreck.API; | ||
using Xunit; | ||
using static Ogooreck.API.ApiSpecification; | ||
using static ApplicationLogic.Marten.Tests.Incidents.Scenarios; | ||
using static ApplicationLogic.Marten.Tests.Incidents.Endpoints; | ||
|
||
namespace ApplicationLogic.Marten.Tests.Incidents; | ||
|
||
public class CancelShoppingCartTests(ApiSpecification<Program> api): | ||
IClassFixture<ApiSpecification<Program>> | ||
{ | ||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantCancelNotExistingShoppingCart(string apiPrefix) => | ||
api.Given() | ||
.When( | ||
DELETE, | ||
URI(ShoppingCart(apiPrefix, ClientId, NotExistingShoppingCartId)) | ||
) | ||
.Then(NOT_FOUND); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantCancelEmptyShoppingCart(string apiPrefix) => | ||
api.Given(OpenedShoppingCart(apiPrefix, ClientId)) | ||
.When( | ||
DELETE, | ||
URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CancelsNonEmptyShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem) | ||
) | ||
.When( | ||
DELETE, | ||
URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())) | ||
) | ||
.Then(NO_CONTENT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantCancelAlreadyCanceledShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenCanceled(apiPrefix, ClientId) | ||
) | ||
.When( | ||
DELETE, | ||
URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task CantCancelConfirmedShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenConfirmed(apiPrefix, ClientId) | ||
) | ||
.When( | ||
DELETE, | ||
URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
public Task ReturnsNonEmptyShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenCanceled(apiPrefix, ClientId) | ||
) | ||
.When(GET, URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>()))) | ||
.Then(OK); | ||
|
||
private static readonly Faker Faker = new(); | ||
private readonly Guid NotExistingShoppingCartId = Guid.NewGuid(); | ||
private readonly Guid ClientId = Guid.NewGuid(); | ||
private readonly ProductItemRequest ProductItem = new(Guid.NewGuid(), Faker.Random.Number(1, 500)); | ||
} |
Oops, something went wrong.