44import io .swagger .v3 .oas .models .OpenAPI ;
55import org .assertj .core .api .Assertions ;
66import org .junit .jupiter .api .Test ;
7+ import org .junit .jupiter .params .ParameterizedTest ;
8+ import org .junit .jupiter .params .provider .Arguments ;
9+ import org .junit .jupiter .params .provider .MethodSource ;
710import org .mockito .Mockito ;
811import org .springframework .test .util .ReflectionTestUtils ;
912import picocli .CommandLine ;
1013
1114import java .util .Collections ;
15+ import java .util .stream .Stream ;
1216
1317@ QuarkusTest
1418class ApiArgumentsTest {
@@ -66,54 +70,30 @@ void shouldThrowExceptionWhenServerAndOpenApiNull() {
6670 .hasMessageContaining ("server" );
6771 }
6872
69- @ Test
70- void shouldSetServerFromOpenApiWhenServerIsNull () {
73+ @ ParameterizedTest (name = "{0}" )
74+ @ MethodSource ("serverResolutionArguments" )
75+ void shouldResolveServerAccordingToPriority (String description , String initialServer , String openApiServerUrl , String expectedServer ) {
7176 CommandLine .Model .CommandSpec spec = Mockito .mock (CommandLine .Model .CommandSpec .class );
7277 Mockito .when (spec .commandLine ()).thenReturn (Mockito .mock (CommandLine .class ));
7378 ApiArguments args = new ApiArguments ();
74- args .setServer (null );
79+ args .setServer (initialServer );
7580
7681 OpenAPI openAPI = new OpenAPI ();
7782 openAPI .setServers (Collections .singletonList (
78- new io .swagger .v3 .oas .models .servers .Server ().url ("http://fromopenapi.com" )
83+ new io .swagger .v3 .oas .models .servers .Server ().url (openApiServerUrl )
7984 ));
8085
8186 args .validateValidServer (spec , openAPI );
82- Assertions .assertThat (args .getServer ()).isEqualTo ("http://fromopenapi.com" );
83- }
84-
85- @ Test
86- void shouldReplaceServerPlaceholder () {
87- CommandLine .Model .CommandSpec spec = Mockito .mock (CommandLine .Model .CommandSpec .class );
88- Mockito .when (spec .commandLine ()).thenReturn (Mockito .mock (CommandLine .class ));
89- ApiArguments args = new ApiArguments ();
90- args .setServer ("http://api.com" );
9187
92- OpenAPI openAPI = new OpenAPI ();
93- openAPI .setServers (Collections .singletonList (
94- new io .swagger .v3 .oas .models .servers .Server ().url ("{apiRoot}/v2" )
95- ));
96- args .validateValidServer (spec , openAPI );
97- // The placeholder should be replaced
98- Assertions .assertThat (args .getServer ()).isEqualTo ("http://api.com/v2" );
88+ Assertions .assertThat (args .getServer ()).isEqualTo (expectedServer );
9989 }
10090
101- @ Test
102- void shouldPreferCliServerOverOpenApiConcreteServer () {
103- CommandLine .Model .CommandSpec spec = Mockito .mock (CommandLine .Model .CommandSpec .class );
104- Mockito .when (spec .commandLine ()).thenReturn (Mockito .mock (CommandLine .class ));
105- ApiArguments args = new ApiArguments ();
106- args .setServer ("http://localhost:8080" );
107-
108- OpenAPI openAPI = new OpenAPI ();
109- openAPI .setServers (Collections .singletonList (
110- new io .swagger .v3 .oas .models .servers .Server ().url ("https://api.example.com" )
111- ));
112-
113- args .validateValidServer (spec , openAPI );
114-
115- // CLI server should take priority over concrete OpenAPI server
116- Assertions .assertThat (args .getServer ()).isEqualTo ("http://localhost:8080" );
91+ private static Stream <Arguments > serverResolutionArguments () {
92+ return Stream .of (
93+ Arguments .of ("should set server from OpenAPI when CLI server is null" , null , "http://fromopenapi.com" , "http://fromopenapi.com" ),
94+ Arguments .of ("should replace OpenAPI placeholder with CLI server" , "http://api.com" , "{apiRoot}/v2" , "http://api.com/v2" ),
95+ Arguments .of ("should prefer CLI server over concrete OpenAPI server" , "http://localhost:8080" , "https://api.example.com" , "http://localhost:8080" )
96+ );
11797 }
11898
11999 @ Test
@@ -138,4 +118,12 @@ void shouldPassWhenServerIsValidUrl() {
138118 Assertions .assertThatCode (() -> args .validateValidServer (spec , null ))
139119 .doesNotThrowAnyException ();
140120 }
121+
122+ @ Test
123+ void shouldRemoveTrailingSlashFromServer () {
124+ ApiArguments args = new ApiArguments ();
125+ args .setServer ("http://api.com/" );
126+ args .validateValidServer (null , null );
127+ Assertions .assertThat (args .getServer ()).isEqualTo ("http://api.com" );
128+ }
141129}
0 commit comments