1- using Azure ;
2-
31using AzureOpenAIProxy . ApiApp . Models ;
42using AzureOpenAIProxy . ApiApp . Repositories ;
53using AzureOpenAIProxy . ApiApp . Services ;
@@ -15,4 +13,89 @@ namespace AzureOpenAIProxy.ApiApp.Tests.Services;
1513
1614public class AdminResourceServiceTests
1715{
16+ [ Fact ]
17+ public void Given_ServiceCollection_When_AddAdminResourceService_Invoked_Then_It_Should_Contain_AdminResourceService ( )
18+ {
19+ // Arrange
20+ var services = new ServiceCollection ( ) ;
21+
22+ // Act
23+ services . AddAdminResourceService ( ) ;
24+
25+ // Assert
26+ services . SingleOrDefault ( p => p . ServiceType == typeof ( IAdminResourceService ) ) . Should ( ) . NotBeNull ( ) ;
27+ }
28+
29+ [ Fact ]
30+ public void Given_Null_Repository_When_Creating_AdminResourceService_Then_It_Should_Throw_Exception ( )
31+ {
32+ // Arrange
33+ IAdminResourceRepository ? repository = null ;
34+
35+ // Act
36+ Action action = ( ) => new AdminResourceService ( repository ! ) ;
37+
38+ // Assert
39+ action . Should ( ) . Throw < ArgumentNullException > ( ) ;
40+ }
41+
42+ [ Fact ]
43+ public async Task Given_Instance_When_CreateResource_Invoked_Then_It_Should_Add_Entity ( )
44+ {
45+ // Arrange
46+ var repository = Substitute . For < IAdminResourceRepository > ( ) ;
47+ var service = new AdminResourceService ( repository ) ;
48+
49+ var resourceDetails = new AdminResourceDetails
50+ {
51+ ResourceId = Guid . NewGuid ( ) ,
52+ FriendlyName = "Test Resource" ,
53+ DeploymentName = "Test Deployment" ,
54+ ResourceType = ResourceType . Chat ,
55+ Endpoint = "https://test.endpoint.com" ,
56+ ApiKey = "test-api-key" ,
57+ Region = "test-region" ,
58+ IsActive = true
59+ } ;
60+
61+ repository . CreateResource ( resourceDetails ) . Returns ( resourceDetails ) ;
62+
63+ // Act
64+ var result = await service . CreateResource ( resourceDetails ) ;
65+
66+ // Assert
67+ await repository . Received ( 1 ) . CreateResource ( Arg . Is < AdminResourceDetails > ( x =>
68+ x . ResourceId == resourceDetails . ResourceId
69+ ) ) ;
70+
71+ result . Should ( ) . BeEquivalentTo ( resourceDetails ) ;
72+ }
73+
74+ [ Fact ]
75+ public async Task Given_RepositoryFails_When_CreateResource_Invoked_Then_It_Should_Throw_Exception ( )
76+ {
77+ // Arrange
78+ var repository = Substitute . For < IAdminResourceRepository > ( ) ;
79+ var service = new AdminResourceService ( repository ) ;
80+
81+ var resourceDetails = new AdminResourceDetails
82+ {
83+ ResourceId = Guid . NewGuid ( ) ,
84+ FriendlyName = "Test Resource" ,
85+ DeploymentName = "Test Deployment" ,
86+ ResourceType = ResourceType . Chat ,
87+ Endpoint = "https://test.endpoint.com" ,
88+ ApiKey = "test-api-key" ,
89+ Region = "test-region" ,
90+ IsActive = true
91+ } ;
92+
93+ repository . CreateResource ( Arg . Any < AdminResourceDetails > ( ) ) . ThrowsAsync ( new InvalidOperationException ( ) ) ;
94+
95+ // Act
96+ Func < Task > act = async ( ) => await service . CreateResource ( resourceDetails ) ;
97+
98+ // Assert
99+ await act . Should ( ) . ThrowAsync < InvalidOperationException > ( ) ;
100+ }
18101}
0 commit comments