@@ -9,26 +9,56 @@ import com.fasterxml.jackson.databind.ObjectMapper;
99import com.pgssoft.httpclient.HttpClientMock;
1010import { {clientPackage} 
1111import { {configPackage} 
12- import { {errorsPackage} 
13- import org.junit.jupiter.api.BeforeEach;
14- import org.junit.jupiter.api.Test;
12+ import org.junit.jupiter.params.ParameterizedTest;
13+ import org.junit.jupiter.params.provider.Arguments;
14+ import org.junit.jupiter.params.provider.MethodSource;
15+ 
16+ import java.util.stream.Stream;
17+ 
1518
1619class OAuth2ClientTest { 
1720    private static final String CLIENT_ID = " client"  
1821    private static final String CLIENT_SECRET = " secret"  
1922    private static final String AUDIENCE = " audience"  
2023    private static final String GRANT_TYPE = " client_credentials"  
21-     private static final String API_TOKEN_ISSUER = " test.fga.dev"  
22-     private static final String POST_URL = " https://" " /oauth/token"  
2324    private static final String ACCESS_TOKEN = " 0123456789"  
2425
2526    private final ObjectMapper mapper = new ObjectMapper(); 
2627    private HttpClientMock mockHttpClient; 
2728
28-     private OAuth2Client oAuth2; 
29+     private static Stream< Arguments>  issuersTokenEndpoint() { 
30+         return Stream.of( 
31+                 Arguments.of(" issuer.fga.example" " https://issuer.fga.example/oauth/token"  
32+                 Arguments.of(" https://issuer.fga.example" " https://issuer.fga.example/oauth/token"  
33+                 Arguments.of(" https://issuer.fga.example/" " https://issuer.fga.example/oauth/token"  
34+                 Arguments.of(" https://issuer.fga.example:8080" " https://issuer.fga.example:8080/oauth/token"  
35+                 Arguments.of(" https://issuer.fga.example:8080/" " https://issuer.fga.example:8080/oauth/token"  
36+                 Arguments.of(" issuer.fga.example/some_endpoint" " https://issuer.fga.example/some_endpoint"  
37+                 Arguments.of(" https://issuer.fga.example/some_endpoint" " https://issuer.fga.example/some_endpoint"  
38+                 Arguments.of(" https://issuer.fga.example:8080/some_endpoint" " https://issuer.fga.example:8080/some_endpoint"  
39+         ); 
40+     }  
2941
30-     @BeforeEach 
31-     public void setup() throws FgaInvalidParameterException { 
42+     @ParameterizedTest
43+     @MethodSource("issuersTokenEndpoint")
44+     public void exchangeToken(String apiTokenIssuer, String tokenEndpointUrl) throws Exception { 
45+         // Given 
46+         OAuth2Client oAuth2 = configureClient(apiTokenIssuer); 
47+         String expectedPostBody = String.format( 
48+                 " {\" client_id\" :\" %s\" ,\" client_secret\" :\" %s\" ,\" audience\" :\" %s\" ,\" grant_type\" :\" %s\" }"  
49+                 CLIENT_ID, CLIENT_SECRET, AUDIENCE, GRANT_TYPE); 
50+         String responseBody = String.format(" {\" access_token\" :\" %s\" }"  
51+         mockHttpClient.onPost(tokenEndpointUrl).withBody(is(expectedPostBody)).doReturn(200, responseBody); 
52+ 
53+         // When 
54+         String result = oAuth2.getAccessToken().get(); 
55+ 
56+         // Then 
57+         mockHttpClient.verify().post(tokenEndpointUrl).withBody(is(expectedPostBody)).called(); 
58+         assertEquals(ACCESS_TOKEN, result); 
59+     }  
60+ 
61+     private OAuth2Client configureClient(String apiTokenIssuer) { 
3262        System.setProperty(" HttpRequestAttempt.debug-logging" " enable"  
3363
3464        mockHttpClient = new HttpClientMock(); 
@@ -38,31 +68,14 @@ class OAuth2ClientTest {
3868                .clientId(CLIENT_ID) 
3969                .clientSecret(CLIENT_SECRET) 
4070                .apiAudience(AUDIENCE) 
41-                 .apiTokenIssuer(API_TOKEN_ISSUER )); 
71+                 .apiTokenIssuer(apiTokenIssuer )); 
4272
4373        var configuration = new Configuration().apiUrl(" "  
4474
4575        var apiClient = mock(ApiClient.class); 
4676        when(apiClient.getHttpClient()).thenReturn(mockHttpClient); 
4777        when(apiClient.getObjectMapper()).thenReturn(mapper); 
4878
49-         oAuth2 = new OAuth2Client(configuration, apiClient); 
50-     }  
51- 
52-     @Test
53-     public void exchangeToken() throws Exception { 
54-         // Given 
55-         String expectedPostBody = String.format( 
56-                 " {\" client_id\" :\" %s\" ,\" client_secret\" :\" %s\" ,\" audience\" :\" %s\" ,\" grant_type\" :\" %s\" }"  
57-                 CLIENT_ID, CLIENT_SECRET, AUDIENCE, GRANT_TYPE); 
58-         String responseBody = String.format(" {\" access_token\" :\" %s\" }"  
59-         mockHttpClient.onPost(POST_URL).withBody(is(expectedPostBody)).doReturn(200, responseBody); 
60- 
61-         // When 
62-         String result = oAuth2.getAccessToken().get(); 
63- 
64-         // Then 
65-         mockHttpClient.verify().post(POST_URL).withBody(is(expectedPostBody)).called(); 
66-         assertEquals(ACCESS_TOKEN, result); 
79+         return new OAuth2Client(configuration, apiClient); 
6780    }  
6881}
0 commit comments