diff --git a/src/Catalog.API/Extensions/Extensions.cs b/src/Catalog.API/Extensions/Extensions.cs index bc0453624..a8165ddfc 100644 --- a/src/Catalog.API/Extensions/Extensions.cs +++ b/src/Catalog.API/Extensions/Extensions.cs @@ -24,7 +24,11 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) builder.Services.AddMigration(); // Add the integration services that consume the DbContext - builder.Services.AddTransient>(); + // 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(sp => + new IntegrationEventLogService( + sp.GetRequiredService(), + typeof(Extensions).Assembly)); builder.Services.AddTransient(); diff --git a/src/IntegrationEventLogEF/Services/IntegrationEventLogService.cs b/src/IntegrationEventLogEF/Services/IntegrationEventLogService.cs index 82a666b47..552f3096d 100644 --- a/src/IntegrationEventLogEF/Services/IntegrationEventLogService.cs +++ b/src/IntegrationEventLogEF/Services/IntegrationEventLogService.cs @@ -7,10 +7,10 @@ public class IntegrationEventLogService : 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(); diff --git a/src/Ordering.API/Apis/OrdersApi.cs b/src/Ordering.API/Apis/OrdersApi.cs index b8c339cf8..70ac80f9d 100644 --- a/src/Ordering.API/Apis/OrdersApi.cs +++ b/src/Ordering.API/Apis/OrdersApi.cs @@ -157,13 +157,11 @@ public static async Task>> 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."); } } } diff --git a/src/Ordering.API/Extensions/Extensions.cs b/src/Ordering.API/Extensions/Extensions.cs index cce11955b..b26b2ee53 100644 --- a/src/Ordering.API/Extensions/Extensions.cs +++ b/src/Ordering.API/Extensions/Extensions.cs @@ -21,7 +21,11 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder) services.AddMigration(); // Add the integration services that consume the DbContext - services.AddTransient>(); + // 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(sp => + new IntegrationEventLogService( + sp.GetRequiredService(), + typeof(Extensions).Assembly)); services.AddTransient(); diff --git a/tests/Ordering.FunctionalTests/OrderingApiTests.cs b/tests/Ordering.FunctionalTests/OrderingApiTests.cs index 9b96d9de3..a7ecd7b32 100644 --- a/tests/Ordering.FunctionalTests/OrderingApiTests.cs +++ b/tests/Ordering.FunctionalTests/OrderingApiTests.cs @@ -133,7 +133,7 @@ public async Task AddNewEmptyOrder() } [Fact] - public async Task AddNewOrder() + public async Task AddNewOrderWithInvalidDataFails() { // Act var item = new BasketItem @@ -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 { 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); }