1+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+ namespace Asp . Versioning . ApiExplorer ;
4+
5+ using Microsoft . AspNetCore . Mvc ;
6+ using Microsoft . AspNetCore . Mvc . Abstractions ;
7+ using Microsoft . AspNetCore . Routing ;
8+ using Microsoft . Extensions . Options ;
9+
10+ public class GroupedApiVersionDescriptionProviderTest
11+ {
12+ [ Fact ]
13+ public void api_version_descriptions_should_collate_expected_versions ( )
14+ {
15+ // arrange
16+ var descriptionProvider = new GroupedApiVersionDescriptionProvider (
17+ new TestEndpointDataSource ( ) ,
18+ new TestActionDescriptorCollectionProvider ( ) ,
19+ Mock . Of < ISunsetPolicyManager > ( ) ,
20+ Options . Create ( new ApiExplorerOptions ( ) { GroupNameFormat = "'v'VVV" } ) ) ;
21+
22+ // act
23+ var descriptions = descriptionProvider . ApiVersionDescriptions ;
24+
25+ // assert
26+ descriptions . Should ( ) . BeEquivalentTo (
27+ new ApiVersionDescription [ ]
28+ {
29+ new ( new ApiVersion ( 0 , 9 ) , "v0.9" , true ) ,
30+ new ( new ApiVersion ( 1 , 0 ) , "v1" , false ) ,
31+ new ( new ApiVersion ( 2 , 0 ) , "v2" , false ) ,
32+ new ( new ApiVersion ( 3 , 0 ) , "v3" , false ) ,
33+ } ) ;
34+ }
35+
36+ [ Fact ]
37+ public void api_version_descriptions_should_collate_expected_versions_with_custom_group ( )
38+ {
39+ // arrange
40+ var provider = new TestActionDescriptorCollectionProvider ( ) ;
41+ var source = new CompositeEndpointDataSource ( Enumerable . Empty < EndpointDataSource > ( ) ) ;
42+ var data = new ApiDescriptionActionData ( ) { GroupName = "Test" } ;
43+
44+ foreach ( var descriptor in provider . ActionDescriptors . Items )
45+ {
46+ descriptor . SetProperty ( data ) ;
47+ }
48+
49+ var descriptionProvider = new GroupedApiVersionDescriptionProvider (
50+ source ,
51+ provider ,
52+ Mock . Of < ISunsetPolicyManager > ( ) ,
53+ Options . Create (
54+ new ApiExplorerOptions ( )
55+ {
56+ GroupNameFormat = "VVV" ,
57+ FormatGroupName = ( groupName , version ) => $ "{ groupName } -{ version } ",
58+ } ) ) ;
59+
60+ // act
61+ var descriptions = descriptionProvider . ApiVersionDescriptions ;
62+
63+ // assert
64+ descriptions . Should ( ) . BeEquivalentTo (
65+ new ApiVersionDescription [ ]
66+ {
67+ new ( new ApiVersion ( 0 , 9 ) , "Test-0.9" , true ) ,
68+ new ( new ApiVersion ( 1 , 0 ) , "Test-1" , false ) ,
69+ new ( new ApiVersion ( 2 , 0 ) , "Test-2" , false ) ,
70+ new ( new ApiVersion ( 3 , 0 ) , "Test-3" , false ) ,
71+ } ) ;
72+ }
73+
74+ [ Fact ]
75+ public void api_version_descriptions_should_apply_sunset_policy ( )
76+ {
77+ // arrange
78+ var expected = new SunsetPolicy ( ) ;
79+ var apiVersion = new ApiVersion ( 0.9 ) ;
80+ var policyManager = new Mock < ISunsetPolicyManager > ( ) ;
81+
82+ policyManager . Setup ( pm => pm . TryGetPolicy ( default , apiVersion , out expected ) ) . Returns ( true ) ;
83+
84+ var descriptionProvider = new GroupedApiVersionDescriptionProvider (
85+ new TestEndpointDataSource ( ) ,
86+ new TestActionDescriptorCollectionProvider ( ) ,
87+ policyManager . Object ,
88+ Options . Create ( new ApiExplorerOptions ( ) { GroupNameFormat = "'v'VVV" } ) ) ;
89+
90+ // act
91+ var description = descriptionProvider . ApiVersionDescriptions . Single ( api => api . GroupName == "v0.9" ) ;
92+
93+ // assert
94+ description . SunsetPolicy . Should ( ) . BeSameAs ( expected ) ;
95+ }
96+ }
0 commit comments