Skip to content

Commit

Permalink
Upgraded api tests to breaking changes in Ogooreck
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Aug 15, 2023
1 parent e990396 commit 8b1fe99
Show file tree
Hide file tree
Showing 41 changed files with 558 additions and 748 deletions.
2 changes: 1 addition & 1 deletion Core.Testing/Core.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.9" />
<PackageReference Include="Ogooreck" Version="0.6.0" />
<PackageReference Include="Ogooreck" Version="0.8.0-rc.2" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions EventSourcing.NetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decider", "Sample\decider\D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reservations", "Sample\HotelManagement\Reservations\Reservations.csproj", "{92DC3F6A-BB06-4E33-A9C7-8262CC68F93C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ogooreck", "..\..\Repos\Ogooreck\src\Ogooreck\Ogooreck.csproj", "{7D39308C-9B20-4F70-99BB-DD3703AE35FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -956,6 +958,10 @@ Global
{92DC3F6A-BB06-4E33-A9C7-8262CC68F93C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92DC3F6A-BB06-4E33-A9C7-8262CC68F93C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92DC3F6A-BB06-4E33-A9C7-8262CC68F93C}.Release|Any CPU.Build.0 = Release|Any CPU
{7D39308C-9B20-4F70-99BB-DD3703AE35FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D39308C-9B20-4F70-99BB-DD3703AE35FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D39308C-9B20-4F70-99BB-DD3703AE35FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D39308C-9B20-4F70-99BB-DD3703AE35FC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
using Carts.Api.Requests;
using Carts.ShoppingCarts;
using Carts.ShoppingCarts.GettingCartById;
using Carts.ShoppingCarts.Products;
using FluentAssertions;
using Ogooreck.API;
using static Ogooreck.API.ApiSpecification;
using Xunit;
using static Ogooreck.API.ApiSpecification;

namespace Carts.Api.Tests.ShoppingCarts.AddingProduct;

public class AddProductFixture: ApiSpecification<Program>, IAsyncLifetime
{
public Guid ShoppingCartId { get; private set; }

public readonly Guid ClientId = Guid.NewGuid();

public async Task InitializeAsync()
{
var openResponse = await Send(
new ApiRequest(POST, URI("/api/ShoppingCarts"), BODY(new OpenShoppingCartRequest(ClientId)))
);

await CREATED(openResponse);
await RESPONSE_LOCATION_HEADER()(openResponse);
using static ShoppingCartsApi;

ShoppingCartId = openResponse.GetCreatedId<Guid>();
}

public Task DisposeAsync() => Task.CompletedTask;
}

public class AddProductTests: IClassFixture<AddProductFixture>
public class AddProductTests: IClassFixture<ApiSpecification<Program>>
{
private readonly AddProductFixture API;
private readonly ApiSpecification<Program> API;

public AddProductTests(AddProductFixture api) => API = api;
public AddProductTests(ApiSpecification<Program> api) => API = api;

[Fact]
[Trait("Category", "Acceptance")]
Expand All @@ -43,26 +23,26 @@ public async Task Post_Should_AddProductItem_To_ShoppingCart()
var product = new ProductItemRequest(Guid.NewGuid(), 1);

await API
.Given(
URI($"/api/ShoppingCarts/{API.ShoppingCartId}/products"),
.Given("Opened Shopping Cart", OpenShoppingCart())
.When(
"Add new product",
POST,
URI(ctx => $"/api/ShoppingCarts/{ctx.OpenedShoppingCartId()}/products"),
BODY(new AddProductRequest(product)),
HEADERS(IF_MATCH(1))
HEADERS(IF_MATCH(0))
)
.When(POST)
.Then(OK);

await API
.Given(URI($"/api/ShoppingCarts/{API.ShoppingCartId}"))
.When(GET_UNTIL(RESPONSE_ETAG_IS(2)))
.Then(OK)
.And()
.When(GET, URI(ctx => $"/api/ShoppingCarts/{ctx.OpenedShoppingCartId()}"))
.Then(
RESPONSE_BODY<ShoppingCartDetails>(details =>
RESPONSE_BODY<ShoppingCartDetails>((details, ctx) =>
{
details.Id.Should().Be(API.ShoppingCartId);
details.Id.Should().Be(ctx.OpenedShoppingCartId());
details.Status.Should().Be(ShoppingCartStatus.Pending);
details.ProductItems.Should().HaveCount(1);
details.ProductItems.Single().ProductItem.Should()
.Be(ProductItem.From(product.ProductId, product.Quantity));
details.Version.Should().Be(2);
var productItem = details.ProductItems.Single();
productItem.Quantity.Should().Be(product.Quantity);
productItem.ProductId.Should().Be(product.ProductId!.Value);
details.Version.Should().Be(1);
})
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,43 @@
using Carts.Api.Requests;
using Carts.ShoppingCarts;
using Carts.ShoppingCarts.GettingCartById;
using Carts.ShoppingCarts.Products;
using FluentAssertions;
using Ogooreck.API;
using Xunit;
using static Ogooreck.API.ApiSpecification;
using static Carts.Api.Tests.ShoppingCarts.ShoppingCartsApi;

namespace Carts.Api.Tests.ShoppingCarts.Canceling;

public class CancelShoppingCartFixture: ApiSpecification<Program>, IAsyncLifetime
public class CancelShoppingCartTests: IClassFixture<ApiSpecification<Program>>
{
public Guid ShoppingCartId { get; private set; }
private readonly ApiSpecification<Program> API;
public CancelShoppingCartTests(ApiSpecification<Program> api) => API = api;

public readonly Guid ClientId = Guid.NewGuid();

public async Task InitializeAsync()
{
var openResponse = await Send(
new ApiRequest(POST, URI("/api/ShoppingCarts"), BODY(new OpenShoppingCartRequest(ClientId)))
);

await CREATED_WITH_DEFAULT_HEADERS(eTag: 1)(openResponse);

ShoppingCartId = openResponse.GetCreatedId<Guid>();
}

public Task DisposeAsync()
{
Dispose();
return Task.CompletedTask;
}
}

public class CancelShoppingCartTests: IClassFixture<CancelShoppingCartFixture>
{
private readonly CancelShoppingCartFixture API;

public CancelShoppingCartTests(CancelShoppingCartFixture api) => API = api;

[Fact]
[Trait("Category", "Acceptance")]
public async Task Delete_Should_Return_OK_And_Cancel_Shopping_Cart()
{
await API
.Given(
URI($"/api/ShoppingCarts/{API.ShoppingCartId}"),
HEADERS(IF_MATCH(1))
public Task Delete_Should_Return_OK_And_Cancel_Shopping_Cart() =>
API
.Given("Opened Shopping Cart", OpenShoppingCart(ClientId))
.When(
"Cancel Shopping Cart",
DELETE,
URI(ctx => $"/api/ShoppingCarts/{ctx.OpenedShoppingCartId()}"),
HEADERS(IF_MATCH(0))
)
.When(DELETE)
.Then(OK);

await API
.Given(
URI($"/api/ShoppingCarts/{API.ShoppingCartId}")
)
.When(GET_UNTIL(RESPONSE_ETAG_IS(2)))
.Then(OK)
.And()
.When(GET, URI(ctx => $"/api/ShoppingCarts/{ctx.OpenedShoppingCartId()}"))
.Until(RESPONSE_ETAG_IS(1))
.Then(
OK,
RESPONSE_BODY(new ShoppingCartDetails
RESPONSE_BODY<ShoppingCartDetails>((details, ctx) =>
{
Id = API.ShoppingCartId,
Status = ShoppingCartStatus.Canceled,
ProductItems = new List<PricedProductItem>(),
ClientId = API.ClientId,
Version = 2,
details.Id.Should().Be(ctx.OpenedShoppingCartId());
details.Status.Should().Be(ShoppingCartStatus.Canceled);
details.ProductItems.Should().BeEmpty();
details.ClientId.Should().Be(ClientId);
details.Version.Should().Be(1);
}));

// API.PublishedExternalEventsOfType<CartFinalized>();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Carts.Api.Requests;
using Carts.ShoppingCarts;
using Carts.ShoppingCarts.GettingCartById;
using Carts.ShoppingCarts.Products;
using FluentAssertions;
using Ogooreck.API;
using Xunit;
using static Ogooreck.API.ApiSpecification;
Expand All @@ -14,82 +14,52 @@ public class ConfirmShoppingCartFixture: ApiSpecification<Program>, IAsyncLifeti

public readonly Guid ClientId = Guid.NewGuid();

public readonly ProductItemRequest ProductItem = new(Guid.NewGuid(), 1);

public decimal UnitPrice;

public async Task InitializeAsync()
{
var openResponse = await Send(
new ApiRequest(POST, URI("/api/ShoppingCarts"), BODY(new OpenShoppingCartRequest(ClientId)))
);

await CREATED_WITH_DEFAULT_HEADERS(eTag: 1)(openResponse);

ShoppingCartId = openResponse.GetCreatedId<Guid>();

var addResponse = await Send(
new ApiRequest(
POST,
URI($"/api/ShoppingCarts/{ShoppingCartId}/products"),
BODY(new AddProductRequest(ProductItem)),
HEADERS(IF_MATCH(1)))
);

await OK(addResponse);

var getResponse = await Send(
new ApiRequest(
GET_UNTIL(RESPONSE_ETAG_IS(2)),
URI($"/api/ShoppingCarts/{ShoppingCartId}")
)
);

var cartDetails = await getResponse.GetResultFromJson<ShoppingCartDetails>();
UnitPrice = cartDetails.ProductItems.Single().UnitPrice;
ShoppingCartId = await Given()
.When(POST, URI("/api/ShoppingCarts"), BODY(new OpenShoppingCartRequest(ClientId)))
.Then(CREATED_WITH_DEFAULT_HEADERS(eTag: 0))
.GetCreatedId<Guid>();
}

public Task DisposeAsync() => Task.CompletedTask;
}

public class ConfirmShoppingCartTests: IClassFixture<ConfirmShoppingCartFixture>
{

private readonly ConfirmShoppingCartFixture API;

public ConfirmShoppingCartTests(ConfirmShoppingCartFixture api) => API = api;

[Fact]
[Trait("Category", "Acceptance")]
public async Task Put_Should_Return_OK_And_Confirm_Shopping_Cart()
public async Task Put_Should_Return_OK_And_Cancel_Shopping_Cart()
{
await API
.Given(
.Given()
.When(
PUT,
URI($"/api/ShoppingCarts/{API.ShoppingCartId}/confirmation"),
HEADERS(IF_MATCH(2))
HEADERS(IF_MATCH(0))
)
.When(PUT)
.Then(OK);

await API
.Given(
URI($"/api/ShoppingCarts/{API.ShoppingCartId}")
)
.When(GET_UNTIL(RESPONSE_ETAG_IS(3)))
.Given()
.When(GET, URI($"/api/ShoppingCarts/{API.ShoppingCartId}"))
.Until(RESPONSE_ETAG_IS(1))
.Then(
OK,
RESPONSE_BODY(new ShoppingCartDetails
RESPONSE_BODY<ShoppingCartDetails>(details =>
{
Id = API.ShoppingCartId,
Status = ShoppingCartStatus.Confirmed,
ClientId = API.ClientId,
ProductItems = new List<PricedProductItem>
{
PricedProductItem.Create(
ProductItem.From(API.ProductItem.ProductId, API.ProductItem.Quantity),
API.UnitPrice
)
},
Version = 3,
details.Id.Should().Be(API.ShoppingCartId);
details.Status.Should().Be(ShoppingCartStatus.Confirmed);
details.ProductItems.Should().BeEmpty();
details.ClientId.Should().Be(API.ClientId);
details.Version.Should().Be(1);
}));

// API.PublishedExternalEventsOfType<CartFinalized>();
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
using Carts.Api.Requests;
using Carts.ShoppingCarts;
using Carts.ShoppingCarts.GettingCartById;
using Carts.ShoppingCarts.Products;
using Core.Testing;
using Xunit;
using FluentAssertions;
using Ogooreck.API;
using Xunit;
using static Ogooreck.API.ApiSpecification;

namespace Carts.Api.Tests.ShoppingCarts.Opening;

public class OpenShoppingCartTests: IClassFixture<TestWebApplicationFactory<Program>>
public class OpenShoppingCartTests: IClassFixture<ApiSpecification<Program>>
{
private readonly ApiSpecification<Program> API;

[Fact]
public Task Post_ShouldReturn_CreatedStatus_With_CartId() =>
API.Scenario(
API.Given(
API.Given()
.When(
POST,
URI("/api/ShoppingCarts/"),
BODY(new OpenShoppingCartRequest(ClientId))
)
.When(POST)
.Then(CREATED_WITH_DEFAULT_HEADERS(eTag: 1)),

.Then(CREATED_WITH_DEFAULT_HEADERS(eTag: 0)),
response =>
API.Given(
URI($"/api/ShoppingCarts/{response.GetCreatedId()}")
)
.When(GET_UNTIL(RESPONSE_ETAG_IS(1)))
API.Given()
.When(GET, URI($"/api/ShoppingCarts/{response.GetCreatedId()}"))
.Until(RESPONSE_ETAG_IS(0))
.Then(
OK,
RESPONSE_BODY(new ShoppingCartDetails
RESPONSE_BODY<ShoppingCartDetails>(details =>
{
Id = response.GetCreatedId<Guid>(),
Status = ShoppingCartStatus.Pending,
ProductItems = new List<PricedProductItem>(),
ClientId = ClientId,
Version = 1,
details.Id.Should().Be(response.GetCreatedId<Guid>());
details.Status.Should().Be(ShoppingCartStatus.Pending);
details.ProductItems.Should().BeEmpty();
details.ClientId.Should().Be(ClientId);
details.Version.Should().Be(0);
}))
);

public OpenShoppingCartTests(TestWebApplicationFactory<Program> fixture) =>
API = ApiSpecification<Program>.Setup(fixture);
public OpenShoppingCartTests(ApiSpecification<Program> api) => API = api;

private readonly ApiSpecification<Program> API;
private readonly Guid ClientId = Guid.NewGuid();
public readonly Guid ClientId = Guid.NewGuid();
}

0 comments on commit 8b1fe99

Please sign in to comment.