### Question:
SpardaOauth2Service implements Oauth2Service, PkceOauth2Extension { @Override public TokenResponse getToken(Map<String, String> headers, Parameters parameters) throws IOException { requireValid(validateGetToken(headers, parameters)); parameters.setClientId(clientId); parameters.setTokenEndpoint(tokenEndpoint); return oauth2Service.getToken(headers, parameters); } private  SpardaOauth2Service(Aspsp aspsp,
                                Oauth2Service oauth2Service,
                                String clientId); static SpardaOauth2Service create(Aspsp aspsp,
                                             HttpClient httpClient,
                                             Pkcs12KeyStore keyStore,
                                             String clientId); @Override URI getAuthorizationRequestUri(Map<String, String> headers, Parameters parameters); @Override List<ValidationError> validateGetAuthorizationRequestUri(Map<String, String> headers, Parameters parameters); @Override TokenResponse getToken(Map<String, String> headers, Parameters parameters); @Override List<ValidationError> validateGetToken(Map<String, String> headers, Parameters parameters); @Override byte[] octetSequence();  }### Answer:
@Test void getToken_authorizationCode() throws IOException { String code = "test-code"; Parameters parameters = new Parameters(); parameters.setRedirectUri(REDIRECT_URI); parameters.setGrantType(AUTHORIZATION_CODE.toString()); parameters.setAuthorizationCode(code); oauth2Service.getToken(Collections.emptyMap(), parameters); HashMap<String, String> expectedBody = new HashMap<>(); expectedBody.put(Parameters.GRANT_TYPE, AUTHORIZATION_CODE.toString()); expectedBody.put(Parameters.CLIENT_ID, CLIENT_ID); expectedBody.put(Parameters.REDIRECT_URI, REDIRECT_URI); expectedBody.put(Parameters.CODE, code); expectedBody.put(Parameters.CODE_VERIFIER, oauth2Service.codeVerifier()); Mockito.verify(httpClient, Mockito.times(1)) .send(ArgumentMatchers.argThat(req -> { boolean tokenExchange = req.method().equalsIgnoreCase("POST") && req.uri().equals(TOKEN_ENDPOINT); if (tokenExchange) { assertEquals(expectedBody, req.urlEncodedBody()); } return tokenExchange; }), Mockito.any()); }
@Test void getToken_refreshToken() throws IOException { String refreshToken = "refresh-token"; Parameters parameters = new Parameters(); parameters.setGrantType(REFRESH_TOKEN.toString()); parameters.setRefreshToken(refreshToken); oauth2Service.getToken(Collections.emptyMap(), parameters); HashMap<String, String> expectedBody = new HashMap<>(); expectedBody.put(Parameters.GRANT_TYPE, REFRESH_TOKEN.toString()); expectedBody.put(Parameters.CLIENT_ID, CLIENT_ID); expectedBody.put(Parameters.REFRESH_TOKEN, refreshToken); Mockito.verify(httpClient, Mockito.times(1)) .send(ArgumentMatchers.argThat(req -> { boolean tokenExchange = req.method().equalsIgnoreCase("POST") && req.uri().equals(TOKEN_ENDPOINT); if (tokenExchange) { assertEquals(expectedBody, req.urlEncodedBody()); } return tokenExchange; }), Mockito.any()); }