1+ using AzureIntegration . Controllers ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using Microsoft . Extensions . Configuration ;
4+ using Moq ;
5+ using Moq . Protected ;
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . Net ;
9+ using System . Net . Http ;
10+ using System . Text . Json ;
11+ using System . Threading ;
12+ using System . Threading . Tasks ;
13+ using Xunit ;
14+
15+ namespace AzureIntegration . Tests . Controllers
16+ {
17+ public class ResourceGroupsControllerTest
18+ {
19+ private readonly Mock < IHttpClientFactory > _mockHttpClientFactory ;
20+ private readonly Mock < IConfiguration > _mockConfiguration ;
21+
22+ public ResourceGroupsControllerTest ( )
23+ {
24+ _mockHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
25+ _mockConfiguration = new Mock < IConfiguration > ( ) ;
26+ }
27+
28+ [ Fact ]
29+ public async Task GetAzureManagementGroups_ReturnsManagementGroups_WhenApiCallSucceeds ( )
30+ {
31+ // Arrange
32+ var mockHttpMessageHandler = new Mock < HttpMessageHandler > ( ) ;
33+ var response = new HttpResponseMessage
34+ {
35+ StatusCode = HttpStatusCode . OK ,
36+ Content = new StringContent ( @"
37+ {
38+ ""value"": [
39+ {
40+ ""id"": ""/providers/Microsoft.Management/managementGroups/mg1"",
41+ ""type"": ""Microsoft.Management/managementGroups"",
42+ ""properties"": {
43+ ""displayName"": ""Management Group 1""
44+ }
45+ },
46+ {
47+ ""id"": ""/providers/Microsoft.Management/managementGroups/mg2"",
48+ ""type"": ""Microsoft.Management/managementGroups"",
49+ ""properties"": {
50+ ""displayName"": ""Management Group 2""
51+ }
52+ }
53+ ]
54+ }" )
55+ } ;
56+
57+ mockHttpMessageHandler
58+ . Protected ( )
59+ . Setup < Task < HttpResponseMessage > > (
60+ "SendAsync" ,
61+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
62+ ItExpr . IsAny < CancellationToken > ( ) )
63+ . ReturnsAsync ( response ) ;
64+
65+ var httpClient = new HttpClient ( mockHttpMessageHandler . Object ) ;
66+ _mockHttpClientFactory
67+ . Setup ( factory => factory . CreateClient ( "AzureServices" ) )
68+ . Returns ( httpClient ) ;
69+
70+ var controller = new ResourceGroupsController ( _mockHttpClientFactory . Object , _mockConfiguration . Object ) ;
71+
72+ // Act
73+ var result = await controller . ManagementGroups ( ) ;
74+
75+ // Assert
76+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
77+ var model = Assert . IsAssignableFrom < List < AzureManagementGroup > > ( viewResult . Model ) ;
78+ Assert . Equal ( 2 , model . Count ) ;
79+ Assert . Equal ( "Management Group 1" , model [ 0 ] . Name ) ;
80+ Assert . Equal ( "/providers/Microsoft.Management/managementGroups/mg1" , model [ 0 ] . Id ) ;
81+ Assert . Equal ( "Microsoft.Management/managementGroups" , model [ 0 ] . Type ) ;
82+ Assert . Equal ( "Management Group 2" , model [ 1 ] . Name ) ;
83+ }
84+
85+ [ Fact ]
86+ public async Task GetAzureManagementGroups_ReturnsEmptyList_WhenNoValuePropertyInResponse ( )
87+ {
88+ // Arrange
89+ var mockHttpMessageHandler = new Mock < HttpMessageHandler > ( ) ;
90+ var response = new HttpResponseMessage
91+ {
92+ StatusCode = HttpStatusCode . OK ,
93+ Content = new StringContent ( @"{ ""someOtherProperty"": [] }" )
94+ } ;
95+
96+ mockHttpMessageHandler
97+ . Protected ( )
98+ . Setup < Task < HttpResponseMessage > > (
99+ "SendAsync" ,
100+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
101+ ItExpr . IsAny < CancellationToken > ( ) )
102+ . ReturnsAsync ( response ) ;
103+
104+ var httpClient = new HttpClient ( mockHttpMessageHandler . Object ) ;
105+ _mockHttpClientFactory
106+ . Setup ( factory => factory . CreateClient ( "AzureServices" ) )
107+ . Returns ( httpClient ) ;
108+
109+ var controller = new ResourceGroupsController ( _mockHttpClientFactory . Object , _mockConfiguration . Object ) ;
110+
111+ // Act
112+ var result = await controller . ManagementGroups ( ) ;
113+
114+ // Assert
115+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
116+ var model = Assert . IsAssignableFrom < List < AzureManagementGroup > > ( viewResult . Model ) ;
117+ Assert . Empty ( model ) ;
118+ }
119+
120+ [ Fact ]
121+ public async Task GetAzureManagementGroups_HandlesNullValues_InResponseProperties ( )
122+ {
123+ // Arrange
124+ var mockHttpMessageHandler = new Mock < HttpMessageHandler > ( ) ;
125+ var response = new HttpResponseMessage
126+ {
127+ StatusCode = HttpStatusCode . OK ,
128+ Content = new StringContent ( @"
129+ {
130+ ""value"": [
131+ {
132+ ""id"": null,
133+ ""type"": null,
134+ ""properties"": {
135+ ""displayName"": null
136+ }
137+ }
138+ ]
139+ }" )
140+ } ;
141+
142+ mockHttpMessageHandler
143+ . Protected ( )
144+ . Setup < Task < HttpResponseMessage > > (
145+ "SendAsync" ,
146+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
147+ ItExpr . IsAny < CancellationToken > ( ) )
148+ . ReturnsAsync ( response ) ;
149+
150+ var httpClient = new HttpClient ( mockHttpMessageHandler . Object ) ;
151+ _mockHttpClientFactory
152+ . Setup ( factory => factory . CreateClient ( "AzureServices" ) )
153+ . Returns ( httpClient ) ;
154+
155+ var controller = new ResourceGroupsController ( _mockHttpClientFactory . Object , _mockConfiguration . Object ) ;
156+
157+ // Act
158+ var result = await controller . ManagementGroups ( ) ;
159+
160+ // Assert
161+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
162+ var model = Assert . IsAssignableFrom < List < AzureManagementGroup > > ( viewResult . Model ) ;
163+ Assert . Single ( model ) ;
164+ Assert . Equal ( string . Empty , model [ 0 ] . Name ) ;
165+ Assert . Equal ( string . Empty , model [ 0 ] . Id ) ;
166+ Assert . Equal ( string . Empty , model [ 0 ] . Type ) ;
167+ }
168+
169+ [ Fact ]
170+ public async Task GetAzureManagementGroups_CallsCorrectEndpoint ( )
171+ {
172+ // Arrange
173+ var mockHttpMessageHandler = new Mock < HttpMessageHandler > ( ) ;
174+ HttpRequestMessage capturedRequest = null ;
175+
176+ mockHttpMessageHandler
177+ . Protected ( )
178+ . Setup < Task < HttpResponseMessage > > (
179+ "SendAsync" ,
180+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
181+ ItExpr . IsAny < CancellationToken > ( ) )
182+ . Callback < HttpRequestMessage , CancellationToken > ( ( request , _ ) => capturedRequest = request )
183+ . ReturnsAsync ( new HttpResponseMessage
184+ {
185+ StatusCode = HttpStatusCode . OK ,
186+ Content = new StringContent ( @"{ ""value"": [] }" )
187+ } ) ;
188+
189+ var httpClient = new HttpClient ( mockHttpMessageHandler . Object ) ;
190+ _mockHttpClientFactory
191+ . Setup ( factory => factory . CreateClient ( "AzureServices" ) )
192+ . Returns ( httpClient ) ;
193+
194+ var controller = new ResourceGroupsController ( _mockHttpClientFactory . Object , _mockConfiguration . Object ) ;
195+
196+ // Act
197+ await controller . ManagementGroups ( ) ;
198+
199+ // Assert
200+ Assert . NotNull ( capturedRequest ) ;
201+ Assert . Equal ( HttpMethod . Get , capturedRequest . Method ) ;
202+ Assert . Equal ( "providers/Microsoft.Management/managementGroups?api-version=2020-05-01" ,
203+ capturedRequest . RequestUri . ToString ( ) ) ;
204+ }
205+ }
206+ }
0 commit comments