diff --git a/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs b/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs index 6fc42bc0..f72ec2db 100644 --- a/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs +++ b/src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs @@ -41,6 +41,13 @@ public interface IAdminEventRepository /// Event details instance. /// Returns the updated record of the event details. Task UpdateEvent(Guid eventId, AdminEventDetails eventDetails); + + /// + /// Deletes the event details. + /// + /// Event details instance. + /// Removed EventID of event details instance. + Task DeleteEvent(AdminEventDetails eventDetails); } /// @@ -65,7 +72,6 @@ public async Task CreateEvent(AdminEventDetails eventDetails) public async Task> GetEvents() { var tableClient = await GetTableClientAsync(); - var eventDetailsList = new List(); await foreach (var entity in tableClient.QueryAsync()) @@ -101,6 +107,15 @@ public async Task UpdateEvent(Guid eventId, AdminEventDetails return eventDetails; } + public async Task DeleteEvent(AdminEventDetails eventDetails) + { + var tableClient = await GetTableClientAsync(); + + await tableClient.DeleteEntityAsync(eventDetails.PartitionKey, eventDetails.RowKey); + + return eventDetails.EventId; + } + private async Task GetTableClientAsync() { TableClient tableClient = _tableServiceClient.GetTableClient(_storageAccountSettings.TableStorage.TableName); diff --git a/src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs b/src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs index 09f21797..d4e0746c 100644 --- a/src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs +++ b/src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs @@ -35,6 +35,13 @@ public interface IAdminEventService /// Event details to update. /// Returns the updated event details. Task UpdateEvent(Guid eventId, AdminEventDetails eventDetails); + + /// + /// Deletes the event details. + /// + /// Event details to update. + /// Removed EventID of event details instance. + Task DeleteEvent(AdminEventDetails eventDetails); } /// @@ -75,6 +82,14 @@ public async Task UpdateEvent(Guid eventId, AdminEventDetails return result; } + + /// + public async Task DeleteEvent(AdminEventDetails eventDetails) + { + var result = await this._repository.DeleteEvent(eventDetails).ConfigureAwait(false); + + return result; + } } /// diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Repositories/AdminEventRepositoryTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Repositories/AdminEventRepositoryTests.cs index f60e733c..bba20443 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Repositories/AdminEventRepositoryTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Repositories/AdminEventRepositoryTests.cs @@ -1,6 +1,4 @@ -using System.Configuration; - -using Azure; +using Azure; using Azure.Data.Tables; using AzureOpenAIProxy.ApiApp.Configurations; @@ -14,15 +12,13 @@ using NSubstitute; using NSubstitute.ExceptionExtensions; -using Xunit.Sdk; - namespace AzureOpenAIProxy.ApiApp.Tests.Repositories; public class AdminEventRepositoryTests { - private StorageAccountSettings mockSettings; - private TableServiceClient mockTableServiceClient; - private TableClient mockTableClient; + private readonly StorageAccountSettings mockSettings; + private readonly TableServiceClient mockTableServiceClient; + private readonly TableClient mockTableClient; public AdminEventRepositoryTests() { @@ -163,4 +159,23 @@ await mockTableClient.Received(1) Arg.Any(), TableUpdateMode.Replace); } + + [Fact] + public async Task Given_Instance_When_DeleteEvent_Invoked_Then_It_Should_Invoke_DeleteEntityAsync_Method() + { + // Arrange + var eventDetails = new AdminEventDetails(); + var repository = new AdminEventRepository(mockTableServiceClient, mockSettings); + var eventId = Guid.NewGuid(); + + eventDetails.EventId = eventId; + + // Act + Guid deletedEventId = await repository.DeleteEvent(eventDetails); + + // Assert + deletedEventId.Should().Be(eventId); + await mockTableClient.Received(1) + .DeleteEntityAsync(Arg.Any(), Arg.Any()); + } } diff --git a/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs b/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs index f0442c76..a1f4bd23 100644 --- a/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs +++ b/test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs @@ -15,6 +15,13 @@ namespace AzureOpenAIProxy.ApiApp.Tests.Services; public class AdminEventServiceTests { + private readonly IAdminEventRepository mockRepository; + + public AdminEventServiceTests() + { + mockRepository = Substitute.For(); + } + [Fact] public void Given_ServiceCollection_When_AddAdminEventService_Invoked_Then_It_Should_Contain_AdminEventService() { @@ -33,8 +40,7 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti { // Arrange var eventDetails = new AdminEventDetails(); - var repository = Substitute.For(); - var service = new AdminEventService(repository); + var service = new AdminEventService(mockRepository); // Act Func func = async () => await service.CreateEvent(eventDetails); @@ -47,8 +53,7 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception() { // Arrange - var repository = Substitute.For(); - var service = new AdminEventService(repository); + var service = new AdminEventService(mockRepository); // Act Func func = async () => await service.GetEvents(); @@ -65,12 +70,11 @@ public async Task Given_Failure_In_Get_Entity_When_GetEvent_Invoked_Then_It_Shou { // Arrange var eventId = Guid.NewGuid(); - var repository = Substitute.For(); - var service = new AdminEventService(repository); + var service = new AdminEventService(mockRepository); var exception = new RequestFailedException(statusCode, "Request Failed", default, default); - repository.GetEvent(Arg.Any()).ThrowsAsync(exception); + mockRepository.GetEvent(Arg.Any()).ThrowsAsync(exception); // Act Func func = () => service.GetEvent(eventId); @@ -85,8 +89,7 @@ public async Task Given_Failure_In_Get_Entity_When_GetEvent_Invoked_Then_It_Shou 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 service = new AdminEventService(mockRepository); var eventDetails = new AdminEventDetails { @@ -95,7 +98,7 @@ public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Retur var guid = Guid.Parse(eventId); - repository.GetEvent(guid).Returns(Task.FromResult(eventDetails)); + mockRepository.GetEvent(guid).Returns(Task.FromResult(eventDetails)); // Act var result = await service.GetEvent(guid); @@ -105,18 +108,37 @@ public async Task Given_Exist_EventId_When_GetEvent_Invoked_Then_It_Should_Retur } [Fact] - public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Exception() + public async Task Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Return_Updated_Event_Details() { // Arrange var eventId = Guid.NewGuid(); var eventDetails = new AdminEventDetails(); - var repository = Substitute.For(); - var service = new AdminEventService(repository); + var service = new AdminEventService(mockRepository); + + mockRepository.UpdateEvent(eventId, eventDetails).Returns(eventDetails); // Act - Func func = async () => await service.UpdateEvent(eventId, eventDetails); + var updatedEventDetails = await service.UpdateEvent(eventId, eventDetails); // Assert - func.Should().ThrowAsync(); + updatedEventDetails.Should().BeEquivalentTo(eventDetails); + } + + [Fact] + public async Task Given_Instance_When_DeleteEvent_Invoked_Then_It_Should_Return_Deleted_Event_Id() + { + // Arrange + var eventId = Guid.NewGuid(); + var eventDetails = new AdminEventDetails(); + var service = new AdminEventService(mockRepository); + + eventDetails.EventId = eventId; + mockRepository.DeleteEvent(eventDetails).Returns(eventDetails.EventId); + + // Act + var deletedEventId = await service.DeleteEvent(eventDetails); + + // Assert + deletedEventId.Should().Be(eventId); } }