From 69098eae19dce4aaca01e6e65f3d0fcc5626431d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 30 Nov 2020 11:16:34 -0800 Subject: [PATCH] test: add failing test for request metadata with callback --- .../auth/oauth2/UserCredentialsTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index 92e8ebe73..341c7781b 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -40,6 +40,7 @@ import com.google.api.client.json.GenericJson; import com.google.api.client.util.Clock; +import com.google.auth.RequestMetadataCallback; import com.google.auth.TestUtils; import com.google.auth.http.AuthHttpConstants; import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory; @@ -56,6 +57,8 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -622,6 +625,38 @@ public void saveAndRestoreUserCredential_saveAndRestored_throws() throws IOExcep assertEquals(userCredentials.getRefreshToken(), restoredCredentials.getRefreshToken()); } + @Test + public void getRequestMetadataWithCallback() throws IOException { + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); + transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET); + transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN); + + UserCredentials userCredentials = + UserCredentials.newBuilder() + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + .setRefreshToken(REFRESH_TOKEN) + .setQuotaProjectId("my-quota-project-id") + .setHttpTransportFactory(transportFactory) + .build(); + final Map> plainMetadata = userCredentials.getRequestMetadata(); + final AtomicBoolean success = new AtomicBoolean(false); + userCredentials.getRequestMetadata(null, null, new RequestMetadataCallback() { + @Override + public void onSuccess(Map> metadata) { + assertEquals(plainMetadata, metadata); + success.set(true); + } + + @Override + public void onFailure(Throwable exception) { + fail("Should not throw a failure."); + } + }); + + assertTrue("Should have run onSuccess() callback", success.get()); + } + static GenericJson writeUserJson( String clientId, String clientSecret, String refreshToken, String quotaProjectId) { GenericJson json = new GenericJson();