diff --git a/src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEventEndpoints.cs b/src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEventEndpoints.cs index b877754b..518c68c5 100644 --- a/src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEventEndpoints.cs +++ b/src/AzureOpenAIProxy.ApiApp/Endpoints/AdminEventEndpoints.cs @@ -120,7 +120,7 @@ public static RouteHandlerBuilder AddGetAdminEvent(this WebApplication app) try { var details = await service.GetEvent(eventId); - return Results.Ok(details); + return details is null? Results.NotFound(): Results.Ok(details); } catch(RequestFailedException ex) { diff --git a/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs b/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs index 95a7d29b..137c2ec8 100644 --- a/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs +++ b/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs @@ -70,7 +70,7 @@ public async Task GetEvent(Guid eventId) partitionKey: "event-details" ).ConfigureAwait(false); - return eventDetail; + return eventDetail.Value; } /// diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs index cf56f8e2..2b82386c 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs @@ -1,12 +1,17 @@ -using AzureOpenAIProxy.ApiApp.Models; +using Azure; + +using AzureOpenAIProxy.ApiApp.Models; using AzureOpenAIProxy.ApiApp.Repositories; using AzureOpenAIProxy.ApiApp.Services; using FluentAssertions; +using Google.Protobuf.WellKnownTypes; + using Microsoft.Extensions.DependencyInjection; using NSubstitute; +using NSubstitute.ExceptionExtensions; namespace AzureOpenAIProxy.ApiApp.Tests.Services; @@ -54,19 +59,51 @@ public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception func.Should().ThrowAsync(); } - [Fact] - public void Given_Instance_When_GetEvent_Invoked_Then_It_Should_Throw_Exception() + + [Theory] + [InlineData(404)] + [InlineData(500)] + public async Task Given_Failure_In_Get_Entity_When_GetEvent_Invoked_Then_It_Should_Throw_Exception(int statusCode) { // Arrange var eventId = Guid.NewGuid(); var repository = Substitute.For(); var service = new AdminEventService(repository); + var exception = new RequestFailedException(statusCode, "Request Failed", default, default); + + repository.GetEvent(Arg.Any()).ThrowsAsync(exception); + // Act - Func func = async () => await service.GetEvent(eventId); + Func func = () => service.GetEvent(eventId); // Assert - func.Should().ThrowAsync(); + var assertion = await func.Should().ThrowAsync(); + assertion.Which.Status.Should().Be(statusCode); + } + + [Theory] + [InlineData("c355cc28-d847-4637-aad9-2f03d39aa51f")] + public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Return_AdminEventDetails(string eventId) + { + // Arrange + var repository = Substitute.For(); + var service = new AdminEventService(repository); + + var eventDetails = new AdminEventDetails + { + RowKey = eventId + }; + + var guid = Guid.Parse(eventId); + + repository.GetEvent(guid).Returns(Task.FromResult(eventDetails)); + + // Act + var result = await service.GetEvent(guid); + + // Assert + result.Should().BeEquivalentTo(eventDetails); } [Fact]