Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Catalog.API/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
builder.Services.AddMigration<CatalogContext, CatalogContextSeed>();

// Add the integration services that consume the DbContext
builder.Services.AddTransient<IIntegrationEventLogService, IntegrationEventLogService<CatalogContext>>();
// Pass this project's assembly so the integration event types can be resolved for deserialization. Using the entry assembly breaks under functional tests, where the entry assembly is the test host and does not contain the derived IntegrationEvent types.
builder.Services.AddTransient<IIntegrationEventLogService>(sp =>
new IntegrationEventLogService<CatalogContext>(
sp.GetRequiredService<CatalogContext>(),
typeof(Extensions).Assembly));

builder.Services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class IntegrationEventLogService<TContext> : IIntegrationEventLogService,
private readonly TContext _context;
private readonly Type[] _eventTypes;

public IntegrationEventLogService(TContext context)
public IntegrationEventLogService(TContext context, Assembly eventTypesAssembly)
{
_context = context;
_eventTypes = Assembly.Load(Assembly.GetEntryAssembly().FullName)
_eventTypes = eventTypesAssembly
.GetTypes()
.Where(t => t.Name.EndsWith(nameof(IntegrationEvent)))
.ToArray();
Expand Down
8 changes: 3 additions & 5 deletions src/Ordering.API/Apis/OrdersApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,11 @@ public static async Task<Results<Ok, BadRequest<string>>> CreateOrderAsync(
if (result)
{
services.Logger.LogInformation("CreateOrderCommand succeeded - RequestId: {RequestId}", requestId);
}
else
{
services.Logger.LogWarning("CreateOrderCommand failed - RequestId: {RequestId}", requestId);
return TypedResults.Ok();
}

return TypedResults.Ok();
services.Logger.LogWarning("CreateOrderCommand failed - RequestId: {RequestId}", requestId);
return TypedResults.BadRequest("Create order failed to process.");
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Ordering.API/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
services.AddMigration<OrderingContext, OrderingContextSeed>();

// Add the integration services that consume the DbContext
services.AddTransient<IIntegrationEventLogService, IntegrationEventLogService<OrderingContext>>();
// Pass this project's assembly so the integration event types can be resolved for deserialization. Using the entry assembly breaks under functional tests, where the entry assembly is the test host and does not contain the derived IntegrationEvent types.
services.AddTransient<IIntegrationEventLogService>(sp =>
new IntegrationEventLogService<OrderingContext>(
sp.GetRequiredService<OrderingContext>(),
typeof(Extensions).Assembly));

services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();

Expand Down
28 changes: 26 additions & 2 deletions tests/Ordering.FunctionalTests/OrderingApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public async Task AddNewEmptyOrder()
}

[Fact]
public async Task AddNewOrder()
public async Task AddNewOrderWithInvalidDataFails()
{
// Act
var item = new BasketItem
Expand All @@ -154,8 +154,32 @@ public async Task AddNewOrder()
};
var response = await _httpClient.PostAsync("api/orders", content, TestContext.Current.CancellationToken);
var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);

// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task AddNewOrder()
{
// Act
var item = new BasketItem
{
Id = "1",
ProductId = 12,
ProductName = "Test",
UnitPrice = 10,
OldUnitPrice = 9,
Quantity = 1,
PictureUrl = null
};
var cardExpirationDate = Convert.ToDateTime("2123-12-22T12:34:24.334Z").ToUniversalTime();
var OrderRequest = new CreateOrderRequest("1", "TestUser", "Istanbul", "Kadikoy", "IS", "TR", "34034", "XXXXXXXXXXXX0005", "test buyer", cardExpirationDate, "123", 1, null, new List<BasketItem> { item });
var content = new StringContent(JsonSerializer.Serialize(OrderRequest), UTF8Encoding.UTF8, "application/json")
{
Headers = { { "x-requestid", Guid.NewGuid().ToString() } }
};
var response = await _httpClient.PostAsync("api/orders", content, TestContext.Current.CancellationToken);
var s = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Expand Down