33using System . Reflection ;
44using System . Security . Cryptography ;
55using System . Text ;
6- using Microsoft . AspNetCore . Http ;
7- using Microsoft . Kiota . Abstractions . Authentication ;
8- using Microsoft . Kiota . Http . HttpClientLibrary ;
96using Microsoft . OpenApi ;
10- using TodoApp . Client ;
11- using TodoApp . Client . Models ;
127
138namespace Swashbuckle . AspNetCore . IntegrationTests ;
149
@@ -87,255 +82,11 @@ await VerifyDirectory(
8782 . UseFileName ( $ "{ nameof ( GeneratesValidClient ) } _{ hashString } ") ;
8883 }
8984
90- [ Fact ]
91- public async Task Can_Manage_Todo_Items_With_Api ( )
92- {
93- // Arrange
94- await WithTodoAppClientAsync ( async ( client ) =>
95- {
96- var cancellationToken = TestContext . Current . CancellationToken ;
97-
98- // Act - Get all the items
99- var items = await client . Api . Items . GetAsync ( cancellationToken : cancellationToken ) ;
100-
101- // Assert - There should be no items
102- Assert . NotNull ( items ) ;
103- Assert . NotNull ( items . Items ) ;
104-
105- var beforeCount = items . Items . Count ;
106-
107- // Arrange
108- var text = "Buy eggs" ;
109-
110- // Act - Add a new item
111- var createdItem = await client . Api . Items . PostAsync (
112- new ( ) { Text = text } ,
113- cancellationToken : cancellationToken ) ;
114-
115- // Assert - An item was created
116- Assert . NotNull ( createdItem ) ;
117- Assert . NotEqual ( default , createdItem . Id ) ;
118-
119- // Arrange - Get the new item's URL and Id
120- var itemId = createdItem . Id ;
121-
122- // Act - Get the item
123- var item = await client . Api . Items [ new ( itemId ) ] . GetAsync ( cancellationToken : cancellationToken ) ;
124-
125- // Assert - Verify the item was created correctly
126- Assert . NotNull ( item ) ;
127- Assert . Equal ( itemId , item . Id ) ;
128- Assert . Null ( item . CompletedAt ) ;
129- Assert . NotEqual ( default , item . CreatedAt ) ;
130- Assert . Equal ( item . CreatedAt . Value , item . LastUpdated ) ;
131- Assert . Null ( item . Priority ) ;
132- Assert . Equal ( text , item . Text ) ;
133-
134- // Act - Update the item to be high priority
135- await client . Api . Items [ new ( itemId ) ] . Priority . PatchAsync (
136- new ( ) { Priority = TodoPriority . High } ,
137- cancellationToken : cancellationToken ) ;
138-
139- item = await client . Api . Items [ new ( itemId ) ] . GetAsync ( cancellationToken : cancellationToken ) ;
140-
141- Assert . NotNull ( item ) ;
142- Assert . Equal ( itemId , item . Id ) ;
143- Assert . Null ( item . CompletedAt ) ;
144- Assert . NotEqual ( default , item . CreatedAt ) ;
145- Assert . Equal ( item . CreatedAt . Value , item . LastUpdated ) ;
146- Assert . Equal ( TodoPriority . High , item . Priority ) ;
147- Assert . Equal ( text , item . Text ) ;
148-
149- // Act - Mark the item as being completed
150- await client . Api . Items [ new ( itemId ) ] . Complete . PostAsync ( cancellationToken : cancellationToken ) ;
151-
152- item = await client . Api . Items [ new ( itemId ) ] . GetAsync ( cancellationToken : cancellationToken ) ;
153-
154- Assert . NotNull ( item ) ;
155- Assert . Equal ( itemId , item . Id ) ;
156- Assert . Equal ( text , item . Text ) ;
157- Assert . NotNull ( item . CompletedAt ) ;
158- Assert . Equal ( item . CompletedAt . Value , item . LastUpdated ) ;
159- Assert . True ( item . CompletedAt . Value > item . CreatedAt ) ;
160-
161- // Act - Get all the items
162- items = await client . Api . Items . GetAsync ( cancellationToken : cancellationToken ) ;
163-
164- // Assert - The item was completed
165- Assert . NotNull ( items ) ;
166- Assert . NotNull ( items . Items ) ;
167- Assert . Equal ( beforeCount + 1 , items . Items . Count ) ;
168- Assert . Contains ( items . Items , ( x ) => x . Id == itemId ) ;
169-
170- item = items . Items . Last ( ) ;
171-
172- Assert . NotNull ( item ) ;
173- Assert . Equal ( itemId , item . Id ) ;
174- Assert . Equal ( text , item . Text ) ;
175- Assert . NotNull ( item . CompletedAt ) ;
176- Assert . Equal ( item . CompletedAt . Value , item . LastUpdated ) ;
177- Assert . True ( item . CompletedAt . Value > item . CreatedAt ) ;
178-
179- // Act - Delete the item
180- await client . Api . Items [ new ( itemId ) ] . DeleteAsync ( cancellationToken : cancellationToken ) ;
181-
182- // Assert - The item no longer exists
183- items = await client . Api . Items . GetAsync ( cancellationToken : cancellationToken ) ;
184-
185- Assert . NotNull ( items ) ;
186- Assert . NotNull ( items . Items ) ;
187- Assert . Equal ( beforeCount , items . Items . Count ) ;
188- Assert . DoesNotContain ( items . Items , ( x ) => x . Id == itemId ) ;
189-
190- // Act
191- var problem = await Assert . ThrowsAsync < ProblemDetails > (
192- ( ) => client . Api . Items [ new ( itemId ) ] . GetAsync ( cancellationToken : cancellationToken ) ) ;
193-
194- // Assert
195- Assert . NotNull ( problem ) ;
196- Assert . Equal ( StatusCodes . Status404NotFound , problem . Status ) ;
197- Assert . Equal ( "Not Found" , problem . Title ) ;
198- Assert . Equal ( "Item not found." , problem . Detail ) ;
199- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.5" , problem . Type ) ;
200- Assert . Null ( problem . Instance ) ;
201-
202- // Act
203- problem = await Assert . ThrowsAsync < ProblemDetails > (
204- ( ) => client . Api . Items [ new ( itemId ) ] . Complete . PostAsync ( cancellationToken : cancellationToken ) ) ;
205-
206- // Assert
207- Assert . NotNull ( problem ) ;
208- Assert . Equal ( StatusCodes . Status404NotFound , problem . Status ) ;
209- Assert . Equal ( "Not Found" , problem . Title ) ;
210- Assert . Equal ( "Item not found." , problem . Detail ) ;
211- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.5" , problem . Type ) ;
212- Assert . Null ( problem . Instance ) ;
213-
214- // Act
215- problem = await Assert . ThrowsAsync < ProblemDetails > (
216- ( ) => client . Api . Items [ new ( itemId ) ] . Priority . PatchAsync ( new ( ) { Priority = TodoPriority . Low } , cancellationToken : cancellationToken ) ) ;
217-
218- // Assert
219- Assert . NotNull ( problem ) ;
220- Assert . Equal ( StatusCodes . Status404NotFound , problem . Status ) ;
221- Assert . Equal ( "Not Found" , problem . Title ) ;
222- Assert . Equal ( "Item not found." , problem . Detail ) ;
223- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.5" , problem . Type ) ;
224- Assert . Null ( problem . Instance ) ;
225- } ) ;
226- }
227-
228- [ Fact ]
229- public async Task Cannot_Create_Todo_Item_With_No_Text ( )
230- {
231- // Arrange
232- await WithTodoAppClientAsync ( async ( client ) =>
233- {
234- var cancellationToken = TestContext . Current . CancellationToken ;
235-
236- // Act
237- var problem = await Assert . ThrowsAsync < ProblemDetails > (
238- ( ) => client . Api . Items . PostAsync ( new ( ) { Text = string . Empty } , cancellationToken : cancellationToken ) ) ;
239-
240- // Assert
241- Assert . NotNull ( problem ) ;
242- Assert . Equal ( StatusCodes . Status400BadRequest , problem . Status ) ;
243- Assert . Equal ( "Bad Request" , problem . Title ) ;
244- Assert . Equal ( "No item text specified." , problem . Detail ) ;
245- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.1" , problem . Type ) ;
246- Assert . Null ( problem . Instance ) ;
247- } ) ;
248- }
249-
250- [ Fact ]
251- public async Task Cannot_Complete_Todo_Item_Multiple_Times ( )
252- {
253- // Arrange
254- await WithTodoAppClientAsync ( async ( client ) =>
255- {
256- var cancellationToken = TestContext . Current . CancellationToken ;
257-
258- var createdItem = await client . Api . Items . PostAsync (
259- new ( ) { Text = "Something" } ,
260- cancellationToken : cancellationToken ) ;
261-
262- await client . Api . Items [ new ( createdItem . Id ) ] . Complete . PostAsync ( cancellationToken : cancellationToken ) ;
263-
264- // Act
265- var problem = await Assert . ThrowsAsync < ProblemDetails > (
266- ( ) => client . Api . Items [ new ( createdItem . Id ) ] . Complete . PostAsync ( cancellationToken : cancellationToken ) ) ;
267-
268- // Assert
269- Assert . NotNull ( problem ) ;
270- Assert . Equal ( StatusCodes . Status400BadRequest , problem . Status ) ;
271- Assert . Equal ( "Bad Request" , problem . Title ) ;
272- Assert . Equal ( "Item already completed." , problem . Detail ) ;
273- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.1" , problem . Type ) ;
274- Assert . Null ( problem . Instance ) ;
275- } ) ;
276- }
277-
278- [ Fact ]
279- public async Task Cannot_Complete_Deleted_Todo_Item ( )
280- {
281- // Arrange
282- await WithTodoAppClientAsync ( async ( client ) =>
283- {
284- var cancellationToken = TestContext . Current . CancellationToken ;
285-
286- var createdItem = await client . Api . Items . PostAsync (
287- new ( ) { Text = "Something" } ,
288- cancellationToken : cancellationToken ) ;
289-
290- await client . Api . Items [ new ( createdItem . Id ) ] . DeleteAsync ( cancellationToken : cancellationToken ) ;
291-
292- // Act
293- var problem = await Assert . ThrowsAsync < ProblemDetails > (
294- ( ) => client . Api . Items [ new ( createdItem . Id ) ] . Complete . PostAsync ( cancellationToken : cancellationToken ) ) ;
295-
296- // Assert
297- Assert . NotNull ( problem ) ;
298- Assert . Equal ( StatusCodes . Status404NotFound , problem . Status ) ;
299- Assert . Equal ( "Not Found" , problem . Title ) ;
300- Assert . Equal ( "Item not found." , problem . Detail ) ;
301- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.5" , problem . Type ) ;
302- Assert . Null ( problem . Instance ) ;
303- } ) ;
304- }
305-
306- [ Fact ]
307- public async Task Cannot_Delete_Todo_Item_Multiple_Times ( )
308- {
309- // Arrange
310- await WithTodoAppClientAsync ( async ( client ) =>
311- {
312- var cancellationToken = TestContext . Current . CancellationToken ;
313-
314- var createdItem = await client . Api . Items . PostAsync (
315- new ( ) { Text = "Something" } ,
316- cancellationToken : cancellationToken ) ;
317-
318- await client . Api . Items [ new ( createdItem . Id ) ] . DeleteAsync ( cancellationToken : cancellationToken ) ;
319-
320- // Act
321- var problem = await Assert . ThrowsAsync < ProblemDetails > (
322- ( ) => client . Api . Items [ new ( createdItem . Id ) ] . DeleteAsync ( cancellationToken : cancellationToken ) ) ;
323-
324- // Assert
325- Assert . NotNull ( problem ) ;
326- Assert . Equal ( StatusCodes . Status404NotFound , problem . Status ) ;
327- Assert . Equal ( "Not Found" , problem . Title ) ;
328- Assert . Equal ( "Item not found." , problem . Detail ) ;
329- Assert . Equal ( "https://tools.ietf.org/html/rfc9110#section-15.5.5" , problem . Type ) ;
330- Assert . Null ( problem . Instance ) ;
331- } ) ;
332- }
333-
33485 [ Fact ]
33586 public async Task VerifyKiotaTodoAppClient ( )
33687 {
33788 await VerifyDirectory (
338- Path . Combine ( GetProjectRoot ( ) , "TodoClient " ) ,
89+ Path . Combine ( GetProjectRoot ( ) , "KiotaTodoClient " ) ,
33990 pattern : "*.cs" ,
34091 options : new ( ) { RecurseSubdirectories = true } ) . UseDirectory ( "snapshots" ) ;
34192 }
@@ -345,18 +96,6 @@ private static string GetProjectRoot() =>
34596 . GetCustomAttributes < AssemblyMetadataAttribute > ( )
34697 . First ( ( p ) => p . Key is "ProjectRoot" )
34798 . Value ! ;
348-
349- private static async Task WithTodoAppClientAsync ( Func < TodoApiClient , Task > callback )
350- {
351- using var httpClient = SwaggerIntegrationTests . GetHttpClientForTestApplication ( typeof ( TodoApp . Program ) ) ;
352-
353- var provider = new AnonymousAuthenticationProvider ( ) ;
354- using var request = new HttpClientRequestAdapter ( provider , httpClient : httpClient ) ;
355-
356- var client = new TodoApiClient ( request ) ;
357-
358- await callback ( client ) ;
359- }
36099}
361100
362101#endif
0 commit comments