1- using CloneDevOpsTemplate . Services ;
1+ using System . Net ;
2+ using System . Net . Http . Json ;
3+ using System . Text . Json ;
24using CloneDevOpsTemplate . Models ;
5+ using CloneDevOpsTemplate . Services ;
6+ using Microsoft . Extensions . Configuration ;
37using Moq ;
48using Moq . Protected ;
5- using System . Net ;
6- using System . Net . Http . Json ;
79using MyTestProject . Service . Tests . Common ;
8- using Microsoft . Extensions . Configuration ;
910
1011namespace CloneDevOpsTemplateTest . Services ;
1112
@@ -163,13 +164,19 @@ public async Task DeleteRepositoryAsync_DeletesRepository()
163164 }
164165
165166 [ Fact ]
166- public async Task CreateImportRequest_CreatesImportRequest ( )
167+ public async Task CreateImportRequestAsync_ReturnsGitImportRequest ( )
167168 {
168169 // Arrange
169170 var projectId = Guid . NewGuid ( ) ;
170171 var repositoryId = Guid . NewGuid ( ) ;
171172 var sourceRepositoryRemoteUrl = "https://example.com/repo.git" ;
172173 var serviceEndpointId = Guid . NewGuid ( ) ;
174+ var gitImportRequest = new GitImportRequest
175+ {
176+ ImportRequestId = 1 ,
177+ Status = GitAsyncOperationStatus . Completed
178+ } ;
179+
173180 _httpMessageHandlerMock . Protected ( )
174181 . Setup < Task < HttpResponseMessage > > (
175182 "SendAsync" ,
@@ -178,18 +185,40 @@ public async Task CreateImportRequest_CreatesImportRequest()
178185 )
179186 . ReturnsAsync ( new HttpResponseMessage
180187 {
181- StatusCode = HttpStatusCode . OK
188+ StatusCode = HttpStatusCode . OK ,
189+ Content = JsonContent . Create ( gitImportRequest )
182190 } ) ;
183191
184192 // Act
185- await _repositoryService . CreateImportRequest ( projectId , repositoryId , sourceRepositoryRemoteUrl , serviceEndpointId ) ;
193+ var result = await _repositoryService . CreateImportRequestAsync ( projectId , repositoryId , sourceRepositoryRemoteUrl , serviceEndpointId ) ;
186194
187195 // Assert
188- _httpMessageHandlerMock . Protected ( ) . Verify (
189- "SendAsync" ,
190- Times . Once ( ) ,
191- ItExpr . IsAny < HttpRequestMessage > ( ) ,
192- ItExpr . IsAny < CancellationToken > ( )
193- ) ;
196+ Assert . NotNull ( result ) ;
197+ Assert . Equal ( gitImportRequest . ImportRequestId , result . ImportRequestId ) ;
198+ Assert . Equal ( gitImportRequest . Status , result . Status ) ;
199+ }
200+
201+ [ Fact ]
202+ public async Task CreateImportRequestAsync_ReturnsNullOnFailure ( )
203+ {
204+ // Arrange
205+ var projectId = Guid . NewGuid ( ) ;
206+ var repositoryId = Guid . NewGuid ( ) ;
207+ var sourceRepositoryRemoteUrl = "https://example.com/repo.git" ;
208+ var serviceEndpointId = Guid . NewGuid ( ) ;
209+
210+ _httpMessageHandlerMock . Protected ( )
211+ . Setup < Task < HttpResponseMessage > > (
212+ "SendAsync" ,
213+ ItExpr . IsAny < HttpRequestMessage > ( ) ,
214+ ItExpr . IsAny < CancellationToken > ( )
215+ )
216+ . ReturnsAsync ( new HttpResponseMessage
217+ {
218+ StatusCode = HttpStatusCode . BadRequest
219+ } ) ;
220+
221+ // Act & Assert
222+ await Assert . ThrowsAsync < JsonException > ( async ( ) => await _repositoryService . CreateImportRequestAsync ( projectId , repositoryId , sourceRepositoryRemoteUrl , serviceEndpointId ) ) ;
194223 }
195224}
0 commit comments